use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class AccumulateTest method testAccumulate1.
@Test
public void testAccumulate1() {
String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule X when\n" + " accumulate ( $p: Person ( getName().startsWith(\"M\")); \n" + " $sum : 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());
}
use of org.drools.modelcompiler.domain.Result 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.Result in project drools by kiegroup.
the class AccumulateTest method testAccumulateConstrainingValueInPattern.
@Test
public void testAccumulateConstrainingValueInPattern() {
String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule X when\n" + " $sum : Integer( this > 50 ) from accumulate ( $p: Person ( getName().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());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class AccumulateTest method testAccumulateConstrainingValue.
@Test
public void testAccumulateConstrainingValue() {
String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule X when\n" + " accumulate ( $p: Person ( getName().startsWith(\"M\")); \n" + " $sum : sum($p.getAge()); $sum > 50 \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());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class AccumulateTest method testAccumulateConstrainingValue2.
@Test
public void testAccumulateConstrainingValue2() {
String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule X when\n" + " accumulate ( $p: Person ( getName().startsWith(\"M\")); \n" + " $sum : sum($p.getAge()); $sum > 100 \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(0, results.size());
}
Aggregations