Search in sources :

Example 6 with TestProcessEventListener

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

the class SubProcessTest method testSynchronousSubProcess.

@Test
public void testSynchronousSubProcess() {
    RuleFlowProcess process = new RuleFlowProcess();
    process.setId("org.drools.core.process.process");
    process.setName("Process");
    StartNode startNode = new StartNode();
    startNode.setName("Start");
    startNode.setId(1);
    process.addNode(startNode);
    EndNode endNode = new EndNode();
    endNode.setName("EndNode");
    endNode.setId(2);
    process.addNode(endNode);
    SubProcessNode subProcessNode = new SubProcessNode();
    subProcessNode.setName("SubProcessNode");
    subProcessNode.setId(3);
    subProcessNode.setProcessId("org.drools.core.process.subprocess");
    process.addNode(subProcessNode);
    new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, subProcessNode, Node.CONNECTION_DEFAULT_TYPE);
    new ConnectionImpl(subProcessNode, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
    RuleFlowProcess subprocess = new RuleFlowProcess();
    subprocess.setId("org.drools.core.process.subprocess");
    subprocess.setName("SubProcess");
    startNode = new StartNode();
    startNode.setName("Start");
    startNode.setId(1);
    subprocess.addNode(startNode);
    endNode = new EndNode();
    endNode.setName("EndNode");
    endNode.setId(2);
    subprocess.addNode(endNode);
    ActionNode actionNode = new ActionNode();
    actionNode.setName("Action");
    DroolsAction action = new DroolsConsequenceAction("java", null);
    action.setMetaData("Action", new Action() {

        public void execute(ProcessContext context) throws Exception {
            logger.info("Executed action");
            executed = true;
        }
    });
    actionNode.setAction(action);
    actionNode.setId(3);
    subprocess.addNode(actionNode);
    new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, actionNode, Node.CONNECTION_DEFAULT_TYPE);
    new ConnectionImpl(actionNode, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
    KieSession ksession = createKieSession(process, subprocess);
    TestProcessEventListener procEventListener = new TestProcessEventListener();
    ksession.addEventListener(procEventListener);
    ksession.startProcess("org.drools.core.process.process");
    assertTrue(executed);
    assertEquals(0, ksession.getProcessInstances().size());
    verifyEventHistory(syncEventorder, 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) Action(org.jbpm.process.instance.impl.Action) DroolsAction(org.jbpm.workflow.core.DroolsAction) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) ActionNode(org.jbpm.workflow.core.node.ActionNode) ConnectionImpl(org.jbpm.workflow.core.impl.ConnectionImpl) ProcessContext(org.kie.api.runtime.process.ProcessContext) EndNode(org.jbpm.workflow.core.node.EndNode) SubProcessNode(org.jbpm.workflow.core.node.SubProcessNode) KieSession(org.kie.api.runtime.KieSession) TestProcessEventListener(org.jbpm.process.test.TestProcessEventListener) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 7 with TestProcessEventListener

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

the class SubProcessTest method testAsynchronousSubProcess.

