Search in sources :

Example 21 with ProcessStartedEvent

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

the class ProcessEventListenerTest method testInternalNodeSignalEvent.

@Test
public void testInternalNodeSignalEvent() {
    Reader source = new StringReader(process);
    builder.addRuleFlow(source);
    KieSession session = createKieSession(builder.getPackages());
    final List<ProcessEvent> processEventList = new ArrayList<ProcessEvent>();
    final ProcessEventListener listener = createProcessEventListener(processEventList);
    ((InternalWorkingMemory) session).getProcessRuntime().addEventListener(listener);
    ProcessInstance processInstance = ((InternalWorkingMemory) session).getProcessRuntime().startProcess("org.drools.core.event");
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
    assertEquals("MyValue", ((VariableScopeInstance) ((org.jbpm.process.instance.ProcessInstance) processInstance).getContextInstance(VariableScope.VARIABLE_SCOPE)).getVariable("MyVar"));
    assertEquals(28, processEventList.size());
    for (ProcessEvent e : processEventList) {
        logger.debug(e.toString());
    }
    assertEquals("org.drools.core.event", ((ProcessStartedEvent) processEventList.get(2)).getProcessInstance().getProcessId());
}
Also used : ProcessEvent(org.kie.api.event.process.ProcessEvent) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) Reader(java.io.Reader) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 22 with ProcessStartedEvent

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

the class ProcessEventSupportTest method testProcessEventListener.

@Test
public void testProcessEventListener() throws Exception {
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    // create a simple package with one process to test the events
    final InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools.test");
    RuleFlowProcess process = new RuleFlowProcess();
    process.setId("org.drools.core.process.event");
    process.setName("Event Process");
    StartNode startNode = new StartNode();
    startNode.setName("Start");
    startNode.setId(1);
    process.addNode(startNode);
    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("Executed action");
        }
    });
    actionNode.setAction(action);
    actionNode.setId(2);
    process.addNode(actionNode);
    new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, actionNode, Node.CONNECTION_DEFAULT_TYPE);
    EndNode endNode = new EndNode();
    endNode.setName("End");
    endNode.setId(3);
    process.addNode(endNode);
    new ConnectionImpl(actionNode, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
    pkg.addProcess(process);
    List<KiePackage> pkgs = new ArrayList<KiePackage>();
    pkgs.add(pkg);
    kbase.addPackages(pkgs);
    KieSession session = kbase.newKieSession();
    final List<ProcessEvent> processEventList = new ArrayList<ProcessEvent>();
    final ProcessEventListener processEventListener = new ProcessEventListener() {

        public void afterNodeLeft(ProcessNodeLeftEvent event) {
            processEventList.add(event);
        }

        public void afterNodeTriggered(ProcessNodeTriggeredEvent event) {
            processEventList.add(event);
        }

        public void afterProcessCompleted(ProcessCompletedEvent event) {
            processEventList.add(event);
        }

        public void afterProcessStarted(ProcessStartedEvent event) {
            processEventList.add(event);
        }

        public void beforeNodeLeft(ProcessNodeLeftEvent event) {
            processEventList.add(event);
        }

        public void beforeNodeTriggered(ProcessNodeTriggeredEvent event) {
            processEventList.add(event);
        }

        public void beforeProcessCompleted(ProcessCompletedEvent event) {
            processEventList.add(event);
        }

        public void beforeProcessStarted(ProcessStartedEvent event) {
            processEventList.add(event);
        }

        public void beforeVariableChanged(ProcessVariableChangedEvent event) {
            processEventList.add(event);
        }

        public void afterVariableChanged(ProcessVariableChangedEvent event) {
            processEventList.add(event);
        }
    };
    session.addEventListener(processEventListener);
    // execute the process
    session.startProcess("org.drools.core.process.event");
    assertEquals(16, processEventList.size());
    assertEquals("org.drools.core.process.event", ((ProcessStartedEvent) processEventList.get(0)).getProcessInstance().getProcessId());
    assertEquals("Start", ((ProcessNodeTriggeredEvent) processEventList.get(1)).getNodeInstance().getNodeName());
    assertEquals("Start", ((ProcessNodeLeftEvent) processEventList.get(2)).getNodeInstance().getNodeName());
    assertEquals("Print", ((ProcessNodeTriggeredEvent) processEventList.get(3)).getNodeInstance().getNodeName());
    assertEquals("Print", ((ProcessNodeLeftEvent) processEventList.get(4)).getNodeInstance().getNodeName());
    assertEquals("End", ((ProcessNodeTriggeredEvent) processEventList.get(5)).getNodeInstance().getNodeName());
    assertEquals("End", ((ProcessNodeLeftEvent) processEventList.get(6)).getNodeInstance().getNodeName());
    assertEquals("org.drools.core.process.event", ((ProcessCompletedEvent) processEventList.get(7)).getProcessInstance().getProcessId());
    assertEquals("org.drools.core.process.event", ((ProcessCompletedEvent) processEventList.get(8)).getProcessInstance().getProcessId());
    assertEquals("End", ((ProcessNodeLeftEvent) processEventList.get(9)).getNodeInstance().getNodeName());
    assertEquals("End", ((ProcessNodeTriggeredEvent) processEventList.get(10)).getNodeInstance().getNodeName());
    assertEquals("Print", ((ProcessNodeLeftEvent) processEventList.get(11)).getNodeInstance().getNodeName());
    assertEquals("Print", ((ProcessNodeTriggeredEvent) processEventList.get(12)).getNodeInstance().getNodeName());
    assertEquals("Start", ((ProcessNodeLeftEvent) processEventList.get(13)).getNodeInstance().getNodeName());
    assertEquals("Start", ((ProcessNodeTriggeredEvent) processEventList.get(14)).getNodeInstance().getNodeName());
    assertEquals("org.drools.core.process.event", ((ProcessStartedEvent) processEventList.get(15)).getProcessInstance().getProcessId());
}
Also used : ProcessVariableChangedEvent(org.kie.api.event.process.ProcessVariableChangedEvent) 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) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) ActionNode(org.jbpm.workflow.core.node.ActionNode) ArrayList(java.util.ArrayList) ConnectionImpl(org.jbpm.workflow.core.impl.ConnectionImpl) ProcessContext(org.kie.api.runtime.process.ProcessContext) KiePackage(org.kie.api.definition.KiePackage) ProcessNodeTriggeredEvent(org.kie.api.event.process.ProcessNodeTriggeredEvent) KieSession(org.kie.api.runtime.KieSession) ProcessNodeLeftEvent(org.kie.api.event.process.ProcessNodeLeftEvent) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) DroolsAction(org.jbpm.workflow.core.DroolsAction) StartNode(org.jbpm.workflow.core.node.StartNode) ProcessEvent(org.kie.api.event.process.ProcessEvent) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) ProcessCompletedEvent(org.kie.api.event.process.ProcessCompletedEvent) EndNode(org.jbpm.workflow.core.node.EndNode) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 23 with ProcessStartedEvent

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

