Search in sources :

Example 1 with NodeInstanceImpl

use of org.jbpm.workflow.instance.impl.NodeInstanceImpl in project jbpm by kiegroup.

the class CancelNodeInstanceCommand method execute.

public Void execute(Context context) {
    logger.debug("About to cancel node instance with id {} on process instance {}", nodeInstanceId, processInstanceId);
    KieSession kieSession = ((RegistryContext) context).lookup(KieSession.class);
    RuleFlowProcessInstance wfp = (RuleFlowProcessInstance) kieSession.getProcessInstance(processInstanceId, false);
    if (wfp == null) {
        throw new ProcessInstanceNotFoundException("Process instance with id " + processInstanceId + " not found");
    }
    NodeInstance nodeInstance = wfp.getNodeInstances(true).stream().filter(ni -> ni.getId() == nodeInstanceId).findFirst().orElse(null);
    if (nodeInstance == null) {
        throw new NodeInstanceNotFoundException("Node instance with id " + nodeInstanceId + " not found");
    }
    logger.debug("Found node instance {} to be canceled", nodeInstance);
    ((NodeInstanceImpl) nodeInstance).cancel();
    logger.debug("Node instance {} canceled successfully", nodeInstance);
    return null;
}
Also used : NodeInstanceImpl(org.jbpm.workflow.instance.impl.NodeInstanceImpl) RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) NodeInstanceNotFoundException(org.jbpm.services.api.NodeInstanceNotFoundException) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext) ProcessInstanceNotFoundException(org.jbpm.services.api.ProcessInstanceNotFoundException) NodeInstance(org.kie.api.runtime.process.NodeInstance)

Example 2 with NodeInstanceImpl

use of org.jbpm.workflow.instance.impl.NodeInstanceImpl in project jbpm by kiegroup.

the class FlowTest method testInclusiveJoinWithLoopAndHumanTasks.

