Search in sources :

Example 6 with AgendaGroupPoppedEvent

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

the class CepEspTest method testFireExpiredEventOnInactiveGroup.

@Test
public void testFireExpiredEventOnInactiveGroup() {
    // DROOLS-1523
    final String drl = "global java.util.List list;\n" + "declare String  @role(event) @expires( 6d ) end\n" + "declare Integer @role(event) @expires( 3d ) end\n" + "\n" + "rule \"RG_1\"\n" + "    agenda-group \"rf-grp1\"\n" + "    when\n" + "        $event: Integer()\n" + "        not String(this after [1ms, 48h] $event)\n" + "    then\n" + "      System.out.println(\"RG_1 fired\");\n" + "      retract($event);\n" + "      list.add(\"RG_1\");\n" + "end\n" + "\n" + "rule \"RG_2\"\n" + "    agenda-group \"rf-grp1\"\n" + "    when\n" + "        $event: String()\n" + "        not Integer(this after [1ms, 144h] $event)\n" + "    then\n" + "      System.out.println(\"RG_2 fired\");\n" + "      list.add(\"RG_2\");\n" + "end\n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    final KieSession kieSession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        kieSession.addEventListener(new DefaultAgendaEventListener() {

            public void agendaGroupPopped(final AgendaGroupPoppedEvent event) {
                if (event.getAgendaGroup().getName().equals("rf-grp0")) {
                    event.getKieRuntime().getAgenda().getAgendaGroup("rf-grp1").setFocus();
                }
            }
        });
        final List<String> list = new ArrayList<>();
        kieSession.setGlobal("list", list);
        final PseudoClockScheduler sessionClock = kieSession.getSessionClock();
        kieSession.insert("DummyEvent");
        // <- OtherDummyEvent
        kieSession.insert(1);
        kieSession.getAgenda().getAgendaGroup("rf-grp0").setFocus();
        // OK nothing happens
        kieSession.fireAllRules();
        assertEquals(2, kieSession.getFactCount());
        sessionClock.advanceTime(145, TimeUnit.HOURS);
        kieSession.getAgenda().getAgendaGroup("rf-grp0").setFocus();
        kieSession.fireAllRules();
        assertEquals("Expiration occured => no more fact in WM", 0, kieSession.getFactCount());
        assertEquals("RG_1 should fire once", 1, list.stream().filter(r -> r.equals("RG_1")).count());
        assertEquals("RG_2 should fire once", 1, list.stream().filter(r -> r.equals("RG_2")).count());
    } finally {
        kieSession.dispose();
    }
}
Also used : AgendaGroupPoppedEvent(org.kie.api.event.rule.AgendaGroupPoppedEvent) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) KieSession(org.kie.api.runtime.KieSession) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)6 Test (org.junit.Test)6 AgendaGroupPoppedEvent (org.kie.api.event.rule.AgendaGroupPoppedEvent)6 List (java.util.List)5 KieBase (org.kie.api.KieBase)5 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)5 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)5 AgendaGroupPushedEvent (org.kie.api.event.rule.AgendaGroupPushedEvent)5 BeforeMatchFiredEvent (org.kie.api.event.rule.BeforeMatchFiredEvent)5 MatchCancelledEvent (org.kie.api.event.rule.MatchCancelledEvent)5 MatchCreatedEvent (org.kie.api.event.rule.MatchCreatedEvent)5 RuleFlowGroupActivatedEvent (org.kie.api.event.rule.RuleFlowGroupActivatedEvent)5 RuleFlowGroupDeactivatedEvent (org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent)5 KieSession (org.kie.api.runtime.KieSession)4 FactHandle (org.kie.api.runtime.rule.FactHandle)3 ObjectDeletedEvent (org.kie.api.event.rule.ObjectDeletedEvent)2 ObjectInsertedEvent (org.kie.api.event.rule.ObjectInsertedEvent)2 ObjectUpdatedEvent (org.kie.api.event.rule.ObjectUpdatedEvent)2 RuleRuntimeEventListener (org.kie.api.event.rule.RuleRuntimeEventListener)2 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)2