Search in sources :

Example 1 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.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 2 with ExecutionResults

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

the class StatelessSessionTest method testInsertObject.

@Test
public void testInsertObject() throws Exception {
    String str = "";
    str += "package org.kie \n";
    str += "import org.drools.compiler.Cheese \n";
    str += "rule rule1 \n";
    str += "  when \n";
    str += "    $c : Cheese() \n";
    str += " \n";
    str += "  then \n";
    str += "    $c.setPrice( 30 ); \n";
    str += "end\n";
    Cheese stilton = new Cheese("stilton", 5);
    final StatelessKieSession ksession = getSession2(ResourceFactory.newByteArrayResource(str.getBytes()));
    final ExecutableCommand cmd = (ExecutableCommand) CommandFactory.newInsert(stilton, "outStilton");
    final BatchExecutionCommandImpl batch = new BatchExecutionCommandImpl(Arrays.asList(new ExecutableCommand<?>[] { cmd }));
    final ExecutionResults result = (ExecutionResults) ksession.execute(batch);
    stilton = (Cheese) result.getValue("outStilton");
    assertEquals(30, stilton.getPrice());
}
Also used : BatchExecutionCommandImpl(org.drools.core.command.runtime.BatchExecutionCommandImpl) ExecutionResults(org.kie.api.runtime.ExecutionResults) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) ExecutableCommand(org.drools.core.command.impl.ExecutableCommand) Cheese(org.drools.compiler.Cheese) Test(org.junit.Test)

Example 3 with ExecutionResults

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

Example 4 with ExecutionResults

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

the class FireAllRulesCommandTest method fiveRulesFiredTest.

@Test
public void fiveRulesFiredTest() {
    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(new Cheese("stilton")));
    commands.add(CommandFactory.newInsert(new Cheese("gruyere")));
    commands.add(CommandFactory.newInsert(new Cheese("cheddar")));
    commands.add(CommandFactory.newInsert(new Cheese("stinky")));
    commands.add(CommandFactory.newInsert(new Cheese("limburger")));
    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(5, 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)

Example 5 with ExecutionResults

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

the class FireAllRulesCommandTest method oneRuleFiredTest.

@Test
public void oneRuleFiredTest() {
    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(new Cheese("stilton")));
    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(1, 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

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