use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class QueryCepTest method noResultTest.
@Test
public void noResultTest() {
QueryResults results = ksession.getQueryResults("EventsFromStream");
assertEquals(0, results.size());
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class QueryCepFireUntilHaltTest method noResultTest.
@Test(timeout = 10000L)
public void noResultTest() {
QueryResults results = ksession.getQueryResults("EventsFromStream");
assertEquals(0, results.size());
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class QueryCepFireUntilHaltTest method withResultTest.
@Test(timeout = 10000L)
public void withResultTest() {
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"));
clock.advanceTime(5, TimeUnit.SECONDS);
secondEntryPoint.insert(new TestEvent("three"));
QueryResults results = ksession.getQueryResults("ZeroToNineteenSeconds");
assertEquals(1, results.size());
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class PatternBuilderForQueryTest 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 BackwardChainingTest method testQueryWithOr.
@Test(timeout = 10000)
public void testQueryWithOr() {
final String drl = "" + "package org.drools.compiler.test \n" + "import java.util.List\n" + "import java.util.ArrayList\n" + "import " + BackwardChainingTest.class.getName() + ".Q\n" + "import " + BackwardChainingTest.class.getName() + ".R\n" + "import " + BackwardChainingTest.class.getName() + ".S\n" + "global List list\n" + "dialect \"mvel\"\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" + "";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("backward-chaining-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession();
try {
final List<Integer> list = new ArrayList<>();
ksession.setGlobal("list", list);
ksession.fireAllRules();
QueryResults results;
list.clear();
results = ksession.getQueryResults("p", new Integer[] { 0 });
for (final QueryResultsRow result : results) {
list.add((Integer) result.get("x"));
}
assertEquals(0, list.size());
list.clear();
results = ksession.getQueryResults("p", new Integer[] { 1 });
for (final 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 (final 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 (final 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 (final QueryResultsRow result : results) {
list.add((Integer) result.get("x"));
}
assertEquals(0, list.size());
list.clear();
results = ksession.getQueryResults("p", new Integer[] { 5 });
for (final QueryResultsRow result : results) {
list.add((Integer) result.get("x"));
}
assertEquals(0, list.size());
list.clear();
results = ksession.getQueryResults("p", new Integer[] { 6 });
for (final 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());
} finally {
ksession.dispose();
}
}
Aggregations