Search in sources :

Example 16 with Toy

use of org.drools.compiler.oopath.model.Toy in project drools by kiegroup.

the class OOPathReactiveTests method testReactiveOnLia.

@Test
public void testReactiveOnLia() {
    final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Man( $toy: /wife/children[age > 10]/toys )\n" + "then\n" + "  list.add( $toy.getName() );\n" + "end\n";
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().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");
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.compiler.oopath.model.Man) Woman(org.drools.compiler.oopath.model.Woman) Child(org.drools.compiler.oopath.model.Child) Toy(org.drools.compiler.oopath.model.Toy) Test(org.junit.Test)

Example 17 with Toy

use of org.drools.compiler.oopath.model.Toy in project drools by kiegroup.

the class OOPathReactiveTests method testReactive2Rules.

@Test
public void testReactive2Rules() {
    final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List toyList\n" + "global java.util.List teenagers\n" + "\n" + "rule R1 when\n" + "  $i : Integer()\n" + "  Man( $toy: /wife/children[age >= $i]/toys )\n" + "then\n" + "  toyList.add( $toy.getName() );\n" + "end\n" + "rule R2 when\n" + "  School( $child: /children[age >= 13] )\n" + "then\n" + "  teenagers.add( $child.getName() );\n" + "end\n";
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    final List<String> toyList = new ArrayList<>();
    ksession.setGlobal("toyList", toyList);
    final List<String> teenagers = new ArrayList<>();
    ksession.setGlobal("teenagers", teenagers);
    final Woman alice = new Woman("Alice", 38);
    final Man bob = new Man("Bob", 40);
    bob.setWife(alice);
    final Child charlie = new Child("Charles", 15);
    final Child debbie = new Child("Debbie", 12);
    alice.addChild(charlie);
    alice.addChild(debbie);
    charlie.addToy(new Toy("car"));
    charlie.addToy(new Toy("ball"));
    debbie.addToy(new Toy("doll"));
    final School school = new School("Da Vinci");
    school.addChild(charlie);
    school.addChild(debbie);
    ksession.insert(13);
    ksession.insert(bob);
    ksession.insert(school);
    ksession.fireAllRules();
    Assertions.assertThat(toyList).containsExactlyInAnyOrder("car", "ball");
    Assertions.assertThat(teenagers).containsExactlyInAnyOrder("Charles");
    toyList.clear();
    debbie.setAge(13);
    ksession.fireAllRules();
    Assertions.assertThat(toyList).containsExactlyInAnyOrder("doll");
    Assertions.assertThat(teenagers).containsExactlyInAnyOrder("Charles", "Debbie");
}
Also used : School(org.drools.compiler.oopath.model.School) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.compiler.oopath.model.Man) Woman(org.drools.compiler.oopath.model.Woman) Child(org.drools.compiler.oopath.model.Child) Toy(org.drools.compiler.oopath.model.Toy) Test(org.junit.Test)

Example 18 with Toy

use of org.drools.compiler.oopath.model.Toy in project drools by kiegroup.

the class OOPathReactiveTests method testAllNonReactiveAfterNonReactivePart.

@Test
public void testAllNonReactiveAfterNonReactivePart() {
    final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Man( $toy: ?/wife/children[age > 10]/toys )\n" + "then\n" + "  list.add( $toy.getName() );\n" + "end\n";
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().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();
    charlie.addToy(new Toy("robot"));
    ksession.fireAllRules();
    Assertions.assertThat(list).isEmpty();
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.compiler.oopath.model.Man) Woman(org.drools.compiler.oopath.model.Woman) Child(org.drools.compiler.oopath.model.Child) Toy(org.drools.compiler.oopath.model.Toy) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)18 Man (org.drools.compiler.oopath.model.Man)18 Toy (org.drools.compiler.oopath.model.Toy)18 Child (org.drools.compiler.oopath.model.Child)17 Woman (org.drools.compiler.oopath.model.Woman)17 KieSession (org.kie.api.runtime.KieSession)16 KieHelper (org.kie.internal.utils.KieHelper)16 Test (org.junit.Test)15 KieBase (org.kie.api.KieBase)2 BabyBoy (org.drools.compiler.oopath.model.BabyBoy)1 BabyGirl (org.drools.compiler.oopath.model.BabyGirl)1 School (org.drools.compiler.oopath.model.School)1 InternalFactHandle (org.drools.core.common.InternalFactHandle)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1 BetaMemory (org.drools.core.reteoo.BetaMemory)1 EntryPointNode (org.drools.core.reteoo.EntryPointNode)1 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)1 LeftTuple (org.drools.core.reteoo.LeftTuple)1 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)1 ReactiveFromNode (org.drools.core.reteoo.ReactiveFromNode)1