Search in sources :

Example 11 with ProcessVariableChangedEvent

use of org.kie.api.event.process.ProcessVariableChangedEvent in project jbpm by kiegroup.

the class PersistentStatefulSessionTest method testProcessListener.

@Test
public void testProcessListener() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("WorkItemsProcess.rf"), ResourceType.DRF);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    final List<ProcessEvent> events = new ArrayList<ProcessEvent>();
    ProcessEventListener listener = new ProcessEventListener() {

        public void afterNodeLeft(ProcessNodeLeftEvent event) {
            logger.debug("After node left: {}", event.getNodeInstance().getNodeName());
            events.add(event);
        }

        public void afterNodeTriggered(ProcessNodeTriggeredEvent event) {
            logger.debug("After node triggered: {}", event.getNodeInstance().getNodeName());
            events.add(event);
        }

        public void afterProcessCompleted(ProcessCompletedEvent event) {
            logger.debug("After process completed");
            events.add(event);
        }

        public void afterProcessStarted(ProcessStartedEvent event) {
            logger.debug("After process started");
            events.add(event);
        }

        public void beforeNodeLeft(ProcessNodeLeftEvent event) {
            logger.debug("Before node left: {}", event.getNodeInstance().getNodeName());
            events.add(event);
        }

        public void beforeNodeTriggered(ProcessNodeTriggeredEvent event) {
            logger.debug("Before node triggered: {}", event.getNodeInstance().getNodeName());
            events.add(event);
        }

        public void beforeProcessCompleted(ProcessCompletedEvent event) {
            logger.debug("Before process completed");
            events.add(event);
        }

        public void beforeProcessStarted(ProcessStartedEvent event) {
            logger.debug("Before process started");
            events.add(event);
        }

        public void afterVariableChanged(ProcessVariableChangedEvent event) {
            logger.debug("After Variable Changed");
            events.add(event);
        }

        public void beforeVariableChanged(ProcessVariableChangedEvent event) {
            logger.debug("Before Variable Changed");
            events.add(event);
        }
    };
    ksession.addEventListener(listener);
    ProcessInstance processInstance = ksession.startProcess("org.drools.test.TestProcess");
    logger.debug("Started process instance {}", processInstance.getId());
    assertEquals(12, events.size());
    assertTrue(events.get(0) instanceof ProcessStartedEvent);
    assertTrue(events.get(1) instanceof ProcessNodeTriggeredEvent);
    assertTrue(events.get(2) instanceof ProcessNodeLeftEvent);
    assertTrue(events.get(3) instanceof ProcessNodeTriggeredEvent);
    assertTrue(events.get(4) instanceof ProcessNodeLeftEvent);
    assertTrue(events.get(5) instanceof ProcessNodeTriggeredEvent);
    assertTrue(events.get(6) instanceof ProcessNodeTriggeredEvent);
    assertTrue(events.get(7) instanceof ProcessNodeLeftEvent);
    assertTrue(events.get(8) instanceof ProcessNodeTriggeredEvent);
    assertTrue(events.get(9) instanceof ProcessNodeLeftEvent);
    assertTrue(events.get(10) instanceof ProcessNodeTriggeredEvent);
    assertTrue(events.get(11) instanceof ProcessStartedEvent);
    ksession.removeEventListener(listener);
    events.clear();
    processInstance = ksession.startProcess("org.drools.test.TestProcess");
    logger.debug("Started process instance {}", processInstance.getId());
    assertTrue(events.isEmpty());
}
Also used : ProcessVariableChangedEvent(org.kie.api.event.process.ProcessVariableChangedEvent) ProcessEvent(org.kie.api.event.process.ProcessEvent) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) ArrayList(java.util.ArrayList) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) ProcessCompletedEvent(org.kie.api.event.process.ProcessCompletedEvent) ClassPathResource(org.drools.core.io.impl.ClassPathResource) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ProcessNodeTriggeredEvent(org.kie.api.event.process.ProcessNodeTriggeredEvent) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) ProcessNodeLeftEvent(org.kie.api.event.process.ProcessNodeLeftEvent) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 12 with ProcessVariableChangedEvent

