Search in sources :

Example 61 with AgendaEventListener

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

the class ActivateAndDeleteOnListenerTest method testEagerEvaluationWithSubSubPath.

@Test
public void testEagerEvaluationWithSubSubPath() {
    final String drl = "package org.simple \n" + "rule xxx \n" + "when \n" + "  $s : String()\n" + "  exists( Boolean() and not(not(Integer()) and not(Double())) )\n" + "then \n" + "end  \n";
    final KieSession ksession = getSessionWithEagerActivation(drl);
    try {
        final List<String> list = new ArrayList<>();
        final AgendaEventListener agendaEventListener = new org.kie.api.event.rule.DefaultAgendaEventListener() {

            public void matchCreated(final org.kie.api.event.rule.MatchCreatedEvent event) {
                list.add("activated");
            }
        };
        ksession.addEventListener(agendaEventListener);
        ksession.insert(Boolean.TRUE);
        assertEquals(0, list.size());
        ksession.insert("test");
        assertEquals(0, list.size());
        ksession.insert(1);
        assertEquals(1, list.size());
    } finally {
        ksession.dispose();
    }
}
Also used : ArrayList(java.util.ArrayList) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) KieSession(org.kie.api.runtime.KieSession) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) Test(org.junit.Test)

Example 62 with AgendaEventListener

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

the class ActivateAndDeleteOnListenerTest method testOrPropagatesThroughSubnetwork.

@Test
public void testOrPropagatesThroughSubnetwork() {
    final String drl = "package org.simple \n" + "global java.util.List list; \n" + "rule yyy \n" + "when \n" + "  Integer(this==1)\n" + "  String()\n" + "  exists( ( Integer(this == 3) and eval(list.add(\"e1\"))) or (Long(this == 4) and eval(list.add(\"e2\"))) )\n" + "then \n" + "end  \n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("activate-delete-test", kieBaseTestConfiguration, drl);
    final KieSessionConfiguration conf = RuleBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(new ForceEagerActivationOption.FILTERED(rule -> rule.getName().equals("yyy")));
    final List<String> list = new ArrayList<>();
    final AgendaEventListener agendaEventListener = new org.kie.api.event.rule.DefaultAgendaEventListener() {

        public void matchCreated(final org.kie.api.event.rule.MatchCreatedEvent event) {
            list.add("add:" + event.getMatch().getRule().getName());
        }

        public void matchCancelled(final org.kie.api.event.rule.MatchCancelledEvent event) {
            list.add("rem:" + event.getMatch().getRule().getName());
        }
    };
    KieSession ksession = null;
    // scenario 1 - Add Integer, then Long (with no change), then delete Integer and check not holds. Then delete Long
    ksession = kbase.newKieSession(conf, null);
    try {
        ksession.addEventListener(agendaEventListener);
        list.clear();
        ksession.setGlobal("list", list);
        ksession.insert("test");
        ksession.insert(1);
        assertEquals(0, list.size());
        FactHandle fhInt3 = ksession.insert(3);
        assertEquals("[e1, add:yyy]", list.toString());
        // No change as the int 1 blocks a token propagating from the not node needed for the long join
        list.clear();
        FactHandle fhLong4 = ksession.insert(Long.valueOf(4));
        assertEquals(0, list.size());
        ksession.delete(fhInt3);
        assertEquals("[e2]", list.toString());
        list.clear();
        ksession.delete(fhLong4);
        assertEquals("[rem:yyy]", list.toString());
    } finally {
        ksession.dispose();
    }
    // Scenario 2 - Add Long, then Integer (with no change), then delete Long and check not holds. Then delete Integer
    ksession = kbase.newKieSession(conf, null);
    try {
        ksession.addEventListener(agendaEventListener);
        list.clear();
        ksession.setGlobal("list", list);
        ksession.insert("test");
        ksession.insert(1);
        assertEquals(0, list.size());
        FactHandle fhLong4 = ksession.insert(Long.valueOf(4));
        assertEquals("[e2, add:yyy]", list.toString());
        // Unlike scenario  1, e1 eval is not blocked so it will still eval, but the outer not still holds so no over all change.
        list.clear();
        FactHandle fhInt3 = ksession.insert(3);
        assertEquals("[e1]", list.toString());
        list.clear();
        ksession.delete(fhLong4);
        assertEquals(0, list.size());
        list.clear();
        ksession.delete(fhInt3);
        assertEquals("[rem:yyy]", list.toString());
    } finally {
        ksession.dispose();
    }
}
Also used : Alarm(org.drools.testcoverage.common.model.Alarm) RunWith(org.junit.runner.RunWith) KieBaseTestConfiguration(org.drools.testcoverage.common.util.KieBaseTestConfiguration) ArrayList(java.util.ArrayList) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) Arrays.asList(java.util.Arrays.asList) RuleBaseFactory(org.drools.core.impl.RuleBaseFactory) TestParametersUtil(org.drools.testcoverage.common.util.TestParametersUtil) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Parameterized(org.junit.runners.Parameterized) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) Collection(java.util.Collection) ForceEagerActivationOption(org.kie.internal.runtime.conf.ForceEagerActivationOption) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) FactHandle(org.kie.api.runtime.rule.FactHandle) List(java.util.List) Person(org.drools.testcoverage.common.model.Person) Sensor(org.drools.testcoverage.common.model.Sensor) KieBaseUtil(org.drools.testcoverage.common.util.KieBaseUtil) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) Assert.assertEquals(org.junit.Assert.assertEquals) FactHandle(org.kie.api.runtime.rule.FactHandle) ForceEagerActivationOption(org.kie.internal.runtime.conf.ForceEagerActivationOption) ArrayList(java.util.ArrayList) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) KieBase(org.kie.api.KieBase) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 63 with AgendaEventListener

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

