Search in sources :

Example 1 with BatchExecutionCommand

use of org.kie.api.command.BatchExecutionCommand 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) {
            ((RegistryContext) 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) ((RegistryContext) 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.drools.core.command.impl.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.drools.core.command.impl.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.drools.core.command.impl.ExecutableCommand)

Example 2 with BatchExecutionCommand

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

the class IncrementalCompilationTest method testUpdateVersionWithKSessionLogger.

@Test
public void testUpdateVersionWithKSessionLogger() {
    // DROOLS-790
    String drl1 = "import java.util.List\n" + "import java.util.ArrayList\n" + "\n" + "rule \"Test1\"\n" + "\n" + "when\n" + "   $a : Integer()\n" + "then\n" + "   insert(new ArrayList());\n" + "end\n";
    String drl2 = "rule \"Test2\"\n" + "when\n" + "   $b : List()\n" + " then\n" + "   $b.isEmpty();\n" + "end";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieModule km = createAndDeployJar(ks, releaseId1, drl1);
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    StatelessKieSession statelessKieSession = kc.newStatelessKieSession();
    KieRuntimeLogger kieRuntimeLogger = ks.getLoggers().newConsoleLogger(statelessKieSession);
    List<Command> cmds = new ArrayList<Command>();
    cmds.add(CommandFactory.newInsertElements(new ArrayList()));
    FireAllRulesCommand fireAllRulesCommand = (FireAllRulesCommand) CommandFactory.newFireAllRules();
    cmds.add(fireAllRulesCommand);
    cmds.add(CommandFactory.newGetObjects("returnedObjects"));
    BatchExecutionCommand batchExecutionCommand = CommandFactory.newBatchExecution(cmds);
    statelessKieSession.execute(batchExecutionCommand);
    kieRuntimeLogger.close();
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    km = createAndDeployJar(ks, releaseId2, drl1 + drl2);
    kc.updateToVersion(km.getReleaseId());
}
Also used : KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) Command(org.kie.api.command.Command) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) ArrayList(java.util.ArrayList) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 3 with BatchExecutionCommand

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

the class ExecuteCommandDisconnectedTest method executeDisconnected.

@Test
public void executeDisconnected() {
    KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    KieSession ksession = kbase.newKieSession();
    ExecutionResultImpl localKresults = new ExecutionResultImpl();
    RequestContext context = RequestContext.create().with(ksession);
    ExecutableRunner runner = ExecutableRunner.create();
    List cmds = new ArrayList();
    cmds.add(new InsertObjectCommand(new String("Hi!"), "handle"));
    BatchExecutionCommand batchCmd = CommandFactory.newBatchExecution(cmds, "kresults");
    ExecuteCommand execCmd = new ExecuteCommand(batchCmd, true);
    ExecutionResults results = execCmd.execute(context);
    assertNotNull(results);
    assertNotNull(results.getFactHandle("handle"));
    assertTrue(((DefaultFactHandle) results.getFactHandle("handle")).isDisconnected());
    cmds = new ArrayList();
    cmds.add(new InsertObjectCommand(new String("Hi!"), "handle"));
    batchCmd = CommandFactory.newBatchExecution(cmds, "kresults");
    execCmd = new ExecuteCommand(batchCmd);
    results = execCmd.execute(context);
    assertNotNull(results);
    assertNotNull(results.getFactHandle("handle"));
    assertFalse(((DefaultFactHandle) results.getFactHandle("handle")).isDisconnected());
}
Also used : KieBase(org.kie.api.KieBase) ExecutionResults(org.kie.api.runtime.ExecutionResults) ExecutionResultImpl(org.drools.core.runtime.impl.ExecutionResultImpl) ExecuteCommand(org.drools.core.command.ExecuteCommand) ArrayList(java.util.ArrayList) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) RequestContext(org.kie.api.runtime.RequestContext) ExecutableRunner(org.kie.api.runtime.ExecutableRunner) Test(org.junit.Test)

Example 4 with BatchExecutionCommand

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

the class StatefulKnowledgeSessionImpl method execute.

public <T> T execute(Command<T> command) {
    ExecutableRunner<RequestContext> runner = ExecutableRunner.create();
    RequestContext context = runner.createContext().with(this.kBase).with(this);
    if (!(command instanceof BatchExecutionCommand)) {
        return runner.execute(command, context);
    }
    try {
        startBatchExecution();
        return runner.execute(command, context);
    } finally {
        endBatchExecution();
        if (kBase.flushModifications()) {
            fireAllRules();
        }
    }
}
Also used : BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) RequestContext(org.kie.api.runtime.RequestContext)

Aggregations

BatchExecutionCommand (org.kie.api.command.BatchExecutionCommand)4 ArrayList (java.util.ArrayList)2 FireAllRulesCommand (org.drools.core.command.runtime.rule.FireAllRulesCommand)2 ExecutionResultImpl (org.drools.core.runtime.impl.ExecutionResultImpl)2 Test (org.junit.Test)2 Command (org.kie.api.command.Command)2 RequestContext (org.kie.api.runtime.RequestContext)2 List (java.util.List)1 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)1 ExecuteCommand (org.drools.core.command.ExecuteCommand)1 ContextImpl (org.drools.core.command.impl.ContextImpl)1 ExecutableCommand (org.drools.core.command.impl.ExecutableCommand)1 RegistryContext (org.drools.core.command.impl.RegistryContext)1 BatchExecutionCommandImpl (org.drools.core.command.runtime.BatchExecutionCommandImpl)1 KieBase (org.kie.api.KieBase)1 KieServices (org.kie.api.KieServices)1 KieModule (org.kie.api.builder.KieModule)1 ReleaseId (org.kie.api.builder.ReleaseId)1 KieRuntimeLogger (org.kie.api.logger.KieRuntimeLogger)1 ExecutableRunner (org.kie.api.runtime.ExecutableRunner)1