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());
}
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());
}
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());
}
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();
}
}
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);
}
Aggregations