use of org.drools.model.impl.ModelImpl in project drools by kiegroup.
the class OOPathFlowTest method testQueryOOPathAccumulate.
@Test
public void testQueryOOPathAccumulate() {
QueryDef queryDef_listSafeCities = query("listSafeCities");
Variable<java.util.List> var_$cities = declarationOf(java.util.List.class, "$cities");
Variable<org.drools.modelcompiler.oopathdtables.Person> var_$p = declarationOf(org.drools.modelcompiler.oopathdtables.Person.class, "$p");
Variable<org.drools.modelcompiler.oopathdtables.InternationalAddress> var_$a = declarationOf(org.drools.modelcompiler.oopathdtables.InternationalAddress.class, "$a", from(var_$p, (_this) -> _this.getAddress()));
Variable<String> var_$city = declarationOf(String.class, "$city", from(var_$a, (_this) -> _this.getCity()));
org.drools.model.Query listSafeCities_build = queryDef_listSafeCities.build(accumulate(and(input(var_$p), expr("$expr$2$", var_$a, (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getState(), "Safecountry")).indexedBy(java.lang.String.class, org.drools.model.Index.ConstraintType.EQUAL, 0, _this -> _this.getState(), "Safecountry").reactOn("state"), expr(var_$city)), accFunction(org.drools.core.base.accumulators.CollectListAccumulateFunction.class, var_$city).as(var_$cities)));
Model model = new ModelImpl().addQuery(listSafeCities_build);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
org.drools.modelcompiler.oopathdtables.Person person = new org.drools.modelcompiler.oopathdtables.Person();
person.setAddress(new org.drools.modelcompiler.oopathdtables.InternationalAddress("", 1, "Milan", "Safecountry"));
ksession.insert(person);
org.drools.modelcompiler.oopathdtables.Person person2 = new org.drools.modelcompiler.oopathdtables.Person();
person2.setAddress(new org.drools.modelcompiler.oopathdtables.InternationalAddress("", 1, "Rome", "Unsafecountry"));
ksession.insert(person2);
QueryResults results = ksession.getQueryResults("listSafeCities");
List cities = (List) results.iterator().next().get("$cities");
assertEquals(1, cities.size());
assertEquals("Milan", cities.get(0));
}
use of org.drools.model.impl.ModelImpl in project drools by kiegroup.
the class PatternDSLTest method testWatch.
@Test
public void testWatch() {
Variable<Person> var_$p = declarationOf(Person.class, "$p");
BitMask mask_$p = BitMask.getPatternMask(org.drools.modelcompiler.domain.Person.class, "age");
Rule rule = rule("R").build(pattern(var_$p).expr("$expr$1$", (_this) -> _this.getAge() < 50, alphaIndexedBy(int.class, Index.ConstraintType.LESS_THAN, 0, _this -> _this.getAge(), 50), reactOn("age")).watch("!age"), on(var_$p).execute((drools, $p) -> {
$p.setAge($p.getAge() + 1);
drools.update($p, mask_$p);
}));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
Person p = new Person("Mario", 40);
ksession.insert(p);
ksession.fireAllRules();
assertEquals(41, p.getAge());
}
use of org.drools.model.impl.ModelImpl in project drools by kiegroup.
the class PatternDSLTest method testNot.
@Test
public void testNot() {
Result result = new Result();
Variable<Person> oldestV = DSL.declarationOf(Person.class);
Variable<Person> otherV = DSL.declarationOf(Person.class);
Rule rule = rule("not").build(pattern(oldestV), not(pattern(otherV).expr("exprA", oldestV, (p1, p2) -> p1.getAge() > p2.getAge())), on(oldestV).execute(p -> result.setValue("Oldest person is " + p.getName())));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Mark", 37));
ksession.insert(new Person("Edson", 35));
ksession.insert(new Person("Mario", 40));
ksession.fireAllRules();
assertEquals("Oldest person is Mario", result.getValue());
}
use of org.drools.model.impl.ModelImpl 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