@Test
public void testAsynchronousSubProcess() {
    RuleFlowProcess process = new RuleFlowProcess();
    process.setId("org.drools.core.process.process");
    process.setName("Process");
    StartNode startNode = new StartNode();
    startNode.setName("Start");
    startNode.setId(1);
    process.addNode(startNode);
    EndNode endNode = new EndNode();
    endNode.setName("EndNode");
    endNode.setId(2);
    process.addNode(endNode);
    SubProcessNode subProcessNode = new SubProcessNode();
    subProcessNode.setName("SubProcessNode");
    subProcessNode.setId(3);
    subProcessNode.setProcessId("org.drools.core.process.subprocess");
    process.addNode(subProcessNode);
    new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, subProcessNode, Node.CONNECTION_DEFAULT_TYPE);
    new ConnectionImpl(subProcessNode, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
    RuleFlowProcess subProcess = new RuleFlowProcess();
    subProcess.setId("org.drools.core.process.subprocess");
    subProcess.setName("SubProcess");
    startNode = new StartNode();
    startNode.setName("Start");
    startNode.setId(1);
    subProcess.addNode(startNode);
    endNode = new EndNode();
    endNode.setName("EndNode");
    endNode.setId(2);
    subProcess.addNode(endNode);
    WorkItemNode workItemNode = new WorkItemNode();
    workItemNode.setName("WorkItem");
    Work work = new WorkImpl();
    work.setName("MyWork");
    workItemNode.setWork(work);
    workItemNode.setId(4);
    subProcess.addNode(workItemNode);
    new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, workItemNode, Node.CONNECTION_DEFAULT_TYPE);
    new ConnectionImpl(workItemNode, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
    KieSession ksession = createKieSession(process, subProcess);
    ksession.getWorkItemManager().registerWorkItemHandler("MyWork", new WorkItemHandler() {

        public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
            logger.info("Executing work item");
            SubProcessTest.this.workItem = workItem;
        }

        public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
        }
    });
    ksession.startProcess("org.drools.core.process.process");
    TestProcessEventListener procEventListener = new TestProcessEventListener();
    ksession.addEventListener(procEventListener);
    assertNotNull(workItem);
    assertEquals(2, ksession.getProcessInstances().size());
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    assertEquals(0, ksession.getProcessInstances().size());
    verifyEventHistory(asyncEventOrder, procEventListener.getEventHistory());
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) StartNode(org.jbpm.workflow.core.node.StartNode) ConnectionImpl(org.jbpm.workflow.core.impl.ConnectionImpl) WorkItem(org.kie.api.runtime.process.WorkItem) WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) EndNode(org.jbpm.workflow.core.node.EndNode) SubProcessNode(org.jbpm.workflow.core.node.SubProcessNode) WorkItemNode(org.jbpm.workflow.core.node.WorkItemNode) Work(org.jbpm.process.core.Work) WorkImpl(org.jbpm.process.core.impl.WorkImpl) KieSession(org.kie.api.runtime.KieSession) WorkItemManager(org.kie.api.runtime.process.WorkItemManager) TestProcessEventListener(org.jbpm.process.test.TestProcessEventListener) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 8 with TestProcessEventListener

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

the class NodeInnerClassesTest method testNodeReading.

@Test
public void testNodeReading() {
    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);
    CompositeNode compositeNode = new CompositeNode();
    compositeNode.setName("CompositeNode");
    compositeNode.setId(2);
    ForEachNode forEachNode = new ForEachNode();
    ForEachNode.ForEachSplitNode split = new ForEachNode.ForEachSplitNode();
    split.setName("ForEachSplit");
    split.setMetaData("hidden", true);
    split.setMetaData("UniqueId", forEachNode.getMetaData("Uniqueid") + ":foreach:split");
    forEachNode.internalAddNode(split);
    forEachNode.linkIncomingConnections(org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE, new CompositeNode.NodeAndType(split, org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE));
    process.addNode(forEachNode);
    KieSession ksession = createKieSession(process);
    TestProcessEventListener procEventListener = new TestProcessEventListener();
    ksession.addEventListener(procEventListener);
    ProcessInstance processInstance = ksession.startProcess("org.drools.core.process.event");
    Assert.assertNotNull(processInstance);
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) Variable(org.jbpm.process.core.context.variable.Variable) ArrayList(java.util.ArrayList) ObjectDataType(org.jbpm.process.core.datatype.impl.type.ObjectDataType) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) TestProcessEventListener(org.jbpm.process.test.TestProcessEventListener) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 9 with TestProcessEventListener

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

the class EventSubProcessTest method testNestedEventSubProcess.

