use of org.jbpm.workflow.instance.node.DynamicNodeInstance in project jbpm by kiegroup.
the class AddDynamicTaskToStageCommand method execute.
@Override
public Void execute(Context context) {
KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
ProcessInstance processInstance = ksession.getProcessInstance(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);
}
CaseFileInstance caseFile = getCaseFile(ksession, caseId);
CaseEventSupport caseEventSupport = getCaseEventSupport(context);
caseEventSupport.fireBeforeDynamicTaskAdded(caseId, caseFile, processInstanceId, nodeType, parameters);
DynamicUtils.addDynamicWorkItem(dynamicContext, ksession, nodeType, parameters);
caseEventSupport.fireAfterDynamicTaskAdded(caseId, caseFile, processInstanceId, nodeType, parameters);
return null;
}
use of org.jbpm.workflow.instance.node.DynamicNodeInstance in project jbpm by kiegroup.
the class TriggerAdHocNodeInStageCommand method execute.
@Override
public Void execute(Context context) {
KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
ProcessInstance processInstance = ksession.getProcessInstance(processInstanceId);
DynamicNodeInstance dynamicContext = (DynamicNodeInstance) ((WorkflowProcessInstanceImpl) processInstance).getNodeInstances(true).stream().filter(ni -> (ni instanceof DynamicNodeInstance) && stageId.equals(ni.getNode().getMetaData().get("UniqueId"))).findFirst().orElseThrow(() -> new StageNotFoundException("No stage found with id " + stageId));
dynamicContext.signalEvent(fragmentName, data);
return null;
}
use of org.jbpm.workflow.instance.node.DynamicNodeInstance in project jbpm by kiegroup.
the class ActivityTest method testAdHocSubProcessAutoCompleteDynamicSubProcess.
@Test
public void testAdHocSubProcessAutoCompleteDynamicSubProcess() throws Exception {
KieBase kbase = createKnowledgeBaseWithoutDumper("BPMN2-AdHocSubProcessAutoComplete.bpmn2", "BPMN2-AdHocSubProcess.drl", "BPMN2-MinimalProcess.bpmn2");
ksession = createKnowledgeSession(kbase);
TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
TestWorkItemHandler workItemHandler2 = new TestWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("OtherTask", workItemHandler2);
ProcessInstance processInstance = ksession.startProcess("AdHocSubProcess");
assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
ksession.fireAllRules();
DynamicNodeInstance dynamicContext = (DynamicNodeInstance) ((WorkflowProcessInstance) processInstance).getNodeInstances().iterator().next();
DynamicUtils.addDynamicSubProcess(dynamicContext, ksession, "Minimal", new HashMap<String, Object>());
ksession = restoreSession(ksession, true);
WorkItem workItem = workItemHandler.getWorkItem();
assertNotNull(workItem);
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
// assertProcessInstanceActive(processInstance.getId(), ksession);
// workItem = workItemHandler2.getWorkItem();
// ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
assertProcessInstanceFinished(processInstance, ksession);
}
use of org.jbpm.workflow.instance.node.DynamicNodeInstance in project jbpm by kiegroup.
the class ActivityTest method testAdHocSubProcessAutoCompleteDynamicTask.
@Test
public void testAdHocSubProcessAutoCompleteDynamicTask() throws Exception {
KieBase kbase = createKnowledgeBaseWithoutDumper("BPMN2-AdHocSubProcessAutoComplete.bpmn2", "BPMN2-AdHocSubProcess.drl");
ksession = createKnowledgeSession(kbase);
TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
TestWorkItemHandler workItemHandler2 = new TestWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("OtherTask", workItemHandler2);
ProcessInstance processInstance = ksession.startProcess("AdHocSubProcess");
assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
DynamicNodeInstance dynamicContext = (DynamicNodeInstance) ((WorkflowProcessInstance) processInstance).getNodeInstances().iterator().next();
DynamicUtils.addDynamicWorkItem(dynamicContext, ksession, "OtherTask", new HashMap<String, Object>());
WorkItem workItem = workItemHandler.getWorkItem();
assertNull(workItem);
ksession = restoreSession(ksession, true);
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
ksession.fireAllRules();
workItem = workItemHandler.getWorkItem();
assertNotNull(workItem);
ksession = restoreSession(ksession, true);
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
assertProcessInstanceActive(processInstance);
workItem = workItemHandler2.getWorkItem();
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
assertProcessInstanceFinished(processInstance, ksession);
ksession.dispose();
}
use of org.jbpm.workflow.instance.node.DynamicNodeInstance in project jbpm by kiegroup.
the class ProcessDynamicNodeTest method testAddDynamicSubProcess.
@Test
public void testAddDynamicSubProcess() {
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>");
Reader source2 = 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=\"subflow\" id=\"org.drools.subflow\" package-name=\"org.drools\" >\n" + "\n" + " <header>\n" + " <variables>\n" + " <variable name=\"x\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>SomeText</value>\n" + " </variable>\n" + " </variables>\n" + " </header>\n" + "\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <actionNode id=\"2\" name=\"Action\" >\n" + " <action type=\"expression\" dialect=\"mvel\" >System.out.println(x);</action>\n" + " </actionNode>\n" + " <workItem id=\"3\" name=\"Work\" >\n" + " <work name=\"Human Task\" />\n" + " </workItem>\n" + " <end id=\"4\" name=\"End\" />\n" + " </nodes>\n" + "\n" + " <connections>\n" + " <connection from=\"1\" to=\"2\" />\n" + " <connection from=\"2\" to=\"3\" />\n" + " <connection from=\"3\" to=\"4\" />\n" + " </connections>\n" + "</process>");
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newReaderResource(source), ResourceType.DRF);
kbuilder.add(ResourceFactory.newReaderResource(source2), 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("x", "NewValue");
assertNull(handler.getWorkItem());
assertEquals(0, dynamicContext.getNodeInstances().size());
DynamicUtils.addDynamicSubProcess(dynamicContext, ksession, "org.drools.subflow", parameters);
assertNotNull(handler.getWorkItem());
assertEquals(1, dynamicContext.getNodeInstances().size());
logger.close();
}
Aggregations