use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class QueryCepFireUntilHaltTest method withNoResultTest.
@Test(timeout = 10000L)
public void withNoResultTest() {
secondEntryPoint.insert(new TestEvent("minusOne"));
clock.advanceTime(5, TimeUnit.SECONDS);
firstEntryPoint.insert(new TestEvent("zero"));
secondEntryPoint.insert(new TestEvent("one"));
clock.advanceTime(10, TimeUnit.SECONDS);
secondEntryPoint.insert(new TestEvent("two"));
// the following expires event "zero" and "one", causing the query to no longer match
clock.advanceTime(10, TimeUnit.SECONDS);
secondEntryPoint.insert(new TestEvent("three"));
QueryResults results = ksession.getQueryResults("ZeroToNineteenSeconds");
assertEquals(0, results.size());
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class QueryBuilderTest method testQuery.
@Test
public void testQuery() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr = new PackageDescr("p1");
final QueryDescr queryDescr = new QueryDescr("query1");
queryDescr.addParameter("String", "$type");
packageDescr.addRule(queryDescr);
final AndDescr lhs = new AndDescr();
queryDescr.setLhs(lhs);
final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
lhs.addDescr(pattern);
pattern.addConstraint(new ExprConstraintDescr("type == $type"));
// Another query, no parameters
QueryDescr queryDescr2 = new QueryDescr("query2");
packageDescr.addRule(queryDescr2);
AndDescr lhs2 = new AndDescr();
queryDescr2.setLhs(lhs2);
PatternDescr pattern2 = new PatternDescr(Cheese.class.getName());
lhs2.addDescr(pattern2);
builder.addPackage(packageDescr);
assertLength(0, builder.getErrors().getErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(Arrays.asList(new KiePackage[] { builder.getPackage(packageDescr.getName()) }));
final KieSession session = kbase.newKieSession();
session.insert(new Cheese("stilton", 15));
QueryResults results = session.getQueryResults("query1", "stilton");
assertEquals(1, results.size());
Object object = results.iterator().next().get("stilton");
assertEquals(new Cheese("stilton", 15), object);
results = session.getQueryResults("query1", new Object[] { "cheddar" });
assertEquals(0, results.size());
session.insert(new Cheese("dolcelatte", 20));
results = session.getQueryResults("query2", new Object[] {});
assertEquals(2, results.size());
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class TraitTest method testTypeRefractionOnQuery2.
@Test
public void testTypeRefractionOnQuery2() {
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 end\n" + "declare trait B extends A end\n" + "declare trait C extends B end\n" + "declare trait D extends A end\n" + "declare trait E extends C, D end\n" + "declare trait F extends E end\n" + "declare trait G extends A 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" + " don( k, G.class ); \n" + " shed( k, B.class ); \n" + "end\n" + "" + "rule RuleA\n" + "when \n" + " $x : A( ) \n" + "then \n" + " System.out.println( $x ); \n " + "end\n" + " \n" + "query queryA1\n" + " $x := 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;
res = ks.getQueryResults("queryA1");
assertEquals(1, res.size());
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class TraitTest method testTypeRefractionOnQuery.
@Test(timeout = 10000)
public void testTypeRefractionOnQuery() {
String source = "declare BaseObject\n" + "@Traitable\n" + "id : String @key\n" + "end\n" + "\n" + "declare trait A\n" + "id : String @key\n" + "end\n" + "\n" + "declare trait B extends A\n" + "end\n" + "\n" + "declare trait C extends A\n" + "end\n" + "\n" + "rule \"init\"\n" + "when\n" + "then\n" + "BaseObject $obj = new BaseObject(\"testid123\");\n" + "insert ($obj);\n" + "don($obj, B.class, true);\n" + "don($obj, C.class, true);\n" + "end\n" + "\n" + "query \"QueryTraitA\"\n" + "a : A()\n" + "end";
KieSession ks = getSessionFromString(source);
TraitFactory.setMode(mode, ks.getKieBase());
ks.fireAllRules();
QueryResults res = ks.getQueryResults("QueryTraitA");
assertEquals(1, res.size());
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class BackwardChainingTest method testGeneology.
@Test(timeout = 10000)
public void testGeneology() throws Exception {
// from http://kti.mff.cuni.cz/~bartak/prolog/genealogy.html
String str = "" + "package org.drools.compiler.test2 \n" + "global java.util.List list\n" + "dialect \"mvel\"\n" + "query man( String name ) \n" + " " + BackwardChainingTest.class.getName() + ".Man( name := name ) \n" + "end\n" + "query woman( String name ) \n" + " " + BackwardChainingTest.class.getName() + ".Woman( name := name ) \n" + "end\n" + "query parent( String parent, String child ) \n" + " " + BackwardChainingTest.class.getName() + ".Parent( parent := parent, child := child ) \n" + "end\n" + "query father( String father, String child ) \n" + " ?man( father; ) \n" + " ?parent( father, child; ) \n" + "end\n" + "query mother( String mother, String child ) \n" + " ?woman( mother; ) \n" + " ?parent( mother, child; ) \n" + "end\n" + "query son( String son, String parent ) \n" + " ?man( son; ) \n" + " ?parent( parent, son; ) \n" + "end\n" + "query daughter( String daughter, String parent ) \n" + " ?woman( daughter; ) \n" + " ?parent( parent, daughter; ) \n" + "end\n" + "query siblings( String c1, String c2 ) \n" + " ?parent( $p, c1; ) \n" + " ?parent( $p, c2; ) \n" + " eval( !c1.equals( c2 ) )\n" + "end\n" + "query fullSiblings( String c1, String c2 )\n" + " ?parent( $p1, c1; ) ?parent( $p1, c2; )\n" + " ?parent( $p2, c1; ) ?parent( $p2, c2; )\n" + " eval( !c1.equals( c2 ) && !$p1.equals( $p2 ) )\n" + "end\n" + "query fullSiblings2( String c1, String c2 )\n" + " ?father( $p1, c1; ) ?father( $p1, c2; )\n" + " ?mother( $p2, c1; ) ?mother( $p2, c2; )\n" + " eval( !c1.equals( c2 ) )\n" + "end\n" + "query uncle( String uncle, String n )\n" + " ?man( uncle; ) ?siblings( uncle, parent; )\n" + " ?parent( parent, n; )\n " + "end\n" + "query aunt( String aunt, String n )\n" + " ?woman( aunt; ) ?siblings( aunt, parent; )\n" + " ?parent( parent, n; )\n " + "end\n" + "query grantParents( String gp, String gc )\n" + " ?parent( gp, p; ) ?parent( p, gc; )\n" + "end\n";
KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(str));
KieSession ksession = createKnowledgeSession(kbase);
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
// grand parents
ksession.insert(new Man("john"));
ksession.insert(new Woman("janet"));
// parent
ksession.insert(new Man("adam"));
ksession.insert(new Parent("john", "adam"));
ksession.insert(new Parent("janet", "adam"));
ksession.insert(new Man("stan"));
ksession.insert(new Parent("john", "stan"));
ksession.insert(new Parent("janet", "stan"));
// grand parents
ksession.insert(new Man("carl"));
ksession.insert(new Woman("tina"));
//
// parent
ksession.insert(new Woman("eve"));
ksession.insert(new Parent("carl", "eve"));
ksession.insert(new Parent("tina", "eve"));
//
// parent
ksession.insert(new Woman("mary"));
ksession.insert(new Parent("carl", "mary"));
ksession.insert(new Parent("tina", "mary"));
ksession.insert(new Man("peter"));
ksession.insert(new Parent("adam", "peter"));
ksession.insert(new Parent("eve", "peter"));
ksession.insert(new Man("paul"));
ksession.insert(new Parent("adam", "paul"));
ksession.insert(new Parent("mary", "paul"));
ksession.insert(new Woman("jill"));
ksession.insert(new Parent("adam", "jill"));
ksession.insert(new Parent("eve", "jill"));
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
QueryResults results = null;
// System.out.println("woman");
list.clear();
results = ksession.getQueryResults("woman", new Object[] { v });
for (QueryResultsRow result : results) {
list.add((String) result.get("name"));
}
assertEquals(5, list.size());
assertContains(new String[] { "janet", "mary", "tina", "eve", "jill" }, list);
list.clear();
// System.out.println("\nman");
results = ksession.getQueryResults("man", new Object[] { v });
for (QueryResultsRow result : results) {
list.add((String) result.get("name"));
}
assertEquals(6, list.size());
assertContains(new String[] { "stan", "john", "peter", "carl", "adam", "paul" }, list);
list.clear();
// System.out.println("\nfather");
results = ksession.getQueryResults("father", new Object[] { v, v });
for (QueryResultsRow result : results) {
list.add(result.get("father") + ", " + result.get("child"));
}
assertEquals(7, list.size());
assertContains(new String[] { "john, adam", "john, stan", "carl, eve", "carl, mary", "adam, peter", "adam, paul", "adam, jill" }, list);
list.clear();
// System.out.println("\nmother");
results = ksession.getQueryResults("mother", new Object[] { v, v });
for (QueryResultsRow result : results) {
list.add(result.get("mother") + ", " + result.get("child"));
}
assertEquals(7, list.size());
assertContains(new String[] { "janet, adam", "janet, stan", "mary, paul", "tina, eve", "tina, mary", "eve, peter", "eve, jill" }, list);
list.clear();
// System.out.println("\nson");
results = ksession.getQueryResults("son", new Object[] { v, v });
for (QueryResultsRow result : results) {
list.add(result.get("son") + ", " + result.get("parent"));
}
assertEquals(8, list.size());
assertContains(new String[] { "stan, john", "stan, janet", "peter, adam", "peter, eve", "adam, john", "adam, janet", "paul, mary", "paul, adam" }, list);
list.clear();
// System.out.println("\ndaughter");
results = ksession.getQueryResults("daughter", new Object[] { v, v });
for (QueryResultsRow result : results) {
list.add(result.get("daughter") + ", " + result.get("parent"));
}
assertEquals(6, list.size());
assertContains(new String[] { "mary, carl", "mary, tina", "eve, carl", "eve, tina", "jill, adam", "jill, eve" }, list);
list.clear();
// System.out.println("\nsiblings");
results = ksession.getQueryResults("siblings", new Object[] { v, v });
for (QueryResultsRow result : results) {
list.add(result.get("c1") + ", " + result.get("c2"));
}
assertEquals(16, list.size());
assertContains(new String[] { "eve, mary", "mary, eve", "adam, stan", "stan, adam", "adam, stan", "stan, adam", "peter, paul", "peter, jill", "paul, peter", "paul, jill", "jill, peter", "jill, paul", "peter, jill", "jill, peter", "eve, mary", "mary, eve" }, list);
list.clear();
// System.out.println("\nfullSiblings");
results = ksession.getQueryResults("fullSiblings", new Object[] { v, v });
for (QueryResultsRow result : results) {
list.add(result.get("c1") + ", " + result.get("c2"));
}
assertEquals(12, list.size());
assertContains(new String[] { "eve, mary", "mary, eve", "adam, stan", "stan, adam", "adam, stan", "stan, adam", "peter, jill", "jill, peter", "peter, jill", "jill, peter", "eve, mary", "mary, eve" }, list);
list.clear();
// System.out.println("\nfullSiblings2");
results = ksession.getQueryResults("fullSiblings", new Object[] { v, v });
for (QueryResultsRow result : results) {
list.add(result.get("c1") + ", " + result.get("c2"));
}
assertEquals(12, list.size());
assertContains(new String[] { "eve, mary", "mary, eve", "adam, stan", "stan, adam", "adam, stan", "stan, adam", "peter, jill", "jill, peter", "peter, jill", "jill, peter", "eve, mary", "mary, eve" }, list);
list.clear();
// System.out.println("\nuncle");
results = ksession.getQueryResults("uncle", new Object[] { v, v });
for (QueryResultsRow result : results) {
list.add(result.get("uncle") + ", " + result.get("n"));
}
assertEquals(6, list.size());
assertContains(new String[] { "stan, peter", "stan, paul", "stan, jill", "stan, peter", "stan, paul", "stan, jill" }, list);
list.clear();
// System.out.println("\naunt");
results = ksession.getQueryResults("aunt", new Object[] { v, v });
for (QueryResultsRow result : results) {
list.add(result.get("aunt") + ", " + result.get("n"));
}
assertEquals(6, list.size());
assertContains(new String[] { "mary, peter", "mary, jill", "mary, peter", "mary, jill", "eve, paul", "eve, paul" }, list);
list.clear();
// System.out.println("\ngrantParents");
results = ksession.getQueryResults("grantParents", new Object[] { v, v });
for (QueryResultsRow result : results) {
list.add(result.get("gp") + ", " + result.get("gc"));
}
assertEquals(12, list.size());
assertContains(new String[] { "carl, peter", "carl, jill", "carl, paul", "john, peter", "john, paul", "john, jill", "janet, peter", "janet, paul", "janet, jill", "tina, peter", "tina, jill", "tina, paul" }, list);
}
Aggregations