use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class QueryTest method testQueryInvokedWithGlobal.
@Test
public void testQueryInvokedWithGlobal() {
String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "global Integer ageG;" + "query olderThan( Person $p, int $age )\n" + " $p := Person(age > $age)\n" + "end\n" + "rule R when\n" + " $p : Person( name.startsWith(\"M\") )\n" + " olderThan( $p, ageG; )\n" + "then\n" + " insert(new Result($p.getName()));\n" + "end";
KieSession ksession = getKieSession(str);
ksession.setGlobal("ageG", 40);
ksession.insert(new Person("Mark", 39));
ksession.insert(new Person("Mario", 41));
ksession.insert(new Person("Edson", 41));
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertEquals(1, results.size());
assertEquals("Mario", results.iterator().next().getValue());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class QueryTest method testQueryInRule.
@Test
public void testQueryInRule() {
String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "query olderThan( Person $p, int $age )\n" + " $p := Person(age > $age)\n" + "end\n" + "rule R when\n" + " olderThan( $p, 40; )\n" + "then\n" + " insert(new Result($p.getName()));\n" + "end";
KieSession ksession = getKieSession(str);
ksession.insert(new Person("Mark", 39));
ksession.insert(new Person("Mario", 41));
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertEquals(1, results.size());
assertEquals("Mario", results.iterator().next().getValue());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class InlineCastTest method testInlineCastThis.
@Test
public void testInlineCastThis() {
String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + " $r : Result()\n" + " $o : Object( this#Person.name == \"Mark\" )\n" + "then\n" + " $r.setValue(\"Found: \" + $o);\n" + "end";
KieSession ksession = getKieSession(str);
Result result = new Result();
ksession.insert(result);
ksession.insert("Mark");
ksession.insert(new Person("Mark", 37));
ksession.insert(new Person("Mario", 40));
ksession.fireAllRules();
assertEquals("Found: Mark", result.getValue());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class CompilerTest method testAgeWithSum.
@Test
public void testAgeWithSum() {
String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + " $p : Person( personAge : age )\n" + " $plusTwo : Person(age == personAge + 2 )\n" + "then\n" + " insert(new Result($plusTwo.getName()));\n" + "end";
KieSession ksession = getKieSession(str);
ksession.insert(new Person("Mario", 40));
ksession.insert(new Person("Mark", 37));
ksession.insert(new Person("Edson", 35));
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertEquals(1, results.size());
assertEquals("Mark", results.iterator().next().getValue());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class CompilerTest method checkConcatenatedFrom.
private void checkConcatenatedFrom(boolean withCondition) {
String str = "import " + Result.class.getCanonicalName() + ";\n" + "import " + Man.class.getCanonicalName() + ";\n" + "import " + Woman.class.getCanonicalName() + ";\n" + "import " + Child.class.getCanonicalName() + ";\n" + "import " + Toy.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R when\n" + " $m : Man(" + (withCondition ? "age > 0" : "") + ")\n" + " $w : Woman() from $m.wife\n" + " $c : Child( age > 10 ) from $w.children\n" + " $t : Toy() from $c.toys\n" + "then\n" + " list.add($t.getName());\n" + "end";
KieSession ksession = getKieSession(str);
Result result = new Result();
ksession.insert(result);
final List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
final Woman alice = new Woman("Alice", 38);
final Man bob = new Man("Bob", 40);
bob.setWife(alice);
final Child charlie = new Child("Charles", 12);
final Child debbie = new Child("Debbie", 10);
alice.addChild(charlie);
alice.addChild(debbie);
charlie.addToy(new Toy("car"));
charlie.addToy(new Toy("ball"));
debbie.addToy(new Toy("doll"));
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("car", "ball");
}
Aggregations