Search in sources :

Example 21 with Man

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

the class OOPathReactiveTest method testNonReactivePart.

@Test
public void testNonReactivePart() {
    final String drl = "import org.drools.mvel.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";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession ksession = kbase.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 : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.mvel.compiler.oopath.model.Man) Woman(org.drools.mvel.compiler.oopath.model.Woman) Child(org.drools.mvel.compiler.oopath.model.Child) Toy(org.drools.mvel.compiler.oopath.model.Toy) Test(org.junit.Test)

Example 22 with Man

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

the class OOPathReactiveTest method testReactivitySettingAttributeInDrl.

@Test
public void testReactivitySettingAttributeInDrl() {
    final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "\n" + "rule R when\n" + "  Man( $child: /wife/children[age >= 10] )\n" + "then\n" + "end\n" + "rule R2 when\n" + "  Man( $child: /wife/children[age < 10] )\n" + "then\n" + "$child.setAge(12);" + "end\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession ksession = kbase.newKieSession();
    final Man bob = new Man("Bob", 40);
    final Woman alice = new Woman("Alice", 38);
    final Child charlie = new Child("Charles", 9);
    final Child debbie = new Child("Debbie", 8);
    bob.setWife(alice);
    alice.addChild(charlie);
    alice.addChild(debbie);
    ksession.insert(bob);
    Assertions.assertThat(ksession.fireAllRules()).isEqualTo(4);
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.mvel.compiler.oopath.model.Man) Woman(org.drools.mvel.compiler.oopath.model.Woman) Child(org.drools.mvel.compiler.oopath.model.Child) Test(org.junit.Test)

Example 23 with Man

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

the class OOPathReactiveTest method testReactive2Rules.

@Test
public void testReactive2Rules() {
    final String drl = "import org.drools.mvel.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";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession ksession = kbase.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.mvel.compiler.oopath.model.School) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.mvel.compiler.oopath.model.Man) Woman(org.drools.mvel.compiler.oopath.model.Woman) Child(org.drools.mvel.compiler.oopath.model.Child) Toy(org.drools.mvel.compiler.oopath.model.Toy) Test(org.junit.Test)

Example 24 with Man

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

the class OOPathReactiveTest method testAllNonReactiveAfterNonReactivePart.

@Test
public void testAllNonReactiveAfterNonReactivePart() {
    final String drl = "import org.drools.mvel.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";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession ksession = kbase.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 : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.mvel.compiler.oopath.model.Man) Woman(org.drools.mvel.compiler.oopath.model.Woman) Child(org.drools.mvel.compiler.oopath.model.Child) Toy(org.drools.mvel.compiler.oopath.model.Toy) Test(org.junit.Test)

Example 25 with Man

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

the class OOPathCastTest method testInlineCastWithConstraint.

@Test
public void testInlineCastWithConstraint() {
    final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Man( name == \"Bob\", $name: /wife/children#BabyGirl[ favoriteDollName.startsWith(\"A\") ].name )\n" + "then\n" + "  list.add( $name );\n" + "end\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession ksession = kbase.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 BabyBoy charlie = new BabyBoy("Charles", 12);
    final BabyGirl debbie = new BabyGirl("Debbie", 8, "Anna");
    final BabyGirl elisabeth = new BabyGirl("Elisabeth", 5, "Zoe");
    final BabyGirl farrah = new BabyGirl("Farrah", 3, "Agatha");
    alice.addChild(charlie);
    alice.addChild(debbie);
    alice.addChild(elisabeth);
    alice.addChild(farrah);
    ksession.insert(bob);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("Debbie", "Farrah");
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) BabyBoy(org.drools.mvel.compiler.oopath.model.BabyBoy) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.mvel.compiler.oopath.model.Man) Woman(org.drools.mvel.compiler.oopath.model.Woman) BabyGirl(org.drools.mvel.compiler.oopath.model.BabyGirl) Test(org.junit.Test)

Aggregations

Man (org.drools.mvel.compiler.oopath.model.Man)34 KieSession (org.kie.api.runtime.KieSession)32 ArrayList (java.util.ArrayList)31 KieBase (org.kie.api.KieBase)29 Child (org.drools.mvel.compiler.oopath.model.Child)27 Woman (org.drools.mvel.compiler.oopath.model.Woman)24 Test (org.junit.Test)24 Toy (org.drools.mvel.compiler.oopath.model.Toy)19 InternalFactHandle (org.drools.core.common.InternalFactHandle)3 BabyBoy (org.drools.mvel.compiler.oopath.model.BabyBoy)2 BabyGirl (org.drools.mvel.compiler.oopath.model.BabyGirl)2 Collection (java.util.Collection)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 ClassObjectType (org.drools.core.base.ClassObjectType)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