the class ActivateAndDeleteOnListenerTest method testOneLazyAndOneImmediateSubPathAfterLia.

@Test
public void testOneLazyAndOneImmediateSubPathAfterLia() {
    final String drl = "package org.simple \n" + "global java.util.List list; \n" + "rule xxx \n" + "when \n" + "  Integer(this == 0)\n" + "  $s : String()\n" + "  exists( ( Integer(this == 3) and eval(list.add(\"e1\"))) or (Long(this == 1) and eval(list.add(\"e2\"))) )\n" + "then \n" + "end  \n" + "rule yyy \n" + "when \n" + "  Integer(this == 0)\n" + "  $s : String()\n" + "  exists( ( Integer(this == 3) and eval(list.add(\"e1\"))) or (Long(this == 1) and eval(list.add(\"e2\"))) )\n" + "then \n" + "end  \n" + "rule zzz \n" + "when \n" + "  Integer(this == 0)\n" + "  $s : String()\n" + "  eval(1==1)\n" + "then \n" + "end  \n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("activate-delete-test", kieBaseTestConfiguration, drl);
    final KieSessionConfiguration conf = RuleBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(new ForceEagerActivationOption.FILTERED(rule -> rule.getName().equals("yyy")));
    final List<String> list = new ArrayList<>();
    final AgendaEventListener agendaEventListener = new org.kie.api.event.rule.DefaultAgendaEventListener() {

        public void matchCreated(final org.kie.api.event.rule.MatchCreatedEvent event) {
            list.add(event.getMatch().getRule().getName());
        }
    };
    KieSession ksession = null;
    // scenario 1 - Only insert the Integer side of 'or'
    ksession = kbase.newKieSession(conf, null);
    try {
        list.clear();
        ksession.setGlobal("list", list);
        ksession.addEventListener(agendaEventListener);
        ksession.insert("test");
        assertEquals(0, list.size());
        ksession.insert(0);
        ksession.insert(3);
        assertEquals("[e1, yyy]", list.toString());
        list.clear();
        ksession.fireAllRules();
        assertEquals("[xxx, zzz]", list.toString());
    } finally {
        ksession.dispose();
    }
    // Scenario 2 - Only insert the Long side of 'or'
    ksession = kbase.newKieSession(conf, null);
    try {
        list.clear();
        ksession.setGlobal("list", list);
        ksession.addEventListener(agendaEventListener);
        ksession.insert("test");
        assertEquals(0, list.size());
        ksession.insert(0);
        ksession.insert(Long.valueOf(1));
        assertEquals("[e2, yyy]", list.toString());
        list.clear();
        ksession.fireAllRules();
        assertEquals("[xxx, zzz]", list.toString());
    } finally {
        ksession.dispose();
    }
}
Also used : Alarm(org.drools.testcoverage.common.model.Alarm) RunWith(org.junit.runner.RunWith) KieBaseTestConfiguration(org.drools.testcoverage.common.util.KieBaseTestConfiguration) ArrayList(java.util.ArrayList) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) Arrays.asList(java.util.Arrays.asList) RuleBaseFactory(org.drools.core.impl.RuleBaseFactory) TestParametersUtil(org.drools.testcoverage.common.util.TestParametersUtil) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Parameterized(org.junit.runners.Parameterized) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) Collection(java.util.Collection) ForceEagerActivationOption(org.kie.internal.runtime.conf.ForceEagerActivationOption) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) FactHandle(org.kie.api.runtime.rule.FactHandle) List(java.util.List) Person(org.drools.testcoverage.common.model.Person) Sensor(org.drools.testcoverage.common.model.Sensor) KieBaseUtil(org.drools.testcoverage.common.util.KieBaseUtil) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) Assert.assertEquals(org.junit.Assert.assertEquals) ForceEagerActivationOption(org.kie.internal.runtime.conf.ForceEagerActivationOption) ArrayList(java.util.ArrayList) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) KieBase(org.kie.api.KieBase) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 64 with AgendaEventListener

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

