use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class GlobalTest method testGlobalInConsequence.
@Test
public void testGlobalInConsequence() {
String str = "package org.mypkg;" + "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "global Result globalResult;" + "rule X when\n" + " $p1 : Person(name == \"Mark\")\n" + "then\n" + " globalResult.setValue($p1.getName() + \" is \" + $p1.getAge());\n" + "end";
KieSession ksession = getKieSession(str);
Result result = new Result();
ksession.setGlobal("globalResult", 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 testNot.
@Test
public void testNot() {
Result result = new Result();
Variable<Person> oldestV = DSL.declarationOf(Person.class);
Variable<Person> otherV = DSL.declarationOf(Person.class);
Rule rule = rule("not").build(pattern(oldestV), not(pattern(otherV).expr("exprA", oldestV, (p1, p2) -> p1.getAge() > p2.getAge())), on(oldestV).execute(p -> result.setValue("Oldest person is " + p.getName())));
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("Oldest person is Mario", result.getValue());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class AccumulateTest method testAccumulateWithAnd2.
@Test
public void testAccumulateWithAnd2() {
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() + $c.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.Result in project drools by kiegroup.
the class AccumulateTest method testAccumulateWithCustomImport.
@Test
public void testAccumulateWithCustomImport() {
String str = "import accumulate " + TestFunction.class.getCanonicalName() + " f;\n" + "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 : f($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(1)));
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class AccumulateTest method testFromAccumulate.
@Test
public void testFromAccumulate() {
String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule X when\n" + " $sum : Number( intValue() > 0 ) from accumulate ( $p: Person ( age > 10, name.startsWith(\"M\") ); \n" + " sum($p.getAge()) \n" + " )\n" + "then\n" + " insert(new Result($sum));\n" + "end";
KieSession ksession = getKieSession(str);
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