Search in sources :

Example 11 with TestProcessEventListener

use of org.jbpm.process.test.TestProcessEventListener in project jbpm by kiegroup.

the class EventTest method testEvent3a.

@Test
public void testEvent3a() {
    RuleFlowProcess process = new RuleFlowProcess();
    process.setId("org.drools.core.process.event");
    process.setName("Event Process");
    List<Variable> variables = new ArrayList<Variable>();
    Variable variable = new Variable();
    variable.setName("event");
    ObjectDataType personDataType = new ObjectDataType();
    personDataType.setClassName("org.drools.Person");
    variable.setType(personDataType);
    variables.add(variable);
    process.getVariableScope().setVariables(variables);
    StartNode startNode = new StartNode();
    startNode.setName("Start");
    startNode.setId(1);
    process.addNode(startNode);
    EventNode eventNode = new EventNode();
    EventTypeFilter eventFilter = new EventTypeFilter();
    eventFilter.setType("myEvent");
    eventNode.addEventFilter(eventFilter);
    eventNode.setVariableName("event");
    eventNode.setId(3);
    process.addNode(eventNode);
    final List<String> myList = new ArrayList<String>();
    ActionNode actionNode = new ActionNode();
    actionNode.setName("Print");
    DroolsAction action = new DroolsConsequenceAction("java", null);
    action.setMetaData("Action", new Action() {

        public void execute(ProcessContext context) throws Exception {
            logger.info("Detected event for person {}", ((Person) context.getVariable("event")).getName());
            myList.add("Executed action");
        }
    });
    actionNode.setAction(action);
    actionNode.setId(4);
    process.addNode(actionNode);
    new ConnectionImpl(eventNode, Node.CONNECTION_DEFAULT_TYPE, actionNode, Node.CONNECTION_DEFAULT_TYPE);
    EventNode eventNode2 = new EventNode();
    eventFilter = new EventTypeFilter();
    eventFilter.setType("myOtherEvent");
    eventNode2.addEventFilter(eventFilter);
    eventNode2.setVariableName("event");
    eventNode2.setId(5);
    process.addNode(eventNode2);
    ActionNode actionNode2 = new ActionNode();
    actionNode2.setName("Print");
    action = new DroolsConsequenceAction("java", null);
    action.setMetaData("Action", new Action() {

        public void execute(ProcessContext context) throws Exception {
            logger.info("Detected other event for person {}", ((Person) context.getVariable("event")).getName());
            myList.add("Executed action");
        }
    });
    actionNode2.setAction(action);
    actionNode2.setId(6);
    process.addNode(actionNode2);
    new ConnectionImpl(eventNode2, Node.CONNECTION_DEFAULT_TYPE, actionNode2, Node.CONNECTION_DEFAULT_TYPE);
    Join join = new Join();
    join.setName("AND Join");
    join.setType(Join.TYPE_AND);
    join.setId(7);
    process.addNode(join);
    new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, join, Node.CONNECTION_DEFAULT_TYPE);
    new ConnectionImpl(actionNode, Node.CONNECTION_DEFAULT_TYPE, join, Node.CONNECTION_DEFAULT_TYPE);
    new ConnectionImpl(actionNode2, Node.CONNECTION_DEFAULT_TYPE, join, Node.CONNECTION_DEFAULT_TYPE);
    EndNode endNode = new EndNode();
    endNode.setName("EndNode");
    endNode.setId(8);
    process.addNode(endNode);
    new ConnectionImpl(join, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
    KieSession ksession = createKieSession(process);
    TestProcessEventListener procEventListener = new TestProcessEventListener();
    ksession.addEventListener(procEventListener);
    System.setProperty("jbpm.loop.level.disabled", "true");
    ProcessInstance processInstance = ksession.startProcess("org.drools.core.process.event");
    assertEquals(0, myList.size());
    Person jack = new Person();
    jack.setName("Jack");
    processInstance.signalEvent("myEvent", jack);
    assertEquals(1, myList.size());
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    processInstance.signalEvent("myEvent", jack);
    assertEquals(2, myList.size());
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    Person john = new Person();
    john.setName("John");
    processInstance.signalEvent("myOtherEvent", john);
    assertEquals(3, myList.size());
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
    System.clearProperty("jbpm.loop.level.disabled");
    verifyEventHistory(test3aEventOrder, procEventListener.getEventHistory());
}
Also used : DroolsAction(org.jbpm.workflow.core.DroolsAction) RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) StartNode(org.jbpm.workflow.core.node.StartNode) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) DroolsAction(org.jbpm.workflow.core.DroolsAction) Action(org.jbpm.process.instance.impl.Action) Variable(org.jbpm.process.core.context.variable.Variable) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) ArrayList(java.util.ArrayList) ActionNode(org.jbpm.workflow.core.node.ActionNode) ConnectionImpl(org.jbpm.workflow.core.impl.ConnectionImpl) Join(org.jbpm.workflow.core.node.Join) ObjectDataType(org.jbpm.process.core.datatype.impl.type.ObjectDataType) ProcessContext(org.kie.api.runtime.process.ProcessContext) EventNode(org.jbpm.workflow.core.node.EventNode) EventTypeFilter(org.jbpm.process.core.event.EventTypeFilter) EndNode(org.jbpm.workflow.core.node.EndNode) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Person(org.jbpm.process.test.Person) TestProcessEventListener(org.jbpm.process.test.TestProcessEventListener) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Aggregations

TestProcessEventListener (org.jbpm.process.test.TestProcessEventListener)11 RuleFlowProcess (org.jbpm.ruleflow.core.RuleFlowProcess)11 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)11 Test (org.junit.Test)11 KieSession (org.kie.api.runtime.KieSession)11 EndNode (org.jbpm.workflow.core.node.EndNode)10 StartNode (org.jbpm.workflow.core.node.StartNode)10 ArrayList (java.util.ArrayList)9 Variable (org.jbpm.process.core.context.variable.Variable)9 ObjectDataType (org.jbpm.process.core.datatype.impl.type.ObjectDataType)9 Action (org.jbpm.process.instance.impl.Action)9 DroolsAction (org.jbpm.workflow.core.DroolsAction)9 ConnectionImpl (org.jbpm.workflow.core.impl.ConnectionImpl)9 DroolsConsequenceAction (org.jbpm.workflow.core.impl.DroolsConsequenceAction)9 ActionNode (org.jbpm.workflow.core.node.ActionNode)9 ProcessContext (org.kie.api.runtime.process.ProcessContext)9 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)8 EventTypeFilter (org.jbpm.process.core.event.EventTypeFilter)7 Person (org.jbpm.process.test.Person)6 EventNode (org.jbpm.workflow.core.node.EventNode)6