Search in sources :

Example 1 with ExecutableCommand

use of org.kie.api.command.ExecutableCommand in project drools by kiegroup.

the class PseudoClockRunner method executeQueue.

private void executeQueue(RequestContext ctx) {
    while (!queue.isEmpty()) {
        Batch batch = queue.remove();
        long timeNow = startTime + batch.getDistance();
        for (KieSession ksession : ksessions) {
            // make sure all sessions are set to timeNow
            updateKieSessionTime(timeNow, batch.getDistance(), ksession);
        }
        for (Command cmd : batch.getCommands()) {
            Object returned = ((ExecutableCommand) cmd).execute(ctx);
            if (returned != null) {
                ctx.setResult(returned);
                if (returned instanceof KieSession) {
                    KieSession ksession = (KieSession) returned;
                    // make sure all sessions are set to timeNow
                    updateKieSessionTime(timeNow, batch.getDistance(), ksession);
                    ksessions.add((KieSession) returned);
                }
            }
        }
    }
}
Also used : Command(org.kie.api.command.Command) ExecutableCommand(org.kie.api.command.ExecutableCommand) ExecutableCommand(org.kie.api.command.ExecutableCommand) KieSession(org.kie.api.runtime.KieSession)

Example 2 with ExecutableCommand

use of org.kie.api.command.ExecutableCommand in project drools by kiegroup.

the class StatelessKnowledgeSessionImpl method execute.

public <T> T execute(Command<T> command) {
    StatefulKnowledgeSession ksession = newWorkingMemory();
    RegistryContext context = new ContextImpl().register(KieSession.class, ksession);
    try {
        if (command instanceof BatchExecutionCommand) {
            context.register(ExecutionResultImpl.class, new ExecutionResultImpl());
        }
        ((StatefulKnowledgeSessionImpl) ksession).startBatchExecution();
        Object o = ((ExecutableCommand) command).execute(context);
        // did the user take control of fireAllRules, if not we will auto execute
        boolean autoFireAllRules = true;
        if (command instanceof FireAllRulesCommand) {
            autoFireAllRules = false;
        } else if (command instanceof BatchExecutionCommandImpl) {
            for (Command nestedCmd : ((BatchExecutionCommandImpl) command).getCommands()) {
                if (nestedCmd instanceof FireAllRulesCommand) {
                    autoFireAllRules = false;
                    break;
                }
            }
        }
        if (autoFireAllRules) {
            ksession.fireAllRules();
        }
        if (command instanceof BatchExecutionCommand) {
            return (T) context.lookup(ExecutionResultImpl.class);
        } else {
            return (T) o;
        }
    } finally {
        ((StatefulKnowledgeSessionImpl) ksession).endBatchExecution();
        dispose(ksession);
    }
}
Also used : FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) RegistryContext(org.kie.internal.command.RegistryContext) ContextImpl(org.drools.core.command.impl.ContextImpl) BatchExecutionCommandImpl(org.drools.core.command.runtime.BatchExecutionCommandImpl) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) ExecutableCommand(org.kie.api.command.ExecutableCommand) Command(org.kie.api.command.Command) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) ExecutionResultImpl(org.drools.core.runtime.impl.ExecutionResultImpl) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) ExecutableCommand(org.kie.api.command.ExecutableCommand)

Example 3 with ExecutableCommand

use of org.kie.api.command.ExecutableCommand in project drools by kiegroup.

the class RuleStatefulScenarioExecutableBuilderTest method testBuilder.

