use of org.drools.model.Query2Def in project drools by kiegroup.
the class FlowTest method testQueryInRuleWithDeclaration.
@Test
public void testQueryInRuleWithDeclaration() {
Variable<Person> personV = declarationOf(Person.class);
Query2Def<Person, Integer> qdef = query("olderThan", Person.class, Integer.class);
Query query = qdef.build(expr("exprA", qdef.getArg1(), qdef.getArg2(), (p, a) -> p.getAge() > a));
Rule rule = rule("R").build(expr("exprB", personV, p -> p.getName().startsWith("M")), qdef.call(personV, valueOf(40)), on(personV).execute((drools, p) -> drools.insert(new Result(p.getName()))));
Model model = new ModelImpl().addQuery(query).addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Mark", 39));
ksession.insert(new Person("Mario", 41));
ksession.insert(new Person("Edson", 41));
ksession.fireAllRules();
Collection<Result> results = (Collection<Result>) ksession.getObjects(new ClassObjectFilter(Result.class));
assertEquals(1, results.size());
assertEquals("Mario", results.iterator().next().getValue());
}
use of org.drools.model.Query2Def in project drools by kiegroup.
the class PatternDSLTest method testQueryInvokingQuery.
@Test
public void testQueryInvokingQuery() {
Variable<Relationship> relV = DSL.declarationOf(Relationship.class);
Query2Def<String, String> query1Def = query("isRelatedTo1", String.class, String.class);
Query2Def<String, String> query2Def = query("isRelatedTo2", String.class, String.class);
Query query2 = query2Def.build(pattern(relV).expr("exprA", query2Def.getArg1(), (r, s) -> r.getStart().equals(s)).expr("exprB", query2Def.getArg2(), (r, e) -> r.getEnd().equals(e)));
Query query1 = query1Def.build(query2Def.call(query1Def.getArg1(), query1Def.getArg2()));
Model model = new ModelImpl().addQuery(query2).addQuery(query1);
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("isRelatedTo1", "A", "B");
assertEquals(1, results.size());
assertEquals("B", results.iterator().next().get(query1Def.getArg2().getName()));
}
Aggregations