use of org.kie.api.runtime.rule.QueryResults 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.QueryResults 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.QueryResults in project drools by kiegroup.
the class QueryTest method testDoubleQueryWithExists.
@Test
public void testDoubleQueryWithExists() throws Exception {
KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_DoubleQueryWithExists.drl"));
KieSession session = createKieSession(kbase);
final Person p1 = new Person("p1", "stilton", 20);
p1.setStatus("europe");
final FactHandle c1FactHandle = session.insert(p1);
final Person p2 = new Person("p2", "stilton", 30);
p2.setStatus("europe");
final FactHandle c2FactHandle = session.insert(p2);
final Person p3 = new Person("p3", "stilton", 40);
p3.setStatus("europe");
final FactHandle c3FactHandle = session.insert(p3);
session.fireAllRules();
QueryResults results = session.getQueryResults("2 persons with the same status");
assertEquals(2, results.size());
// europe=[ 1, 2 ], america=[ 3 ]
p3.setStatus("america");
session.update(c3FactHandle, p3);
session.fireAllRules();
results = session.getQueryResults("2 persons with the same status");
assertEquals(1, results.size());
// europe=[ 1 ], america=[ 2, 3 ]
p2.setStatus("america");
session.update(c2FactHandle, p2);
session.fireAllRules();
results = session.getQueryResults("2 persons with the same status");
assertEquals(1, results.size());
// europe=[ ], america=[ 1, 2, 3 ]
p1.setStatus("america");
session.update(c1FactHandle, p1);
session.fireAllRules();
results = getQueryResults(session, "2 persons with the same status");
assertEquals(2, results.size());
// europe=[ 2 ], america=[ 1, 3 ]
p2.setStatus("europe");
session.update(c2FactHandle, p2);
session.fireAllRules();
results = getQueryResults(session, "2 persons with the same status");
assertEquals(1, results.size());
// europe=[ 1, 2 ], america=[ 3 ]
p1.setStatus("europe");
session.update(c1FactHandle, p1);
session.fireAllRules();
results = session.getQueryResults("2 persons with the same status");
assertEquals(1, results.size());
// europe=[ 1, 2, 3 ], america=[ ]
p3.setStatus("europe");
session.update(c3FactHandle, p3);
session.fireAllRules();
results = session.getQueryResults("2 persons with the same status");
assertEquals(2, results.size());
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class QueryTest3 method doIt.
private void doIt(Object o1, Object o2, String query, int expected, boolean doUpdate, boolean doRetract) {
KieSession knowledgeSession = knowledgeBase.newKieSession();
try {
knowledgeSession.insert(o1);
FactHandle handle2 = knowledgeSession.insert(o2);
if (doUpdate) {
knowledgeSession.update(handle2, o2);
} else if (doRetract) {
knowledgeSession.retract(handle2);
handle2 = knowledgeSession.insert(o2);
}
QueryResults queryResults = knowledgeSession.getQueryResults(query);
assertEquals(expected, queryResults.size());
} finally {
knowledgeSession.dispose();
}
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class OOPathQueriesTest method testQueryFromCode.
@Test
public void testQueryFromCode() {
final String drl = "import org.drools.compiler.oopath.model.Thing;\n" + "query isContainedIn( Thing $x, Thing $y )\n" + " $y := /$x/children\n" + "or\n" + " ( $z := /$x/children and isContainedIn( $z, $y; ) )\n" + "end\n";
final Thing smartphone = new Thing("smartphone");
final List<String> itemList = Arrays.asList(new String[] { "display", "keyboard", "processor" });
itemList.stream().map(item -> new Thing(item)).forEach((thing) -> smartphone.addChild(thing));
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
ksession.insert(smartphone);
final QueryResults queryResults = ksession.getQueryResults("isContainedIn", new Object[] { smartphone, Variable.v });
final List<String> resultList = StreamSupport.stream(queryResults.spliterator(), false).map(row -> ((Thing) row.get("$y")).getName()).collect(Collectors.toList());
assertThat(resultList).as("Query does not contain all items").containsAll(itemList);
ksession.dispose();
}
Aggregations