Search in sources :

Example 1 with StateNodeInstance

use of org.jbpm.workflow.instance.node.StateNodeInstance in project jbpm by kiegroup.

the class ProcessStateTest method testManualSignalState.

@Test
public void testManualSignalState() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    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=\"flow\" id=\"org.drools.state\" package-name=\"org.drools\" version=\"1\" >\n" + "\n" + "  <header>\n" + "  </header>\n" + "\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <state id=\"2\" name=\"StateA\" >\n" + "      <constraints>\n" + "        <constraint toNodeId=\"3\" name=\"toB\" />\n" + "       <constraint toNodeId=\"4\" name=\"toC\" />\n" + "      </constraints>\n" + "    </state>\n" + "    <state id=\"3\" name=\"StateB\" />\n" + "    <state id=\"4\" name=\"StateC\" />\n" + "    <end id=\"5\" name=\"End\" />\n" + "  </nodes>\n" + "\n" + "  <connections>\n" + "    <connection from=\"1\" to=\"2\" />\n" + "    <connection from=\"2\" to=\"3\" />\n" + "    <connection from=\"2\" to=\"4\" />\n" + "    <connection from=\"3\" to=\"2\" />\n" + "    <connection from=\"4\" to=\"5\" />\n" + "  </connections>\n" + "\n" + "</process>");
    kbuilder.add(ResourceFactory.newReaderResource(source), ResourceType.DRF);
    KieBase kbase = kbuilder.newKieBase();
    KieSession ksession = kbase.newKieSession();
    // start process
    WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.startProcess("org.drools.state");
    // should be in state A
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    Collection<NodeInstance> nodeInstances = processInstance.getNodeInstances();
    assertEquals(1, nodeInstances.size());
    StateNodeInstance stateInstance = (StateNodeInstance) nodeInstances.iterator().next();
    assertEquals("StateA", stateInstance.getNodeName());
    // signal "toB" so we move to state B
    processInstance.signalEvent("signal", "toB");
    nodeInstances = processInstance.getNodeInstances();
    assertEquals(1, nodeInstances.size());
    stateInstance = (StateNodeInstance) nodeInstances.iterator().next();
    assertEquals("StateB", stateInstance.getNodeName());
    // if no constraint specified for a connection,
    // we default to the name of the target node
    // signal "StateA", so we move back to state A
    processInstance.signalEvent("signal", "StateA");
    nodeInstances = processInstance.getNodeInstances();
    assertEquals(1, nodeInstances.size());
    stateInstance = (StateNodeInstance) nodeInstances.iterator().next();
    assertEquals("StateA", stateInstance.getNodeName());
    // signal "toC" so we move to state C
    processInstance.signalEvent("signal", "toC");
    nodeInstances = processInstance.getNodeInstances();
    assertEquals(1, nodeInstances.size());
    stateInstance = (StateNodeInstance) nodeInstances.iterator().next();
    assertEquals("StateC", stateInstance.getNodeName());
    // signal something completely wrong, this should simply be ignored
    processInstance.signalEvent("signal", "Invalid");
    nodeInstances = processInstance.getNodeInstances();
    assertEquals(1, nodeInstances.size());
    stateInstance = (StateNodeInstance) nodeInstances.iterator().next();
    assertEquals("StateC", stateInstance.getNodeName());
    // signal "End", so we move to the end
    processInstance.signalEvent("signal", "End");
    nodeInstances = processInstance.getNodeInstances();
    assertEquals(0, nodeInstances.size());
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
}
Also used : StateNodeInstance(org.jbpm.workflow.instance.node.StateNodeInstance) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieBase(org.kie.api.KieBase) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) NodeInstance(org.kie.api.runtime.process.NodeInstance) StateNodeInstance(org.jbpm.workflow.instance.node.StateNodeInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 2 with StateNodeInstance

use of org.jbpm.workflow.instance.node.StateNodeInstance in project jbpm by kiegroup.

the class ProcessStateTest method testTimerState.

@Test
public void testTimerState() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    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=\"flow\" id=\"org.drools.state\" package-name=\"org.drools\" version=\"1\" >\n" + "\n" + "  <header>\n" + "    <globals>\n" + "      <global identifier=\"list\" type=\"java.util.List\" />\n" + "    </globals>\n" + "    <variables>\n" + "      <variable name=\"s\" >\n" + "        <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "        <value>a</value>\n" + "      </variable>\n" + "    </variables>\n" + "  </header>\n" + "\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <state id=\"2\" name=\"State\" >\n" + "      <timers>\n" + "        <timer id=\"1\" delay=\"1s\" period=\"2s\" >\n" + "          <action type=\"expression\" dialect=\"mvel\" >list.add(\"Timer1\" + s);</action>\n" + "        </timer>\n" + "        <timer id=\"2\" delay=\"1s\" period=\"2s\" >\n" + "          <action type=\"expression\" dialect=\"mvel\" >list.add(\"Timer2\" + s);</action>\n" + "        </timer>\n" + "      </timers>\n" + "    </state>\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" + "\n" + "</process>");
    kbuilder.add(ResourceFactory.newReaderResource(source), ResourceType.DRF);
    KieBase kbase = kbuilder.newKieBase();
    final KieSession ksession = kbase.newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    new Thread(new Runnable() {

        public void run() {
            ksession.fireUntilHalt();
        }
    }).start();
    // start process
    WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.startProcess("org.drools.state");
    // should be in state A
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    Collection<NodeInstance> nodeInstances = processInstance.getNodeInstances();
    assertEquals(1, nodeInstances.size());
    StateNodeInstance stateInstance = (StateNodeInstance) nodeInstances.iterator().next();
    assertEquals("State", stateInstance.getNodeName());
    assertEquals(0, list.size());
    try {
        Thread.sleep(4000);
    } catch (InterruptedException e) {
    }
    assertEquals(4, list.size());
    assertTrue(list.contains("Timer1a"));
    assertTrue(list.contains("Timer2a"));
    processInstance.signalEvent("signal", "End");
    nodeInstances = processInstance.getNodeInstances();
    assertEquals(0, nodeInstances.size());
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
    assertEquals(4, list.size());
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
    }
    assertEquals(4, list.size());
    ksession.halt();
}
Also used : StateNodeInstance(org.jbpm.workflow.instance.node.StateNodeInstance) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieBase(org.kie.api.KieBase) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) NodeInstance(org.kie.api.runtime.process.NodeInstance) StateNodeInstance(org.jbpm.workflow.instance.node.StateNodeInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 3 with StateNodeInstance

use of org.jbpm.workflow.instance.node.StateNodeInstance in project jbpm by kiegroup.

the class ProcessStateTest method testActionState.

@Test
public void testActionState() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    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=\"flow\" id=\"org.drools.state\" package-name=\"org.drools\" version=\"1\" >\n" + "\n" + "  <header>\n" + "    <globals>\n" + "      <global identifier=\"list\" type=\"java.util.List\" />\n" + "    </globals>\n" + "    <variables>\n" + "      <variable name=\"s\" >\n" + "        <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "        <value>a</value>\n" + "      </variable>\n" + "    </variables>\n" + "  </header>\n" + "\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <state id=\"2\" name=\"State\" >\n" + "      <onEntry>" + "        <action type=\"expression\" dialect=\"mvel\" >list.add(\"Action1\" + s);</action>\n" + "        <action type=\"expression\" dialect=\"java\" >list.add(\"Action2\" + s);</action>\n" + "      </onEntry>\n" + "      <onExit>\n" + "        <action type=\"expression\" dialect=\"mvel\" >list.add(\"Action3\" + s);</action>\n" + "        <action type=\"expression\" dialect=\"java\" >list.add(\"Action4\" + s);</action>\n" + "      </onExit>\n" + "    </state>\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" + "\n" + "</process>");
    kbuilder.add(ResourceFactory.newReaderResource(source), ResourceType.DRF);
    KieBase kbase = kbuilder.newKieBase();
    KieSession ksession = kbase.newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    // start process
    WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.startProcess("org.drools.state");
    // should be in state A
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    Collection<NodeInstance> nodeInstances = processInstance.getNodeInstances();
    assertEquals(1, nodeInstances.size());
    StateNodeInstance stateInstance = (StateNodeInstance) nodeInstances.iterator().next();
    assertEquals("State", stateInstance.getNodeName());
    assertEquals(2, list.size());
    assertTrue(list.contains("Action1a"));
    assertTrue(list.contains("Action2a"));
    processInstance.signalEvent("signal", "End");
    nodeInstances = processInstance.getNodeInstances();
    assertEquals(0, nodeInstances.size());
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
    assertEquals(4, list.size());
    assertTrue(list.contains("Action3a"));
    assertTrue(list.contains("Action4a"));
}
Also used : StateNodeInstance(org.jbpm.workflow.instance.node.StateNodeInstance) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieBase(org.kie.api.KieBase) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) NodeInstance(org.kie.api.runtime.process.NodeInstance) StateNodeInstance(org.jbpm.workflow.instance.node.StateNodeInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 4 with StateNodeInstance

use of org.jbpm.workflow.instance.node.StateNodeInstance in project jbpm by kiegroup.

the class AbstractProtobufProcessInstanceMarshaller method readNodeInstanceContent.

protected NodeInstanceImpl readNodeInstanceContent(JBPMMessages.ProcessInstance.NodeInstance _node, MarshallerReaderContext context, WorkflowProcessInstance processInstance) throws IOException {
    NodeInstanceImpl nodeInstance = null;
    NodeInstanceContent _content = _node.getContent();
    switch(_content.getType()) {
        case RULE_SET_NODE:
            nodeInstance = new RuleSetNodeInstance();
            ((RuleSetNodeInstance) nodeInstance).setRuleFlowGroup(_content.getRuleSet().getRuleFlowGroup());
            if (_content.getRuleSet().getTimerInstanceIdCount() > 0) {
                List<Long> timerInstances = new ArrayList<Long>();
                for (Long _timerId : _content.getRuleSet().getTimerInstanceIdList()) {
                    timerInstances.add(_timerId);
                }
                ((RuleSetNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
            }
            if (_content.getRuleSet().getMapEntryCount() > 0) {
                Map<String, FactHandle> factInfo = new HashMap<String, FactHandle>();
                for (TextMapEntry entry : _content.getRuleSet().getMapEntryList()) {
                    factInfo.put(entry.getName(), DefaultFactHandle.createFromExternalFormat(entry.getValue()));
                }
                ((RuleSetNodeInstance) nodeInstance).setFactHandles(factInfo);
            }
            break;
        case HUMAN_TASK_NODE:
            nodeInstance = new HumanTaskNodeInstance();
            ((HumanTaskNodeInstance) nodeInstance).internalSetWorkItemId(_content.getHumanTask().getWorkItemId());
            if (_content.getHumanTask().getTimerInstanceIdCount() > 0) {
                List<Long> timerInstances = new ArrayList<Long>();
                for (Long _timerId : _content.getHumanTask().getTimerInstanceIdList()) {
                    timerInstances.add(_timerId);
                }
                ((HumanTaskNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
            }
            break;
        case WORK_ITEM_NODE:
            nodeInstance = new WorkItemNodeInstance();
            ((WorkItemNodeInstance) nodeInstance).internalSetWorkItemId(_content.getWorkItem().getWorkItemId());
            if (_content.getWorkItem().getTimerInstanceIdCount() > 0) {
                List<Long> timerInstances = new ArrayList<Long>();
                for (Long _timerId : _content.getWorkItem().getTimerInstanceIdList()) {
                    timerInstances.add(_timerId);
                }
                ((WorkItemNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
            }
            break;
        case SUBPROCESS_NODE:
            nodeInstance = new SubProcessNodeInstance();
            ((SubProcessNodeInstance) nodeInstance).internalSetProcessInstanceId(_content.getSubProcess().getProcessInstanceId());
            if (_content.getSubProcess().getTimerInstanceIdCount() > 0) {
                List<Long> timerInstances = new ArrayList<Long>();
                for (Long _timerId : _content.getSubProcess().getTimerInstanceIdList()) {
                    timerInstances.add(_timerId);
                }
                ((SubProcessNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
            }
            break;
        case MILESTONE_NODE:
            nodeInstance = new MilestoneNodeInstance();
            if (_content.getMilestone().getTimerInstanceIdCount() > 0) {
                List<Long> timerInstances = new ArrayList<Long>();
                for (Long _timerId : _content.getMilestone().getTimerInstanceIdList()) {
                    timerInstances.add(_timerId);
                }
                ((MilestoneNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
            }
            break;
        case TIMER_NODE:
            nodeInstance = new TimerNodeInstance();
            ((TimerNodeInstance) nodeInstance).internalSetTimerId(_content.getTimer().getTimerId());
            break;
        case ASYNC_EVENT_NODE:
            nodeInstance = new AsyncEventNodeInstance();
            ((AsyncEventNodeInstance) nodeInstance).setEventType(_content.getAsyncEvent().getEventType());
            break;
        case EVENT_NODE:
            nodeInstance = new EventNodeInstance();
            break;
        case JOIN_NODE:
            nodeInstance = new JoinInstance();
            if (_content.getJoin().getTriggerCount() > 0) {
                Map<Long, Integer> triggers = new HashMap<Long, Integer>();
                for (JBPMMessages.ProcessInstance.NodeInstanceContent.JoinNode.JoinTrigger _join : _content.getJoin().getTriggerList()) {
                    triggers.put(_join.getNodeId(), _join.getCounter());
                }
                ((JoinInstance) nodeInstance).internalSetTriggers(triggers);
            }
            break;
        case FOR_EACH_NODE:
            nodeInstance = new ForEachNodeInstance();
            break;
        case COMPOSITE_CONTEXT_NODE:
            nodeInstance = new CompositeContextNodeInstance();
            if (_content.getComposite().getTimerInstanceIdCount() > 0) {
                List<Long> timerInstances = new ArrayList<Long>();
                for (Long _timerId : _content.getComposite().getTimerInstanceIdList()) {
                    timerInstances.add(_timerId);
                }
                ((CompositeContextNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
            }
            break;
        case DYNAMIC_NODE:
            nodeInstance = new DynamicNodeInstance();
            if (_content.getComposite().getTimerInstanceIdCount() > 0) {
                List<Long> timerInstances = new ArrayList<Long>();
                for (Long _timerId : _content.getComposite().getTimerInstanceIdList()) {
                    timerInstances.add(_timerId);
                }
                ((CompositeContextNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
            }
            break;
        case STATE_NODE:
            nodeInstance = new StateNodeInstance();
            if (_content.getState().getTimerInstanceIdCount() > 0) {
                List<Long> timerInstances = new ArrayList<Long>();
                for (Long _timerId : _content.getState().getTimerInstanceIdList()) {
                    timerInstances.add(_timerId);
                }
                ((CompositeContextNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
            }
            break;
        case EVENT_SUBPROCESS_NODE:
            nodeInstance = new EventSubProcessNodeInstance();
            if (_content.getComposite().getTimerInstanceIdCount() > 0) {
                List<Long> timerInstances = new ArrayList<Long>();
                for (Long _timerId : _content.getComposite().getTimerInstanceIdList()) {
                    timerInstances.add(_timerId);
                }
                ((CompositeContextNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
            }
            break;
        default:
            throw new IllegalArgumentException("Unknown node type: " + _content.getType());
    }
    return nodeInstance;
}
Also used : AsyncEventNodeInstance(org.jbpm.workflow.instance.node.AsyncEventNodeInstance) JoinInstance(org.jbpm.workflow.instance.node.JoinInstance) DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HumanTaskNodeInstance(org.jbpm.workflow.instance.node.HumanTaskNodeInstance) NodeInstanceContent(org.jbpm.marshalling.impl.JBPMMessages.ProcessInstance.NodeInstanceContent) RuleSetNodeInstance(org.jbpm.workflow.instance.node.RuleSetNodeInstance) NodeInstanceImpl(org.jbpm.workflow.instance.impl.NodeInstanceImpl) MilestoneNodeInstance(org.jbpm.workflow.instance.node.MilestoneNodeInstance) StateNodeInstance(org.jbpm.workflow.instance.node.StateNodeInstance) CompositeContextNodeInstance(org.jbpm.workflow.instance.node.CompositeContextNodeInstance) TextMapEntry(org.jbpm.marshalling.impl.JBPMMessages.ProcessInstance.NodeInstanceContent.RuleSetNode.TextMapEntry) WorkItemNodeInstance(org.jbpm.workflow.instance.node.WorkItemNodeInstance) EventSubProcessNodeInstance(org.jbpm.workflow.instance.node.EventSubProcessNodeInstance) EventSubProcessNodeInstance(org.jbpm.workflow.instance.node.EventSubProcessNodeInstance) SubProcessNodeInstance(org.jbpm.workflow.instance.node.SubProcessNodeInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) ForEachNodeInstance(org.jbpm.workflow.instance.node.ForEachNodeInstance) TimerNodeInstance(org.jbpm.workflow.instance.node.TimerNodeInstance) AsyncEventNodeInstance(org.jbpm.workflow.instance.node.AsyncEventNodeInstance) EventNodeInstance(org.jbpm.workflow.instance.node.EventNodeInstance)

Example 5 with StateNodeInstance

use of org.jbpm.workflow.instance.node.StateNodeInstance in project jbpm by kiegroup.

the class AbstractProtobufProcessInstanceMarshaller method writeNodeInstanceContent.

protected JBPMMessages.ProcessInstance.NodeInstanceContent writeNodeInstanceContent(JBPMMessages.ProcessInstance.NodeInstance.Builder _node, NodeInstance nodeInstance, MarshallerWriteContext context) throws IOException {
    JBPMMessages.ProcessInstance.NodeInstanceContent.Builder _content = null;
    if (nodeInstance instanceof RuleSetNodeInstance) {
        _content = JBPMMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(NodeInstanceType.RULE_SET_NODE);
        List<Long> timerInstances = ((RuleSetNodeInstance) nodeInstance).getTimerInstances();
        JBPMMessages.ProcessInstance.NodeInstanceContent.RuleSetNode.Builder _ruleSet = JBPMMessages.ProcessInstance.NodeInstanceContent.RuleSetNode.newBuilder();
        _ruleSet.setRuleFlowGroup(((RuleSetNodeInstance) nodeInstance).getRuleFlowGroup());
        if (timerInstances != null) {
            for (Long id : timerInstances) {
                _ruleSet.addTimerInstanceId(id);
            }
        }
        Map<String, FactHandle> facts = ((RuleSetNodeInstance) nodeInstance).getFactHandles();
        if (facts != null && facts.size() > 0) {
            for (Map.Entry<String, FactHandle> entry : facts.entrySet()) {
                JBPMMessages.ProcessInstance.NodeInstanceContent.RuleSetNode.TextMapEntry.Builder _textMapEntry = JBPMMessages.ProcessInstance.NodeInstanceContent.RuleSetNode.TextMapEntry.newBuilder();
                _textMapEntry.setName(entry.getKey());
                _textMapEntry.setValue(entry.getValue().toExternalForm());
                _ruleSet.addMapEntry(_textMapEntry.build());
            }
        }
        _content.setRuleSet(_ruleSet.build());
    } else if (nodeInstance instanceof HumanTaskNodeInstance) {
        JBPMMessages.ProcessInstance.NodeInstanceContent.HumanTaskNode.Builder _task = JBPMMessages.ProcessInstance.NodeInstanceContent.HumanTaskNode.newBuilder().setWorkItemId(((HumanTaskNodeInstance) nodeInstance).getWorkItemId());
        List<Long> timerInstances = ((HumanTaskNodeInstance) nodeInstance).getTimerInstances();
        if (timerInstances != null) {
            for (Long id : timerInstances) {
                _task.addTimerInstanceId(id);
            }
        }
        _content = JBPMMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(NodeInstanceType.HUMAN_TASK_NODE).setHumanTask(_task.build());
    } else if (nodeInstance instanceof WorkItemNodeInstance) {
        JBPMMessages.ProcessInstance.NodeInstanceContent.WorkItemNode.Builder _wi = JBPMMessages.ProcessInstance.NodeInstanceContent.WorkItemNode.newBuilder().setWorkItemId(((WorkItemNodeInstance) nodeInstance).getWorkItemId());
        List<Long> timerInstances = ((WorkItemNodeInstance) nodeInstance).getTimerInstances();
        if (timerInstances != null) {
            for (Long id : timerInstances) {
                _wi.addTimerInstanceId(id);
            }
        }
        _content = JBPMMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(NodeInstanceType.WORK_ITEM_NODE).setWorkItem(_wi.build());
    } else if (nodeInstance instanceof SubProcessNodeInstance) {
        JBPMMessages.ProcessInstance.NodeInstanceContent.SubProcessNode.Builder _sp = JBPMMessages.ProcessInstance.NodeInstanceContent.SubProcessNode.newBuilder().setProcessInstanceId(((SubProcessNodeInstance) nodeInstance).getProcessInstanceId());
        List<Long> timerInstances = ((SubProcessNodeInstance) nodeInstance).getTimerInstances();
        if (timerInstances != null) {
            for (Long id : timerInstances) {
                _sp.addTimerInstanceId(id);
            }
        }
        _content = JBPMMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(NodeInstanceType.SUBPROCESS_NODE).setSubProcess(_sp.build());
    } else if (nodeInstance instanceof MilestoneNodeInstance) {
        JBPMMessages.ProcessInstance.NodeInstanceContent.MilestoneNode.Builder _ms = JBPMMessages.ProcessInstance.NodeInstanceContent.MilestoneNode.newBuilder();
        List<Long> timerInstances = ((MilestoneNodeInstance) nodeInstance).getTimerInstances();
        if (timerInstances != null) {
            for (Long id : timerInstances) {
                _ms.addTimerInstanceId(id);
            }
        }
        _content = JBPMMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(NodeInstanceType.MILESTONE_NODE).setMilestone(_ms.build());
    } else if (nodeInstance instanceof AsyncEventNodeInstance) {
        JBPMMessages.ProcessInstance.NodeInstanceContent.AsyncEventNode.Builder _asyncEvent = JBPMMessages.ProcessInstance.NodeInstanceContent.AsyncEventNode.newBuilder();
        _asyncEvent.setEventType(((AsyncEventNodeInstance) nodeInstance).getEventType());
        _content = JBPMMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(NodeInstanceType.ASYNC_EVENT_NODE).setAsyncEvent(_asyncEvent.build());
    } else if (nodeInstance instanceof EventNodeInstance) {
        _content = JBPMMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(NodeInstanceType.EVENT_NODE);
    } else if (nodeInstance instanceof TimerNodeInstance) {
        _content = JBPMMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(NodeInstanceType.TIMER_NODE).setTimer(JBPMMessages.ProcessInstance.NodeInstanceContent.TimerNode.newBuilder().setTimerId(((TimerNodeInstance) nodeInstance).getTimerId()).build());
    } else if (nodeInstance instanceof JoinInstance) {
        JBPMMessages.ProcessInstance.NodeInstanceContent.JoinNode.Builder _join = JBPMMessages.ProcessInstance.NodeInstanceContent.JoinNode.newBuilder();
        Map<Long, Integer> triggers = ((JoinInstance) nodeInstance).getTriggers();
        List<Long> keys = new ArrayList<Long>(triggers.keySet());
        Collections.sort(keys, new Comparator<Long>() {

            public int compare(Long o1, Long o2) {
                return o1.compareTo(o2);
            }
        });
        for (Long key : keys) {
            _join.addTrigger(JBPMMessages.ProcessInstance.NodeInstanceContent.JoinNode.JoinTrigger.newBuilder().setNodeId(key).setCounter(triggers.get(key)).build());
        }
        _content = JBPMMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(NodeInstanceType.JOIN_NODE).setJoin(_join.build());
    } else if (nodeInstance instanceof StateNodeInstance) {
        JBPMMessages.ProcessInstance.NodeInstanceContent.StateNode.Builder _state = JBPMMessages.ProcessInstance.NodeInstanceContent.StateNode.newBuilder();
        List<Long> timerInstances = ((StateNodeInstance) nodeInstance).getTimerInstances();
        if (timerInstances != null) {
            for (Long id : timerInstances) {
                _state.addTimerInstanceId(id);
            }
        }
        _content = JBPMMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(NodeInstanceType.STATE_NODE).setState(_state.build());
    } else if (nodeInstance instanceof ForEachNodeInstance) {
        JBPMMessages.ProcessInstance.NodeInstanceContent.ForEachNode.Builder _foreach = JBPMMessages.ProcessInstance.NodeInstanceContent.ForEachNode.newBuilder();
        ForEachNodeInstance forEachNodeInstance = (ForEachNodeInstance) nodeInstance;
        List<NodeInstance> nodeInstances = new ArrayList<NodeInstance>(forEachNodeInstance.getNodeInstances());
        Collections.sort(nodeInstances, new Comparator<NodeInstance>() {

            public int compare(NodeInstance o1, NodeInstance o2) {
                return (int) (o1.getId() - o2.getId());
            }
        });
        for (NodeInstance subNodeInstance : nodeInstances) {
            if (subNodeInstance instanceof CompositeContextNodeInstance) {
                _foreach.addNodeInstance(writeNodeInstance(context, subNodeInstance));
            }
        }
        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) forEachNodeInstance.getContextInstance(VariableScope.VARIABLE_SCOPE);
        if (variableScopeInstance != null) {
            List<Map.Entry<String, Object>> variables = new ArrayList<Map.Entry<String, Object>>(variableScopeInstance.getVariables().entrySet());
            Collections.sort(variables, new Comparator<Map.Entry<String, Object>>() {

                public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2) {
                    return o1.getKey().compareTo(o2.getKey());
                }
            });
            for (Map.Entry<String, Object> variable : variables) {
                _foreach.addVariable(ProtobufProcessMarshaller.marshallVariable(context, variable.getKey(), variable.getValue()));
            }
        }
        List<Map.Entry<String, Integer>> iterationlevels = new ArrayList<Map.Entry<String, Integer>>(forEachNodeInstance.getIterationLevels().entrySet());
        Collections.sort(iterationlevels, new Comparator<Map.Entry<String, Integer>>() {

            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o1.getKey().compareTo(o2.getKey());
            }
        });
        for (Map.Entry<String, Integer> level : iterationlevels) {
            if (level.getKey() != null && level.getValue() != null) {
                _foreach.addIterationLevels(JBPMMessages.IterationLevel.newBuilder().setId(level.getKey()).setLevel(level.getValue()));
            }
        }
        _content = JBPMMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(NodeInstanceType.FOR_EACH_NODE).setForEach(_foreach.build());
    } else if (nodeInstance instanceof CompositeContextNodeInstance) {
        JBPMMessages.ProcessInstance.NodeInstanceContent.CompositeContextNode.Builder _composite = JBPMMessages.ProcessInstance.NodeInstanceContent.CompositeContextNode.newBuilder();
        JBPMMessages.ProcessInstance.NodeInstanceType _type = null;
        if (nodeInstance instanceof DynamicNodeInstance) {
            _type = JBPMMessages.ProcessInstance.NodeInstanceType.DYNAMIC_NODE;
        } else if (nodeInstance instanceof EventSubProcessNodeInstance) {
            _type = JBPMMessages.ProcessInstance.NodeInstanceType.EVENT_SUBPROCESS_NODE;
        } else {
            _type = JBPMMessages.ProcessInstance.NodeInstanceType.COMPOSITE_CONTEXT_NODE;
        }
        CompositeContextNodeInstance compositeNodeInstance = (CompositeContextNodeInstance) nodeInstance;
        List<Long> timerInstances = ((CompositeContextNodeInstance) nodeInstance).getTimerInstances();
        if (timerInstances != null) {
            for (Long id : timerInstances) {
                _composite.addTimerInstanceId(id);
            }
        }
        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) compositeNodeInstance.getContextInstance(VariableScope.VARIABLE_SCOPE);
        if (variableScopeInstance != null) {
            List<Map.Entry<String, Object>> variables = new ArrayList<Map.Entry<String, Object>>(variableScopeInstance.getVariables().entrySet());
            Collections.sort(variables, new Comparator<Map.Entry<String, Object>>() {

                public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2) {
                    return o1.getKey().compareTo(o2.getKey());
                }
            });
            for (Map.Entry<String, Object> variable : variables) {
                _composite.addVariable(ProtobufProcessMarshaller.marshallVariable(context, variable.getKey(), variable.getValue()));
            }
        }
        List<Map.Entry<String, Integer>> iterationlevels = new ArrayList<Map.Entry<String, Integer>>(compositeNodeInstance.getIterationLevels().entrySet());
        Collections.sort(iterationlevels, new Comparator<Map.Entry<String, Integer>>() {

            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o1.getKey().compareTo(o2.getKey());
            }
        });
        for (Map.Entry<String, Integer> level : iterationlevels) {
            if (level.getKey() != null && level.getValue() != null) {
                _composite.addIterationLevels(JBPMMessages.IterationLevel.newBuilder().setId(level.getKey()).setLevel(level.getValue()));
            }
        }
        List<NodeInstance> nodeInstances = new ArrayList<NodeInstance>(compositeNodeInstance.getNodeInstances());
        Collections.sort(nodeInstances, new Comparator<NodeInstance>() {

            public int compare(NodeInstance o1, NodeInstance o2) {
                return (int) (o1.getId() - o2.getId());
            }
        });
        for (NodeInstance subNodeInstance : nodeInstances) {
            _composite.addNodeInstance(writeNodeInstance(context, subNodeInstance));
        }
        List<ContextInstance> exclusiveGroupInstances = compositeNodeInstance.getContextInstances(ExclusiveGroup.EXCLUSIVE_GROUP);
        if (exclusiveGroupInstances != null) {
            for (ContextInstance contextInstance : exclusiveGroupInstances) {
                JBPMMessages.ProcessInstance.ExclusiveGroupInstance.Builder _excl = JBPMMessages.ProcessInstance.ExclusiveGroupInstance.newBuilder();
                ExclusiveGroupInstance exclusiveGroupInstance = (ExclusiveGroupInstance) contextInstance;
                Collection<NodeInstance> groupNodeInstances = exclusiveGroupInstance.getNodeInstances();
                for (NodeInstance groupNodeInstance : groupNodeInstances) {
                    _excl.addGroupNodeInstanceId(groupNodeInstance.getId());
                }
                _composite.addExclusiveGroup(_excl.build());
            }
        }
        _content = JBPMMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(_type).setComposite(_composite.build());
    } else {
        throw new IllegalArgumentException("Unknown node instance type: " + nodeInstance);
    }
    return _content.build();
}
Also used : AsyncEventNodeInstance(org.jbpm.workflow.instance.node.AsyncEventNodeInstance) JoinInstance(org.jbpm.workflow.instance.node.JoinInstance) DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ArrayList(java.util.ArrayList) NodeInstanceType(org.jbpm.marshalling.impl.JBPMMessages.ProcessInstance.NodeInstanceType) Comparator(java.util.Comparator) HumanTaskNodeInstance(org.jbpm.workflow.instance.node.HumanTaskNodeInstance) List(java.util.List) ArrayList(java.util.ArrayList) NodeInstanceContent(org.jbpm.marshalling.impl.JBPMMessages.ProcessInstance.NodeInstanceContent) RuleSetNodeInstance(org.jbpm.workflow.instance.node.RuleSetNodeInstance) StateNodeInstance(org.jbpm.workflow.instance.node.StateNodeInstance) CompositeContextNodeInstance(org.jbpm.workflow.instance.node.CompositeContextNodeInstance) WorkItemNodeInstance(org.jbpm.workflow.instance.node.WorkItemNodeInstance) EventSubProcessNodeInstance(org.jbpm.workflow.instance.node.EventSubProcessNodeInstance) ContextInstance(org.jbpm.process.instance.ContextInstance) SwimlaneContextInstance(org.jbpm.process.instance.context.swimlane.SwimlaneContextInstance) Collection(java.util.Collection) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) Map(java.util.Map) HashMap(java.util.HashMap) TimerNodeInstance(org.jbpm.workflow.instance.node.TimerNodeInstance) RuleSetNodeInstance(org.jbpm.workflow.instance.node.RuleSetNodeInstance) EventSubProcessNodeInstance(org.jbpm.workflow.instance.node.EventSubProcessNodeInstance) StateNodeInstance(org.jbpm.workflow.instance.node.StateNodeInstance) TimerNodeInstance(org.jbpm.workflow.instance.node.TimerNodeInstance) CompositeContextNodeInstance(org.jbpm.workflow.instance.node.CompositeContextNodeInstance) DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) AsyncEventNodeInstance(org.jbpm.workflow.instance.node.AsyncEventNodeInstance) SubProcessNodeInstance(org.jbpm.workflow.instance.node.SubProcessNodeInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) HumanTaskNodeInstance(org.jbpm.workflow.instance.node.HumanTaskNodeInstance) EventNodeInstance(org.jbpm.workflow.instance.node.EventNodeInstance) ForEachNodeInstance(org.jbpm.workflow.instance.node.ForEachNodeInstance) MilestoneNodeInstance(org.jbpm.workflow.instance.node.MilestoneNodeInstance) WorkItemNodeInstance(org.jbpm.workflow.instance.node.WorkItemNodeInstance) TextMapEntry(org.jbpm.marshalling.impl.JBPMMessages.ProcessInstance.NodeInstanceContent.RuleSetNode.TextMapEntry) VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) ExclusiveGroupInstance(org.jbpm.process.instance.context.exclusive.ExclusiveGroupInstance) MilestoneNodeInstance(org.jbpm.workflow.instance.node.MilestoneNodeInstance) TextMapEntry(org.jbpm.marshalling.impl.JBPMMessages.ProcessInstance.NodeInstanceContent.RuleSetNode.TextMapEntry) EventSubProcessNodeInstance(org.jbpm.workflow.instance.node.EventSubProcessNodeInstance) SubProcessNodeInstance(org.jbpm.workflow.instance.node.SubProcessNodeInstance) ForEachNodeInstance(org.jbpm.workflow.instance.node.ForEachNodeInstance) AsyncEventNodeInstance(org.jbpm.workflow.instance.node.AsyncEventNodeInstance) EventNodeInstance(org.jbpm.workflow.instance.node.EventNodeInstance)

Aggregations

StateNodeInstance (org.jbpm.workflow.instance.node.StateNodeInstance)5 WorkflowProcessInstance (org.kie.api.runtime.process.WorkflowProcessInstance)5 ArrayList (java.util.ArrayList)4 NodeInstance (org.kie.api.runtime.process.NodeInstance)4 Reader (java.io.Reader)3 StringReader (java.io.StringReader)3 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)3 Test (org.junit.Test)3 KieBase (org.kie.api.KieBase)3 KieSession (org.kie.api.runtime.KieSession)3 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)3 HashMap (java.util.HashMap)2 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)2 NodeInstanceContent (org.jbpm.marshalling.impl.JBPMMessages.ProcessInstance.NodeInstanceContent)2 TextMapEntry (org.jbpm.marshalling.impl.JBPMMessages.ProcessInstance.NodeInstanceContent.RuleSetNode.TextMapEntry)2 AsyncEventNodeInstance (org.jbpm.workflow.instance.node.AsyncEventNodeInstance)2 CompositeContextNodeInstance (org.jbpm.workflow.instance.node.CompositeContextNodeInstance)2 DynamicNodeInstance (org.jbpm.workflow.instance.node.DynamicNodeInstance)2 EventNodeInstance (org.jbpm.workflow.instance.node.EventNodeInstance)2 EventSubProcessNodeInstance (org.jbpm.workflow.instance.node.EventSubProcessNodeInstance)2