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));
}
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());
}
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));
}
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));
}
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());
}
Aggregations