Search in sources :

Example 1 with RuleRuntimeEventListener

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

the class ConsequenceTest method testMVELConsequenceWithoutSemiColon1.

@Test
public void testMVELConsequenceWithoutSemiColon1() throws Exception {
    String drl = "";
    drl += "package test\n";
    drl += "import org.drools.compiler.Person\n";
    drl += "import org.drools.compiler.Pet\n";
    drl += "rule test dialect 'mvel'\n";
    drl += "when\n";
    drl += "    $person:Person()\n";
    drl += "    $pet:Pet()\n";
    drl += "then\n";
    drl += "    delete($person) // some comment\n";
    drl += "    delete($pet) // another comment\n";
    drl += "end\n";
    final KieBase kbase = loadKnowledgeBaseFromString(drl);
    final KieSession ksession = createKnowledgeSession(kbase);
    // create working memory mock listener
    final RuleRuntimeEventListener wml = Mockito.mock(RuleRuntimeEventListener.class);
    ksession.addEventListener(wml);
    final FactHandle personFH = ksession.insert(new Person("Toni"));
    final FactHandle petFH = ksession.insert(new Pet("Toni"));
    final int fired = ksession.fireAllRules();
    assertEquals(1, fired);
    // capture the arguments and check that the retracts happened
    final ArgumentCaptor<ObjectDeletedEvent> retracts = ArgumentCaptor.forClass(ObjectDeletedEvent.class);
    verify(wml, times(2)).objectDeleted(retracts.capture());
    final List<ObjectDeletedEvent> values = retracts.getAllValues();
    assertThat(values.get(0).getFactHandle(), is(personFH));
    assertThat(values.get(1).getFactHandle(), is(petFH));
}
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) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Pet(org.drools.compiler.Pet) Test(org.junit.Test)

Example 2 with RuleRuntimeEventListener

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

the class RuleRuntimeEventTest method testEventModel.

@Test
public void testEventModel() throws Exception {
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_EventModel.drl"));
    final KieSession wm = createKnowledgeSession(kbase);
    final RuleRuntimeEventListener wmel = mock(RuleRuntimeEventListener.class);
    wm.addEventListener(wmel);
    final Cheese stilton = new Cheese("stilton", 15);
    final FactHandle stiltonHandle = wm.insert(stilton);
    final ArgumentCaptor<ObjectInsertedEvent> oic = ArgumentCaptor.forClass(org.kie.api.event.rule.ObjectInsertedEvent.class);
    verify(wmel).objectInserted(oic.capture());
    assertSame(stiltonHandle, oic.getValue().getFactHandle());
    wm.update(stiltonHandle, stilton);
    final ArgumentCaptor<org.kie.api.event.rule.ObjectUpdatedEvent> ouc = ArgumentCaptor.forClass(org.kie.api.event.rule.ObjectUpdatedEvent.class);
    verify(wmel).objectUpdated(ouc.capture());
    assertSame(stiltonHandle, ouc.getValue().getFactHandle());
    wm.delete(stiltonHandle);
    final ArgumentCaptor<ObjectDeletedEvent> orc = ArgumentCaptor.forClass(ObjectDeletedEvent.class);
    verify(wmel).objectDeleted(orc.capture());
    assertSame(stiltonHandle, orc.getValue().getFactHandle());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) Cheese(org.drools.compiler.Cheese) ObjectInsertedEvent(org.kie.api.event.rule.ObjectInsertedEvent) ObjectDeletedEvent(org.kie.api.event.rule.ObjectDeletedEvent) RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 3 with RuleRuntimeEventListener

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

the class RuleChainingTest method testRuleChainingWithLogicalInserts.

