Search in sources :

Example 1 with Man

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

the class OOPathTest method testIndexedAccess.

@Test
public void testIndexedAccess() {
    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[0]/toys[1] )\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", 11);
    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("ball");
}
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 2 with Man

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

the class OOPathTest method testPrimitives.

@Test
public void testPrimitives() {
    // DROOLS-1266
    final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Adult( $x: /children.age )\n" + "then\n" + "  list.add( $x );\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 Man bob = new Man("Bob", 40);
    bob.addChild(new Child("Charles", 12));
    bob.addChild(new Child("Debbie", 8));
    ksession.insert(bob);
    ksession.fireAllRules();
    Assertions.assertThat(list).hasSize(2);
}
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) Child(org.drools.compiler.oopath.model.Child) Test(org.junit.Test)

Example 3 with Man

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

the class OOPathTest method testBackReferenceConstraint.

@Test
public void testBackReferenceConstraint() {
    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/toys[ name.length == ../name.length ] )\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("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");
}
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 4 with Man

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

the class OOPathTest method testWith2Peers.

@Test
public void testWith2Peers() {
    // DROOLS-1589
    String header = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n\n";
    String drl1 = "rule R1 when\n" + "  Man( $m: /wife[age == 25] )\n" + "then\n" + "  list.add($m.getName());\n" + "end\n\n";
    String drl2 = "rule R2 when\n" + "  Man( $m: /wife[age == 26] )\n" + "then\n" + "  list.add($m.getName());\n" + "end\n\n";
    String drl3 = "rule R3 when\n" + "  Man( $m: /wife[age == 27] )\n" + "then\n" + "  list.add($m.getName());\n" + "end\n\n";
    final KieSession ksession = new KieHelper().addContent(header + drl1 + drl2 + drl3, ResourceType.DRL).build().newKieSession();
    final List<String> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    final Man bob = new Man("John", 25);
    bob.setWife(new Woman("Jane", 25));
    ksession.insert(bob);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("Jane", list.get(0));
    list.clear();
    bob.getWife().setAge(26);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("Jane", list.get(0));
    list.clear();
    bob.getWife().setAge(27);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("Jane", list.get(0));
    list.clear();
    bob.getWife().setAge(28);
    ksession.fireAllRules();
    assertEquals(0, list.size());
}
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) Test(org.junit.Test)

Example 5 with Man

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

the class OOPathTest method testNotReactivePeer.

@Test
public void testNotReactivePeer() {
    // DROOLS-1727
    String drl1 = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n\n" + "rule R1 when\n" + "  not String()\n" + "  $a : Man( name == \"Mario\" )\n" + "then\n" + "  list.add(\"Found\");\n" + "  insert($a.getName());\n" + "end\n\n" + "rule R2 when\n" + "  not String()\n" + "  $a : Man( $c: /children[age == 6], name == \"Mario\" )\n" + "then\n" + "  list.add(\"Found\");\n" + "  insert($a.getName());\n" + "end\n\n";
    KieSession ksession = new KieHelper().addContent(drl1, ResourceType.DRL).build().newKieSession();
    List<String> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    Man mario = new Man("Mario", 40);
    mario.addChild(new Child("Sofia", 6));
    ksession.insert(mario);
    ksession.fireAllRules();
    assertEquals(1, list.size());
}
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) Child(org.drools.compiler.oopath.model.Child) Test(org.junit.Test)

Aggregations

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