use of org.drools.modelcompiler.domain.Toy in project drools by kiegroup.
the class FromTest method testFromOr.
@Test
public void testFromOr() {
String str = "package org.drools.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "import " + Toy.class.getCanonicalName() + "\n" + "import " + ToysStore.class.getCanonicalName() + "\n" + "global java.util.List list;\n" + "rule R\n" + "when\n" + " Person($age : age)\n" + " Address($c : city)\n" + " $store : ToysStore(cityName == $c)\n" + " (or Toy( targetAge == $age ) from $store.firstFloorToys \n" + " Toy( targetAge == $age ) from $store.secondFloorToys\n" + " )\n" + "then\n" + " list.add($store.getStoreName());\n" + "end \n";
KieSession ksession = getKieSession(str);
List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
ksession.insert(new Person("Leonardo", 3));
ksession.insert(new Address("Milan"));
Toy car = new Toy("Car", 3);
Toy bicycle = new Toy("Bicycle", 3);
Toy computer = new Toy("Computer", 7);
ksession.insert(car);
ksession.insert(bicycle);
ksession.insert(computer);
ToysStore ts = new ToysStore("Milan", "Toystore1");
ts.getFirstFloorToys().add(car);
ts.getSecondFloorToys().addAll(Arrays.asList(bicycle, computer));
ksession.insert(ts);
ksession.fireAllRules();
Assertions.assertThat(list).contains("Toystore1");
}
use of org.drools.modelcompiler.domain.Toy in project drools by kiegroup.
the class ConstraintNormalizationTest method testNormalizationForPropertyReactivity.
@Test
public void testNormalizationForPropertyReactivity() {
final String str = "package org.drools.test;\n" + "import " + Person.class.getCanonicalName() + ";\n" + "import " + Toy.class.getCanonicalName() + ";\n" + "rule R1 when \n" + " $t : Toy($owner : owner)\n" + " $p : Person($owner == name)\n" + "then\n" + " $p.setAge(20);" + " update($p);" + "end\n" + "rule R2 when \n" + " $p : Person(age == 20)\n" + "then\n" + "end\n";
final KieSession ksession = getKieSession(str);
final Toy t = new Toy("Ball");
t.setOwner("Toshiya");
final Person p = new Person("Toshiya", 45);
ksession.insert(t);
ksession.insert(p);
// no infinite loop
assertEquals(2, ksession.fireAllRules(10));
}
use of org.drools.modelcompiler.domain.Toy 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.Toy 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.Toy in project drools by kiegroup.
the class OOPathFlowTest 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(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");
list.clear();
debbie.setAge(11);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("doll");
}
Aggregations