the class AccumulateTest method execTestAccumulateMultipleFunctions.

private void execTestAccumulateMultipleFunctions(final String fileName) {
    final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("accumulate-test", kieBaseTestConfiguration, fileName);
    final KieSession ksession = kbase.newKieSession();
    try {
        final AgendaEventListener ael = mock(AgendaEventListener.class);
        ksession.addEventListener(ael);
        final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 10), new Cheese("stilton", 3), new Cheese("stilton", 5), new Cheese("brie", 15), new Cheese("brie", 17), new Cheese("provolone", 8) };
        final Person bob = new Person("Bob", "stilton");
        final FactHandle[] cheeseHandles = new FactHandle[cheese.length];
        for (int i = 0; i < cheese.length; i++) {
            cheeseHandles[i] = ksession.insert(cheese[i]);
        }
        final FactHandle bobHandle = ksession.insert(bob);
        // ---------------- 1st scenario
        ksession.fireAllRules();
        final ArgumentCaptor<AfterMatchFiredEvent> cap = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
        Mockito.verify(ael).afterMatchFired(cap.capture());
        Match activation = cap.getValue().getMatch();
        assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(18));
        assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(3));
        assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(6));
        Mockito.reset(ael);
        // ---------------- 2nd scenario
        final int index = 1;
        cheese[index].setPrice(9);
        ksession.update(cheeseHandles[index], cheese[index]);
        ksession.fireAllRules();
        Mockito.verify(ael).afterMatchFired(cap.capture());
        activation = cap.getValue().getMatch();
        assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(24));
        assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(5));
        assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(8));
        Mockito.reset(ael);
        // ---------------- 3rd scenario
        bob.setLikes("brie");
        ksession.update(bobHandle, bob);
        ksession.fireAllRules();
        Mockito.verify(ael).afterMatchFired(cap.capture());
        activation = cap.getValue().getMatch();
        assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(32));
        assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(15));
        assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(16));
        Mockito.reset(ael);
        // ---------------- 4th scenario
        ksession.delete(cheeseHandles[3]);
        ksession.fireAllRules();
        Mockito.verify(ael).afterMatchFired(cap.capture());
        activation = cap.getValue().getMatch();
        assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(17));
        assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(17));
        assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(17));
    } finally {
        ksession.dispose();
    }
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.testcoverage.common.model.Cheese) Person(org.drools.testcoverage.common.model.Person) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Match(org.kie.api.runtime.rule.Match)