the class ProcessEventSupportTest method testProcessEventListenerWithEndEvent.

@Test
public void testProcessEventListenerWithEndEvent() throws Exception {
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    // create a simple package with one process to test the events
    final InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools.test");
    RuleFlowProcess process = new RuleFlowProcess();
    process.setId("org.drools.core.process.event");
    process.setName("Event Process");
    StartNode startNode = new StartNode();
    startNode.setName("Start");
    startNode.setId(1);
    process.addNode(startNode);
    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("Executed action");
        }
    });
    actionNode.setAction(action);
    actionNode.setId(2);
    process.addNode(actionNode);
    new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, actionNode, Node.CONNECTION_DEFAULT_TYPE);
    EndNode endNode = new EndNode();
    endNode.setName("End");
    endNode.setId(3);
    endNode.setTerminate(false);
    process.addNode(endNode);
    new ConnectionImpl(actionNode, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
    pkg.addProcess(process);
    List<KiePackage> pkgs = new ArrayList<KiePackage>();
    pkgs.add(pkg);
    kbase.addPackages(pkgs);
    KieSession session = kbase.newKieSession();
    final List<ProcessEvent> processEventList = new ArrayList<ProcessEvent>();
    final ProcessEventListener processEventListener = new ProcessEventListener() {

        public void afterNodeLeft(ProcessNodeLeftEvent event) {
            processEventList.add(event);
        }

        public void afterNodeTriggered(ProcessNodeTriggeredEvent event) {
            processEventList.add(event);
        }

        public void afterProcessCompleted(ProcessCompletedEvent event) {
            processEventList.add(event);
        }

        public void afterProcessStarted(ProcessStartedEvent event) {
            processEventList.add(event);
        }

        public void beforeNodeLeft(ProcessNodeLeftEvent event) {
            processEventList.add(event);
        }

        public void beforeNodeTriggered(ProcessNodeTriggeredEvent event) {
            processEventList.add(event);
        }

        public void beforeProcessCompleted(ProcessCompletedEvent event) {
            processEventList.add(event);
        }

        public void beforeProcessStarted(ProcessStartedEvent event) {
            processEventList.add(event);
        }

        public void beforeVariableChanged(ProcessVariableChangedEvent event) {
            processEventList.add(event);
        }

        public void afterVariableChanged(ProcessVariableChangedEvent event) {
            processEventList.add(event);
        }
    };
    session.addEventListener(processEventListener);
    // execute the process
    session.startProcess("org.drools.core.process.event");
    assertEquals(14, processEventList.size());
    assertEquals("org.drools.core.process.event", ((ProcessStartedEvent) processEventList.get(0)).getProcessInstance().getProcessId());
    assertEquals("Start", ((ProcessNodeTriggeredEvent) processEventList.get(1)).getNodeInstance().getNodeName());
    assertEquals("Start", ((ProcessNodeLeftEvent) processEventList.get(2)).getNodeInstance().getNodeName());
    assertEquals("Print", ((ProcessNodeTriggeredEvent) processEventList.get(3)).getNodeInstance().getNodeName());
    assertEquals("Print", ((ProcessNodeLeftEvent) processEventList.get(4)).getNodeInstance().getNodeName());
    assertEquals("End", ((ProcessNodeTriggeredEvent) processEventList.get(5)).getNodeInstance().getNodeName());
    assertEquals("End", ((ProcessNodeLeftEvent) processEventList.get(6)).getNodeInstance().getNodeName());
    assertEquals("End", ((ProcessNodeLeftEvent) processEventList.get(7)).getNodeInstance().getNodeName());
    assertEquals("End", ((ProcessNodeTriggeredEvent) processEventList.get(8)).getNodeInstance().getNodeName());
    assertEquals("Print", ((ProcessNodeLeftEvent) processEventList.get(9)).getNodeInstance().getNodeName());
    assertEquals("Print", ((ProcessNodeTriggeredEvent) processEventList.get(10)).getNodeInstance().getNodeName());
    assertEquals("Start", ((ProcessNodeLeftEvent) processEventList.get(11)).getNodeInstance().getNodeName());
    assertEquals("Start", ((ProcessNodeTriggeredEvent) processEventList.get(12)).getNodeInstance().getNodeName());
    assertEquals("org.drools.core.process.event", ((ProcessStartedEvent) processEventList.get(13)).getProcessInstance().getProcessId());
}
Also used : ProcessVariableChangedEvent(org.kie.api.event.process.ProcessVariableChangedEvent) 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) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) ActionNode(org.jbpm.workflow.core.node.ActionNode) ArrayList(java.util.ArrayList) ConnectionImpl(org.jbpm.workflow.core.impl.ConnectionImpl) ProcessContext(org.kie.api.runtime.process.ProcessContext) KiePackage(org.kie.api.definition.KiePackage) ProcessNodeTriggeredEvent(org.kie.api.event.process.ProcessNodeTriggeredEvent) KieSession(org.kie.api.runtime.KieSession) ProcessNodeLeftEvent(org.kie.api.event.process.ProcessNodeLeftEvent) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) DroolsAction(org.jbpm.workflow.core.DroolsAction) StartNode(org.jbpm.workflow.core.node.StartNode) ProcessEvent(org.kie.api.event.process.ProcessEvent) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) ProcessCompletedEvent(org.kie.api.event.process.ProcessCompletedEvent) EndNode(org.jbpm.workflow.core.node.EndNode) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 24 with ProcessStartedEvent

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

