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 MoreBatchExecutionTest method testQuery.
@Test
public void testQuery() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/simple_query_test.drl"), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
ksession = createKnowledgeSession(kbase);
ksession.insert(new Cheese("stinky", 5));
ksession.insert(new Cheese("smelly", 7));
List<Command<?>> commands = new ArrayList<Command<?>>();
commands.add(CommandFactory.newQuery("numStinkyCheeses", "simple query"));
Command<?> cmds = CommandFactory.newBatchExecution(commands);
ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
assertNotNull("Batch execution result is null!", result);
Object queryResultsObject = result.getValue("numStinkyCheeses");
assertTrue("Retrieved object is null or incorrect!", queryResultsObject != null && queryResultsObject instanceof QueryResults);
assertEquals(1, ((QueryResults) queryResultsObject).size());
}
use of org.kie.api.runtime.ExecutionResults in project drools by kiegroup.
the class StatelessSessionTest method testQuery.
@Test
public void testQuery() throws Exception {
String str = "";
str += "package org.kie.test \n";
str += "import org.drools.compiler.Cheese \n";
str += "query cheeses \n";
str += " stilton : Cheese(type == 'stilton') \n";
str += " cheddar : Cheese(type == 'cheddar', price == stilton.price) \n";
str += "end\n";
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
kbase = SerializationHelper.serializeObject(kbase);
final StatelessKieSession ksession = kbase.newStatelessKieSession();
final Cheese stilton1 = new Cheese("stilton", 1);
final Cheese cheddar1 = new Cheese("cheddar", 1);
final Cheese stilton2 = new Cheese("stilton", 2);
final Cheese cheddar2 = new Cheese("cheddar", 2);
final Cheese stilton3 = new Cheese("stilton", 3);
final Cheese cheddar3 = new Cheese("cheddar", 3);
final Set set = new HashSet();
List list = new ArrayList();
list.add(stilton1);
list.add(cheddar1);
set.add(list);
list = new ArrayList();
list.add(stilton2);
list.add(cheddar2);
set.add(list);
list = new ArrayList();
list.add(stilton3);
list.add(cheddar3);
set.add(list);
final List<Command> cmds = new ArrayList<Command>();
cmds.add(CommandFactory.newInsert(stilton1));
cmds.add(CommandFactory.newInsert(stilton2));
cmds.add(CommandFactory.newInsert(stilton3));
cmds.add(CommandFactory.newInsert(cheddar1));
cmds.add(CommandFactory.newInsert(cheddar2));
cmds.add(CommandFactory.newInsert(cheddar3));
cmds.add(CommandFactory.newQuery("cheeses", "cheeses"));
final ExecutionResults batchResult = (ExecutionResults) ksession.execute(CommandFactory.newBatchExecution(cmds));
final org.kie.api.runtime.rule.QueryResults results = (org.kie.api.runtime.rule.QueryResults) batchResult.getValue("cheeses");
assertEquals(3, results.size());
assertEquals(2, results.getIdentifiers().length);
final Set newSet = new HashSet();
for (final org.kie.api.runtime.rule.QueryResultsRow result : results) {
list = new ArrayList();
list.add(result.get("stilton"));
list.add(result.get("cheddar"));
newSet.add(list);
}
assertEquals(set, newSet);
}
use of org.kie.api.runtime.ExecutionResults in project jbpm by kiegroup.
the class BusinessRuleTaskHandler method handleStateless.
protected void handleStateless(WorkItem workItem, String kieSessionName, Map<String, Object> parameters, Map<String, Object> results) {
logger.debug("Evalating rules in stateless session with name {}", kieSessionName);
StatelessKieSession kieSession = kieContainer.newStatelessKieSession(kieSessionName);
List<Command<?>> commands = new ArrayList<Command<?>>();
for (Entry<String, Object> entry : parameters.entrySet()) {
String inputKey = workItem.getId() + "_" + entry.getKey();
commands.add(commandsFactory.newInsert(entry.getValue(), inputKey, true, null));
}
commands.add(commandsFactory.newFireAllRules("Fired"));
BatchExecutionCommand executionCommand = commandsFactory.newBatchExecution(commands);
ExecutionResults executionResults = kieSession.execute(executionCommand);
logger.debug("{} rules fired", executionResults.getValue("Fired"));
for (Entry<String, Object> entry : parameters.entrySet()) {
String inputKey = workItem.getId() + "_" + entry.getKey();
String key = entry.getKey().replaceAll(workItem.getId() + "_", "");
results.put(key, executionResults.getValue(inputKey));
}
}
use of org.kie.api.runtime.ExecutionResults in project drools by kiegroup.
the class QueryTest method testRecursiveQueryWithBatchCommand.
@Test
public void testRecursiveQueryWithBatchCommand() throws Exception {
String str = "package org.test;\n" + "import " + Person.class.getCanonicalName() + ";" + "query isContainedIn(String x, String y)\n" + " Location (x, y;)\n" + " or\n" + " ( Location (z, y;) and ?isContainedIn(x, z;))\n" + "end\n" + "declare Location\n" + " thing : String\n" + " location : String\n" + "end";
KieServices kieServices = KieServices.Factory.get();
KieSession ksession = getKieSession(str);
FactType locationType = ksession.getKieBase().getFactType("org.test", "Location");
// a pear is in the kitchen
final Object pear = locationType.newInstance();
locationType.set(pear, "thing", "pear");
locationType.set(pear, "location", "kitchen");
// a desk is in the office
final Object desk = locationType.newInstance();
locationType.set(desk, "thing", "desk");
locationType.set(desk, "location", "office");
// a flashlight is on the desk
final Object flashlight = locationType.newInstance();
locationType.set(flashlight, "thing", "flashlight");
locationType.set(flashlight, "location", "desk");
// an envelope is on the desk
final Object envelope = locationType.newInstance();
locationType.set(envelope, "thing", "envelope");
locationType.set(envelope, "location", "desk");
// a key is in the envelope
final Object key = locationType.newInstance();
locationType.set(key, "thing", "key");
locationType.set(key, "location", "envelope");
// create working memory objects
final List<Command<?>> commands = new ArrayList<Command<?>>();
// Location instances
commands.add(kieServices.getCommands().newInsert(pear));
commands.add(kieServices.getCommands().newInsert(desk));
commands.add(kieServices.getCommands().newInsert(flashlight));
commands.add(kieServices.getCommands().newInsert(envelope));
commands.add(kieServices.getCommands().newInsert(key));
// fire all rules
final String queryAlias = "myQuery";
commands.add(kieServices.getCommands().newQuery(queryAlias, "isContainedIn", new Object[] { Variable.v, "office" }));
final ExecutionResults results = ksession.execute(kieServices.getCommands().newBatchExecution(commands, null));
final QueryResults qResults = (QueryResults) results.getValue(queryAlias);
final List<String> l = new ArrayList<String>();
for (QueryResultsRow r : qResults) {
l.add((String) r.get("x"));
}
// items in the office should be the following
Assertions.assertThat(l.size()).isEqualTo(4);
Assertions.assertThat(l.contains("desk")).isTrue();
Assertions.assertThat(l.contains("flashlight")).isTrue();
Assertions.assertThat(l.contains("envelope")).isTrue();
Assertions.assertThat(l.contains("key")).isTrue();
}
Aggregations