Search in sources :

Example 56 with KieHelper

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

the class OOPathTest method testRecursiveOOPathQuery.

@Test
public void testRecursiveOOPathQuery() {
    final String drl = "import org.drools.compiler.oopath.model.Thing;\n" + "global java.util.List list\n" + "\n" + "rule \"Print all things contained in the Office\" when\n" + "    $office : Thing( name == \"office\" )\n" + "    isContainedIn( $office, thing; )\n" + "then\n" + "    list.add( thing.getName() );\n" + "end\n" + "\n" + "query isContainedIn( Thing $x, Thing $y )\n" + "    $y := /$x/children\n" + "or\n" + "    ( $z := /$x/children and isContainedIn( $z, $y; ) )\n" + "end\n";
    final Thing house = new Thing("house");
    final Thing office = new Thing("office");
    house.addChild(office);
    final Thing kitchen = new Thing("kitchen");
    house.addChild(kitchen);
    final Thing knife = new Thing("knife");
    kitchen.addChild(knife);
    final Thing cheese = new Thing("cheese");
    kitchen.addChild(cheese);
    final Thing desk = new Thing("desk");
    office.addChild(desk);
    final Thing chair = new Thing("chair");
    office.addChild(chair);
    final Thing computer = new Thing("computer");
    desk.addChild(computer);
    final Thing draw = new Thing("draw");
    desk.addChild(draw);
    final Thing key = new Thing("key");
    draw.addChild(key);
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    final List<String> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    ksession.insert(house);
    ksession.insert(office);
    ksession.insert(kitchen);
    ksession.insert(knife);
    ksession.insert(cheese);
    ksession.insert(desk);
    ksession.insert(chair);
    ksession.insert(computer);
    ksession.insert(draw);
    ksession.insert(key);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("desk", "chair", "key", "draw", "computer");
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Thing(org.drools.compiler.oopath.model.Thing) Test(org.junit.Test)

Example 57 with KieHelper

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

the class OOPathTest method testDereferencedDeclarationOutsideOOPath.

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

Example 58 with KieHelper

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

Example 59 with KieHelper

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

the class OOPathTest method testMiscListMethods.

@Test
public void testMiscListMethods() {
    final String drl = "import org.drools.compiler.oopath.model.*;\n" + "\n" + "rule R2 when\n" + "  TMDirectory( $id: name, $p: /files[size >= 100] )\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 TMDirectory x = new TMDirectory("X");
    final TMDirectory y = new TMDirectory("Y");
    ksession.insert(x);
    ksession.insert(y);
    ksession.fireAllRules();
    assertFalse(factsCollection(ksession).contains("X.File0"));
    assertFalse(factsCollection(ksession).contains("X.File1"));
    assertFalse(factsCollection(ksession).contains("Y.File0"));
    assertFalse(factsCollection(ksession).contains("Y.File1"));
    final TMFile file0 = new TMFile("File0", 47);
    final TMFile file1 = new TMFile("File1", 47);
    final TMFile file2 = new TMFile("File2", 47);
    x.getFiles().add(file2);
    x.getFiles().addAll(0, Arrays.asList(file0, file1));
    y.getFiles().add(0, file2);
    y.getFiles().add(0, file0);
    y.getFiles().add(1, file1);
    ksession.fireAllRules();
    assertFalse(factsCollection(ksession).contains("X.File0"));
    assertFalse(factsCollection(ksession).contains("X.File1"));
    assertFalse(factsCollection(ksession).contains("X.File2"));
    assertFalse(factsCollection(ksession).contains("Y.File0"));
    assertFalse(factsCollection(ksession).contains("Y.File1"));
    assertFalse(factsCollection(ksession).contains("Y.File2"));
    file0.setSize(999);
    ksession.fireAllRules();
    assertTrue(factsCollection(ksession).contains("X.File0"));
    assertFalse(factsCollection(ksession).contains("X.File1"));
    assertFalse(factsCollection(ksession).contains("X.File2"));
    assertTrue(factsCollection(ksession).contains("Y.File0"));
    assertFalse(factsCollection(ksession).contains("Y.File1"));
    assertFalse(factsCollection(ksession).contains("Y.File2"));
    // removing File1 from Y
    y.getFiles().remove(1);
    file1.setSize(999);
    ksession.fireAllRules();
    assertTrue(factsCollection(ksession).contains("X.File0"));
    assertTrue(factsCollection(ksession).contains("X.File1"));
    assertFalse(factsCollection(ksession).contains("X.File2"));
    assertTrue(factsCollection(ksession).contains("Y.File0"));
    assertFalse(factsCollection(ksession).contains("Y.File1"));
    assertFalse(factsCollection(ksession).contains("Y.File2"));
    file2.setSize(999);
    ksession.fireAllRules();
    assertTrue(factsCollection(ksession).contains("X.File0"));
    assertTrue(factsCollection(ksession).contains("X.File1"));
    assertTrue(factsCollection(ksession).contains("X.File2"));
    assertTrue(factsCollection(ksession).contains("Y.File0"));
    assertFalse(factsCollection(ksession).contains("Y.File1"));
    assertTrue(factsCollection(ksession).contains("Y.File2"));
    final TMFile file0R = new TMFile("File0R", 999);
    x.getFiles().set(0, file0R);
    ksession.fireAllRules();
    assertFalse(factsCollection(ksession).contains("X.File0"));
    assertTrue(factsCollection(ksession).contains("X.File0R"));
    assertTrue(factsCollection(ksession).contains("X.File1"));
    assertTrue(factsCollection(ksession).contains("X.File2"));
    assertTrue(factsCollection(ksession).contains("Y.File0"));
    assertFalse(factsCollection(ksession).contains("Y.File1"));
    assertTrue(factsCollection(ksession).contains("Y.File2"));
}
Also used : TMFile(org.drools.compiler.oopath.model.TMFile) TMDirectory(org.drools.compiler.oopath.model.TMDirectory) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 60 with KieHelper

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

the class OOPathAccumulateTest method testAccumulateCollection.

private void testAccumulateCollection(final String accumulateFunction, final Integer... expectedResults) {
    final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.Collection<Integer> globalVar\n" + "\n" + "rule R when\n" + "  accumulate ( Adult( $child: /children ) ; $accumulateResult: " + accumulateFunction + "($child.getAge()) )\n" + "then\n" + "  kcontext.getKieRuntime().setGlobal(\"globalVar\", $accumulateResult);\n" + "end\n";
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    final Man bob = new Man("Bob", 40);
    bob.addChild(new Child("Charles", 12));
    bob.addChild(new Child("Debbie", 8));
    ksession.insert(bob);
    ksession.fireAllRules();
    final Collection<Integer> result = (Collection<Integer>) ksession.getGlobal("globalVar");
    Assertions.assertThat(result).containsExactlyInAnyOrder(expectedResults);
}
Also used : KieHelper(org.kie.internal.utils.KieHelper) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.compiler.oopath.model.Man) Child(org.drools.compiler.oopath.model.Child)

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