use of org.kie.api.runtime.rule.QueryResultsRow in project drools by kiegroup.
the class BasicUpdateTest method verifyFactsWithQuery.
private <T> void verifyFactsWithQuery(final Class<T> expectedClassOfFacts, final String queryToGetFacts, final T... factsToVerify) {
final QueryResults results = ksession.getQueryResults(queryToGetFacts);
assertThat(results).isNotEmpty();
final QueryResultsRow resultsRow = results.iterator().next();
assertThat(resultsRow.get("$" + queryToGetFacts)).isInstanceOf(List.class);
final List<Object> objects = (List<Object>) resultsRow.get("$" + queryToGetFacts);
assertThat(objects).hasSize(factsToVerify.length);
assertThat(objects).hasOnlyElementsOfType(expectedClassOfFacts);
assertThat(objects).containsAll(Arrays.asList(factsToVerify));
}
use of org.kie.api.runtime.rule.QueryResultsRow in project drools by kiegroup.
the class QueryTest method testQuery.
@Test
public void testQuery() throws Exception {
KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("simple_query_test.drl"));
KieSession session = createKieSession(kbase);
final Cheese stilton = new Cheese("stinky", 5);
FactHandle factHandle = session.insert(stilton);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
String queryName = "simple query";
org.kie.api.runtime.rule.QueryResults results = getQueryResults(session, queryName);
assertEquals(1, results.size());
QueryResultsRow row = results.iterator().next();
if (row instanceof FlatQueryResultRow) {
FlatQueryResultRow flatRow = (FlatQueryResultRow) row;
assertEquals(0, flatRow.getIdentifiers().size());
} else if (row instanceof QueryResultsRowImpl) {
QueryResultsRowImpl rowImpl = (QueryResultsRowImpl) row;
assertEquals(0, rowImpl.getDeclarations().size());
}
}
use of org.kie.api.runtime.rule.QueryResultsRow in project drools by kiegroup.
the class QueryTest method testQueryWithCollect.
@Test
public void testQueryWithCollect() throws Exception {
KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_Query.drl"));
KieSession session = createKieSession(kbase);
session.fireAllRules();
QueryResults results = getQueryResults(session, "collect objects");
assertEquals(1, results.size());
final QueryResultsRow row = results.iterator().next();
final List list = (List) row.get("$list");
assertEquals(2, list.size());
}
use of org.kie.api.runtime.rule.QueryResultsRow in project drools by kiegroup.
the class QueryTest method runQueryListenerTest.
public void runQueryListenerTest(QueryListenerOption option) throws IOException, ClassNotFoundException {
String str = "";
str += "package org.drools.compiler.integrationtests\n";
str += "import " + Cheese.class.getCanonicalName() + " \n";
str += "query cheeses(String $type) \n";
str += " $cheese : Cheese(type == $type) \n";
str += "end\n";
KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(str));
KieSession ksession = createKieSession(kbase, option);
// insert some data into the session
for (int i = 0; i < 10000; i++) {
ksession.insert(new Cheese(i % 2 == 0 ? "stilton" : "brie"));
}
// query the session
List<Cheese> cheeses;
for (int i = 0; i < 100; i++) {
org.kie.api.runtime.rule.QueryResults queryResults = ksession.getQueryResults("cheeses", new Object[] { "stilton" });
cheeses = new ArrayList<Cheese>();
for (QueryResultsRow row : queryResults) {
cheeses.add((Cheese) row.get("$cheese"));
}
assertEquals(5000, cheeses.size());
}
}
use of org.kie.api.runtime.rule.QueryResultsRow 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