use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class CepQueryTest method testQueryWithAfter.
/**
* Tests query using temporal operator 'after' on events from two entry points.
*/
// (timeout=10000)
@Test
public void testQueryWithAfter() {
this.eventsInitialization();
QueryResults results = ksession.getQueryResults("EventsAfterZeroToNineSeconds");
assertEquals("Unexpected query result length", 1, results.size());
assertEquals("Unexpected query result content", "one", ((TestEvent) results.iterator().next().get("$result")).getName());
}
use of org.kie.api.runtime.rule.QueryResults 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.QueryResults in project drools by kiegroup.
the class DroolsAbstractPMMLTest method queryIntegerField.
protected double queryIntegerField(String target, String modelName) {
QueryResults results = getKSession().getQueryResults(target, modelName, Variable.v);
assertEquals(1, results.size());
return (Integer) results.iterator().next().get("$result");
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class DroolsAbstractPMMLTest method queryStringField.
protected String queryStringField(String target, String modelName) {
QueryResults results = getKSession().getQueryResults(target, modelName, Variable.v);
assertEquals(1, results.size());
return (String) results.iterator().next().get("$result");
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class DroolsAbstractPMMLTest method queryDoubleField.
protected double queryDoubleField(String target, String modelName) {
QueryResults results = getKSession().getQueryResults(target, modelName, Variable.v);
assertEquals(1, results.size());
return (Double) results.iterator().next().get("$result");
}
Aggregations