@Test
public void testInclusiveJoinWithLoopAndHumanTasks() throws Exception {
    KieBase kbase = createKnowledgeBase("BPMN2-InclusiveGatewayWithHumanTasksProcess.bpmn2");
    ksession = createKnowledgeSession(kbase);
    final Map<String, Integer> nodeInstanceExecutionCounter = new HashMap<String, Integer>();
    ksession.addEventListener(new DefaultProcessEventListener() {

        @Override
        public void beforeNodeTriggered(ProcessNodeTriggeredEvent event) {
            logger.info("{} {}", event.getNodeInstance().getNodeName(), ((NodeInstanceImpl) event.getNodeInstance()).getLevel());
            Integer value = nodeInstanceExecutionCounter.get(event.getNodeInstance().getNodeName());
            if (value == null) {
                value = new Integer(0);
            }
            value++;
            nodeInstanceExecutionCounter.put(event.getNodeInstance().getNodeName(), value);
        }
    });
    TestWorkItemHandler handler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("firstXor", true);
    params.put("secondXor", true);
    params.put("thirdXor", true);
    ProcessInstance processInstance = ksession.startProcess("InclusiveWithAdvancedLoop", params);
    // simulate completion of first task
    assertProcessInstanceActive(processInstance);
    ksession.getWorkItemManager().completeWorkItem(handler.getWorkItem().getId(), null);
    assertProcessInstanceActive(processInstance);
    List<WorkItem> workItems = handler.getWorkItems();
    assertNotNull(workItems);
    assertEquals(2, workItems.size());
    WorkItem remainingWork = null;
    for (WorkItem wi : workItems) {
        assertProcessInstanceActive(processInstance);
        // complete second task that will trigger converging OR gateway
        if (wi.getParameter("NodeName").equals("HT Form2")) {
            ksession.getWorkItemManager().completeWorkItem(wi.getId(), null);
        } else {
            remainingWork = wi;
        }
    }
    assertProcessInstanceActive(processInstance);
    ksession.getWorkItemManager().completeWorkItem(remainingWork.getId(), null);
    assertProcessInstanceActive(processInstance);
    ksession.getWorkItemManager().completeWorkItem(handler.getWorkItem().getId(), null);
    assertProcessInstanceCompleted(processInstance);
    assertEquals(13, nodeInstanceExecutionCounter.size());
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("Start"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("HT Form1"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("and1"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("HT Form2"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("xor1"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("xor2"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("HT Form3"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("Koniec"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("xor 3"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("HT Form4"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("xor4"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("Koniec2"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("or1"));
}
Also used : NodeInstanceImpl(org.jbpm.workflow.instance.impl.NodeInstanceImpl) TestWorkItemHandler(org.jbpm.bpmn2.objects.TestWorkItemHandler) HashMap(java.util.HashMap) WorkItem(org.kie.api.runtime.process.WorkItem) KieBase(org.kie.api.KieBase) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) ProcessNodeTriggeredEvent(org.kie.api.event.process.ProcessNodeTriggeredEvent) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) Test(org.junit.Test)

Example 3 with NodeInstanceImpl

use of org.jbpm.workflow.instance.impl.NodeInstanceImpl in project jbpm by kiegroup.

the class CreateNewNodeFactory method getNodeInstance.

public NodeInstance getNodeInstance(Node node, WorkflowProcessInstance processInstance, NodeInstanceContainer nodeInstanceContainer) {
    try {
        NodeInstanceImpl nodeInstance = (NodeInstanceImpl) this.cls.newInstance();
        nodeInstance.setNodeId(node.getId());
        nodeInstance.setNodeInstanceContainer(nodeInstanceContainer);
        nodeInstance.setProcessInstance(processInstance);
        String uniqueId = (String) node.getMetaData().get("UniqueId");
        assert uniqueId != null : node.getClass().getSimpleName() + " [" + node.getName() + "] does not have a unique id.";
        if (uniqueId == null) {
            uniqueId = node.getId() + "";
        }
        nodeInstance.setMetaData("UniqueId", uniqueId);
        int level = ((org.jbpm.workflow.instance.NodeInstanceContainer) nodeInstanceContainer).getLevelForNode(uniqueId);
        nodeInstance.setLevel(level);
        return nodeInstance;
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Unable to instantiate node: '" + this.cls.getName() + "':" + e.getMessage());
    }
}
Also used : NodeInstanceImpl(org.jbpm.workflow.instance.impl.NodeInstanceImpl) NodeInstanceContainer(org.kie.api.runtime.process.NodeInstanceContainer)

Example 4 with NodeInstanceImpl

use of org.jbpm.workflow.instance.impl.NodeInstanceImpl in project jbpm by kiegroup.

the class ReuseNodeFactory method getNodeInstance.

public NodeInstance getNodeInstance(Node node, WorkflowProcessInstance processInstance, NodeInstanceContainer nodeInstanceContainer) {
    NodeInstance result = ((org.jbpm.workflow.instance.NodeInstanceContainer) nodeInstanceContainer).getFirstNodeInstance(node.getId());
    if (result != null) {
        return result;
    }
    try {
        NodeInstanceImpl nodeInstance = (NodeInstanceImpl) cls.newInstance();
        nodeInstance.setNodeId(node.getId());
        nodeInstance.setNodeInstanceContainer(nodeInstanceContainer);
        nodeInstance.setProcessInstance(processInstance);
        String uniqueId = (String) node.getMetaData().get("UniqueId");
        assert uniqueId != null : node.getName() + " does not have a unique id.";
        if (uniqueId == null) {
            uniqueId = node.getId() + "";
        }
        nodeInstance.setMetaData("UniqueId", uniqueId);
        int level = ((org.jbpm.workflow.instance.NodeInstanceContainer) nodeInstanceContainer).getLevelForNode(uniqueId);
        nodeInstance.setLevel(level);
        return nodeInstance;
    } catch (Exception e) {
        throw new RuntimeException("Unable to instantiate node '" + this.cls.getName() + "': " + e.getMessage());
    }
}
Also used : NodeInstanceImpl(org.jbpm.workflow.instance.impl.NodeInstanceImpl) NodeInstanceContainer(org.kie.api.runtime.process.NodeInstanceContainer) NodeInstance(org.kie.api.runtime.process.NodeInstance)

Example 5 with NodeInstanceImpl

use of org.jbpm.workflow.instance.impl.NodeInstanceImpl in project jbpm by kiegroup.

the class AbstractProcessInstanceMarshaller method readNodeInstanceContent.

protected NodeInstanceImpl readNodeInstanceContent(int nodeType, ObjectInputStream stream, MarshallerReaderContext context, WorkflowProcessInstance processInstance) throws IOException {
    NodeInstanceImpl nodeInstance = null;
    switch(nodeType) {
        case PersisterEnums.RULE_SET_NODE_INSTANCE:
            nodeInstance = new RuleSetNodeInstance();
            int nbTimerInstances = stream.readInt();
            if (nbTimerInstances > 0) {
                List<Long> timerInstances = new ArrayList<Long>();
                for (int i = 0; i < nbTimerInstances; i++) {
                    timerInstances.add(stream.readLong());
                }
                ((RuleSetNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
            }
            break;
        case PersisterEnums.HUMAN_TASK_NODE_INSTANCE:
            nodeInstance = new HumanTaskNodeInstance();
            ((HumanTaskNodeInstance) nodeInstance).internalSetWorkItemId(stream.readLong());
            nbTimerInstances = stream.readInt();
            if (nbTimerInstances > 0) {
                List<Long> timerInstances = new ArrayList<Long>();
                for (int i = 0; i < nbTimerInstances; i++) {
                    timerInstances.add(stream.readLong());
                }
                ((HumanTaskNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
            }
            break;
        case PersisterEnums.WORK_ITEM_NODE_INSTANCE:
            nodeInstance = new WorkItemNodeInstance();
            ((WorkItemNodeInstance) nodeInstance).internalSetWorkItemId(stream.readLong());
            nbTimerInstances = stream.readInt();
            if (nbTimerInstances > 0) {
                List<Long> timerInstances = new ArrayList<Long>();
                for (int i = 0; i < nbTimerInstances; i++) {
                    timerInstances.add(stream.readLong());
                }
                ((WorkItemNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
            }
            break;
        case PersisterEnums.SUB_PROCESS_NODE_INSTANCE:
            nodeInstance = new SubProcessNodeInstance();
            ((SubProcessNodeInstance) nodeInstance).internalSetProcessInstanceId(stream.readLong());
            nbTimerInstances = stream.readInt();
            if (nbTimerInstances > 0) {
                List<Long> timerInstances = new ArrayList<Long>();
                for (int i = 0; i < nbTimerInstances; i++) {
                    timerInstances.add(stream.readLong());
                }
                ((SubProcessNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
            }
            break;
        case PersisterEnums.MILESTONE_NODE_INSTANCE:
            nodeInstance = new MilestoneNodeInstance();
            nbTimerInstances = stream.readInt();
            if (nbTimerInstances > 0) {
                List<Long> timerInstances = new ArrayList<Long>();
                for (int i = 0; i < nbTimerInstances; i++) {
                    timerInstances.add(stream.readLong());
                }
                ((MilestoneNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
            }
            break;
        case PersisterEnums.TIMER_NODE_INSTANCE:
            nodeInstance = new TimerNodeInstance();
            ((TimerNodeInstance) nodeInstance).internalSetTimerId(stream.readLong());
            break;
        case PersisterEnums.EVENT_NODE_INSTANCE:
            nodeInstance = new EventNodeInstance();
            break;
        case PersisterEnums.JOIN_NODE_INSTANCE:
            nodeInstance = new JoinInstance();
            int number = stream.readInt();
            if (number > 0) {
                Map<Long, Integer> triggers = new HashMap<Long, Integer>();
                for (int i = 0; i < number; i++) {
                    long l = stream.readLong();
                    int count = stream.readInt();
                    triggers.put(l, count);
                }
                ((JoinInstance) nodeInstance).internalSetTriggers(triggers);
            }
            break;
        case PersisterEnums.COMPOSITE_NODE_INSTANCE:
            nodeInstance = new CompositeContextNodeInstance();
            nbTimerInstances = stream.readInt();
            if (nbTimerInstances > 0) {
                List<Long> timerInstances = new ArrayList<Long>();
                for (int i = 0; i < nbTimerInstances; i++) {
                    timerInstances.add(stream.readLong());
                }
                ((CompositeContextNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
            }
            break;
        case PersisterEnums.FOR_EACH_NODE_INSTANCE:
            nodeInstance = new ForEachNodeInstance();
            break;
        case PersisterEnums.DYNAMIC_NODE_INSTANCE:
            nodeInstance = new DynamicNodeInstance();
            nbTimerInstances = stream.readInt();
            if (nbTimerInstances > 0) {
                List<Long> timerInstances = new ArrayList<Long>();
                for (int i = 0; i < nbTimerInstances; i++) {
                    timerInstances.add(stream.readLong());
                }
                ((CompositeContextNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
            }
            break;
        case PersisterEnums.STATE_NODE_INSTANCE:
            nodeInstance = new StateNodeInstance();
            nbTimerInstances = stream.readInt();
            if (nbTimerInstances > 0) {
                List<Long> timerInstances = new ArrayList<Long>();
                for (int i = 0; i < nbTimerInstances; i++) {
                    timerInstances.add(stream.readLong());
                }
                ((CompositeContextNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
            }
            break;
        default:
            throw new IllegalArgumentException("Unknown node type: " + nodeType);
    }
    return nodeInstance;
}
Also used : NodeInstanceImpl(org.jbpm.workflow.instance.impl.NodeInstanceImpl) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Aggregations

NodeInstanceImpl (org.jbpm.workflow.instance.impl.NodeInstanceImpl)20 NodeInstance (org.kie.api.runtime.process.NodeInstance)9 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)7 WorkflowProcessInstance (org.kie.api.runtime.process.WorkflowProcessInstance)7 HashMap (java.util.HashMap)6 TestWorkItemHandler (org.jbpm.bpmn2.objects.TestWorkItemHandler)4 NodeInstanceLog (org.jbpm.process.audit.NodeInstanceLog)4 SubProcessNodeInstance (org.jbpm.workflow.instance.node.SubProcessNodeInstance)4 WorkItemNodeInstance (org.jbpm.workflow.instance.node.WorkItemNodeInstance)4 Test (org.junit.Test)4 KieBase (org.kie.api.KieBase)4 Node (org.kie.api.definition.process.Node)4 NodeImpl (org.jbpm.workflow.core.impl.NodeImpl)3 CompositeContextNodeInstance (org.jbpm.workflow.instance.node.CompositeContextNodeInstance)3 ForEachNodeInstance (org.jbpm.workflow.instance.node.ForEachNodeInstance)3 HumanTaskNodeInstance (org.jbpm.workflow.instance.node.HumanTaskNodeInstance)3 TimerNodeInstance (org.jbpm.workflow.instance.node.TimerNodeInstance)3 DefaultProcessEventListener (org.kie.api.event.process.DefaultProcessEventListener)3 ProcessNodeTriggeredEvent (org.kie.api.event.process.ProcessNodeTriggeredEvent)3 KieSession (org.kie.api.runtime.KieSession)3