use of org.drools.modelcompiler.domain.Adult in project drools by kiegroup.
the class AccumulateTest method testAccumulateWithAnd3.
@Test
public void testAccumulateWithAnd3() {
String str = "import " + Adult.class.getCanonicalName() + ";\n" + "import " + Child.class.getCanonicalName() + ";\n" + "import " + Result.class.getCanonicalName() + ";\n" + "rule R when\n" + " accumulate( $x : Child( age < 10 ) and $y : Adult( name == $x.parent ), $parentAge : sum($x.getAge() + $y.getAge()) )\n" + "then\n" + " insert(new Result($parentAge));\n" + "end";
KieSession ksession = getKieSession(str);
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);
// The original DSL test returns a double while the exec model returns an integer
assertEquals(((Number) results.iterator().next().getValue()).intValue(), 49);
}
use of org.drools.modelcompiler.domain.Adult in project drools by kiegroup.
the class AccumulateTest method testAccumulateWithAnd.
@Test
public void testAccumulateWithAnd() {
String str = "import " + Adult.class.getCanonicalName() + ";\n" + "import " + Child.class.getCanonicalName() + ";\n" + "import " + Result.class.getCanonicalName() + ";\n" + "rule R when\n" + " accumulate( $c : Child( age < 10 ) and $a : Adult( name == $c.parent ), $parentAge : sum($a.getAge()) )\n" + "then\n" + " insert(new Result($parentAge));\n" + "end";
KieSession ksession = getKieSession(str);
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(43)));
}
use of org.drools.modelcompiler.domain.Adult in project drools by kiegroup.
the class CompilerTest method testBindWith2Arguments.
@Test
public void testBindWith2Arguments() {
String str = "import " + Adult.class.getCanonicalName() + ";\n" + "import " + Child.class.getCanonicalName() + ";\n" + "import " + Result.class.getCanonicalName() + ";\n" + "rule R when\n" + " $y : Adult( $sum : (name.length + age) )\n" + "then\n" + " insert(new Result($sum));\n" + "end";
KieSession ksession = getKieSession(str);
Adult a = new Adult("Mario", 43);
ksession.insert(a);
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertEquals(((Number) results.iterator().next().getValue()).intValue(), 48);
}
use of org.drools.modelcompiler.domain.Adult in project drools by kiegroup.
the class CompilerTest method testFrom.
@Test
public void testFrom() {
String str = "import " + Result.class.getCanonicalName() + ";\n" + "import " + Adult.class.getCanonicalName() + ";\n" + "import " + Child.class.getCanonicalName() + ";\n" + "rule R when\n" + " $r : Result()\n" + " $a : Adult()\n" + " $c : Child( age > 8 ) from $a.children\n" + "then\n" + " $r.setValue($c.getName());\n" + "end";
KieSession ksession = getKieSession(str);
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.Adult 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