use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class FlowTest method testBeta.
@Test
public void testBeta() {
Result result = new Result();
Variable<Person> markV = declarationOf(Person.class);
Variable<Person> olderV = declarationOf(Person.class);
Rule rule = rule("beta").build(expr("exprA", markV, p -> p.getName().equals("Mark")).indexedBy(String.class, ConstraintType.EQUAL, 1, Person::getName, "Mark").reactOn("name", // also react on age, see RuleDescr.lookAheadFieldsOfIdentifier
"age"), expr("exprB", olderV, p -> !p.getName().equals("Mark")).indexedBy(String.class, ConstraintType.NOT_EQUAL, 1, Person::getName, "Mark").reactOn("name"), expr("exprC", olderV, markV, (p1, p2) -> p1.getAge() > p2.getAge()).indexedBy(int.class, ConstraintType.GREATER_THAN, 0, Person::getAge, Person::getAge).reactOn("age"), on(olderV, markV).execute((p1, p2) -> result.setValue(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();
assertEquals("Mario is older than Mark", result.getValue());
result.setValue(null);
ksession.delete(marioFH);
ksession.fireAllRules();
assertNull(result.getValue());
mark.setAge(34);
ksession.update(markFH, mark, "age");
ksession.fireAllRules();
assertEquals("Edson is older than Mark", result.getValue());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class FlowTest method testFrom.
@Test
public void testFrom() throws Exception {
Variable<Result> resultV = declarationOf(Result.class);
Variable<Adult> dadV = declarationOf(Adult.class);
Variable<Child> childV = declarationOf(Child.class, from(dadV, adult -> adult.getChildren()));
Rule rule = rule("from").build(expr("exprA", childV, c -> c.getAge() > 8), on(childV, resultV).execute((c, r) -> r.setValue(c.getName())));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
Result result = new Result();
ksession.insert(result);
Adult dad = new Adult("dad", 40);
dad.addChild(new Child("Alan", 10));
dad.addChild(new Child("Betty", 7));
ksession.insert(dad);
ksession.fireAllRules();
assertEquals("Alan", result.getValue());
}
use of org.drools.modelcompiler.domain.Result 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.modelcompiler.domain.Result in project drools by kiegroup.
the class FlowTest 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(expr("exprA", markV, p -> p.getName().equals("Mark")).indexedBy(String.class, ConstraintType.EQUAL, 1, Person::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()))), expr("exprB", olderV, p -> !p.getName().equals("Mark")).indexedBy(String.class, ConstraintType.NOT_EQUAL, 1, Person::getName, "Mark").reactOn("name"), expr("exprC", olderV, markV, (p1, p2) -> p1.getAge() > p2.getAge()).indexedBy(int.class, ConstraintType.GREATER_THAN, 0, Person::getAge, Person::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 FlowTest method testAccumulateAnd.
@Test
public void testAccumulateAnd() {
Variable<Number> var_$sum = declarationOf(Number.class, "$sum");
Variable<Person> var_$p = declarationOf(Person.class, "$p");
Variable<Integer> var_$expr$3$ = declarationOf(Integer.class, "$expr$3$");
org.drools.model.Rule rule = rule("X").build(bind(var_$expr$3$).as(var_$p, (_this) -> _this.getAge()), accumulate(and(expr("$expr$1$", var_$p, (_this) -> _this.getAge() > 10).indexedBy(int.class, org.drools.model.Index.ConstraintType.GREATER_THAN, 0, _this -> _this.getAge(), 10).reactOn("age"), expr("$expr$2$", var_$p, (_this) -> _this.getName().startsWith("M"))), accFunction(org.drools.core.base.accumulators.IntegerSumAccumulateFunction.class, var_$expr$3$).as(var_$sum)), 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());
}
Aggregations