use of org.kie.api.runtime.rule.QueryResults 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();
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class QueryTest method testUnificationParameterInPattern.
@Test
public void testUnificationParameterInPattern() {
String str = "import " + Person.class.getCanonicalName() + ";" + "query personsAges(int ages)\n" + "$p : Person(ages := age)\n" + "end\n";
KieSession ksession = getKieSession(str);
ksession.insert(new Person("Mark", 39));
ksession.insert(new Person("Mario", 41));
QueryResults results = ksession.getQueryResults("personsAges", 41);
assertEquals(1, results.size());
Person p = (Person) results.iterator().next().get("$p");
assertEquals("Mario", p.getName());
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class QueryTest method testQueryOneArgumentWithoutType.
@Test
public void testQueryOneArgumentWithoutType() {
String str = "import " + Person.class.getCanonicalName() + ";" + "query olderThan( $age )\n" + " $p : Person(age > (Integer)$age)\n" + "end ";
KieSession ksession = getKieSession(str);
ksession.insert(new Person("Mark", 39));
ksession.insert(new Person("Mario", 41));
QueryResults results = ksession.getQueryResults("olderThan", 40);
assertEquals(1, results.size());
Person p = (Person) results.iterator().next().get("$p");
assertEquals("Mario", p.getName());
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class JpaPersistenceTraitTest method testTraitWithJPAOnFreshKieBase.
@Test
public void testTraitWithJPAOnFreshKieBase() {
// DROOLS-904
String str = "package org.drools.trait.test; " + "global java.util.List list; " + "declare TBean2 " + " @propertyReactive " + " @Traitable " + "end " + "declare trait Mask " + " @propertyReactive " + "end " + "query getTraits( Mask $m ) " + " $m := Mask() " + "end " + "rule Init when then don( new TBean2(), Mask.class ); end " + "rule Trig when String() then don( new TBean2(), Mask.class ); end " + "rule Main when $m : Mask() then list.add( $m ); end ";
List list = new ArrayList();
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());
StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
ksession.setGlobal("list", list);
ksession.fireAllRules();
long id = ksession.getIdentifier();
InternalKnowledgeBase kbase2 = KnowledgeBaseFactory.newKnowledgeBase();
TraitFactory.setMode(VirtualPropertyMode.MAP, kbase);
kbase2.addPackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession2 = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase2, null, env);
ksession.setGlobal("list", list);
ksession2.insert("go");
ksession2.fireAllRules();
assertEquals(2, list.size());
Class<?> oldProxyClass = list.get(0).getClass();
Class<?> newProxyClass = list.get(1).getClass();
assertNotSame(oldProxyClass, newProxyClass);
QueryResults qry = ksession2.getQueryResults("getTraits", Variable.v);
assertEquals(2, qry.size());
java.util.Iterator<QueryResultsRow> iter = qry.iterator();
int j = 0;
while (iter.hasNext()) {
QueryResultsRow row = iter.next();
Object entry = row.get("$m");
assertNotNull(entry);
assertSame(newProxyClass, entry.getClass());
j++;
}
assertEquals(2, j);
for (Object o : ksession2.getObjects()) {
if (o.getClass().getName().contains("Mask")) {
assertSame(newProxyClass, o.getClass());
}
}
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class PMMLUsageDemoTest method invokePmmlWithTrait.
@Test
public void invokePmmlWithTrait() {
String extraDrl = "package org.drools.pmml.pmml_4_2.test;" + "" + "import org.drools.core.factmodel.traits.Entity;" + "" + "rule \"Init\" " + "when " + " $s : String( this == \"trigger\" ) " + "then " + " System.out.println( \"Trig\" ); " + " Entity o = new Entity(); " + " insert( o ); \n" + "" + // both soft and hard fields will be used to feed data into the model
"" + " MockColdTrait input = don( o, MockColdTrait.class ); " + " modify( input ) { " + " setTemp( 22.0 );" + " } " + "end " + "" + "" + "rule Log when $x : MockColdTrait() then System.out.println( \"IN \" + $x ); end " + "rule Log2 when $x : Cold() then System.out.println( \"OUT \" + $x ); end ";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write(ResourceFactory.newClassPathResource(pmmlSource).setResourceType(ResourceType.PMML));
kfs.write(ResourceFactory.newByteArrayResource(extraDrl.getBytes()).setSourcePath("rules.drl").setResourceType(ResourceType.DRL));
Results res = ks.newKieBuilder(kfs).buildAll().getResults();
if (res.hasMessages(Message.Level.ERROR)) {
System.out.println(res.getMessages(Message.Level.ERROR));
}
assertEquals(0, res.getMessages(Message.Level.ERROR).size());
KieSession kSession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
kSession.insert("trigger");
kSession.fireAllRules();
System.out.println(reportWMObjects(kSession));
QueryResults qrs = kSession.getQueryResults("Cold", "MockCold", Variable.v);
assertTrue(qrs.iterator().hasNext());
Object val = qrs.iterator().next().get("$result");
Assert.assertEquals(0.56, val);
}
Aggregations