Search in sources :

Example 1 with DynamicNodeInstance

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;
}
Also used : IdentityProvider(org.kie.internal.identity.IdentityProvider) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) RegistryContext(org.drools.core.command.impl.RegistryContext) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) Context(org.kie.api.runtime.Context) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) Map(java.util.Map) DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) KieSession(org.kie.api.runtime.KieSession) StageNotFoundException(org.jbpm.casemgmt.api.StageNotFoundException) DynamicUtils(org.jbpm.workflow.instance.node.DynamicUtils) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) StageNotFoundException(org.jbpm.casemgmt.api.StageNotFoundException) DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext) ProcessInstance(org.kie.api.runtime.process.ProcessInstance)

Example 2 with DynamicNodeInstance

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;
}
Also used : IdentityProvider(org.kie.internal.identity.IdentityProvider) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) RegistryContext(org.drools.core.command.impl.RegistryContext) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) Context(org.kie.api.runtime.Context) DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) KieSession(org.kie.api.runtime.KieSession) StageNotFoundException(org.jbpm.casemgmt.api.StageNotFoundException) StageNotFoundException(org.jbpm.casemgmt.api.StageNotFoundException) DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext) ProcessInstance(org.kie.api.runtime.process.ProcessInstance)

Example 3 with DynamicNodeInstance

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);
}
Also used : TestWorkItemHandler(org.jbpm.bpmn2.objects.TestWorkItemHandler) DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) KieBase(org.kie.api.KieBase) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) WorkItem(org.kie.api.runtime.process.WorkItem) Test(org.junit.Test)

Example 4 with DynamicNodeInstance

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();
}
Also used : TestWorkItemHandler(org.jbpm.bpmn2.objects.TestWorkItemHandler) DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) KieBase(org.kie.api.KieBase) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) WorkItem(org.kie.api.runtime.process.WorkItem) Test(org.junit.Test)

Example 5 with DynamicNodeInstance

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();
}
Also used : TestWorkItemHandler(org.jbpm.integrationtests.handler.TestWorkItemHandler) DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) HashMap(java.util.HashMap) Reader(java.io.Reader) StringReader(java.io.StringReader) KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieBase(org.kie.api.KieBase) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.jbpm.process.instance.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Aggregations

DynamicNodeInstance (org.jbpm.workflow.instance.node.DynamicNodeInstance)14 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)8 WorkflowProcessInstance (org.kie.api.runtime.process.WorkflowProcessInstance)8 KieSession (org.kie.api.runtime.KieSession)7 HashMap (java.util.HashMap)6 Test (org.junit.Test)6 KieBase (org.kie.api.KieBase)5 Map (java.util.Map)4 WorkItem (org.kie.api.runtime.process.WorkItem)4 ArrayList (java.util.ArrayList)3 RegistryContext (org.drools.core.command.impl.RegistryContext)3 TestWorkItemHandler (org.jbpm.bpmn2.objects.TestWorkItemHandler)3 StageNotFoundException (org.jbpm.casemgmt.api.StageNotFoundException)3 EventNodeInstance (org.jbpm.workflow.instance.node.EventNodeInstance)3 EventSubProcessNodeInstance (org.jbpm.workflow.instance.node.EventSubProcessNodeInstance)3 KieRuntimeLogger (org.kie.api.logger.KieRuntimeLogger)3 FactHandle (org.kie.api.runtime.rule.FactHandle)3 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)2