Search in sources :

Example 1 with MatchCancelledEvent

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

the class Misc2Test method testLockOnActive1.

@Test
public void testLockOnActive1() {
    // the modify changes the hashcode of TradeHeader
    // this forces the 'from' to think it's new. This results in an insert and a delete propagation from the 'from'
    // With Property Reactivity enabled by default this also required adding a @watch(*) annotation
    String drl = "" + "package org.drools.test; \n" + "import org.drools.compiler.integrationtests.Misc2Test.TradeBooking;\n" + "import org.drools.compiler.integrationtests.Misc2Test.TradeHeader;\n" + "rule \"Rule1\" \n" + "salience 1 \n" + "when\n" + "  $booking: TradeBooking() @watch(*) \n" + "  $trade: TradeHeader() from $booking.getTrade()\n" + "  not String()\n" + "then\n" + "  $trade.setAction(\"New\");\n" + "  modify($booking) {}\n" + "  insert (\"run\");\n" + "end;\n" + "\n" + "rule \"Rule2\"\n" + "lock-on-active true\n" + "when\n" + "  $booking: TradeBooking( ) @watch(*) \n" + "  $trade: Object( ) from $booking.getTrade()\n" + "then\n" + "end";
    KieBase kb = loadKnowledgeBaseFromString(drl);
    KieSession ks = kb.newKieSession();
    ReteDumper.dumpRete(kb);
    final List created = new ArrayList();
    final List cancelled = new ArrayList();
    final List fired = new ArrayList();
    ks.addEventListener(new DefaultAgendaEventListener() {

        public void matchCreated(MatchCreatedEvent event) {
            created.add(event.getMatch().getRule().getName());
        }

        public void matchCancelled(MatchCancelledEvent event) {
            cancelled.add(event.getMatch().getRule().getName());
        }

        public void afterMatchFired(AfterMatchFiredEvent event) {
            fired.add(event.getMatch().getRule().getName());
        }
    });
    ks.fireAllRules();
    TradeBooking tb = new TradeBookingImpl(new TradeHeaderImpl());
    ks.insert(tb);
    assertEquals(1, ks.fireAllRules());
    assertEquals(3, created.size());
    assertEquals(2, cancelled.size());
    assertEquals(1, fired.size());
    assertEquals("Rule2", created.get(0));
    assertEquals("Rule1", created.get(1));
    assertEquals("Rule2", created.get(2));
    assertEquals("Rule2", cancelled.get(0));
    assertEquals("Rule2", cancelled.get(1));
    assertEquals("Rule1", fired.get(0));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Example 2 with MatchCancelledEvent

use of org.kie.api.event.rule.MatchCancelledEvent in project jbpm by kiegroup.

the class ProcessFlowControlTest method testRuleFlowClear.

@Test
public void testRuleFlowClear() throws Exception {
    builder.addPackageFromDrl(new InputStreamReader(getClass().getResourceAsStream("test_ruleflowClear.drl")));
    builder.addRuleFlow(new InputStreamReader(getClass().getResourceAsStream("test_ruleflowClear.rfm")));
    KieSession workingMemory = createKieSession(true, builder.getPackages());
    final List<String> list = new ArrayList<String>();
    workingMemory.setGlobal("list", list);
    final List<Match> activations = new ArrayList<Match>();
    AgendaEventListener listener = new DefaultAgendaEventListener() {

        public void matchCancelled(MatchCancelledEvent event) {
            activations.add(event.getMatch());
        }
    };
    workingMemory.addEventListener(listener);
    InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda();
    // assertEquals( 0,
    // agenda.getRuleFlowGroup( "flowgroup-1" ).size() );
    // We need to call fireAllRules here to get the InitialFact into the system, to the eval(true)'s kick in
    workingMemory.fireAllRules();
    agenda.evaluateEagerList();
    // Now we have 4 in the RuleFlow, but not yet in the agenda
    assertEquals(4, agenda.sizeOfRuleFlowGroup("flowgroup-1"));
    // Check they aren't in the Agenda
    assertEquals(0, agenda.getAgendaGroup("MAIN").size());
    // Check we have 0 activation cancellation events
    assertEquals(0, activations.size());
    ((InternalAgenda) workingMemory.getAgenda()).clearAndCancelRuleFlowGroup("flowgroup-1");
    // Check the AgendaGroup and RuleFlowGroup  are now empty
    assertEquals(0, agenda.getAgendaGroup("MAIN").size());
    assertEquals(0, agenda.sizeOfRuleFlowGroup("flowgroup-1"));
    // Check we have four activation cancellation events
    assertEquals(4, activations.size());
}
Also used : InternalAgenda(org.drools.core.common.InternalAgenda) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) 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) Match(org.kie.api.runtime.rule.Match) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 3 with MatchCancelledEvent

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

