Search in sources :

Example 76 with AgendaEventListener

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

the class StrictAnnotationTest method testEagerEvaluation.

@Test
public void testEagerEvaluation() {
    String str = "package org.simple \n" + "@Propagation(EAGER) rule xxx \n" + "when \n" + "  $s : String()\n" + "then \n" + "end  \n" + "@Propagation(EAGER) rule yyy \n" + "when \n" + "  $s : String()\n" + "then \n" + "end  \n";
    KieSessionConfiguration conf = RuleBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ForceEagerActivationOption.YES);
    Map<String, String> kieModuleConfigurationProperties = new HashMap<>();
    kieModuleConfigurationProperties.put(LanguageLevelOption.PROPERTY_NAME, LanguageLevelOption.DRL6_STRICT.toString());
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, kieModuleConfigurationProperties, str);
    KieSession ksession = kbase.newKieSession(conf, null);
    try {
        final List list = new ArrayList();
        AgendaEventListener agendaEventListener = new DefaultAgendaEventListener() {

            public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) {
                list.add("activated");
            }
        };
        ksession.addEventListener(agendaEventListener);
        ksession.insert("test");
        assertEquals(2, list.size());
    } finally {
        ksession.dispose();
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) KieBase(org.kie.api.KieBase) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 77 with AgendaEventListener

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

the class ActivityTest method testRuleTaskWithFacts.

@Test
@RequirePersistence(false)
public void testRuleTaskWithFacts() throws Exception {
    KieBase kbase = createKnowledgeBaseWithoutDumper("BPMN2-RuleTaskWithFact.bpmn2", "BPMN2-RuleTask3.drl");
    ksession = createKnowledgeSession(kbase);
    ksession.addEventListener(new AgendaEventListener() {

        public void matchCreated(MatchCreatedEvent event) {
        }

        public void matchCancelled(MatchCancelledEvent event) {
        }

        public void beforeRuleFlowGroupDeactivated(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent event) {
        }

        public void beforeRuleFlowGroupActivated(org.kie.api.event.rule.RuleFlowGroupActivatedEvent event) {
        }

        public void beforeMatchFired(BeforeMatchFiredEvent event) {
        }

        public void agendaGroupPushed(org.kie.api.event.rule.AgendaGroupPushedEvent event) {
        }

        public void agendaGroupPopped(org.kie.api.event.rule.AgendaGroupPoppedEvent event) {
        }

        public void afterRuleFlowGroupDeactivated(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent event) {
        }

        public void afterRuleFlowGroupActivated(org.kie.api.event.rule.RuleFlowGroupActivatedEvent event) {
            ksession.fireAllRules();
        }

        public void afterMatchFired(AfterMatchFiredEvent event) {
        }
    });
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("x", "SomeString");
    ProcessInstance processInstance = ksession.startProcess("RuleTask", params);
    assertProcessInstanceFinished(processInstance, ksession);
    params = new HashMap<String, Object>();
    try {
        processInstance = ksession.startProcess("RuleTask", params);
        fail("Should fail");
    } catch (Exception e) {
        e.printStackTrace();
    }
    params = new HashMap<String, Object>();
    params.put("x", "SomeString");
    processInstance = ksession.startProcess("RuleTask", params);
    assertProcessInstanceFinished(processInstance, ksession);
}
Also used : HashMap(java.util.HashMap) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) KieBase(org.kie.api.KieBase) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) DebugAgendaEventListener(org.kie.api.event.rule.DebugAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) BeforeMatchFiredEvent(org.kie.api.event.rule.BeforeMatchFiredEvent) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) Test(org.junit.Test) RequirePersistence(org.jbpm.bpmn2.test.RequirePersistence)

Example 78 with AgendaEventListener

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

the class AbstractRuntimeManager method registerItems.

protected void registerItems(RuntimeEngine runtime) {
    RegisterableItemsFactory factory = environment.getRegisterableItemsFactory();
    // process handlers
    Map<String, WorkItemHandler> handlers = factory.getWorkItemHandlers(runtime);
    for (Entry<String, WorkItemHandler> entry : handlers.entrySet()) {
        runtime.getKieSession().getWorkItemManager().registerWorkItemHandler(entry.getKey(), entry.getValue());
    }
    // register globals
    Map<String, Object> globals = factory.getGlobals(runtime);
    for (Entry<String, Object> entry : globals.entrySet()) {
        runtime.getKieSession().setGlobal(entry.getKey(), entry.getValue());
    }
    // process listeners
    List<ProcessEventListener> processListeners = factory.getProcessEventListeners(runtime);
    for (ProcessEventListener listener : processListeners) {
        runtime.getKieSession().addEventListener(listener);
    }
    // agenda listeners
    List<AgendaEventListener> agendaListeners = factory.getAgendaEventListeners(runtime);
    for (AgendaEventListener listener : agendaListeners) {
        runtime.getKieSession().addEventListener(listener);
    }
    // working memory listeners
    List<RuleRuntimeEventListener> wmListeners = factory.getRuleRuntimeEventListeners(runtime);
    for (RuleRuntimeEventListener listener : wmListeners) {
        runtime.getKieSession().addEventListener(listener);
    }
}
Also used : ProcessEventListener(org.kie.api.event.process.ProcessEventListener) InternalRegisterableItemsFactory(org.kie.internal.runtime.manager.InternalRegisterableItemsFactory) RegisterableItemsFactory(org.kie.api.runtime.manager.RegisterableItemsFactory) WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener)

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