use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class AccumulateTest method testAccumulateWithProperty.
@Test
public void testAccumulateWithProperty() {
String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule X when\n" + " accumulate ( $person: Person ( getName().startsWith(\"M\")); \n" + " $sum : sum($person.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 CompilerTest method testRuleExtends.
@Test
public void testRuleExtends() {
String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R1 when\n" + " $r : Result()\n" + "then\n" + "end\n" + "rule R2 extends R1 when\n" + " $p1 : Person(name == \"Mark\")\n" + "then\n" + "end\n" + "rule R3 extends R2 when\n" + " $p2 : Person(name != \"Mark\", 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);
assertEquals(1, ksession.fireAllRules());
ksession.insert(new Person("Edson", 35));
ksession.insert(new Person("Mario", 40));
assertEquals(0, ksession.fireAllRules());
ksession.insert(new Person("Mark", 37));
assertEquals(2, ksession.fireAllRules());
assertEquals("Mario is older than Mark", result.getValue());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class CompilerTest method testSimpleDelete.
@Test
public void testSimpleDelete() {
String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + " $p : Person( name.length == 4 )\n" + "then\n" + " Result r = new Result($p.getName());" + " insert(r);\n" + " delete($p);\n" + "end";
KieSession ksession = getKieSession(str);
ksession.insert(new Person("Mark", 37));
ksession.insert(new Person("Mario", 40));
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertEquals(1, results.size());
assertEquals("Mark", results.iterator().next().getValue());
assertEquals(1, getObjectsIntoList(ksession, Person.class).size());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class CompilerTest method testNonBreakingNamedConsequence.
@Test
public void testNonBreakingNamedConsequence() {
String str = "import " + Result.class.getCanonicalName() + ";\n" + "import " + Person.class.getCanonicalName() + ";\n" + "rule R when\n" + " $r : Result()\n" + " $p1 : Person(name == \"Mark\")\n" + " if ( age < 30 ) break[FoundYoungMark]" + " else if ( age > 50) break[FoundOldMark]\n" + " else do[FoundMark]\n" + " $p2 : Person(name != \"Mark\", age > $p1.age)\n" + "then\n" + " $r.addValue($p2.getName() + \" is older than \" + $p1.getName());\n" + "then[FoundYoungMark]\n" + " $r.addValue(\"Found young \" + $p1.getName());\n" + "then[FoundOldMark]\n" + " $r.addValue(\"Found old \" + $p1.getName());\n" + "then[FoundMark]\n" + " $r.addValue(\"Found \" + $p1.getName());\n" + "end";
KieSession ksession = getKieSession(str);
Result result = new Result();
ksession.insert(result);
ksession.insert(new Person("Mark", 37));
ksession.insert(new Person("Edson", 35));
ksession.insert(new Person("Mario", 40));
ksession.fireAllRules();
Collection results = (Collection) result.getValue();
assertEquals(2, results.size());
assertTrue(results.containsAll(asList("Found Mark", "Mario is older than Mark")));
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class CompilerTest method testEqualityCheckOnNull.
@Test
public void testEqualityCheckOnNull() {
String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule R when\n" + " Person($name : name == \"Mario\")\n" + "then\n" + " insert(new Result($name));\n" + "end";
KieSession ksession = getKieSession(str);
final Person mario = new Person("Mario", 40);
final Person luca = new Person(null, 33);
ksession.insert(mario);
ksession.insert(luca);
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertEquals(1, results.size());
assertEquals("Mario", results.iterator().next().getValue());
}
Aggregations