use of org.drools.modelcompiler.domain.Result 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.modelcompiler.domain.Result 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.modelcompiler.domain.Result 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.modelcompiler.domain.Result in project drools by kiegroup.
the class PatternDSLTest method testAccumulate.
@Test
public void testAccumulate() {
Result result = new Result();
Variable<Person> person = declarationOf(Person.class);
Variable<Integer> resultSum = declarationOf(Integer.class);
Variable<Double> resultAvg = declarationOf(Double.class);
Variable<Integer> age = declarationOf(Integer.class);
Rule rule = rule("accumulate").build(accumulate(pattern(person).expr(p -> p.getName().startsWith("M")).bind(age, Person::getAge), accFunction(org.drools.core.base.accumulators.IntegerSumAccumulateFunction.class, age).as(resultSum), accFunction(org.drools.core.base.accumulators.AverageAccumulateFunction.class, age).as(resultAvg)), on(resultSum, resultAvg).execute((sum, avg) -> result.setValue("total = " + sum + "; average = " + avg)));
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("total = 77; average = 38.5", result.getValue());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class QueryTest method testQueryInRuleWithDeclaration.
@Test
public void testQueryInRuleWithDeclaration() {
String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "query olderThan( Person $p, int $age )\n" + " $p := Person(age > $age)\n" + "end\n" + "rule R when\n" + " $p : Person( name.startsWith(\"M\") )\n" + " olderThan( $p, 40; )\n" + "then\n" + " insert(new Result($p.getName()));\n" + "end";
KieSession ksession = getKieSession(str);
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());
}
Aggregations