Example 65 with AgendaEventListener

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

the class AccumulateTest method testAccumulateMultipleFunctionsConstraint.

@Test(timeout = 10000)
public void testAccumulateMultipleFunctionsConstraint() {
    final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("accumulate-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_AccumulateMultipleFunctionsConstraint.drl");
    final KieSession ksession = kbase.newKieSession();
    try {
        final AgendaEventListener ael = mock(AgendaEventListener.class);
        ksession.addEventListener(ael);
        final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 10), new Cheese("stilton", 3), new Cheese("stilton", 5), new Cheese("brie", 3), new Cheese("brie", 17), new Cheese("provolone", 8) };
        final Person bob = new Person("Bob", "stilton");
        final FactHandle[] cheeseHandles = new FactHandle[cheese.length];
        for (int i = 0; i < cheese.length; i++) {
            cheeseHandles[i] = ksession.insert(cheese[i]);
        }
        final FactHandle bobHandle = ksession.insert(bob);
        // ---------------- 1st scenario
        ksession.fireAllRules();
        final ArgumentCaptor<AfterMatchFiredEvent> cap = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
        Mockito.verify(ael).afterMatchFired(cap.capture());
        Match activation = cap.getValue().getMatch();
        assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(18));
        assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(3));
        assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(6));
        Mockito.reset(ael);
        // ---------------- 2nd scenario
        final int index = 1;
        cheese[index].setPrice(9);
        ksession.update(cheeseHandles[index], cheese[index]);
        ksession.fireAllRules();
        Mockito.verify(ael, Mockito.never()).afterMatchFired(Mockito.any(AfterMatchFiredEvent.class));
        Mockito.reset(ael);
        // ---------------- 3rd scenario
        bob.setLikes("brie");
        ksession.update(bobHandle, bob);
        ksession.fireAllRules();
        Mockito.verify(ael).afterMatchFired(cap.capture());
        activation = cap.getValue().getMatch();
        assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(20));
        assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(3));
        assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(10));
    } finally {
        ksession.dispose();
    }
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.testcoverage.common.model.Cheese) Person(org.drools.testcoverage.common.model.Person) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Match(org.kie.api.runtime.rule.Match) Test(org.junit.Test)

Aggregations

AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)78 Test (org.junit.Test)72 KieSession (org.kie.api.runtime.KieSession)59 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)49 KieBase (org.kie.api.KieBase)38 ArrayList (java.util.ArrayList)33 MatchCreatedEvent (org.kie.api.event.rule.MatchCreatedEvent)25 DefaultAgendaEventListener (org.kie.api.event.rule.DefaultAgendaEventListener)22 List (java.util.List)21 MatchCancelledEvent (org.kie.api.event.rule.MatchCancelledEvent)18 FactHandle (org.kie.api.runtime.rule.FactHandle)17 DebugAgendaEventListener (org.kie.api.event.rule.DebugAgendaEventListener)14 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)12 EntryPoint (org.kie.api.runtime.rule.EntryPoint)11 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)10 DefaultAgendaEventListener (org.drools.core.event.DefaultAgendaEventListener)9 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)7 StockTick (org.drools.testcoverage.common.model.StockTick)7 BeforeMatchFiredEvent (org.kie.api.event.rule.BeforeMatchFiredEvent)7 Arrays.asList (java.util.Arrays.asList)6