the class ActivateAndDeleteOnListenerTest method testEagerEvaluationWith2Paths.

@Test
public void testEagerEvaluationWith2Paths() {
    final String drl = "package org.simple \n" + "rule xxx \n" + "when \n" + "  $s : String()\n" + "  $i : Integer()\n" + "then \n" + "end  \n" + "rule yyy \n" + "when \n" + "  $s : String()\n" + "  $i : Integer()\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() {

            @Override
            public void matchCreated(final org.kie.api.event.rule.MatchCreatedEvent event) {
                list.add("activated");
            }

            @Override
            public void matchCancelled(final MatchCancelledEvent event) {
                list.add("cancelled");
            }
        };
        ksession.addEventListener(agendaEventListener);
        ksession.insert("test");
        assertEquals(0, list.size());
        final FactHandle fh = ksession.insert(1);
        assertEquals(2, list.size());
        assertEquals("activated", list.get(0));
        assertEquals("activated", list.get(1));
        list.clear();
        ksession.delete(fh);
        assertEquals(2, list.size());
        assertEquals("cancelled", list.get(0));
        assertEquals("cancelled", list.get(1));
    } finally {
        ksession.dispose();
    }
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) 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 4 with MatchCancelledEvent

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

the class Misc2Test method testLockOnActive2.

@Test
public void testLockOnActive2() {
    // the modify changes the hashcode of TradeHeader
    // this forces the 'from' to think it's new. This results in an insert and a delete propagation from the 'from'
    // With Property Reactivity enabled by default this also required adding a @watch(*) annotation
    String drl = "" + "package org.drools.test; \n" + "import org.drools.mvel.integrationtests.Misc2Test.TradeBooking;\n" + "import org.drools.mvel.integrationtests.Misc2Test.TradeHeader;\n" + "rule \"Rule1\" \n" + "lock-on-active true\n" + "salience 1 \n" + "when\n" + "  $booking: TradeBooking()\n" + "  $trade: TradeHeader() @watch(*) from $booking.getTrade()\n" + "then\n" + "  $trade.setAction(\"New\");\n" + "  modify($booking) {}\n" + "end;\n" + "\n" + "rule \"Rule2\"\n" + "when\n" + "  $booking: TradeBooking( )\n" + "  $trade: Object( ) @watch(*) from $booking.getTrade()\n" + "then\n" + "end";
    KieBase kb = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession ks = kb.newKieSession();
    final List created = new ArrayList();
    final List cancelled = new ArrayList();
    final List fired = new ArrayList();
    ks.addEventListener(new DefaultAgendaEventListener() {

        public void matchCreated(MatchCreatedEvent event) {
            created.add(event.getMatch().getRule().getName());
        }

        public void matchCancelled(MatchCancelledEvent event) {
            cancelled.add(event.getMatch().getRule().getName());
        }

        public void afterMatchFired(AfterMatchFiredEvent event) {
            fired.add(event.getMatch().getRule().getName());
        }
    });
    ks.fireAllRules();
    TradeBooking tb = new TradeBookingImpl(new TradeHeaderImpl());
    ks.insert(tb);
    assertEquals(2, ks.fireAllRules());
    assertEquals(3, created.size());
    assertEquals(1, cancelled.size());
    assertEquals(2, fired.size());
    assertEquals("Rule1", created.get(0));
    assertEquals("Rule1", created.get(1));
    assertEquals("Rule2", created.get(2));
    assertEquals("Rule1", cancelled.get(0));
    assertEquals("Rule1", fired.get(0));
    assertEquals("Rule2", fired.get(1));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) FactWithList(org.drools.mvel.integrationtests.facts.FactWithList) List(java.util.List) FactWithString(org.drools.mvel.integrationtests.facts.FactWithString) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Example 5 with MatchCancelledEvent

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

