Search in sources :

Example 1 with ObjectUpdatedEvent

use of org.kie.api.event.rule.ObjectUpdatedEvent 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)

Example 2 with ObjectUpdatedEvent

use of org.kie.api.event.rule.ObjectUpdatedEvent in project drools by kiegroup.

the class RuleRuntimeEventSupportTest method testRuleRuntimeEventListener.

@Test
public void testRuleRuntimeEventListener() {
    final KieBase rb = KnowledgeBaseFactory.newKnowledgeBase();
    final KieSession wm = rb.newKieSession();
    final List wmList = new ArrayList();
    final RuleRuntimeEventListener workingMemoryListener = new RuleRuntimeEventListener() {

        public void objectInserted(ObjectInsertedEvent event) {
            wmList.add(event);
        }

        public void objectUpdated(ObjectUpdatedEvent event) {
            wmList.add(event);
        }

        public void objectDeleted(ObjectDeletedEvent event) {
            wmList.add(event);
        }
    };
    wm.addEventListener(workingMemoryListener);
    assertEquals(1, wm.getRuleRuntimeEventListeners().size());
    final Cheese stilton = new Cheese("stilton", 15);
    final Cheese cheddar = new Cheese("cheddar", 17);
    final FactHandle stiltonHandle = wm.insert(stilton);
    ObjectInsertedEvent oae = (ObjectInsertedEvent) wmList.get(0);
    assertSame(stiltonHandle, oae.getFactHandle());
    wm.update(stiltonHandle, cheddar);
    final ObjectUpdatedEvent ome = (ObjectUpdatedEvent) wmList.get(1);
    assertSame(stiltonHandle, ome.getFactHandle());
    assertEquals(cheddar, ome.getObject());
    assertEquals(stilton, ome.getOldObject());
    wm.retract(stiltonHandle);
    final ObjectDeletedEvent ore = (ObjectDeletedEvent) wmList.get(2);
    assertSame(stiltonHandle, ore.getFactHandle());
    final FactHandle cheddarHandle = wm.insert(cheddar);
    oae = (ObjectInsertedEvent) wmList.get(3);
    assertSame(cheddarHandle, oae.getFactHandle());
}
Also used : ObjectDeletedEvent(org.kie.api.event.rule.ObjectDeletedEvent) RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) ObjectUpdatedEvent(org.kie.api.event.rule.ObjectUpdatedEvent) Cheese(org.drools.core.test.model.Cheese) ObjectInsertedEvent(org.kie.api.event.rule.ObjectInsertedEvent) Test(org.junit.Test)

Example 3 with ObjectUpdatedEvent

use of org.kie.api.event.rule.ObjectUpdatedEvent in project drools by kiegroup.

the class RuleRuntimeEventSupportTest method testRemoveRuleRuntimeEventListener.

@Test
public void testRemoveRuleRuntimeEventListener() {
    KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    KieSession ksession = kbase.newKieSession();
    final List wmList = new ArrayList();
    final RuleRuntimeEventListener eventListener = new RuleRuntimeEventListener() {

        public void objectInserted(ObjectInsertedEvent event) {
            wmList.add(event);
        }

        public void objectUpdated(ObjectUpdatedEvent event) {
            wmList.add(event);
        }

        public void objectDeleted(ObjectDeletedEvent event) {
            wmList.add(event);
        }
    };
    ksession.addEventListener(eventListener);
    ksession.removeEventListener(eventListener);
    final Cheese stilton = new Cheese("stilton", 15);
    final Cheese cheddar = new Cheese("cheddar", 17);
    final FactHandle stiltonHandle = ksession.insert(stilton);
    assertTrue(wmList.isEmpty());
    ksession.update(stiltonHandle, stilton);
    assertTrue(wmList.isEmpty());
    ksession.retract(stiltonHandle);
    assertTrue(wmList.isEmpty());
    ksession.insert(cheddar);
    assertTrue(wmList.isEmpty());
}
Also used : ObjectDeletedEvent(org.kie.api.event.rule.ObjectDeletedEvent) RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) ObjectUpdatedEvent(org.kie.api.event.rule.ObjectUpdatedEvent) Cheese(org.drools.core.test.model.Cheese) ObjectInsertedEvent(org.kie.api.event.rule.ObjectInsertedEvent) Test(org.junit.Test)

