use of org.drools.model.impl.ModelImpl 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.impl.ModelImpl in project drools by kiegroup.
the class PatternDSLTest method testAccumulateConstrainingValue.
@Test
public void testAccumulateConstrainingValue() {
Variable<org.drools.modelcompiler.domain.Person> var_$p = declarationOf(org.drools.modelcompiler.domain.Person.class, "$p");
Variable<Integer> var_$expr$5$ = declarationOf(Integer.class, "$expr$5$");
Variable<java.lang.Integer> var_$sum = declarationOf(java.lang.Integer.class, "$sum");
Rule rule = rule("X").build(accumulate(pattern(var_$p).expr("$expr$4$", (_this) -> _this.getName().startsWith("M")).bind(var_$expr$5$, (_this) -> _this.getAge()), accFunction(org.drools.core.base.accumulators.IntegerSumAccumulateFunction.class, var_$expr$5$).as(var_$sum)), pattern(var_$sum).expr("$expr$3$", (_this) -> _this > 50), on(var_$sum).execute((drools, $sum) -> {
drools.insert(new Result($sum));
}));
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();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertEquals(1, results.size());
assertEquals(77, results.iterator().next().getValue());
}
use of org.drools.model.impl.ModelImpl in project drools by kiegroup.
the class PatternDSLTest method testOr.
@Test
public void testOr() {
Result result = new Result();
Variable<Person> personV = declarationOf(Person.class);
Variable<Person> markV = declarationOf(Person.class);
Variable<String> nameV = declarationOf(String.class);
Rule rule = rule("or").build(or(pattern(personV).expr("exprA", p -> p.getName().equals("Mark")), and(pattern(markV).expr("exprA", p -> p.getName().equals("Mark")), pattern(personV).expr("exprB", markV, (p1, p2) -> p1.getAge() > p2.getAge()))), pattern(nameV).expr("exprC", personV, (s, p) -> s.equals(p.getName())), on(nameV).execute(result::setValue));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert("Mario");
ksession.insert(new Person("Mark", 37));
ksession.insert(new Person("Edson", 35));
ksession.insert(new Person("Mario", 40));
ksession.fireAllRules();
assertEquals("Mario", result.getValue());
}
use of org.drools.model.impl.ModelImpl in project drools by kiegroup.
the class PatternDSLTest method testBreakingNamedConsequence.
@Test
public void testBreakingNamedConsequence() {
Variable<Result> resultV = declarationOf(Result.class);
Variable<Person> markV = declarationOf(Person.class);
Variable<Person> olderV = declarationOf(Person.class);
Rule rule = rule("beta").build(pattern(resultV), pattern(markV).expr("exprA", p -> p.getName().equals("Mark"), alphaIndexedBy(String.class, Index.ConstraintType.EQUAL, 1, p -> p.getName(), "Mark"), reactOn("name", "age")), when("cond1", markV, p -> p.getAge() < 30).then(on(markV, resultV).breaking().execute((p, r) -> r.addValue("Found young " + p.getName()))).elseWhen("cond2", markV, p -> p.getAge() > 50).then(on(markV, resultV).breaking().execute((p, r) -> r.addValue("Found old " + p.getName()))).elseWhen().then(on(markV, resultV).breaking().execute((p, r) -> r.addValue("Found " + p.getName()))), pattern(olderV).expr("exprB", p -> !p.getName().equals("Mark"), alphaIndexedBy(String.class, Index.ConstraintType.NOT_EQUAL, 1, p -> p.getName(), "Mark"), reactOn("name")).expr("exprC", markV, (p1, p2) -> p1.getAge() > p2.getAge(), betaIndexedBy(int.class, Index.ConstraintType.GREATER_THAN, 0, p -> p.getAge(), p -> p.getAge()), reactOn("age")), on(olderV, markV, resultV).execute((p1, p2, r) -> r.addValue(p1.getName() + " is older than " + p2.getName())));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
Result result = new Result();
ksession.insert(result);
ksession.insert(new Person("Mark", 37));
ksession.insert(new Person("Edson", 35));
ksession.insert(new Person("Mario", 40));
ksession.fireAllRules();
Collection<String> results = (Collection<String>) result.getValue();
assertEquals(1, results.size());
assertEquals("Found Mark", results.iterator().next());
}
use of org.drools.model.impl.ModelImpl in project drools by kiegroup.
the class PatternDSLTest method testBetaWithBinding.
@Test
public void testBetaWithBinding() {
Variable<Person> markV = declarationOf(Person.class);
Variable<Integer> markAge = declarationOf(Integer.class);
Variable<Person> olderV = declarationOf(Person.class);
Rule rule = rule("beta").build(pattern(markV).expr("exprA", p -> p.getName().equals("Mark"), alphaIndexedBy(String.class, Index.ConstraintType.EQUAL, 1, p -> p.getName(), "Mark"), reactOn("name", "age")).bind(markAge, p -> p.getAge(), reactOn("age")), pattern(olderV).expr("exprB", p -> !p.getName().equals("Mark"), alphaIndexedBy(String.class, Index.ConstraintType.NOT_EQUAL, 1, p -> p.getName(), "Mark"), reactOn("name")).expr("exprC", markAge, (p1, age) -> p1.getAge() > age, betaIndexedBy(int.class, Index.ConstraintType.GREATER_THAN, 0, p -> p.getAge(), int.class::cast), reactOn("age")), on(olderV, markV).execute((drools, p1, p2) -> drools.insert(p1.getName() + " is older than " + p2.getName())));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
Person mark = new Person("Mark", 37);
Person edson = new Person("Edson", 35);
Person mario = new Person("Mario", 40);
FactHandle markFH = ksession.insert(mark);
FactHandle edsonFH = ksession.insert(edson);
FactHandle marioFH = ksession.insert(mario);
ksession.fireAllRules();
Collection<String> results = getObjectsIntoList(ksession, String.class);
Assertions.assertThat(results).containsExactlyInAnyOrder("Mario is older than Mark");
ksession.delete(marioFH);
ksession.fireAllRules();
mark.setAge(34);
ksession.update(markFH, mark, "age");
ksession.fireAllRules();
results = getObjectsIntoList(ksession, String.class);
Assertions.assertThat(results).containsExactlyInAnyOrder("Mario is older than Mark", "Edson is older than Mark");
}
Aggregations