the class ProcessTest method main.

public static final void main(String[] args) {
    try {
        // load up the knowledge base
        final KieSession ksession = getKieSession();
        UIWorkItemHandler handler = new UIWorkItemHandler();
        ksession.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
        handler.setVisible(true);
        ksession.getWorkItemManager().registerWorkItemHandler("Email", new WorkItemHandler() {

            public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
                System.out.println("Sending email ...");
                manager.completeWorkItem(workItem.getId(), null);
            }

            public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
            }
        });
        KieRuntimeLogger logger = KieServices.Factory.get().getLoggers().newFileLogger(ksession, "test");
        // start a new process instance
        Person person = new Person("john", "John Doe");
        person.setAge(20);
        Request request = new Request("12345");
        request.setPersonId("john");
        request.setAmount(1000L);
        ksession.insert(person);
        ksession.insert(request);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("request", request);
        WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.startProcess("com.sample.requestHandling", params);
        ksession.insert(processInstance);
        ksession.fireAllRules();
        // signaling to select tasks in the ad-hoc sub-process
        ksession.signalEvent("Investigate", null, processInstance.getId());
        // dynamically adding a new sub-process
        DynamicNodeInstance dynamicNodeInstance = (DynamicNodeInstance) processInstance.getNodeInstances().iterator().next();
        DynamicUtils.addDynamicSubProcess(dynamicNodeInstance, ksession, "com.sample.contactCustomer", null);
        // event processing to detect too much processes being started
        ksession.addEventListener(new DefaultProcessEventListener() {

            public void beforeProcessStarted(ProcessStartedEvent event) {
                ksession.insert(event);
            }
        });
        Request request2 = new Request("12346");
        request2.setPersonId("john");
        request2.setAmount(1000L);
        params = new HashMap<String, Object>();
        params.put("request", request2);
        ksession.startProcess("com.sample.requestHandling", params);
        ksession.fireAllRules();
        Request request3 = new Request("12347");
        request3.setPersonId("john");
        request3.setAmount(1000L);
        params = new HashMap<String, Object>();
        params.put("request", request);
        ksession.startProcess("com.sample.requestHandling", params);
        ksession.fireAllRules();
        Request request4 = new Request("12348");
        request4.setPersonId("john");
        request4.setAmount(1000L);
        params = new HashMap<String, Object>();
        params.put("request", request4);
        ksession.startProcess("com.sample.requestHandling", params);
        ksession.fireAllRules();
        Request request5 = new Request("12349");
        request5.setPersonId("john");
        request5.setAmount(1000L);
        params = new HashMap<String, Object>();
        params.put("request", request5);
        ksession.startProcess("com.sample.requestHandling", params);
        ksession.fireAllRules();
        Request request6 = new Request("12350");
        request6.setPersonId("john");
        request6.setAmount(1000L);
        params = new HashMap<String, Object>();
        params.put("request", request6);
        processInstance = (WorkflowProcessInstance) ksession.startProcess("com.sample.requestHandling", params);
        FactHandle handle = ksession.insert(request6);
        ksession.insert(processInstance);
        ksession.fireAllRules();
        // exception handling when canceling request
        request6.setCanceled(true);
        ksession.update(handle, request6);
        ksession.fireAllRules();
        logger.close();
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
Also used : DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) HashMap(java.util.HashMap) FactHandle(org.kie.api.runtime.rule.FactHandle) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) WorkItem(org.kie.api.runtime.process.WorkItem) WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) KieSession(org.kie.api.runtime.KieSession) WorkItemManager(org.kie.api.runtime.process.WorkItemManager) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance)

