use of org.kie.api.runtime.ExecutionResults in project drools by kiegroup.
the class MoreBatchExecutionTest method testFireAllRules.
@Test
public void testFireAllRules() {
KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(this.getClass(), kieBaseTestConfiguration, "org/drools/mvel/integrationtests/drl/test_ImportFunctions.drl");
ksession = kbase.newKieSession();
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 SimpleBatchExecutionTest method testGetGlobalCommand.
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testGetGlobalCommand() throws Exception {
ksession.insert(new Integer(5));
ksession.insert(new Integer(7));
ksession.fireAllRules();
ksession.setGlobal("globalCheeseCountry", "France");
List<Command<?>> commands = new ArrayList<Command<?>>();
commands.add(CommandFactory.newGetGlobal("globalCheeseCountry", "cheeseCountry"));
Command cmds = CommandFactory.newBatchExecution(commands);
ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
assertNotNull("GetGlobalCommand result is null!", result);
Object global = result.getValue("cheeseCountry");
assertNotNull("Retrieved global fact is null!", global);
assertEquals("Retrieved global is not equal to 'France'.", "France", global);
}
use of org.kie.api.runtime.ExecutionResults in project drools by kiegroup.
the class SimpleBatchExecutionTest method testGetObjectsCommand.
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testGetObjectsCommand() throws Exception {
String expected_1 = "expected_1";
String expected_2 = "expected_2";
FactHandle handle_1 = ksession.insert(expected_1);
FactHandle handle_2 = ksession.insert(expected_2);
ksession.fireAllRules();
Object object = ksession.getObject(handle_1);
assertNotNull(object);
assertEquals(expected_1, object);
object = ksession.getObject(handle_2);
assertNotNull(object);
assertEquals(expected_2, object);
List<Command<?>> commands = new ArrayList<Command<?>>();
commands.add(CommandFactory.newGetObjects("out_list"));
Command cmds = CommandFactory.newBatchExecution(commands);
ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
assertNotNull("GetObjectsCommand result is null!", result);
List<Object> objectList = (List) result.getValue("out_list");
assertTrue("Retrieved object list is null or empty!", objectList != null && !objectList.isEmpty());
Collection<? extends Object> factList = ksession.getObjects();
Object[] expectedArr = { expected_1, expected_2 };
List<Object> expectedList = new ArrayList<Object>(Arrays.asList(expectedArr));
assertTrue("Expected " + expectedList.size() + " objects but retrieved " + factList.size(), factList.size() == expectedList.size());
for (Object fact : factList) {
if (expectedList.contains(fact)) {
expectedList.remove(fact);
}
}
assertTrue("Retrieved object list did not contain expected objects.", expectedList.isEmpty());
}
use of org.kie.api.runtime.ExecutionResults in project drools by kiegroup.
the class SimpleBatchExecutionTest method testInsertElementsCommand.
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testInsertElementsCommand() throws Exception {
String expected_1 = "expected_1";
String expected_2 = "expected_2";
Object[] expectedArr = { expected_1, expected_2 };
Collection<Object> factCollection = Arrays.asList(expectedArr);
List<Command<?>> commands = new ArrayList<Command<?>>();
commands.add(CommandFactory.newInsertElements(factCollection, "out_list", true, null));
Command cmds = CommandFactory.newBatchExecution(commands);
ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
Collection<? extends Object> outList = (Collection<? extends Object>) result.getValue("out_list");
assertNotNull(outList);
ksession.fireAllRules();
List<Object> expectedList = new ArrayList<Object>(Arrays.asList(expectedArr));
Collection<? extends Object> factList = ksession.getObjects();
assertTrue("Expected " + expectedList.size() + " objects but retrieved " + factList.size(), factList.size() == expectedList.size());
for (Object fact : factList) {
if (expectedList.contains(fact)) {
expectedList.remove(fact);
}
}
assertTrue("Retrieved object list did not contain expected objects.", expectedList.isEmpty());
}
use of org.kie.api.runtime.ExecutionResults in project drools by kiegroup.
the class FireAllRulesCommandTest method infiniteLoopTerminatesAtMaxTest.
@Test
public void infiniteLoopTerminatesAtMaxTest() {
String str = "";
str += "package org.drools.mvel.integrationtests \n";
str += "import " + Cheese.class.getCanonicalName() + " \n";
str += "rule StringRule \n";
str += " when \n";
str += " $c : Cheese() \n";
str += " then \n";
str += " update($c); \n";
str += "end \n";
StatelessKieSession ksession = getSession(str);
List<Command<?>> commands = new ArrayList<Command<?>>();
commands.add(CommandFactory.newInsert(new Cheese("stilton")));
FireAllRulesCommand farc = (FireAllRulesCommand) CommandFactory.newFireAllRules(10);
farc.setOutIdentifier("num-rules-fired");
commands.add(farc);
ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands));
int fired = Integer.parseInt(results.getValue("num-rules-fired").toString());
assertEquals(10, fired);
}
Aggregations