Search in sources :

Example 11 with ExecutionResults

use of org.kie.api.runtime.ExecutionResults in project drools by kiegroup.

the class MoreBatchExecutionTest method testFireAllRules.

@Test
public void testFireAllRules() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/drl/test_ImportFunctions.drl"), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    ksession = createKnowledgeSession(kbase);
    final Cheese cheese = new Cheese("stilton", 15);
    ksession.insert(cheese);
    List<?> list = new ArrayList();
    ksession.setGlobal("list", list);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newFireAllRules("fired"));
    Command<?> cmds = CommandFactory.newBatchExecution(commands);
    ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
    assertNotNull("Batch execution result is null!", result);
    Object firedObject = result.getValue("fired");
    assertTrue("Retrieved object is null or incorrect!", firedObject != null && firedObject instanceof Integer);
    assertEquals(4, firedObject);
    list = (List<?>) ksession.getGlobal("list");
    assertEquals(4, list.size());
    assertEquals("rule1", list.get(0));
    assertEquals("rule2", list.get(1));
    assertEquals("rule3", list.get(2));
    assertEquals("rule4", list.get(3));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Command(org.kie.api.command.Command) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) Cheese(org.drools.compiler.Cheese) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 12 with ExecutionResults

use of org.kie.api.runtime.ExecutionResults 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 13 with ExecutionResults

use of org.kie.api.runtime.ExecutionResults 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.mvel.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.kie.api.command.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.mvel.compiler.Cheese) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 14 with ExecutionResults

use of org.kie.api.runtime.ExecutionResults in project drools by kiegroup.

the class ActivationGroupTest method testClearActivationGroup.

@Test
public void testClearActivationGroup() {
    // DROOLS-5685 ClearActivationGroup command changed its behaviour
    String str = "import java.util.List;\n" + "\n" + "global List list\n" + "\n" + "rule \"First rule in first activation group\" @Propagation(IMMEDIATE)\n" + "activation-group \"first-group\"\n" + "    salience 10\n" + "when\n" + "then\n" + "    list.add(\"First rule in first activation group executed\");\n" + "end\n" + "\n" + "rule \"Second rule in first activation group\" @Propagation(IMMEDIATE)\n" + "activation-group \"first-group\"\n" + "    salience 5\n" + "when\n" + "then\n" + "    list.add(\"Second rule in first activation group executed\");\n" + "end\n" + "\n" + "rule \"Rule without activation group\" @Propagation(IMMEDIATE)\n" + "    salience 2\n" + "when\n" + "then\n" + "    list.add(\"Rule without activation group executed\");\n" + "end";
    List<Command<?>> commands = new ArrayList<Command<?>>();
    KieSession ksession = getKieSession(str);
    KieCommands commandsFactory = KieServices.get().getCommands();
    BatchExecutionCommand batchExecution = commandsFactory.newBatchExecution(commands, KIE_SESSION);
    commands.add(commandsFactory.newSetGlobal(LIST_NAME, new ArrayList<String>(), LIST_OUTPUT_NAME));
    // Replace if/after Clear command is added to command factory.
    // commands.add(commandsFactory.newClearActivationGroup(ACTIVATION_GROUP));
    commands.add(new ClearActivationGroupCommand(ACTIVATION_GROUP));
    commands.add(commandsFactory.newFireAllRules());
    commands.add(commandsFactory.newGetGlobal(LIST_NAME, LIST_OUTPUT_NAME));
    ExecutionResults result = ksession.execute(batchExecution);
    List<?> outcome = (List<?>) result.getValue(LIST_OUTPUT_NAME);
    assertNotNull(outcome);
    assertEquals(1, outcome.size());
    assertEquals("Rule without activation group executed", outcome.get(0));
}
Also used : KieCommands(org.kie.api.command.KieCommands) ClearActivationGroupCommand(org.drools.core.command.runtime.rule.ClearActivationGroupCommand) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) Command(org.kie.api.command.Command) ClearActivationGroupCommand(org.drools.core.command.runtime.rule.ClearActivationGroupCommand) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 15 with ExecutionResults

use of org.kie.api.runtime.ExecutionResults 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) InsertObjectCommand(org.drools.core.command.runtime.rule.InsertObjectCommand) ExecutableRunner(org.kie.api.runtime.ExecutableRunner) Test(org.junit.Test)

Aggregations

ExecutionResults (org.kie.api.runtime.ExecutionResults)41 Test (org.junit.Test)39 ArrayList (java.util.ArrayList)37 Command (org.kie.api.command.Command)35 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)17 FireAllRulesCommand (org.drools.core.command.runtime.rule.FireAllRulesCommand)11 Cheese (org.drools.compiler.Cheese)10 Cheese (org.drools.mvel.compiler.Cheese)10 List (java.util.List)9 KieSession (org.kie.api.runtime.KieSession)8 KieBase (org.kie.api.KieBase)7 FactHandle (org.kie.api.runtime.rule.FactHandle)5 BatchExecutionCommand (org.kie.api.command.BatchExecutionCommand)4 QueryResults (org.kie.api.runtime.rule.QueryResults)4 ExecutableCommand (org.drools.core.command.impl.ExecutableCommand)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 ExecutionResultImpl (org.drools.core.runtime.impl.ExecutionResultImpl)3 KieServices (org.kie.api.KieServices)3 ExecutableCommand (org.kie.api.command.ExecutableCommand)3 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)3