use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class FlowTest method test3Patterns.
@Test
public void test3Patterns() {
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("myrule").build(expr("exprA", markV, p -> p.getName().equals("Mark")), expr("exprB", personV, markV, (p1, p2) -> p1.getAge() > p2.getAge()), expr("exprC", nameV, 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 FlowTest method testNamedConsequence.
@Test
public void testNamedConsequence() {
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", // also react on age, see RuleDescr.lookAheadFieldsOfIdentifier
"age"), on(markV, resultV).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(2, results.size());
assertTrue(results.containsAll(asList("Found Mark", "Mario is older than Mark")));
}
use of org.drools.modelcompiler.domain.Result 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.modelcompiler.domain.Result in project drools by kiegroup.
the class FlowTest method testGlobalInConsequence.
@Test
public void testGlobalInConsequence() {
Variable<Person> markV = declarationOf(Person.class);
Global<Result> resultG = globalOf(Result.class, "org.mypkg");
Rule rule = rule("org.mypkg", "global").build(expr("exprA", markV, p -> p.getName().equals("Mark")).indexedBy(String.class, ConstraintType.EQUAL, 0, Person::getName, "Mark").reactOn("name"), on(markV, resultG).execute((p, r) -> r.setValue(p.getName() + " is " + p.getAge())));
Model model = new ModelImpl().addRule(rule).addGlobal(resultG);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
Result result = new Result();
ksession.setGlobal(resultG.getName(), result);
Person mark = new Person("Mark", 37);
Person edson = new Person("Edson", 35);
Person mario = new Person("Mario", 40);
ksession.insert(mark);
ksession.insert(edson);
ksession.insert(mario);
ksession.fireAllRules();
assertEquals("Mark is 37", result.getValue());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class FlowTest method testAccumuluateWithAnd2.
@Test
public void testAccumuluateWithAnd2() {
Variable<Object> var_$pattern_Object$1$ = declarationOf(Object.class, "$pattern_Object$1$");
Variable<Child> var_$c = declarationOf(Child.class, "$c");
Variable<Adult> var_$a = declarationOf(Adult.class, "$a");
Variable<Integer> var_$parentAge = declarationOf(Integer.class, "$parentAge");
Variable<Integer> var_$expr$5$ = declarationOf(Integer.class, "$expr$5$");
org.drools.model.Rule rule = rule("R").build(bind(var_$expr$5$).as(var_$a, var_$c, ($a, $c) -> $a.getAge() + $c.getAge()), accumulate(and(expr("$expr$1$", var_$c, (_this) -> _this.getAge() < 10).indexedBy(int.class, org.drools.model.Index.ConstraintType.LESS_THAN, 0, _this -> _this.getAge(), 10).reactOn("age"), expr("$expr$2$", var_$a, var_$c, (_this, $c) -> _this.getName().equals($c.getParent())).reactOn("name")), accFunction(org.drools.core.base.accumulators.IntegerSumAccumulateFunction.class, var_$expr$5$).as(var_$parentAge)), on(var_$parentAge).execute((drools, $parentAge) -> {
drools.insert(new Result($parentAge));
}));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
Adult a = new Adult("Mario", 43);
Child c = new Child("Sofia", 6, "Mario");
ksession.insert(a);
ksession.insert(c);
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertThat(results, hasItem(new Result(49)));
}
Aggregations