use of org.drools.model.Query2Def in project drools by kiegroup.
the class PatternDSLTest method testQueryInRule.
@Test
public void testQueryInRule() {
Variable<Person> personV = DSL.declarationOf(Person.class);
Query2Def<Person, Integer> qdef = query("olderThan", Person.class, Integer.class);
Query query = qdef.build(pattern(qdef.getArg1()).expr("exprA", qdef.getArg2(), (p, a) -> p.getAge() > a));
Variable<Person> personVRule = DSL.declarationOf(Person.class);
Rule rule = rule("R").build(qdef.call(personVRule, valueOf(40)), on(personVRule).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.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 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.drools.model.Query2Def in project drools by kiegroup.
the class FlowTest method testQueryInRule.
@Test
public void testQueryInRule() {
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));
Variable<Person> personVRule = declarationOf(Person.class);
Rule rule = rule("R").build(qdef.call(personVRule, valueOf(40)), on(personVRule).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.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 FlowTest method testQueryInvokedWithGlobal.
@Test
public void testQueryInvokedWithGlobal() {
Global<Integer> ageG = globalOf(Integer.class, "defaultpkg", "ageG");
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(), (_this, $age) -> _this.getAge() > $age));
Rule rule = rule("R").build(expr("exprB", personV, p -> p.getName().startsWith("M")), qdef.call(personV, ageG), on(personV).execute((drools, p) -> drools.insert(new Result(p.getName()))));
Model model = new ModelImpl().addGlobal(ageG).addQuery(query).addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.setGlobal("ageG", 40);
ksession.insert(new Person("Mark", 39));
ksession.insert(new Person("Mario", 41));
ksession.insert(new Person("Edson", 41));
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, 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 FlowTest method testQueryInvokingQuery.
@Test
public void testQueryInvokingQuery() {
Variable<Relationship> relV = 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(expr("exprA", relV, query2Def.getArg1(), (r, s) -> r.getStart().equals(s)), expr("exprB", relV, 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