use of org.drools.modelcompiler.domain.Man in project drools by kiegroup.
the class OOPathTest method testBackReferenceConstraint.
@Test
public void testBackReferenceConstraint() {
final String str = "import org.drools.modelcompiler.domain.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Man( $toy: /wife/children/toys[ name.length == ../name.length ] )\n" + "then\n" + " list.add( $toy.getName() );\n" + "end\n";
KieSession ksession = getKieSession(str);
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("Carl", 12);
final Child debbie = new Child("Debbie", 8);
alice.addChild(charlie);
alice.addChild(debbie);
charlie.addToy(new Toy("car"));
charlie.addToy(new Toy("ball"));
debbie.addToy(new Toy("doll"));
debbie.addToy(new Toy("guitar"));
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("ball", "guitar");
}
use of org.drools.modelcompiler.domain.Man in project drools by kiegroup.
the class PatternDSLTest method testReactiveOOPath.
@Test
public void testReactiveOOPath() {
Global<List> listG = globalOf(List.class, "defaultpkg", "list");
Variable<Man> manV = declarationOf(Man.class);
Variable<Woman> wifeV = declarationOf(Woman.class, reactiveFrom(manV, Man::getWife));
Variable<Child> childV = declarationOf(Child.class, reactiveFrom(wifeV, Woman::getChildren));
Variable<Toy> toyV = declarationOf(Toy.class, reactiveFrom(childV, Child::getToys));
Rule rule = rule("oopath").build(pattern(manV), pattern(wifeV), pattern(childV).expr("exprA", c -> c.getAge() > 10), pattern(toyV), on(toyV, listG).execute((t, l) -> l.add(t.getName())));
Model model = new ModelImpl().addGlobal(listG).addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
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");
list.clear();
debbie.setAge(11);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("doll");
}
use of org.drools.modelcompiler.domain.Man 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");
}
use of org.drools.modelcompiler.domain.Man in project drools by kiegroup.
the class FlowTest method testConcatenatedFrom.
@Test
public void testConcatenatedFrom() {
Global<List> listG = globalOf(List.class, "defaultpkg", "list");
Variable<Man> manV = declarationOf(Man.class);
Variable<Woman> wifeV = declarationOf(Woman.class, from(manV, Man::getWife));
Variable<Child> childV = declarationOf(Child.class, from(wifeV, Woman::getChildren));
Variable<Toy> toyV = declarationOf(Toy.class, from(childV, Child::getToys));
Rule rule = rule("froms").build(expr("exprA", childV, c -> c.getAge() > 10), on(toyV, listG).execute((t, l) -> l.add(t.getName())));
Model model = new ModelImpl().addGlobal(listG).addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
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");
}
use of org.drools.modelcompiler.domain.Man in project drools by kiegroup.
the class FromTest method testFromExpression.
@Test
public void testFromExpression() {
final String str = "import org.drools.modelcompiler.domain.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Man( $wife : wife )\n" + " $child: Child( age > 10 ) from $wife.children\n" + "then\n" + " list.add( $child.getName() );\n" + "end\n";
KieSession ksession = getKieSession(str);
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);
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("Charles");
}
Aggregations