Search in sources :

Example 11 with ObjectUpdatedEvent

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

the class OOPathTest method testListIteratorMisc.

@Test
public void testListIteratorMisc() {
    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");
    ksession.insert(x);
    ksession.insert(y);
    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"));
    final TMFile file0 = new TMFile("File0", 999);
    final TMFile file1 = new TMFile("File1", 999);
    final TMFile file2 = new TMFile("File2", 999);
    final ListIterator<TMFile> xIterator = x.getFiles().listIterator();
    final ListIterator<TMFile> yIterator = y.getFiles().listIterator();
    xIterator.add(file0);
    yIterator.add(file2);
    ksession.fireAllRules();
    assertTrue(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"));
    assertTrue(factsCollection(ksession).contains("Y.File2"));
    xIterator.add(file1);
    yIterator.previous();
    yIterator.add(file1);
    ksession.fireAllRules();
    assertTrue(factsCollection(ksession).contains("X.File0"));
    assertTrue(factsCollection(ksession).contains("X.File1"));
    assertFalse(factsCollection(ksession).contains("X.File2"));
    assertFalse(factsCollection(ksession).contains("Y.File0"));
    assertTrue(factsCollection(ksession).contains("Y.File1"));
    assertTrue(factsCollection(ksession).contains("Y.File2"));
    xIterator.add(file2);
    yIterator.previous();
    yIterator.add(file0);
    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"));
    System.out.println(x.getFiles());
    System.out.println(y.getFiles());
    xIterator.previous();
    xIterator.set(new TMFile("File2R", 999));
    yIterator.previous();
    yIterator.set(new TMFile("File0R", 999));
    ksession.fireAllRules();
    assertTrue(factsCollection(ksession).contains("X.File0"));
    assertTrue(factsCollection(ksession).contains("X.File1"));
    assertFalse(factsCollection(ksession).contains("X.File2"));
    assertFalse(factsCollection(ksession).contains("Y.File0"));
    assertTrue(factsCollection(ksession).contains("Y.File1"));
    assertTrue(factsCollection(ksession).contains("Y.File2"));
    assertTrue(factsCollection(ksession).contains("X.File2R"));
    assertTrue(factsCollection(ksession).contains("Y.File0R"));
}
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 12 with ObjectUpdatedEvent

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

the class SequentialTest method testEvents.

@Test
public void testEvents() throws Exception {
    String str = "";
    str += "package org.drools.compiler.test\n";
    str += "import org.drools.compiler.Message\n";
    str += "rule \"Hello World\"\n";
    str += "when\n";
    str += "    Message( )\n";
    str += "then\n";
    str += "    System.out.println( drools.getKieRuntime() );\n";
    str += "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
    StatelessKieSession ksession = createStatelessKnowledgeSession(kbase);
    final List list = new ArrayList();
    ksession.addEventListener(new AgendaEventListener() {

        public void matchCancelled(MatchCancelledEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }

        public void matchCreated(MatchCreatedEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }

        public void afterMatchFired(AfterMatchFiredEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }

        public void agendaGroupPopped(AgendaGroupPoppedEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }

        public void agendaGroupPushed(AgendaGroupPushedEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }

        public void beforeMatchFired(BeforeMatchFiredEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }

        public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }

        public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }

        public void beforeRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }

        public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }
    });
    ksession.addEventListener(new RuleRuntimeEventListener() {

        public void objectInserted(ObjectInsertedEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }

        public void objectDeleted(ObjectDeletedEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }

        public void objectUpdated(ObjectUpdatedEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }
    });
    ksession.execute(new Message("help"));
    assertEquals(4, list.size());
}
Also used : Message(org.drools.compiler.Message) ArrayList(java.util.ArrayList) ObjectUpdatedEvent(org.kie.api.event.rule.ObjectUpdatedEvent) RuleFlowGroupDeactivatedEvent(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent) ObjectInsertedEvent(org.kie.api.event.rule.ObjectInsertedEvent) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) AgendaGroupPoppedEvent(org.kie.api.event.rule.AgendaGroupPoppedEvent) ObjectDeletedEvent(org.kie.api.event.rule.ObjectDeletedEvent) RuleFlowGroupActivatedEvent(org.kie.api.event.rule.RuleFlowGroupActivatedEvent) RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) KieBase(org.kie.api.KieBase) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) BeforeMatchFiredEvent(org.kie.api.event.rule.BeforeMatchFiredEvent) ArrayList(java.util.ArrayList) List(java.util.List) AgendaGroupPushedEvent(org.kie.api.event.rule.AgendaGroupPushedEvent) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) DynamicRulesTest(org.drools.compiler.integrationtests.DynamicRulesTest) Test(org.junit.Test)

Example 13 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 14 with ObjectUpdatedEvent

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

the class SequentialTest method testEvents.