use of org.kie.api.event.process.ProcessVariableChangedEvent in project jbpm by kiegroup.

the class IterableListenerAssert method assertChangedVariable.

/**
 * Asserts that the variable was changed
 *
 * @param it       listener that listened to the process
 * @param name     name of the variable
 * @param oldValue expected old value or null
 * @param newValue expected new value or null
 */
public static void assertChangedVariable(IterableProcessEventListener it, String name, Object oldValue, Object newValue) {
    TrackedEvent event = getEvent(it);
    Assertions.assertThat(event.getMethod()).isEqualTo(BEFORE_VARIABLE);
    ProcessVariableChangedEvent orig = event.getEvent();
    Assertions.assertThat(orig.getVariableId()).isEqualTo(name);
    assertChangedVariableValues(orig, oldValue, newValue);
    event = it.next();
    Assertions.assertThat(event.getMethod()).isEqualTo(AFTER_VARIABLE);
    orig = event.getEvent();
    Assertions.assertThat(orig.getVariableId()).isEqualTo(name);
    assertChangedVariableValues(orig, oldValue, newValue);
}
Also used : ProcessVariableChangedEvent(org.kie.api.event.process.ProcessVariableChangedEvent) TrackedEvent(org.jbpm.test.listener.IterableProcessEventListener.TrackedEvent)

Example 13 with ProcessVariableChangedEvent

use of org.kie.api.event.process.ProcessVariableChangedEvent in project jbpm by kiegroup.

the class IterableListenerAssert method assertChangedMultipleInstancesVariable.

/**
 * Asserts that the variable was changed within multipleInstances
 * subprocess. Variable has prefix identifying this MI subprocess.
 *
 * @param it       listener that listened to the process
 * @param name     name of the variable
 * @param oldValue expected old value or null
 * @param newValue expected new value or null
 */
public static void assertChangedMultipleInstancesVariable(IterableProcessEventListener it, String name, Object oldValue, Object newValue) {
    TrackedEvent event = getEvent(it);
    Assertions.assertThat(event.getMethod()).isEqualTo(BEFORE_VARIABLE);
    ProcessVariableChangedEvent orig = event.getEvent();
    Assertions.assertThat(orig.getVariableId().endsWith(":" + name)).isTrue();
    assertChangedVariableValues(orig, oldValue, newValue);
    event = it.next();
    Assertions.assertThat(event.getMethod()).isEqualTo(AFTER_VARIABLE);
    orig = event.getEvent();
    Assertions.assertThat(orig.getVariableId().endsWith(":" + name)).isTrue();
    assertChangedVariableValues(orig, oldValue, newValue);
}
Also used : ProcessVariableChangedEvent(org.kie.api.event.process.ProcessVariableChangedEvent) TrackedEvent(org.jbpm.test.listener.IterableProcessEventListener.TrackedEvent)

Aggregations

ProcessVariableChangedEvent (org.kie.api.event.process.ProcessVariableChangedEvent)13 Test (org.junit.Test)8 ProcessStartedEvent (org.kie.api.event.process.ProcessStartedEvent)8 ProcessNodeTriggeredEvent (org.kie.api.event.process.ProcessNodeTriggeredEvent)7 ArrayList (java.util.ArrayList)6 ProcessCompletedEvent (org.kie.api.event.process.ProcessCompletedEvent)6 ProcessEventListener (org.kie.api.event.process.ProcessEventListener)6 ProcessNodeLeftEvent (org.kie.api.event.process.ProcessNodeLeftEvent)6 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)5 RuleFlowProcess (org.jbpm.ruleflow.core.RuleFlowProcess)5 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)5 ProcessEvent (org.kie.api.event.process.ProcessEvent)5 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)4 Action (org.jbpm.process.instance.impl.Action)4 DroolsAction (org.jbpm.workflow.core.DroolsAction)4 ConnectionImpl (org.jbpm.workflow.core.impl.ConnectionImpl)4 DroolsConsequenceAction (org.jbpm.workflow.core.impl.DroolsConsequenceAction)4 ActionNode (org.jbpm.workflow.core.node.ActionNode)4 EndNode (org.jbpm.workflow.core.node.EndNode)4