use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class FlowTest method testPositionalRecursiveQueryWithUnification.
@Test
public void testPositionalRecursiveQueryWithUnification() {
Variable<Relationship> var_$pattern_Relationship$1$ = declarationOf(Relationship.class);
Variable<Relationship> var_$pattern_Relationship$2$ = declarationOf(Relationship.class);
Variable<String> var_$unificationExpr$1$ = declarationOf(String.class);
Query2Def<String, String> queryDef_isRelatedTo = query("isRelatedTo", String.class, String.class);
Query query = queryDef_isRelatedTo.build(or(and(expr("exprA", var_$pattern_Relationship$1$, queryDef_isRelatedTo.getArg1(), (r, s) -> r.getStart().equals(s)), expr("exprB", var_$pattern_Relationship$1$, queryDef_isRelatedTo.getArg2(), (r, e) -> r.getEnd().equals(e))), and(and(bind(var_$unificationExpr$1$).as(var_$pattern_Relationship$2$, relationship -> relationship.getStart()), expr("exprD", var_$pattern_Relationship$2$, queryDef_isRelatedTo.getArg2(), (r, e) -> r.getEnd().equals(e))), queryDef_isRelatedTo.call(queryDef_isRelatedTo.getArg1(), var_$unificationExpr$1$))));
Model model = new ModelImpl().addQuery(query);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Relationship("A", "B"));
ksession.insert(new Relationship("B", "C"));
QueryResults results = ksession.getQueryResults("isRelatedTo", "A", "C");
assertEquals(1, results.size());
assertEquals("B", results.iterator().next().get(var_$unificationExpr$1$.getName()));
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class FlowTest method testQueryWithNamedArg.
@Test
public void testQueryWithNamedArg() {
Variable<Person> personV = declarationOf(Person.class, "$p");
Query1Def<Integer> qdef = query("olderThan", Integer.class, "ageArg");
Query query = qdef.build(expr("exprA", personV, qdef.getArg("ageArg", Integer.class), (p, a) -> p.getAge() > a));
Model model = new ModelImpl().addQuery(query);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Mark", 39));
ksession.insert(new Person("Mario", 41));
QueryResults results = ksession.getQueryResults("olderThan", 40);
assertEquals(1, results.size());
Person p = (Person) results.iterator().next().get("$p");
assertEquals("Mario", p.getName());
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class FlowTest method testQuery.
@Test
public void testQuery() {
Variable<Person> personV = declarationOf(Person.class, "$p");
Query1Def<Integer> qdef = query("olderThan", Integer.class);
Query query = qdef.build(expr("exprA", personV, qdef.getArg1(), (p, a) -> p.getAge() > a));
Model model = new ModelImpl().addQuery(query);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Mark", 39));
ksession.insert(new Person("Mario", 41));
QueryResults results = ksession.getQueryResults("olderThan", 40);
assertEquals(1, results.size());
Person p = (Person) results.iterator().next().get("$p");
assertEquals("Mario", p.getName());
}
use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.
the class MultithreadTest method testConcurrentQueries.
@Test(timeout = 5000)
public void testConcurrentQueries() {
// DROOLS-175
final StringBuilder drl = new StringBuilder();
drl.append("package org.drools.test;\n" + "" + "query foo( ) \n" + " Object() from new Object() \n" + "end\n" + "" + "rule XYZ when then end \n");
final KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
knowledgeBuilder.add(ResourceFactory.newByteArrayResource(drl.toString().getBytes()), ResourceType.DRL);
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(knowledgeBuilder.getKnowledgePackages());
final KieSession ksession = kbase.newKieSession();
final Executor executor = Executors.newCachedThreadPool(new ThreadFactory() {
public Thread newThread(final Runnable r) {
final Thread t = new Thread(r);
t.setDaemon(true);
return t;
}
});
final int THREAD_NR = 5;
final CompletionService<Boolean> ecs = new ExecutorCompletionService<Boolean>(executor);
for (int i = 0; i < THREAD_NR; i++) {
ecs.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
boolean succ = false;
try {
final QueryResults res = ksession.getQueryResults("foo", Variable.v);
succ = (res.size() == 1);
return succ;
} catch (final Exception e) {
e.printStackTrace();
return succ;
}
}
});
}
boolean success = true;
for (int i = 0; i < THREAD_NR; i++) {
try {
success = ecs.take().get() && success;
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
Assertions.assertThat(success).isTrue();
ksession.dispose();
}
use of org.kie.api.runtime.rule.QueryResults 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));
}
Aggregations