Example 25 with ProcessStartedEvent

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

the class StandaloneBPMNProcessTest method testSignalStart.

@Test
public void testSignalStart() throws Exception {
    KieBase kbase = createKnowledgeBase("BPMN2-SignalStart.bpmn2");
    KieSession ksession = createKnowledgeSession(kbase);
    final List<Long> list = new ArrayList<Long>();
    ksession.addEventListener(new DefaultProcessEventListener() {

        public void afterProcessStarted(ProcessStartedEvent event) {
            list.add(event.getProcessInstance().getId());
        }
    });
    ksession.signalEvent("MySignal", "NewValue");
    assertThat(list.size()).isEqualTo(1);
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Aggregations

ProcessStartedEvent (org.kie.api.event.process.ProcessStartedEvent)57 Test (org.junit.Test)55 DefaultProcessEventListener (org.kie.api.event.process.DefaultProcessEventListener)48 ArrayList (java.util.ArrayList)45 NodeLeftCountDownProcessEventListener (org.jbpm.test.listener.NodeLeftCountDownProcessEventListener)29 KieBase (org.kie.api.KieBase)29 KieSession (org.kie.api.runtime.KieSession)23 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)19 ProcessEventListener (org.kie.api.event.process.ProcessEventListener)17 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)15 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)12 HashMap (java.util.HashMap)10 ProcessCompletedEvent (org.kie.api.event.process.ProcessCompletedEvent)9 ProcessNodeLeftEvent (org.kie.api.event.process.ProcessNodeLeftEvent)8 ProcessNodeTriggeredEvent (org.kie.api.event.process.ProcessNodeTriggeredEvent)8 ProcessVariableChangedEvent (org.kie.api.event.process.ProcessVariableChangedEvent)8 RuntimeEnvironment (org.kie.api.runtime.manager.RuntimeEnvironment)8 TestWorkItemHandler (org.jbpm.bpmn2.objects.TestWorkItemHandler)7 ProcessEvent (org.kie.api.event.process.ProcessEvent)6 WorkItem (org.kie.api.runtime.process.WorkItem)6