@Test
public void testRuleChainingWithLogicalInserts() throws Exception {
    final KieBase kbase = loadKnowledgeBase("test_RuleChaining.drl");
    final KieSession ksession = createKnowledgeSession(kbase);
    // create working memory mock listener
    final RuleRuntimeEventListener wml = Mockito.mock(RuleRuntimeEventListener.class);
    final org.kie.api.event.rule.AgendaEventListener ael = Mockito.mock(org.kie.api.event.rule.AgendaEventListener.class);
    ksession.addEventListener(wml);
    ksession.addEventListener(ael);
    final int fired = ksession.fireAllRules();
    assertEquals(3, fired);
    // capture the arguments and check that the rules fired in the proper sequence
    final ArgumentCaptor<AfterMatchFiredEvent> actvs = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
    verify(ael, times(3)).afterMatchFired(actvs.capture());
    final List<AfterMatchFiredEvent> values = actvs.getAllValues();
    assertThat(values.get(0).getMatch().getRule().getName(), is("init"));
    assertThat(values.get(1).getMatch().getRule().getName(), is("r1"));
    assertThat(values.get(2).getMatch().getRule().getName(), is("r2"));
    verify(ael, never()).matchCancelled(any(org.kie.api.event.rule.MatchCancelledEvent.class));
    verify(wml, times(2)).objectInserted(any(org.kie.api.event.rule.ObjectInsertedEvent.class));
    verify(wml, never()).objectDeleted(any(ObjectDeletedEvent.class));
}
Also used : AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) ObjectDeletedEvent(org.kie.api.event.rule.ObjectDeletedEvent) RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 4 with RuleRuntimeEventListener

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

the class TraitTest method testTraitdeleteOrder.

@Test(timeout = 10000)
public void testTraitdeleteOrder() {
    String drl = "" + "package org.drools.core.factmodel.traits.test;\n" + "\n" + "import org.drools.core.factmodel.traits.*;\n" + "import java.util.*;\n" + "\n" + "declare trait A end \n" + "declare trait B extends A end \n" + "declare trait C end \n" + "\n" + "rule \"don\"\n" + "when \n" + "  $e : Entity() \n" + "then\n" + "  don( $e, A.class ); \n" + "  don( $e, C.class ); \n" + "  don( $e, B.class ); \n" + "end\n" + "";
    KieSession ksession = loadKnowledgeBaseFromString(drl).newKieSession();
    TraitFactory.setMode(mode, ksession.getKieBase());
    FactHandle handle = ksession.insert(new Entity());
    ksession.fireAllRules();
    final ArrayList list = new ArrayList();
    ksession.addEventListener(new RuleRuntimeEventListener() {

        public void objectInserted(org.kie.api.event.rule.ObjectInsertedEvent objectInsertedEvent) {
        }

        public void objectUpdated(org.kie.api.event.rule.ObjectUpdatedEvent objectUpdatedEvent) {
        }

        public void objectDeleted(org.kie.api.event.rule.ObjectDeletedEvent objectRetractedEvent) {
            Object o = objectRetractedEvent.getOldObject();
            if (o instanceof TraitProxy) {
                String traitName = ((TraitProxy) o)._getTraitName();
                list.add(traitName.substring(traitName.lastIndexOf(".") + 1));
            }
        }
    });
    ksession.delete(handle);
    ksession.fireAllRules();
    System.out.println(list);
    assertEquals(Arrays.asList("B", "C", "A"), list);
}
Also used : Entity(org.drools.core.factmodel.traits.Entity) TraitProxy(org.drools.core.factmodel.traits.TraitProxy) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 5 with RuleRuntimeEventListener

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

Aggregations

Test (org.junit.Test)12 RuleRuntimeEventListener (org.kie.api.event.rule.RuleRuntimeEventListener)12 KieSession (org.kie.api.runtime.KieSession)11 KieBase (org.kie.api.KieBase)10 ObjectDeletedEvent (org.kie.api.event.rule.ObjectDeletedEvent)7 FactHandle (org.kie.api.runtime.rule.FactHandle)7 ArrayList (java.util.ArrayList)5 ObjectInsertedEvent (org.kie.api.event.rule.ObjectInsertedEvent)5 ObjectUpdatedEvent (org.kie.api.event.rule.ObjectUpdatedEvent)5 List (java.util.List)4 Person (org.drools.compiler.Person)3 Cheese (org.drools.core.test.model.Cheese)3 Cheese (org.drools.compiler.Cheese)2 StockTick (org.drools.compiler.StockTick)2 StockTickInterface (org.drools.compiler.StockTickInterface)2 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)2 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)2 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)2 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)2 MatchCreatedEvent (org.kie.api.event.rule.MatchCreatedEvent)2