@Test
public void testEvents() throws Exception {
    String str = "";
    str += "package org.drools.mvel.compiler.test\n";
    str += "import org.drools.mvel.compiler.Message\n";
    str += "rule \"Hello World\"\n";
    str += "when\n";
    str += "    Message( )\n";
    str += "then\n";
    str += "    System.out.println( drools.getKieRuntime() );\n";
    str += "end\n";
    final KieModule kieModule = KieUtil.getKieModuleFromDrls("test", kieBaseTestConfiguration, str);
    final KieBase kbase = KieBaseUtil.newKieBaseFromKieModuleWithAdditionalOptions(kieModule, kieBaseTestConfiguration, SequentialOption.YES);
    StatelessKieSession ksession = kbase.newStatelessKieSession();
    final List list = new ArrayList();
    ksession.addEventListener(new AgendaEventListener() {

        public void matchCancelled(MatchCancelledEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }

        public void matchCreated(MatchCreatedEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }

        public void afterMatchFired(AfterMatchFiredEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }

        public void agendaGroupPopped(AgendaGroupPoppedEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }

        public void agendaGroupPushed(AgendaGroupPushedEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }

        public void beforeMatchFired(BeforeMatchFiredEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }

        public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }

        public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }

        public void beforeRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }

        public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }
    });
    ksession.addEventListener(new RuleRuntimeEventListener() {

        public void objectInserted(ObjectInsertedEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }

        public void objectDeleted(ObjectDeletedEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }

        public void objectUpdated(ObjectUpdatedEvent event) {
            assertNotNull(event.getKieRuntime());
            list.add(event);
        }
    });
    ksession.execute(new Message("help"));
    assertEquals(4, list.size());
}
Also used : Message(org.drools.mvel.compiler.Message) ArrayList(java.util.ArrayList) ObjectUpdatedEvent(org.kie.api.event.rule.ObjectUpdatedEvent) RuleFlowGroupDeactivatedEvent(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent) ObjectInsertedEvent(org.kie.api.event.rule.ObjectInsertedEvent) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) AgendaGroupPoppedEvent(org.kie.api.event.rule.AgendaGroupPoppedEvent) ObjectDeletedEvent(org.kie.api.event.rule.ObjectDeletedEvent) RuleFlowGroupActivatedEvent(org.kie.api.event.rule.RuleFlowGroupActivatedEvent) RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) KieBase(org.kie.api.KieBase) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) BeforeMatchFiredEvent(org.kie.api.event.rule.BeforeMatchFiredEvent) ArrayList(java.util.ArrayList) List(java.util.List) AgendaGroupPushedEvent(org.kie.api.event.rule.AgendaGroupPushedEvent) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) KieModule(org.kie.api.builder.KieModule) DynamicRulesTest(org.drools.mvel.integrationtests.DynamicRulesTest) Test(org.junit.Test)

Example 15 with ObjectUpdatedEvent

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

the class BindTest method testFactBindings.

@Test
public void testFactBindings() {
    final String drl = "package org.drools.compiler.integrationtests.drl;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Person.class.getCanonicalName() + ";\n" + " \n" + "rule \"simple rule\"\n" + "    no-loop true\n" + "    when\n" + "        $person : Person( name == \"big cheese\", $cheese : cheese )\n" + "    then\n" + "        update( $cheese );\n" + "        update( $person );\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("bind-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final RuleRuntimeEventListener wmel = mock(RuleRuntimeEventListener.class);
        ksession.addEventListener(wmel);
        final Person bigCheese = new Person("big cheese");
        final Cheese cheddar = new Cheese("cheddar", 15);
        bigCheese.setCheese(cheddar);
        final FactHandle bigCheeseHandle = ksession.insert(bigCheese);
        final FactHandle cheddarHandle = ksession.insert(cheddar);
        ksession.fireAllRules();
        final ArgumentCaptor<ObjectUpdatedEvent> arg = ArgumentCaptor.forClass(org.kie.api.event.rule.ObjectUpdatedEvent.class);
        verify(wmel, times(2)).objectUpdated(arg.capture());
        org.kie.api.event.rule.ObjectUpdatedEvent event = arg.getAllValues().get(0);
        assertSame(cheddarHandle, event.getFactHandle());
        assertSame(cheddar, event.getOldObject());
        assertSame(cheddar, event.getObject());
        event = arg.getAllValues().get(1);
        assertSame(bigCheeseHandle, event.getFactHandle());
        assertSame(bigCheese, event.getOldObject());
        assertSame(bigCheese, event.getObject());
    } finally {
        ksession.dispose();
    }
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) Cheese(org.drools.testcoverage.common.model.Cheese) ObjectUpdatedEvent(org.kie.api.event.rule.ObjectUpdatedEvent) ObjectUpdatedEvent(org.kie.api.event.rule.ObjectUpdatedEvent) RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.testcoverage.common.model.Person) 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