Example 4 with ObjectUpdatedEvent

use of org.kie.api.event.rule.ObjectUpdatedEvent in project drools by kiegroup.

the class RuleRuntimeEventSupportTest method testAddRuleRuntimeEventListener.

@Test
public void testAddRuleRuntimeEventListener() {
    KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    KieSession ksession = kbase.newKieSession();
    final List wmList = new ArrayList();
    final RuleRuntimeEventListener eventListener = new RuleRuntimeEventListener() {

        public void objectInserted(ObjectInsertedEvent event) {
            wmList.add(event);
        }

        public void objectUpdated(ObjectUpdatedEvent event) {
            wmList.add(event);
        }

        public void objectDeleted(ObjectDeletedEvent event) {
            wmList.add(event);
        }
    };
    ksession.addEventListener(eventListener);
    final Cheese stilton = new Cheese("stilton", 15);
    final Cheese cheddar = new Cheese("cheddar", 17);
    final FactHandle stiltonHandle = ksession.insert(stilton);
    final ObjectInsertedEvent oae = (ObjectInsertedEvent) wmList.get(0);
    assertSame(stiltonHandle, oae.getFactHandle());
    ksession.update(stiltonHandle, stilton);
    final ObjectUpdatedEvent ome = (ObjectUpdatedEvent) wmList.get(1);
    assertSame(stiltonHandle, ome.getFactHandle());
    ksession.retract(stiltonHandle);
    final ObjectDeletedEvent ore = (ObjectDeletedEvent) wmList.get(2);
    assertSame(stiltonHandle, ore.getFactHandle());
    ksession.insert(cheddar);
}
Also used : ObjectDeletedEvent(org.kie.api.event.rule.ObjectDeletedEvent) RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) ObjectUpdatedEvent(org.kie.api.event.rule.ObjectUpdatedEvent) Cheese(org.drools.core.test.model.Cheese) ObjectInsertedEvent(org.kie.api.event.rule.ObjectInsertedEvent) Test(org.junit.Test)

Example 5 with ObjectUpdatedEvent

use of org.kie.api.event.rule.ObjectUpdatedEvent in project drools by kiegroup.

the class OOPathTest method testListIteratorRemove.

@Test
public void testListIteratorRemove() {
    final String drl = "import org.drools.mvel.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";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession ksession = kbase.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.mvel.compiler.oopath.model.TMFile) ObjectDeletedEvent(org.kie.api.event.rule.ObjectDeletedEvent) KieBase(org.kie.api.KieBase) TMDirectory(org.drools.mvel.compiler.oopath.model.TMDirectory) 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

Test (org.junit.Test)16 ObjectUpdatedEvent (org.kie.api.event.rule.ObjectUpdatedEvent)16 ObjectDeletedEvent (org.kie.api.event.rule.ObjectDeletedEvent)15 ObjectInsertedEvent (org.kie.api.event.rule.ObjectInsertedEvent)15 KieSession (org.kie.api.runtime.KieSession)14 KieBase (org.kie.api.KieBase)12 RuleRuntimeEventListener (org.kie.api.event.rule.RuleRuntimeEventListener)10 ArrayList (java.util.ArrayList)9 List (java.util.List)8 FactHandle (org.kie.api.runtime.rule.FactHandle)7 Cheese (org.drools.core.test.model.Cheese)6 DefaultRuleRuntimeEventListener (org.kie.api.event.rule.DefaultRuleRuntimeEventListener)6 TMDirectory (org.drools.compiler.oopath.model.TMDirectory)3 TMFile (org.drools.compiler.oopath.model.TMFile)3 TMDirectory (org.drools.mvel.compiler.oopath.model.TMDirectory)3 TMFile (org.drools.mvel.compiler.oopath.model.TMFile)3 KieHelper (org.kie.internal.utils.KieHelper)3 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)2 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)2 AgendaGroupPoppedEvent (org.kie.api.event.rule.AgendaGroupPoppedEvent)2