Search in sources :

Example 1 with Command

use of org.kie.api.command.Command 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 : ExecutableCommand(org.drools.core.command.impl.ExecutableCommand) Command(org.kie.api.command.Command) ExecutableCommand(org.drools.core.command.impl.ExecutableCommand) KieSession(org.kie.api.runtime.KieSession)

Example 2 with Command

use of org.kie.api.command.Command 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 3 with Command

use of org.kie.api.command.Command 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 4 with Command

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

the class StatelessSessionTest method testSetGlobal.

@Test
public void testSetGlobal() throws Exception {
    String str = "";
    str += "package org.kie \n";
    str += "import org.drools.compiler.Cheese \n";
    str += "global java.util.List list1 \n";
    str += "global java.util.List list2 \n";
    str += "global java.util.List list3 \n";
    str += "rule rule1 \n";
    str += "  when \n";
    str += "    $c : Cheese() \n";
    str += " \n";
    str += "  then \n";
    str += "    $c.setPrice( 30 ); \n";
    str += "    list1.add( $c ); \n";
    str += "    list2.add( $c ); \n";
    str += "    list3.add( $c ); \n";
    str += "end\n";
    final Cheese stilton = new Cheese("stilton", 5);
    final List list1 = new ArrayList();
    List list2 = new ArrayList();
    List list3 = new ArrayList();
    final StatelessKieSession ksession = getSession2(ResourceFactory.newByteArrayResource(str.getBytes()));
    final Command setGlobal1 = CommandFactory.newSetGlobal("list1", list1);
    final Command setGlobal2 = CommandFactory.newSetGlobal("list2", list2, true);
    final Command setGlobal3 = CommandFactory.newSetGlobal("list3", list3, "outList3");
    final Command insert = CommandFactory.newInsert(stilton);
    final List cmds = new ArrayList();
    cmds.add(setGlobal1);
    cmds.add(setGlobal2);
    cmds.add(setGlobal3);
    cmds.add(insert);
    final ExecutionResults result = (ExecutionResults) ksession.execute(CommandFactory.newBatchExecution(cmds));
    assertEquals(30, stilton.getPrice());
    assertNull(result.getValue("list1"));
    list2 = (List) result.getValue("list2");
    assertEquals(1, list2.size());
    assertSame(stilton, list2.get(0));
    list3 = (List) result.getValue("outList3");
    assertEquals(1, list3.size());
    assertSame(stilton, list3.get(0));
}
Also used : ExecutableCommand(org.drools.core.command.impl.ExecutableCommand) Command(org.kie.api.command.Command) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Cheese(org.drools.compiler.Cheese) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 5 with Command

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

the class FireAllRulesCommandTest method zeroRulesFiredTest.

@Test
public void zeroRulesFiredTest() {
    String str = "";
    str += "package org.drools.compiler.integrationtests \n";
    str += "import " + Cheese.class.getCanonicalName() + " \n";
    str += "rule StringRule \n";
    str += " when \n";
    str += " $c : Cheese() \n";
    str += " then \n";
    str += " System.out.println($c); \n";
    str += "end \n";
    StatelessKieSession ksession = getSession(str);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newInsert("not cheese"));
    commands.add(CommandFactory.newFireAllRules("num-rules-fired"));
    ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands));
    int fired = Integer.parseInt(results.getValue("num-rules-fired").toString());
    assertEquals(0, fired);
}
Also used : Command(org.kie.api.command.Command) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Cheese(org.drools.compiler.Cheese) Test(org.junit.Test)

Aggregations

Command (org.kie.api.command.Command)38 ArrayList (java.util.ArrayList)34 Test (org.junit.Test)34 ExecutionResults (org.kie.api.runtime.ExecutionResults)18 KieSession (org.kie.api.runtime.KieSession)14 Cheese (org.drools.compiler.Cheese)10 KieBase (org.kie.api.KieBase)9 KieServices (org.kie.api.KieServices)9 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)9 FireAllRulesCommand (org.drools.core.command.runtime.rule.FireAllRulesCommand)7 Resource (org.kie.api.io.Resource)7 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)6 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)6 List (java.util.List)5 ExecutableCommand (org.drools.core.command.impl.ExecutableCommand)5 KieSessionTest (org.drools.testcoverage.common.KieSessionTest)4 FactType (org.kie.api.definition.type.FactType)3 FactHandle (org.kie.api.runtime.rule.FactHandle)3 QueryResults (org.kie.api.runtime.rule.QueryResults)3 BatchExecutionCommandImpl (org.drools.core.command.runtime.BatchExecutionCommandImpl)2