@Test
public void testNestedEventSubProcess() throws Exception {
    RuleFlowProcess process = new RuleFlowProcess();
    process.setAutoComplete(true);
    String processId = "org.jbpm.process.event.subprocess";
    process.setId(processId);
    process.setName("Event SubProcess 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);
    NodeCreator<StartNode> startNodeCreator = new NodeCreator<StartNode>(process, StartNode.class);
    NodeCreator<EndNode> endNodeCreator = new NodeCreator<EndNode>(process, EndNode.class);
    NodeCreator<CompositeNode> compNodeCreator = new NodeCreator<CompositeNode>(process, CompositeNode.class);
    // outer process
    StartNode startNode = startNodeCreator.createNode("start0");
    CompositeNode compositeNode = compNodeCreator.createNode("comp0");
    connect(startNode, compositeNode);
    EndNode endNode = endNodeCreator.createNode("end0");
    connect(compositeNode, endNode);
    // 1rst level nested subprocess
    startNodeCreator.setNodeContainer(compositeNode);
    endNodeCreator.setNodeContainer(compositeNode);
    compNodeCreator.setNodeContainer(compositeNode);
    startNode = startNodeCreator.createNode("start1");
    compositeNode = compNodeCreator.createNode("comp1");
    connect(startNode, compositeNode);
    endNode = endNodeCreator.createNode("end1");
    connect(compositeNode, endNode);
    // 2nd level subprocess
    startNodeCreator.setNodeContainer(compositeNode);
    endNodeCreator.setNodeContainer(compositeNode);
    NodeCreator<WorkItemNode> workItemNodeCreator = new NodeCreator<WorkItemNode>(compositeNode, WorkItemNode.class);
    startNode = startNodeCreator.createNode("start2");
    WorkItemNode workItemNode = workItemNodeCreator.createNode("workItem2");
    Work work = new WorkImpl();
    String workItemName = "play";
    work.setName(workItemName);
    workItemNode.setWork(work);
    connect(startNode, workItemNode);
    endNode = endNodeCreator.createNode("end2");
    connect(workItemNode, endNode);
    // (3rd level) event sub process in 2nd level subprocess
    NodeCreator<EventSubProcessNode> espNodeCreator = new NodeCreator<EventSubProcessNode>(compositeNode, EventSubProcessNode.class);
    EventSubProcessNode espNode = espNodeCreator.createNode("eventSub2");
    EventTypeFilter eventFilter = new EventTypeFilter();
    String EVENT_NAME = "subEvent";
    eventFilter.setType(EVENT_NAME);
    espNode.addEvent(eventFilter);
    startNodeCreator.setNodeContainer(espNode);
    endNodeCreator.setNodeContainer(espNode);
    NodeCreator<ActionNode> actionNodeCreator = new NodeCreator<ActionNode>(espNode, ActionNode.class);
    startNode = startNodeCreator.createNode("start3*");
    ActionNode actionNode = actionNodeCreator.createNode("print3*");
    actionNode.setName("Print");
    final List<String> eventList = new ArrayList<String>();
    DroolsAction action = new DroolsConsequenceAction("java", null);
    action.setMetaData("Action", new Action() {

        public void execute(ProcessContext context) throws Exception {
            eventList.add("Executed action");
        }
    });
    actionNode.setAction(action);
    connect(startNode, actionNode);
    endNode = endNodeCreator.createNode("end3*");
    connect(actionNode, endNode);
    // run process
    KieSession ksession = createKieSession(process);
    TestProcessEventListener procEventListener = new TestProcessEventListener();
    ksession.addEventListener(procEventListener);
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler(workItemName, workItemHandler);
    ProcessInstance processInstance = ksession.startProcess(processId);
    processInstance.signalEvent(EVENT_NAME, null);
    assertEquals("Event " + EVENT_NAME + " did not fire!", 1, eventList.size());
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItems().removeLast().getId(), null);
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
    verifyEventHistory(nestedEventOrder, procEventListener.getEventHistory());
}
Also used : TestWorkItemHandler(org.jbpm.process.test.TestWorkItemHandler) RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) 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) EventSubProcessNode(org.jbpm.workflow.core.node.EventSubProcessNode) ArrayList(java.util.ArrayList) ActionNode(org.jbpm.workflow.core.node.ActionNode) ObjectDataType(org.jbpm.process.core.datatype.impl.type.ObjectDataType) ProcessContext(org.kie.api.runtime.process.ProcessContext) EventTypeFilter(org.jbpm.process.core.event.EventTypeFilter) WorkItemNode(org.jbpm.workflow.core.node.WorkItemNode) Work(org.jbpm.process.core.Work) WorkImpl(org.jbpm.process.core.impl.WorkImpl) KieSession(org.kie.api.runtime.KieSession) DroolsAction(org.jbpm.workflow.core.DroolsAction) StartNode(org.jbpm.workflow.core.node.StartNode) CompositeNode(org.jbpm.workflow.core.node.CompositeNode) EndNode(org.jbpm.workflow.core.node.EndNode) NodeCreator(org.jbpm.process.test.NodeCreator) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) TestProcessEventListener(org.jbpm.process.test.TestProcessEventListener) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 10 with TestProcessEventListener

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

the class EventTest method testEvent3.

@Test
public void testEvent3() {
    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);
    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());
    Person john = new Person();
    john.setName("John");
    processInstance.signalEvent("myOtherEvent", john);
    assertEquals(2, myList.size());
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
    verifyEventHistory(test3EventOrder, 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