Search in sources :

Example 6 with DynamicNodeInstance

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

Example 7 with DynamicNodeInstance

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());
    }
}
Also used : IdentityProvider(org.kie.internal.identity.IdentityProvider) RegistryContext(org.drools.core.command.impl.RegistryContext) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) ProcessDefinitionNotFoundException(org.jbpm.services.api.ProcessDefinitionNotFoundException) ProcessInstanceNotFoundException(org.jbpm.services.api.ProcessInstanceNotFoundException) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) Context(org.kie.api.runtime.Context) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) 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) ProcessDefinitionNotFoundException(org.jbpm.services.api.ProcessDefinitionNotFoundException) ProcessInstanceNotFoundException(org.jbpm.services.api.ProcessInstanceNotFoundException)

Example 8 with DynamicNodeInstance

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;
            }
        }
    }
}
Also used : DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) TimerInstance(org.jbpm.process.instance.timer.TimerInstance) HashMap(java.util.HashMap) EventNodeInterface(org.jbpm.workflow.core.node.EventNodeInterface) EventSubProcessNode(org.jbpm.workflow.core.node.EventSubProcessNode) DynamicNode(org.jbpm.workflow.core.node.DynamicNode) AsyncEventNode(org.jbpm.workflow.core.node.AsyncEventNode) StateBasedNode(org.jbpm.workflow.core.node.StateBasedNode) EventSubProcessNode(org.jbpm.workflow.core.node.EventSubProcessNode) ActionNode(org.jbpm.workflow.core.node.ActionNode) EndNode(org.jbpm.workflow.core.node.EndNode) EventNode(org.jbpm.workflow.core.node.EventNode) Node(org.kie.api.definition.process.Node) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) EventSubProcessNodeInstance(org.jbpm.workflow.instance.node.EventSubProcessNodeInstance) AsyncEventNode(org.jbpm.workflow.core.node.AsyncEventNode) EventNode(org.jbpm.workflow.core.node.EventNode) EventNodeInstanceInterface(org.jbpm.workflow.instance.node.EventNodeInstanceInterface) DynamicNode(org.jbpm.workflow.core.node.DynamicNode) EventListener(org.kie.api.runtime.process.EventListener) EventSubProcessNodeInstance(org.jbpm.workflow.instance.node.EventSubProcessNodeInstance) DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) EndNodeInstance(org.jbpm.workflow.instance.node.EndNodeInstance) NodeInstance(org.jbpm.workflow.instance.NodeInstance) EventNodeInstance(org.jbpm.workflow.instance.node.EventNodeInstance) CompositeNodeInstance(org.jbpm.workflow.instance.node.CompositeNodeInstance) EventNodeInstance(org.jbpm.workflow.instance.node.EventNodeInstance) WorkflowProcess(org.kie.api.definition.process.WorkflowProcess) Map(java.util.Map) HashMap(java.util.HashMap)

Example 9 with DynamicNodeInstance

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);
}
Also used : DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance)

Example 10 with DynamicNodeInstance

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

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