Search in sources :

Example 46 with KieHelper

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

the class OOPathTest method testConstraintExternalToOopath.

@Test
public void testConstraintExternalToOopath() {
    // DROOLS-2135
    final String drl = "import " + Parent.class.getCanonicalName() + ";\n" + "import " + Son.class.getCanonicalName() + ";\n" + "global java.util.List list\n\n" + "rule R when\n" + "  Parent( $child : /children, $child.name == \"joe\" )\n" + "then\n" + "  list.add( $child.getName() );\n" + "end\n";
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    List<String> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    Son joe = new Son("joe");
    Son jack = new Son("jack");
    Parent parent = new Parent(Arrays.asList(joe, jack));
    ksession.insert(parent);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("joe", list.get(0));
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 47 with KieHelper

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

the class OOPathTest method testAddAllRemoveIdx.

@Test
public void testAddAllRemoveIdx() {
    final String drl = "import org.drools.compiler.oopath.model.*;\n" + "\n" + "rule R2 when\n" + "  Group( $id: name, $p: /members[age >= 30] )\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 Group x = new Group("X");
    final Group y = new Group("Y");
    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"));
    final Adult ada = new Adult("Ada", 29);
    final Adult bea = new Adult("Bea", 29);
    final List<Person> bothList = Arrays.asList(new Person[] { ada, bea });
    x.getMembers().addAll(bothList);
    y.getMembers().addAll(bothList);
    ksession.insert(x);
    ksession.insert(y);
    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"));
    ada.setAge(30);
    ksession.fireAllRules();
    assertTrue(factsCollection(ksession).contains("X.Ada"));
    assertFalse(factsCollection(ksession).contains("X.Bea"));
    assertTrue(factsCollection(ksession).contains("Y.Ada"));
    assertFalse(factsCollection(ksession).contains("Y.Bea"));
    // removing Bea from Y
    y.getMembers().remove(1);
    bea.setAge(30);
    ksession.fireAllRules();
    assertTrue(factsCollection(ksession).contains("X.Ada"));
    assertTrue(factsCollection(ksession).contains("X.Bea"));
    assertTrue(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) Person(org.drools.compiler.oopath.model.Person) Test(org.junit.Test)

Example 48 with KieHelper

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

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

the class OOPathTest method testDoubleAdd.

@Test
public void testDoubleAdd() {
    // 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 Group x = new Group("X");
    final Group y = new Group("Y");
    ksession.insert(x);
    ksession.insert(y);
    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"));
    final Adult ada = new Adult("Ada", 20);
    final Adult bea = new Adult("Bea", 20);
    x.addPerson(ada);
    x.addPerson(bea);
    y.addPerson(ada);
    y.addPerson(bea);
    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 50 with KieHelper

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

the class OOPathTest method testListIteratorRemove.

@Test
public void testListIteratorRemove() {
    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" + "  insertLogical(      $id + \".\" + $p.getName() );\n" + "end\n";
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    ksession.addEventListener(new DefaultRuleRuntimeEventListener() {

        @Override
        public void objectDeleted(ObjectDeletedEvent event) {
            System.out.println(event.getOldObject() + " -> " + "_");
        }

        @Override
        public void objectInserted(ObjectInsertedEvent event) {
            System.out.println("_" + " -> " + event.getObject());
        }

        @Override
        public void objectUpdated(ObjectUpdatedEvent event) {
            System.out.println(event.getOldObject() + " -> " + event.getObject());
        }
    });
    final TMDirectory x = new TMDirectory("X");
    final TMDirectory y = new TMDirectory("Y");
    final TMFile file0 = new TMFile("File0", 999);
    final TMFile file1 = new TMFile("File1", 999);
    final TMFile file2 = new TMFile("File2", 999);
    x.getFiles().addAll(Arrays.asList(file0, file1, file2));
    y.getFiles().addAll(Arrays.asList(file0, file1, file2));
    ksession.insert(x);
    ksession.insert(y);
    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"));
    assertTrue(factsCollection(ksession).contains("Y.File1"));
    assertTrue(factsCollection(ksession).contains("Y.File2"));
    final ListIterator<TMFile> xIterator = x.getFiles().listIterator(1);
    final ListIterator<TMFile> yIterator = y.getFiles().listIterator();
    xIterator.next();
    xIterator.remove();
    yIterator.next();
    yIterator.next();
    yIterator.next();
    yIterator.remove();
    ksession.fireAllRules();
    assertTrue(factsCollection(ksession).contains("X.File0"));
    assertFalse(factsCollection(ksession).contains("X.File1"));
    assertTrue(factsCollection(ksession).contains("X.File2"));
    assertTrue(factsCollection(ksession).contains("Y.File0"));
    assertTrue(factsCollection(ksession).contains("Y.File1"));
    assertFalse(factsCollection(ksession).contains("Y.File2"));
    assertTrue(xIterator.hasNext());
    assertTrue(xIterator.hasPrevious());
    assertFalse(yIterator.hasNext());
    assertTrue(yIterator.hasPrevious());
    xIterator.next();
    xIterator.remove();
    yIterator.previous();
    yIterator.remove();
    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"));
    assertFalse(xIterator.hasNext());
    assertTrue(xIterator.hasPrevious());
    assertFalse(yIterator.hasNext());
    assertTrue(yIterator.hasPrevious());
    xIterator.previous();
    xIterator.remove();
    yIterator.previous();
    yIterator.remove();
    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"));
    assertFalse(xIterator.hasNext());
    assertFalse(xIterator.hasPrevious());
    assertFalse(yIterator.hasNext());
    assertFalse(yIterator.hasPrevious());
}
Also used : TMFile(org.drools.compiler.oopath.model.TMFile) ObjectDeletedEvent(org.kie.api.event.rule.ObjectDeletedEvent) TMDirectory(org.drools.compiler.oopath.model.TMDirectory) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) ObjectUpdatedEvent(org.kie.api.event.rule.ObjectUpdatedEvent) ObjectInsertedEvent(org.kie.api.event.rule.ObjectInsertedEvent) DefaultRuleRuntimeEventListener(org.kie.api.event.rule.DefaultRuleRuntimeEventListener) 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