the class Misc2Test method testLockOnActive1.

@Test
public void testLockOnActive1() {
    // the modify changes the hashcode of TradeHeader
    // this forces the 'from' to think it's new. This results in an insert and a delete propagation from the 'from'
    // With Property Reactivity enabled by default this also required adding a @watch(*) annotation
    String drl = "" + "package org.drools.test; \n" + "import org.drools.mvel.integrationtests.Misc2Test.TradeBooking;\n" + "import org.drools.mvel.integrationtests.Misc2Test.TradeHeader;\n" + "rule \"Rule1\" \n" + "salience 1 \n" + "when\n" + "  $booking: TradeBooking()\n" + "  $trade: TradeHeader() @watch(*) from $booking.getTrade()\n" + "  not String()\n" + "then\n" + "  $trade.setAction(\"New\");\n" + "  modify($booking) {}\n" + "  insert (\"run\");\n" + "end;\n" + "\n" + "rule \"Rule2\"\n" + "lock-on-active true\n" + "when\n" + "  $booking: TradeBooking( )\n" + "  $trade: Object( ) @watch(*) from $booking.getTrade()\n" + "then\n" + "end";
    KieBase kb = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession ks = kb.newKieSession();
    // ReteDumper.dumpRete(kb);
    final List created = new ArrayList();
    final List cancelled = new ArrayList();
    final List fired = new ArrayList();
    ks.addEventListener(new DefaultAgendaEventListener() {

        public void matchCreated(MatchCreatedEvent event) {
            created.add(event.getMatch().getRule().getName());
        }

        public void matchCancelled(MatchCancelledEvent event) {
            cancelled.add(event.getMatch().getRule().getName());
        }

        public void afterMatchFired(AfterMatchFiredEvent event) {
            fired.add(event.getMatch().getRule().getName());
        }
    });
    ks.fireAllRules();
    TradeBooking tb = new TradeBookingImpl(new TradeHeaderImpl());
    ks.insert(tb);
    assertEquals(1, ks.fireAllRules());
    assertEquals(3, created.size());
    assertEquals(2, cancelled.size());
    assertEquals(1, fired.size());
    assertEquals("Rule2", created.get(0));
    assertEquals("Rule1", created.get(1));
    assertEquals("Rule2", created.get(2));
    assertEquals("Rule2", cancelled.get(0));
    assertEquals("Rule2", cancelled.get(1));
    assertEquals("Rule1", fired.get(0));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) FactWithList(org.drools.mvel.integrationtests.facts.FactWithList) List(java.util.List) FactWithString(org.drools.mvel.integrationtests.facts.FactWithString) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)16 MatchCancelledEvent (org.kie.api.event.rule.MatchCancelledEvent)16 MatchCreatedEvent (org.kie.api.event.rule.MatchCreatedEvent)15 ArrayList (java.util.ArrayList)13 KieBase (org.kie.api.KieBase)13 KieSession (org.kie.api.runtime.KieSession)13 List (java.util.List)11 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)10 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)10 DefaultAgendaEventListener (org.kie.api.event.rule.DefaultAgendaEventListener)8 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)8 BeforeMatchFiredEvent (org.kie.api.event.rule.BeforeMatchFiredEvent)6 FactHandle (org.kie.api.runtime.rule.FactHandle)6 AgendaGroupPoppedEvent (org.kie.api.event.rule.AgendaGroupPoppedEvent)5 AgendaGroupPushedEvent (org.kie.api.event.rule.AgendaGroupPushedEvent)5 RuleFlowGroupActivatedEvent (org.kie.api.event.rule.RuleFlowGroupActivatedEvent)5 RuleFlowGroupDeactivatedEvent (org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent)5 Arrays.asList (java.util.Arrays.asList)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 InternalAgenda (org.drools.core.common.InternalAgenda)2