use of org.drools.modelcompiler.domain.Result 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.Result in project drools by kiegroup.
the class AccumulateTest method testAccumulate2.
@Test
public void testAccumulate2() {
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" + " $average : average($p.getAge()) \n" + " ) \n" + "then\n" + " insert(new Result($sum));\n" + " insert(new Result($average));\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);
assertThat(results, hasItem(new Result(38.5)));
assertThat(results, hasItem(new Result(77)));
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class AccumulateTest method testAccumulateMultipleFunctionsConstrainingValues.
@Test
public void testAccumulateMultipleFunctionsConstrainingValues() {
String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule X when\n" + " accumulate ( Person ( $age : age > 36); \n" + " $sum : sum($age), \n" + " $min : min($age) \n" + " ; $sum > 50, $min > 30\n" + " ) \n" + "then\n" + " insert(new Result($sum));\n" + " insert(new Result($min));\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);
assertThat(results, hasItem(new Result(37)));
assertThat(results, hasItem(new Result(77)));
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class AccumulateTest method testAccumulateMultipleFunctions.
@Test
public void testAccumulateMultipleFunctions() {
String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule X when\n" + " accumulate ( Person ( $age : age > 36); \n" + " $sum : sum($age), \n" + " $average : average($age) \n" + " ) \n" + "then\n" + " insert(new Result($sum));\n" + " insert(new Result($average));\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);
assertThat(results, hasItem(new Result(38.5d)));
assertThat(results, hasItem(new Result(77)));
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class CompilerTest method testEmptyPattern.
@Test
public void testEmptyPattern() {
String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule R when\n" + " Person() \n" + "then\n" + " insert(new Result(\"ok\"));\n" + "end";
KieSession ksession = getKieSession(str);
Person mario = new Person("Mario", 40);
ksession.insert(mario);
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertEquals(1, results.size());
assertEquals("ok", results.iterator().next().getValue());
}
Aggregations