@Test
public void testBuilder() {
    when(executableBuilderMock.newApplicationContext(anyString())).thenReturn(executableBuilderMock);
    when(executableBuilderMock.setKieContainer(any())).thenReturn(kieContainerFluent);
    when(kieContainerFluent.newSessionCustomized(any(), any())).thenReturn(kieSessionFluentMock);
    when(kieSessionFluentMock.dispose()).thenReturn(executableBuilderMock);
    when(kieSessionFluentMock.addCommand(any())).thenReturn(kieSessionFluentMock);
    when(executableRunnerMock.execute(Mockito.<Executable>any())).thenReturn(requestContextMock);
    when(requestContextMock.getOutputs()).thenReturn(Collections.emptyMap());
    RuleStatefulScenarioExecutableBuilder builder = new RuleStatefulScenarioExecutableBuilder(null, null) {

        @Override
        protected ExecutableBuilder createExecutableBuilder() {
            return executableBuilderMock;
        }

        @Override
        protected ExecutableRunner<RequestContext> createExecutableRunner() {
            return executableRunnerMock;
        }
    };
    Object toInsert = new Object();
    String agendaGroup = "agendaGroup";
    String ruleFlowGroup = "ruleFlowGroup";
    FactMappingValue indexFMV = new FactMappingValue(FactIdentifier.INDEX, ExpressionIdentifier.INDEX, null);
    builder.setActiveAgendaGroup(agendaGroup);
    verify(kieSessionFluentMock, times(1)).setActiveAgendaGroup(eq(agendaGroup));
    reset(kieContainerFluent);
    builder.setActiveRuleFlowGroup(ruleFlowGroup);
    verify(kieSessionFluentMock, times(1)).setActiveAgendaGroup(eq(agendaGroup));
    reset(kieContainerFluent);
    builder.insert(toInsert);
    verify(kieSessionFluentMock, times(1)).insert(eq(toInsert));
    reset(kieContainerFluent);
    builder.addInternalCondition(String.class, obj -> null, new ScenarioResult(indexFMV, null));
    Map<String, Object> result = builder.run();
    verify(kieSessionFluentMock, times(1)).fireAllRules();
    verify(kieSessionFluentMock, times(3)).addCommand(commandArgumentCaptor.capture());
    List<ExecutableCommand<?>> allAddCommands = commandArgumentCaptor.getAllValues();
    assertTrue(allAddCommands.stream().map(Object::getClass).anyMatch(ValidateFactCommand.class::isAssignableFrom));
    assertTrue(allAddCommands.stream().map(Object::getClass).anyMatch(AddCoverageListenerCommand.class::isAssignableFrom));
    assertTrue(result.containsKey(RuleScenarioExecutableBuilder.COVERAGE_LISTENER));
    verify(kieSessionFluentMock, times(1)).out(eq(RULES_AVAILABLE));
}
Also used : ScenarioResult(org.drools.scenariosimulation.backend.runner.model.ScenarioResult) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) ExecutableCommand(org.kie.api.command.ExecutableCommand) RequestContext(org.kie.api.runtime.RequestContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 4 with ExecutableCommand

use of org.kie.api.command.ExecutableCommand in project drools by kiegroup.

the class AssertEquals method execute.

public Void execute(Context context) {
    Object actualObject = ((ExecutableCommand) command).execute(context);
    if (this.mvelString != null) {
        actualObject = CoreComponentsBuilder.get().getMVELExecutor().eval(this.mvelString, actualObject);
    }
    if (this.expectedIdentifier != null) {
        this.expectedObject = context.get(this.expectedIdentifier);
    }
    Map vars = new HashMap();
    vars.put("expected", expectedObject);
    vars.put("actual", actualObject);
    if (((Boolean) CoreComponentsBuilder.get().getMVELExecutor().eval("expected != actual", vars))) {
        throw new AssertionError(format(this.message, expectedObject, actualObject));
    }
    return null;
}
Also used : HashMap(java.util.HashMap) ExecutableCommand(org.kie.api.command.ExecutableCommand) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with ExecutableCommand

use of org.kie.api.command.ExecutableCommand in project drools by kiegroup.

the class PseudoClockRunner method executeBatch.

private void executeBatch(Batch batch, RequestContext ctx) {
    // everything else must be handled by a priority queue and timer afterwards.
    for (Command cmd : batch.getCommands()) {
        Object returned = ((ExecutableCommand) cmd).execute(ctx);
        if (returned != null) {
            ctx.setResult(returned);
            if (returned instanceof KieSession) {
                KieSession ksession = (KieSession) returned;
                // make sure all sessions are set to timeNow
                updateKieSessionTime(startTime, 0, ksession);
                ksessions.add((KieSession) returned);
            }
        }
    }
}
Also used : Command(org.kie.api.command.Command) ExecutableCommand(org.kie.api.command.ExecutableCommand) ExecutableCommand(org.kie.api.command.ExecutableCommand) KieSession(org.kie.api.runtime.KieSession)

Aggregations

ExecutableCommand (org.kie.api.command.ExecutableCommand)6 Command (org.kie.api.command.Command)3 BatchExecutionCommandImpl (org.drools.core.command.runtime.BatchExecutionCommandImpl)2 Test (org.junit.Test)2 KieSession (org.kie.api.runtime.KieSession)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ContextImpl (org.drools.core.command.impl.ContextImpl)1 FireAllRulesCommand (org.drools.core.command.runtime.rule.FireAllRulesCommand)1 ExecutionResultImpl (org.drools.core.runtime.impl.ExecutionResultImpl)1 Cheese (org.drools.mvel.compiler.Cheese)1 FactMappingValue (org.drools.scenariosimulation.api.model.FactMappingValue)1 ScenarioResult (org.drools.scenariosimulation.backend.runner.model.ScenarioResult)1 BatchExecutionCommand (org.kie.api.command.BatchExecutionCommand)1 ExecutionResults (org.kie.api.runtime.ExecutionResults)1 RequestContext (org.kie.api.runtime.RequestContext)1 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)1 RegistryContext (org.kie.internal.command.RegistryContext)1 StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1