use of org.jbpm.workflow.instance.node.DynamicNodeInstance in project jbpm by kiegroup.
the class ProcessDynamicNodeTest method testAddDynamicWorkItem.
@Test
public void testAddDynamicWorkItem() {
Reader source = new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + " type=\"RuleFlow\" name=\"ruleflow\" id=\"org.drools.dynamic\" package-name=\"org.drools\" >\n" + "\n" + " <header>\n" + " </header>\n" + "\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <dynamic id=\"2\" name=\"DynamicNode\" >\n" + " <nodes>\n" + " <actionNode id=\"1\" name=\"Action\" >\n" + " <action type=\"expression\" dialect=\"mvel\" >System.out.println(\"Action\");</action>\n" + " </actionNode>\n" + " </nodes>\n" + " <connections>\n" + " </connections>\n" + " <in-ports/>\n" + " <out-ports/>\n" + " </dynamic>\n" + " <end id=\"3\" name=\"End\" />\n" + " </nodes>\n" + "\n" + " <connections>\n" + " <connection from=\"1\" to=\"2\" />\n" + " <connection from=\"2\" to=\"3\" />\n" + " </connections>\n" + "</process>");
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newReaderResource(source), ResourceType.DRF);
KieBase kbase = kbuilder.newKieBase();
KieSession ksession = kbase.newKieSession();
KieRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
TestWorkItemHandler handler = new TestWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
// start a new process instance
ProcessInstance processInstance = (ProcessInstance) ksession.startProcess("org.drools.dynamic");
DynamicNodeInstance dynamicContext = (DynamicNodeInstance) ((WorkflowProcessInstance) processInstance).getNodeInstances().iterator().next();
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("TaskName", "Dynamic Task");
assertNull(handler.getWorkItem());
assertEquals(0, dynamicContext.getNodeInstances().size());
DynamicUtils.addDynamicWorkItem(dynamicContext, ksession, "Human Task", parameters);
assertNotNull(handler.getWorkItem());
assertEquals(1, dynamicContext.getNodeInstances().size());
logger.close();
}
use of org.jbpm.workflow.instance.node.DynamicNodeInstance in project jbpm by kiegroup.
the class AddDynamicProcessToStageCommand method execute.
@Override
public Long execute(Context context) {
KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
ProcessInstance processInstance = ksession.getProcessInstance(processInstanceId);
if (processInstance == null) {
throw new ProcessInstanceNotFoundException("No process instance found with id " + processInstanceId);
}
DynamicNodeInstance dynamicContext = (DynamicNodeInstance) ((WorkflowProcessInstanceImpl) processInstance).getNodeInstances(true).stream().filter(ni -> (ni instanceof DynamicNodeInstance) && stageId.equals(ni.getNode().getMetaData().get("UniqueId"))).findFirst().orElse(null);
if (dynamicContext == null) {
throw new StageNotFoundException("No stage found with id " + stageId);
}
try {
CaseFileInstance caseFile = getCaseFile(ksession, caseId);
CaseEventSupport caseEventSupport = getCaseEventSupport(context);
caseEventSupport.fireBeforeDynamicProcessAdded(caseId, caseFile, processInstanceId, processId, parameters);
long subProcessInstanceId = DynamicUtils.addDynamicSubProcess(dynamicContext, ksession, processId, parameters);
if (subProcessInstanceId < 0) {
throw new ProcessDefinitionNotFoundException("No process definition found with id: " + processId);
}
caseEventSupport.fireAfterDynamicProcessAdded(caseId, caseFile, processInstanceId, processId, parameters, subProcessInstanceId);
return subProcessInstanceId;
} catch (IllegalArgumentException e) {
throw new ProcessDefinitionNotFoundException(e.getMessage());
}
}
use of org.jbpm.workflow.instance.node.DynamicNodeInstance in project jbpm by kiegroup.
the class WorkflowProcessInstanceImpl method signalEvent.
@SuppressWarnings("unchecked")
public void signalEvent(String type, Object event) {
logger.debug("Signal {} received with data {} in process instance {}", type, event, getId());
synchronized (this) {
if (getState() != ProcessInstance.STATE_ACTIVE) {
return;
}
if ("timerTriggered".equals(type)) {
TimerInstance timer = (TimerInstance) event;
if (timer.getId() == slaTimerId) {
handleSLAViolation();
// no need to pass the event along as it was purely for SLA tracking
return;
}
}
if ("slaViolation".equals(type)) {
handleSLAViolation();
// no need to pass the event along as it was purely for SLA tracking
return;
}
List<NodeInstance> currentView = new ArrayList<NodeInstance>(this.nodeInstances);
try {
this.activatingNodeIds = new ArrayList<String>();
List<EventListener> listeners = eventListeners.get(type);
if (listeners != null) {
for (EventListener listener : listeners) {
listener.signalEvent(type, event);
}
}
listeners = externalEventListeners.get(type);
if (listeners != null) {
for (EventListener listener : listeners) {
listener.signalEvent(type, event);
}
}
for (Node node : getWorkflowProcess().getNodes()) {
if (node instanceof EventNodeInterface) {
if (((EventNodeInterface) node).acceptsEvent(type, event, (e) -> resolveVariable(e))) {
if (node instanceof EventNode && ((EventNode) node).getFrom() == null) {
EventNodeInstance eventNodeInstance = (EventNodeInstance) getNodeInstance(node);
eventNodeInstance.signalEvent(type, event);
} else {
if (node instanceof EventSubProcessNode && ((resolveVariables(((EventSubProcessNode) node).getEvents()).contains(type)))) {
EventSubProcessNodeInstance eventNodeInstance = (EventSubProcessNodeInstance) getNodeInstance(node);
eventNodeInstance.signalEvent(type, event);
}
if (node instanceof DynamicNode && type.equals(((DynamicNode) node).getActivationEventName())) {
DynamicNodeInstance dynamicNodeInstance = (DynamicNodeInstance) getNodeInstance(node);
dynamicNodeInstance.signalEvent(type, event);
} else {
List<NodeInstance> nodeInstances = getNodeInstances(node.getId(), currentView);
if (nodeInstances != null && !nodeInstances.isEmpty()) {
for (NodeInstance nodeInstance : nodeInstances) {
((EventNodeInstanceInterface) nodeInstance).signalEvent(type, event);
}
}
}
}
}
}
}
if (((org.jbpm.workflow.core.WorkflowProcess) getWorkflowProcess()).isDynamic()) {
for (Node node : getWorkflowProcess().getNodes()) {
if (type.equals(node.getName()) && node.getIncomingConnections().isEmpty()) {
NodeInstance nodeInstance = getNodeInstance(node);
if (event != null) {
Map<String, Object> dynamicParams = new HashMap<>();
if (event instanceof Map) {
dynamicParams.putAll((Map<String, Object>) event);
} else {
dynamicParams.put("Data", event);
}
((org.jbpm.workflow.instance.NodeInstance) nodeInstance).setDynamicParameters(dynamicParams);
}
((org.jbpm.workflow.instance.NodeInstance) nodeInstance).trigger(null, NodeImpl.CONNECTION_DEFAULT_TYPE);
}
}
}
} finally {
if (this.activatingNodeIds != null) {
this.activatingNodeIds.clear();
this.activatingNodeIds = null;
}
}
}
}
use of org.jbpm.workflow.instance.node.DynamicNodeInstance in project jbpm by kiegroup.
the class RequestUI method addSubProcessInstance.
private void addSubProcessInstance() {
DynamicNodeInstance dynamicNodeInstance = (DynamicNodeInstance) processInstance.getNodeInstances().iterator().next();
DynamicUtils.addDynamicSubProcess(dynamicNodeInstance, ksession, "com.sample.contactCustomer", null);
}
use of org.jbpm.workflow.instance.node.DynamicNodeInstance 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();
}
}
Aggregations