Search in sources :

Example 51 with KieHelper

use of org.kie.internal.utils.KieHelper 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 52 with KieHelper

use of org.kie.internal.utils.KieHelper 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 53 with KieHelper

use of org.kie.internal.utils.KieHelper 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 54 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class OOPathTest method testDoubleRemove.

@Test
public void testDoubleRemove() {
    // DROOLS-1376
    final String drl = "import org.drools.compiler.oopath.model.*;\n" + "\n" + "rule R2 when\n" + "  Group( $id: name, $p: /members[age >= 20] )\n" + "then\n" + "  System.out.println( $id + \".\" + $p.getName() );\n" + "  insertLogical(      $id + \".\" + $p.getName() );\n" + "end\n";
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    final Adult ada = new Adult("Ada", 20);
    final Adult bea = new Adult("Bea", 20);
    final Group x = new Group("X");
    final Group y = new Group("Y");
    x.addPerson(ada);
    x.addPerson(bea);
    y.addPerson(ada);
    y.addPerson(bea);
    ksession.insert(x);
    ksession.insert(y);
    ksession.fireAllRules();
    assertTrue(factsCollection(ksession).contains("X.Ada"));
    assertTrue(factsCollection(ksession).contains("X.Bea"));
    assertTrue(factsCollection(ksession).contains("Y.Ada"));
    assertTrue(factsCollection(ksession).contains("Y.Bea"));
    x.removePerson(ada);
    x.removePerson(bea);
    y.removePerson(ada);
    y.removePerson(bea);
    ksession.fireAllRules();
    assertFalse(factsCollection(ksession).contains("X.Ada"));
    assertFalse(factsCollection(ksession).contains("X.Bea"));
    assertFalse(factsCollection(ksession).contains("Y.Ada"));
    assertFalse(factsCollection(ksession).contains("Y.Bea"));
}
Also used : Group(org.drools.compiler.oopath.model.Group) KieHelper(org.kie.internal.utils.KieHelper) Adult(org.drools.compiler.oopath.model.Adult) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 55 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class OOPathTest method testOOPathWithLocalInnerDeclaration.

@Test
public void testOOPathWithLocalInnerDeclaration() {
    // DROOLS-1411
    final String drl = "import org.drools.compiler.oopath.model.*;\n" + "import " + TMFileSetQuater.class.getCanonicalName() + ";\n" + "global java.util.Set duplicateNames; \n" + "\n" + "rule DIFF_FILES_BUT_WITH_SAME_FILENAME when\n" + "  $ic1 : TMFileWithParentObj( $curName : name, $curId : id, \n" + "                               $ic2: /parent#TMFileSetQuater/files[name == $curName, id != $curId ] )\n" + "then\n" + "  System.out.println( $ic1 + \" \" + $ic2 );\n" + "  duplicateNames.add( $ic1.getName() );\n" + "end\n";
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    final Set duplicateNames = new HashSet();
    ksession.setGlobal("duplicateNames", duplicateNames);
    final TMFileSetQuater x = new TMFileSetQuater("X");
    final TMFileWithParentObj file0 = new TMFileWithParentObj(0, "File0", 47, x);
    final TMFileWithParentObj file1 = new TMFileWithParentObj(1, "File1", 47, x);
    final TMFileWithParentObj file2 = new TMFileWithParentObj(2, "File0", 47, x);
    x.getFiles().addAll(Arrays.asList(file0, file1, file2));
    ksession.insert(x);
    ksession.insert(file0);
    ksession.insert(file1);
    ksession.insert(file2);
    ksession.fireAllRules();
    assertTrue(duplicateNames.contains("File0"));
    assertFalse(duplicateNames.contains("File1"));
}
Also used : HashSet(java.util.HashSet) TMFileSet(org.drools.compiler.oopath.model.TMFileSet) Set(java.util.Set) ReactiveSet(org.drools.core.phreak.ReactiveSet) TMFileWithParentObj(org.drools.compiler.oopath.model.TMFileWithParentObj) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

KieHelper (org.kie.internal.utils.KieHelper)467 Test (org.junit.Test)427 KieSession (org.kie.api.runtime.KieSession)355 ArrayList (java.util.ArrayList)194 KieBase (org.kie.api.KieBase)152 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)77 Person (org.drools.compiler.Person)61 FactHandle (org.kie.api.runtime.rule.FactHandle)55 List (java.util.List)44 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)41 RuleUnitExecutor (org.kie.api.runtime.rule.RuleUnitExecutor)38 InternalRuleUnitExecutor (org.drools.core.impl.InternalRuleUnitExecutor)37 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)34 Man (org.drools.compiler.oopath.model.Man)29 Child (org.drools.compiler.oopath.model.Child)24 Woman (org.drools.compiler.oopath.model.Woman)23 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)23 InternalFactHandle (org.drools.core.common.InternalFactHandle)18 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)18 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)18