use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class GlobalTest method testGlobalInConstraint.
@Test
public void testGlobalInConstraint() {
String str = "package org.mypkg;" + "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "global java.lang.String nameG;" + "global Result resultG;" + "rule X when\n" + " $p1 : Person(nameG == name)\n" + "then\n" + " resultG.setValue($p1.getName() + \" is \" + $p1.getAge());\n" + "end";
KieSession ksession = getKieSession(str);
ksession.setGlobal("nameG", "Mark");
Result result = new Result();
ksession.setGlobal("resultG", result);
ksession.insert(new Person("Mark", 37));
ksession.insert(new Person("Edson", 35));
ksession.insert(new Person("Mario", 40));
ksession.fireAllRules();
assertEquals("Mark is 37", result.getValue());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class PatternDSLTest 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(pattern(markV).expr("exprA", p -> p.getName().equals("Mark"), alphaIndexedBy(String.class, Index.ConstraintType.EQUAL, 1, p -> p.getName(), "Mark"), reactOn("name", "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", 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).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 PatternDSLTest 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$");
Rule rule = rule("R").build(accumulate(and(pattern(var_$c).expr("$expr$1$", (_this) -> _this.getAge() < 10, alphaIndexedBy(int.class, Index.ConstraintType.LESS_THAN, 0, _this -> _this.getAge(), 10), reactOn("age")), pattern(var_$a).expr("$expr$2$", var_$c, (_this, $c) -> _this.getName().equals($c.getParent()), reactOn("name")).bind(var_$expr$5$, var_$c, ($a, $c) -> $a.getAge() + $c.getAge())), 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)));
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class PropertyReactivityTest method testImplicitWatchWithDeclaration.
@Test
public void testImplicitWatchWithDeclaration() {
String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + " $r : Result()\n" + " $p1 : Person( $a : address )\n" + " $p2 : Person(name != \"Mark\", this != $p1, age > $p1.age)\n" + "then\n" + " $r.setValue($p2.getName() + \" is older than \" + $p1.getName());\n" + "end";
KieSession ksession = getKieSession(str);
Result result = new Result();
ksession.insert(result);
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 PropertyReactivityTest method testImplicitWatch.
@Test
public void testImplicitWatch() {
String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + " $r : Result()\n" + " $p1 : Person()\n" + " $p2 : Person(name != \"Mark\", this != $p1, age > $p1.age)\n" + "then\n" + " $r.setValue($p2.getName() + \" is older than \" + $p1.getName());\n" + "end";
KieSession ksession = getKieSession(str);
Result result = new Result();
ksession.insert(result);
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());
}
Aggregations