use of org.kie.api.command.Command in project drools by kiegroup.
the class SimpleBatchExecutionTest method testGetObjectCommand.
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testGetObjectCommand() 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 fact = ksession.getObject(handle_1);
assertNotNull(fact);
assertEquals(expected_1, fact);
List<Command<?>> commands = new ArrayList<Command<?>>();
commands.add(CommandFactory.newGetObject(handle_1, "out_1"));
commands.add(CommandFactory.newGetObject(handle_2, "out_2"));
Command cmds = CommandFactory.newBatchExecution(commands);
ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
assertNotNull("GetObjectCommand result is null!", result);
assertEquals(expected_1, result.getValue("out_1"));
assertEquals(expected_2, result.getValue("out_2"));
}
use of org.kie.api.command.Command in project drools by kiegroup.
the class SimpleBatchExecutionTest method testInsertObjectCommand.
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testInsertObjectCommand() throws Exception {
String expected_1 = "expected_1";
String expected_2 = "expected_2";
List<Command<?>> commands = new ArrayList<Command<?>>();
commands.add(CommandFactory.newInsert(expected_1, "out_1"));
commands.add(CommandFactory.newInsert(expected_2, "out_2"));
Command cmds = CommandFactory.newBatchExecution(commands);
ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
Object fact_1 = result.getValue("out_1");
assertNotNull(fact_1);
Object fact_2 = result.getValue("out_2");
assertNotNull(fact_2);
ksession.fireAllRules();
Object[] expectedArr = { expected_1, expected_2 };
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.command.Command 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.command.Command 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.command.Command in project drools by kiegroup.
the class TraitTest method traitsInBatchExecution.
// BZ #748752
@Test(timeout = 10000)
public void traitsInBatchExecution() {
String str = "package org.jboss.qa.brms.traits\n" + "import org.drools.compiler.Person;\n" + "import org.drools.core.factmodel.traits.Traitable;\n" + "" + "global java.util.List list;" + "" + "declare Person \n" + " @Traitable \n" + "end \n" + "" + "declare trait Student\n" + " school : String\n" + "end\n" + "\n" + "rule \"create student\" \n" + " when\n" + " $student : Person( age < 26 )\n" + " then\n" + " Student s = don( $student, Student.class );\n" + " s.setSchool(\"Masaryk University\");\n" + "end\n" + "\n" + "rule \"print student\"\n" + " when\n" + " student : Person( this isA Student )\n" + " then" + " list.add( 1 );\n" + " System.out.println(\"Person is a student: \" + student);\n" + "end\n" + "\n" + "rule \"print school\"\n" + " when\n" + " Student( $school : school )\n" + " then\n" + " list.add( 2 );\n" + " System.out.println(\"Student is attending \" + $school);\n" + "end";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ByteArrayResource(str.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
throw new RuntimeException(kbuilder.getErrors().toString());
}
List list = new ArrayList();
KieBase kbase = kbuilder.newKieBase();
TraitFactory.setMode(mode, kbase);
StatelessKieSession ksession = kbase.newStatelessKieSession();
ksession.setGlobal("list", list);
List<Command<?>> commands = new ArrayList<Command<?>>();
Person student = new Person("student", 18);
commands.add(CommandFactory.newInsert(student));
System.out.println("Starting execution...");
commands.add(CommandFactory.newFireAllRules());
ksession.execute(CommandFactory.newBatchExecution(commands));
System.out.println("Finished...");
assertEquals(2, list.size());
assertTrue(list.contains(1));
assertTrue(list.contains(2));
}
Aggregations