use of org.kie.api.runtime.rule.QueryResultsRow in project drools by kiegroup.
the class BackwardChainingTest method testNaniSearchs.
private void testNaniSearchs(PropertySpecificOption propertySpecificOption) throws Exception {
// http://www.amzi.com/AdventureInProlog/advtop.php
String str = "" + "package org.drools.compiler.test \n" + "import java.util.List\n" + "import java.util.ArrayList\n" + "import java.util.Map\n" + "import java.util.HashMap\n" + "global List list\n" + "dialect \"mvel\"\n" + "declare Room" + " name : String\n" + "end\n" + "\n" + "declare Location\n" + " thing : String \n" + " location : String \n" + "end" + "\n" + "declare Door\n" + " fromLocation : String\n" + " toLocation : String\n" + "end" + "\n" + "declare Edible\n" + " thing : String\n" + "end" + "\n" + "declare TastesYucky\n" + " thing : String\n" + "end\n" + "\n" + "declare Here\n" + " place : String \n" + "end\n" + "\n" + "query whereFood( String x, String y ) \n" + " ( Location(x, y;) and\n" + " Edible(x;) ) " + " or \n " + " ( Location(z, y;) and ?whereFood(x, z;) )\n" + "end\n" + "query connect( String x, String y ) \n" + " Door(x, y;)\n" + " or \n" + " Door(y, x;)\n" + "end\n" + "\n" + "query isContainedIn( String x, String y ) \n" + " Location(x, y;)\n" + " or \n" + " ( Location(z, y;) and ?isContainedIn(x, z;) )\n" + "end\n" + "\n" + "query look(String place, List things, List food, List exits ) \n" + " Here(place;)\n" + " things := List() from accumulate( Location(thing, place;),\n" + " collectList( thing ) )\n" + " food := List() from accumulate( ?whereFood(thing, place;) ," + " collectList( thing ) )\n" + " exits := List() from accumulate( ?connect(place, exit;),\n" + " collectList( exit ) )\n" + "end\n" + "\n" + "rule reactiveLook when\n" + " Here( place : place) \n" + " ?look(place, things, food, exits;)\n" + "then\n" + " Map map = new HashMap();" + " list.add(map);" + " map.put( 'place', place); " + " map.put( 'things', things); " + " map.put( 'food', food); " + " map.put( 'exits', exits); " + " System.out.println( \"You are in the \" + place);\n" + " System.out.println( \" You can see \" + things );\n" + " System.out.println( \" You can eat \" + food );\n" + " System.out.println( \" You can go to \" + exits );\n" + "end\n" + "\n" + "rule init when\n" + "then\n" + " insert( new Room(\"kitchen\") );\n" + " insert( new Room(\"office\") );\n" + " insert( new Room(\"hall\") );\n" + " insert( new Room(\"dining room\") );\n" + " insert( new Room(\"cellar\") );\n" + " \n" + " insert( new Location(\"apple\", \"kitchen\") );\n" + " insert( new Location(\"desk\", \"office\") );\n" + " insert( new Location(\"apple\", \"desk\") );\n" + " insert( new Location(\"flashlight\", \"desk\") );\n" + " insert( new Location(\"envelope\", \"desk\") );\n" + " insert( new Location(\"key\", \"envelope\") );\n" + " insert( new Location(\"washing machine\", \"cellar\") );\n" + " insert( new Location(\"nani\", \"washing machine\") );\n" + " insert( new Location(\"broccoli\", \"kitchen\") );\n" + " insert( new Location(\"crackers\", \"kitchen\") );\n" + " insert( new Location(\"computer\", \"office\") );\n" + " \n" + " insert( new Door(\"office\", \"hall\") );\n" + " insert( new Door(\"kitchen\", \"office\") );\n" + " insert( new Door(\"hall\", \"dining room\") );\n" + " insert( new Door(\"kitchen\", \"cellar\") );\n" + " insert( new Door(\"dining room\", \"kitchen\") );\n" + " \n" + " insert( new Edible(\"apple\") );\n" + " insert( new Edible(\"crackers\") );\n" + " \n" + " insert( new TastesYucky(\"broccoli\") ); " + "end\n" + "" + "rule go1 when\n" + " String( this == 'go1' )\n" + "then\n" + " insert( new Here(\"kitchen\") );\n" + "end\n" + "\n" + "rule go2 when\n" + " String( this == 'go2' )\n" + " $h : Here( place == \"kitchen\")" + "then\n" + " modify( $h ) { place = \"office\" };\n" + "end\n" + "";
KieHelper helper = new KieHelper(propertySpecificOption);
helper.addContent(str, ResourceType.DRL);
KieSession ksession = helper.build().newKieSession();
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
ksession.setGlobal("list", list);
QueryResults results = null;
ksession.fireAllRules();
ksession.insert("go1");
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
Map<String, Object> map = (Map) list.get(0);
assertEquals("kitchen", map.get("place"));
List<String> items = (List<String>) map.get("things");
assertEquals(3, items.size());
assertContains(new String[] { "apple", "broccoli", "crackers" }, items);
items = (List<String>) map.get("food");
assertEquals(2, items.size());
assertContains(new String[] { "apple", "crackers" }, items);
items = (List<String>) map.get("exits");
assertEquals(3, items.size());
assertContains(new String[] { "office", "cellar", "dining room" }, items);
ksession.insert("go2");
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true, false);
ksession.fireAllRules();
map = (Map) list.get(1);
assertEquals("office", map.get("place"));
items = (List<String>) map.get("things");
assertEquals(2, items.size());
assertContains(new String[] { "computer", "desk" }, items);
items = (List<String>) map.get("food");
assertEquals(1, items.size());
assertContains(new String[] { "apple" }, // notice the apple is on the desk in the office
items);
items = (List<String>) map.get("exits");
assertEquals(2, items.size());
assertContains(new String[] { "hall", "kitchen" }, items);
results = ksession.getQueryResults("isContainedIn", new Object[] { "key", "office" });
assertEquals(1, results.size());
QueryResultsRow result = results.iterator().next();
assertEquals("key", result.get("x"));
assertEquals("office", result.get("y"));
results = ksession.getQueryResults("isContainedIn", new Object[] { "key", Variable.v });
List<List<String>> l = new ArrayList<List<String>>();
for (QueryResultsRow r : results) {
l.add(Arrays.asList(new String[] { (String) r.get("x"), (String) r.get("y") }));
}
assertEquals(3, results.size());
assertContains(Arrays.asList(new String[] { "key", "desk" }), l);
assertContains(Arrays.asList(new String[] { "key", "office" }), l);
assertContains(Arrays.asList(new String[] { "key", "envelope" }), l);
results = ksession.getQueryResults("isContainedIn", new Object[] { Variable.v, "office" });
l = new ArrayList<List<String>>();
for (QueryResultsRow r : results) {
l.add(Arrays.asList(new String[] { (String) r.get("x"), (String) r.get("y") }));
}
assertEquals(6, results.size());
assertContains(Arrays.asList(new String[] { "desk", "office" }), l);
assertContains(Arrays.asList(new String[] { "computer", "office" }), l);
assertContains(Arrays.asList(new String[] { "apple", "office" }), l);
assertContains(Arrays.asList(new String[] { "envelope", "office" }), l);
assertContains(Arrays.asList(new String[] { "flashlight", "office" }), l);
assertContains(Arrays.asList(new String[] { "key", "office" }), l);
results = ksession.getQueryResults("isContainedIn", new Object[] { Variable.v, Variable.v });
l = new ArrayList<List<String>>();
for (QueryResultsRow r : results) {
l.add(Arrays.asList(new String[] { (String) r.get("x"), (String) r.get("y") }));
}
assertEquals(17, results.size());
assertContains(Arrays.asList(new String[] { "apple", "kitchen" }), l);
assertContains(Arrays.asList(new String[] { "apple", "desk" }), l);
assertContains(Arrays.asList(new String[] { "envelope", "desk" }), l);
assertContains(Arrays.asList(new String[] { "desk", "office" }), l);
assertContains(Arrays.asList(new String[] { "computer", "office" }), l);
assertContains(Arrays.asList(new String[] { "washing machine", "cellar" }), l);
assertContains(Arrays.asList(new String[] { "key", "envelope" }), l);
assertContains(Arrays.asList(new String[] { "broccoli", "kitchen" }), l);
assertContains(Arrays.asList(new String[] { "nani", "washing machine" }), l);
assertContains(Arrays.asList(new String[] { "crackers", "kitchen" }), l);
assertContains(Arrays.asList(new String[] { "flashlight", "desk" }), l);
assertContains(Arrays.asList(new String[] { "nani", "cellar" }), l);
assertContains(Arrays.asList(new String[] { "apple", "office" }), l);
assertContains(Arrays.asList(new String[] { "envelope", "office" }), l);
assertContains(Arrays.asList(new String[] { "flashlight", "office" }), l);
assertContains(Arrays.asList(new String[] { "key", "office" }), l);
assertContains(Arrays.asList(new String[] { "key", "desk" }), l);
}
use of org.kie.api.runtime.rule.QueryResultsRow in project drools by kiegroup.
the class OOPathDtablesTest method verifyQueryResults.
private void verifyQueryResults(final QueryResults results) {
assertThat(results).isNotEmpty();
final QueryResultsRow resultsRow = results.iterator().next();
assertThat(resultsRow.get("$cities")).isInstanceOf(List.class);
final List<String> cities = (List<String>) resultsRow.get("$cities");
assertThat(cities).containsExactlyInAnyOrder("Nice City", "Big City");
}
use of org.kie.api.runtime.rule.QueryResultsRow in project drools by kiegroup.
the class QueryTest method testQueryZeroArgs.
@Test
public void testQueryZeroArgs() {
String str = "import " + Person.class.getCanonicalName() + ";" + "global java.lang.Integer ageG;" + "query \"older than\"\n" + " $p : Person(age > ageG)\n" + "end ";
KieSession ksession = getKieSession(str);
ksession.setGlobal("ageG", 40);
ksession.insert(new Person("Mark", 39));
ksession.insert(new Person("Mario", 41));
QueryResults results = ksession.getQueryResults("older than", 40);
assertEquals(1, results.size());
QueryResultsRow res = results.iterator().next();
Person p = (Person) res.get("$p");
assertEquals("Mario", p.getName());
}
use of org.kie.api.runtime.rule.QueryResultsRow in project drools by kiegroup.
the class TraitTest method testTypeRefractionOnQueryWithIsA.
@Test(timeout = 10000)
public void testTypeRefractionOnQueryWithIsA() {
String source = "package t.x \n" + "import java.util.*; \n" + "import org.drools.core.factmodel.traits.Thing \n" + "import org.drools.core.factmodel.traits.Traitable \n" + "\n" + "global java.util.List list; \n" + "\n" + "" + "declare trait A @propertyReactive end\n" + "declare trait B extends A @propertyReactive end\n" + "declare trait C extends B @propertyReactive end\n" + "declare trait D extends A @propertyReactive end\n" + "declare trait E extends C, D @propertyReactive end\n" + "declare trait F extends E @propertyReactive end\n" + "" + "declare Kore\n" + " @Traitable\n" + "end\n" + "" + "rule Init when\n" + "then\n" + " Kore k = new Kore();\n" + " don( k, C.class ); \n" + " don( k, D.class ); \n" + " don( k, E.class ); \n" + " don( k, B.class ); \n" + " don( k, A.class ); \n" + " don( k, F.class ); \n" + " shed( k, B.class ); \n" + "end\n" + "" + " \n" + "query queryA\n" + " $x := Kore( this isA A ) \n" + "end\n" + "";
KieSession ks = getSessionFromString(source);
TraitFactory.setMode(mode, ks.getKieBase());
List list = new ArrayList();
ks.setGlobal("list", list);
ks.fireAllRules();
QueryResults res = ks.getQueryResults("queryA");
Iterator<QueryResultsRow> iter = res.iterator();
Object a = iter.next().get("$x");
assertFalse(iter.hasNext());
assertEquals(1, res.size());
}
use of org.kie.api.runtime.rule.QueryResultsRow in project drools by kiegroup.
the class BackwardChainingTest method testQueryWithOr.
@Test(timeout = 10000)
public void testQueryWithOr() throws Exception {
String str = "" + "package org.drools.compiler.test \n" + "import java.util.List\n" + "import java.util.ArrayList\n" + "global List list\n" + "dialect \"mvel\"\n" + "\n" + "import " + BackwardChainingTest.class.getName() + ".Q\n" + "import " + BackwardChainingTest.class.getName() + ".R\n" + "import " + BackwardChainingTest.class.getName() + ".S\n" + "\n" + "query q(int x)\n" + " Q( x := value )\n" + "end\n" + "\n" + "query r(int x)\n" + " R( x := value )\n" + "end\n" + "\n" + "query s(int x)\n" + " S( x := value ) \n" + "end\n" + "\n" + "query p(int x)\n" + " (?q(x;) and ?r(x;) ) \n" + " or\n" + " ?s(x;)\n" + "end\n" + "rule init when\n" + "then\n" + " insert( new Q(1) );\n " + " insert( new Q(5) );\n " + " insert( new Q(6) );\n " + " insert( new R(1) );\n " + " insert( new R(4) );\n " + " insert( new R(6) );\n " + " insert( new R(2) );\n " + " insert( new S(2) );\n " + " insert( new S(3) );\n " + " insert( new S(6) );\n " + "end\n" + "";
KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(str));
KieSession ksession = createKnowledgeSession(kbase);
List<Integer> list = new ArrayList<Integer>();
ksession.setGlobal("list", list);
ksession.fireAllRules();
QueryResults results = null;
list.clear();
results = ksession.getQueryResults("p", new Integer[] { 0 });
for (QueryResultsRow result : results) {
list.add((Integer) result.get("x"));
}
assertEquals(0, list.size());
list.clear();
results = ksession.getQueryResults("p", new Integer[] { 1 });
for (QueryResultsRow result : results) {
list.add((Integer) result.get("x"));
}
assertEquals(1, list.size());
assertEquals(1, list.get(0).intValue());
list.clear();
results = ksession.getQueryResults("p", new Integer[] { 2 });
for (QueryResultsRow result : results) {
list.add((Integer) result.get("x"));
}
assertEquals(1, list.size());
assertEquals(2, list.get(0).intValue());
list.clear();
results = ksession.getQueryResults("p", new Integer[] { 3 });
for (QueryResultsRow result : results) {
list.add((Integer) result.get("x"));
}
assertEquals(1, list.size());
assertEquals(3, list.get(0).intValue());
list.clear();
results = ksession.getQueryResults("p", new Integer[] { 4 });
for (QueryResultsRow result : results) {
list.add((Integer) result.get("x"));
}
assertEquals(0, list.size());
list.clear();
results = ksession.getQueryResults("p", new Integer[] { 5 });
for (QueryResultsRow result : results) {
list.add((Integer) result.get("x"));
}
assertEquals(0, list.size());
list.clear();
results = ksession.getQueryResults("p", new Integer[] { 6 });
for (QueryResultsRow result : results) {
list.add((Integer) result.get("x"));
}
assertEquals(2, list.size());
assertEquals(6, list.get(0).intValue());
assertEquals(6, list.get(1).intValue());
}
Aggregations