Search in sources :

Example 6 with ExclusiveGroupInstance

use of org.jbpm.process.instance.context.exclusive.ExclusiveGroupInstance in project jbpm by kiegroup.

the class AbstractProcessInstanceMarshaller method readNodeInstance.

public NodeInstance readNodeInstance(MarshallerReaderContext context, NodeInstanceContainer nodeInstanceContainer, WorkflowProcessInstance processInstance) throws IOException {
    ObjectInputStream stream = context.stream;
    long id = stream.readLong();
    long nodeId = stream.readLong();
    int nodeType = stream.readShort();
    NodeInstanceImpl nodeInstance = readNodeInstanceContent(nodeType, stream, context, processInstance);
    nodeInstance.setNodeId(nodeId);
    nodeInstance.setNodeInstanceContainer(nodeInstanceContainer);
    nodeInstance.setProcessInstance((org.jbpm.workflow.instance.WorkflowProcessInstance) processInstance);
    nodeInstance.setId(id);
    switch(nodeType) {
        case PersisterEnums.COMPOSITE_NODE_INSTANCE:
        case PersisterEnums.DYNAMIC_NODE_INSTANCE:
            int nbVariables = stream.readInt();
            if (nbVariables > 0) {
                Context variableScope = ((org.jbpm.process.core.Process) ((org.jbpm.process.instance.ProcessInstance) processInstance).getProcess()).getDefaultContext(VariableScope.VARIABLE_SCOPE);
                VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((CompositeContextNodeInstance) nodeInstance).getContextInstance(variableScope);
                for (int i = 0; i < nbVariables; i++) {
                    String name = stream.readUTF();
                    try {
                        Object value = stream.readObject();
                        variableScopeInstance.internalSetVariable(name, value);
                    } catch (ClassNotFoundException e) {
                        throw new IllegalArgumentException("Could not reload variable " + name);
                    }
                }
            }
            while (stream.readShort() == PersisterEnums.NODE_INSTANCE) {
                readNodeInstance(context, (CompositeContextNodeInstance) nodeInstance, processInstance);
            }
            int exclusiveGroupInstances = stream.readInt();
            for (int i = 0; i < exclusiveGroupInstances; i++) {
                ExclusiveGroupInstance exclusiveGroupInstance = new ExclusiveGroupInstance();
                ((org.jbpm.process.instance.ProcessInstance) processInstance).addContextInstance(ExclusiveGroup.EXCLUSIVE_GROUP, exclusiveGroupInstance);
                int nodeInstances = stream.readInt();
                for (int j = 0; j < nodeInstances; j++) {
                    long nodeInstanceId = stream.readLong();
                    NodeInstance groupNodeInstance = processInstance.getNodeInstance(nodeInstanceId);
                    if (groupNodeInstance == null) {
                        throw new IllegalArgumentException("Could not find node instance when deserializing exclusive group instance: " + nodeInstanceId);
                    }
                    exclusiveGroupInstance.addNodeInstance(groupNodeInstance);
                }
            }
            break;
        case PersisterEnums.FOR_EACH_NODE_INSTANCE:
            while (stream.readShort() == PersisterEnums.NODE_INSTANCE) {
                readNodeInstance(context, (ForEachNodeInstance) nodeInstance, processInstance);
            }
            break;
        default:
    }
    return nodeInstance;
}
Also used : NodeInstanceImpl(org.jbpm.workflow.instance.impl.NodeInstanceImpl) SwimlaneContext(org.jbpm.process.core.context.swimlane.SwimlaneContext) MarshallerWriteContext(org.drools.core.marshalling.impl.MarshallerWriteContext) MarshallerReaderContext(org.drools.core.marshalling.impl.MarshallerReaderContext) Context(org.jbpm.process.core.Context) ExclusiveGroupInstance(org.jbpm.process.instance.context.exclusive.ExclusiveGroupInstance) Process(org.kie.api.definition.process.Process) VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) ObjectInputStream(java.io.ObjectInputStream)

Example 7 with ExclusiveGroupInstance

use of org.jbpm.process.instance.context.exclusive.ExclusiveGroupInstance in project jbpm by kiegroup.

the class AbstractProcessInstanceMarshaller method readProcessInstance.

// Input methods
public ProcessInstance readProcessInstance(MarshallerReaderContext context) throws IOException {
    ObjectInputStream stream = context.stream;
    InternalKnowledgeBase kBase = context.kBase;
    InternalWorkingMemory wm = context.wm;
    WorkflowProcessInstanceImpl processInstance = createProcessInstance();
    processInstance.setId(stream.readLong());
    String processId = stream.readUTF();
    processInstance.setProcessId(processId);
    Process process = kBase.getProcess(processId);
    if (kBase != null) {
        processInstance.setProcess(process);
    }
    processInstance.setState(stream.readInt());
    long nodeInstanceCounter = stream.readLong();
    processInstance.setKnowledgeRuntime(wm.getKnowledgeRuntime());
    int nbSwimlanes = stream.readInt();
    if (nbSwimlanes > 0) {
        Context swimlaneContext = ((org.jbpm.process.core.Process) process).getDefaultContext(SwimlaneContext.SWIMLANE_SCOPE);
        SwimlaneContextInstance swimlaneContextInstance = (SwimlaneContextInstance) processInstance.getContextInstance(swimlaneContext);
        for (int i = 0; i < nbSwimlanes; i++) {
            String name = stream.readUTF();
            String value = stream.readUTF();
            swimlaneContextInstance.setActorId(name, value);
        }
    }
    while (stream.readShort() == PersisterEnums.NODE_INSTANCE) {
        readNodeInstance(context, processInstance, processInstance);
    }
    int exclusiveGroupInstances = stream.readInt();
    for (int i = 0; i < exclusiveGroupInstances; i++) {
        ExclusiveGroupInstance exclusiveGroupInstance = new ExclusiveGroupInstance();
        processInstance.addContextInstance(ExclusiveGroup.EXCLUSIVE_GROUP, exclusiveGroupInstance);
        int nodeInstances = stream.readInt();
        for (int j = 0; j < nodeInstances; j++) {
            long nodeInstanceId = stream.readLong();
            NodeInstance nodeInstance = processInstance.getNodeInstance(nodeInstanceId);
            if (nodeInstance == null) {
                throw new IllegalArgumentException("Could not find node instance when deserializing exclusive group instance: " + nodeInstanceId);
            }
            exclusiveGroupInstance.addNodeInstance(nodeInstance);
        }
    }
    // Process Variables
    // - Number of Variables = keys.size()
    // For Each Variable
    // - Variable Key
    // - Marshalling Strategy Index
    // - Marshalled Object
    int nbVariables = stream.readInt();
    if (nbVariables > 0) {
        Context variableScope = ((org.jbpm.process.core.Process) process).getDefaultContext(VariableScope.VARIABLE_SCOPE);
        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) processInstance.getContextInstance(variableScope);
        for (int i = 0; i < nbVariables; i++) {
            String name = stream.readUTF();
            try {
                ObjectMarshallingStrategy strategy = null;
                int index = stream.readInt();
                // This is the old way of de/serializing strategy objects
                if (index >= 0) {
                    strategy = context.resolverStrategyFactory.getStrategy(index);
                } else // This is the new way
                if (index == -2) {
                    String strategyClassName = context.stream.readUTF();
                    if (!StringUtils.isEmpty(strategyClassName)) {
                        strategy = context.resolverStrategyFactory.getStrategyObject(strategyClassName);
                        if (strategy == null) {
                            throw new IllegalStateException("No strategy of type " + strategyClassName + " available.");
                        }
                    }
                }
                // If either way retrieves a strategy, use it
                Object value = null;
                if (strategy != null) {
                    value = strategy.read(stream);
                }
                variableScopeInstance.internalSetVariable(name, value);
            } catch (ClassNotFoundException e) {
                throw new IllegalArgumentException("Could not reload variable " + name);
            }
        }
    }
    processInstance.internalSetNodeInstanceCounter(nodeInstanceCounter);
    if (wm != null) {
        processInstance.reconnect();
    }
    return processInstance;
}
Also used : SwimlaneContext(org.jbpm.process.core.context.swimlane.SwimlaneContext) MarshallerWriteContext(org.drools.core.marshalling.impl.MarshallerWriteContext) MarshallerReaderContext(org.drools.core.marshalling.impl.MarshallerReaderContext) Context(org.jbpm.process.core.Context) ExclusiveGroupInstance(org.jbpm.process.instance.context.exclusive.ExclusiveGroupInstance) SwimlaneContextInstance(org.jbpm.process.instance.context.swimlane.SwimlaneContextInstance) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) Process(org.kie.api.definition.process.Process) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) ObjectInputStream(java.io.ObjectInputStream)

Example 8 with ExclusiveGroupInstance

use of org.jbpm.process.instance.context.exclusive.ExclusiveGroupInstance in project jbpm by kiegroup.

the class AbstractProtobufProcessInstanceMarshaller method writeProcessInstance.

// Output methods
public JBPMMessages.ProcessInstance writeProcessInstance(MarshallerWriteContext context, ProcessInstance processInstance) throws IOException {
    WorkflowProcessInstanceImpl workFlow = (WorkflowProcessInstanceImpl) processInstance;
    JBPMMessages.ProcessInstance.Builder _instance = JBPMMessages.ProcessInstance.newBuilder().setId(workFlow.getId()).setProcessId(workFlow.getProcessId()).setState(workFlow.getState()).setNodeInstanceCounter(workFlow.getNodeInstanceCounter()).setProcessType(workFlow.getProcess().getType()).setParentProcessInstanceId(workFlow.getParentProcessInstanceId()).setSignalCompletion(workFlow.isSignalCompletion()).setSlaCompliance(workFlow.getSlaCompliance());
    if (workFlow.getProcessXml() != null) {
        _instance.setProcessXml(workFlow.getProcessXml());
    }
    if (workFlow.getDescription() != null) {
        _instance.setDescription(workFlow.getDescription());
    }
    if (workFlow.getDeploymentId() != null) {
        _instance.setDeploymentId(workFlow.getDeploymentId());
    }
    _instance.addAllCompletedNodeIds(workFlow.getCompletedNodeIds());
    if (workFlow.getCorrelationKey() != null) {
        _instance.setCorrelationKey(workFlow.getCorrelationKey());
    }
    if (workFlow.getSlaDueDate() != null) {
        _instance.setSlaDueDate(workFlow.getSlaDueDate().getTime());
    }
    if (workFlow.getSlaTimerId() != null) {
        _instance.setSlaTimerId(workFlow.getSlaTimerId());
    }
    SwimlaneContextInstance swimlaneContextInstance = (SwimlaneContextInstance) workFlow.getContextInstance(SwimlaneContext.SWIMLANE_SCOPE);
    if (swimlaneContextInstance != null) {
        Map<String, String> swimlaneActors = swimlaneContextInstance.getSwimlaneActors();
        for (Map.Entry<String, String> entry : swimlaneActors.entrySet()) {
            _instance.addSwimlaneContext(JBPMMessages.ProcessInstance.SwimlaneContextInstance.newBuilder().setSwimlane(entry.getKey()).setActorId(entry.getValue()).build());
        }
    }
    List<NodeInstance> nodeInstances = new ArrayList<NodeInstance>(workFlow.getNodeInstances());
    Collections.sort(nodeInstances, new Comparator<NodeInstance>() {

        public int compare(NodeInstance o1, NodeInstance o2) {
            return (int) (o1.getId() - o2.getId());
        }
    });
    for (NodeInstance nodeInstance : nodeInstances) {
        _instance.addNodeInstance(writeNodeInstance(context, nodeInstance));
    }
    List<ContextInstance> exclusiveGroupInstances = workFlow.getContextInstances(ExclusiveGroup.EXCLUSIVE_GROUP);
    if (exclusiveGroupInstances != null) {
        for (ContextInstance contextInstance : exclusiveGroupInstances) {
            JBPMMessages.ProcessInstance.ExclusiveGroupInstance.Builder _exclusive = JBPMMessages.ProcessInstance.ExclusiveGroupInstance.newBuilder();
            ExclusiveGroupInstance exclusiveGroupInstance = (ExclusiveGroupInstance) contextInstance;
            Collection<NodeInstance> groupNodeInstances = exclusiveGroupInstance.getNodeInstances();
            for (NodeInstance nodeInstance : groupNodeInstances) {
                _exclusive.addGroupNodeInstanceId(nodeInstance.getId());
            }
            _instance.addExclusiveGroup(_exclusive.build());
        }
    }
    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) workFlow.getContextInstance(VariableScope.VARIABLE_SCOPE);
    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) {
        if (variable.getValue() != null) {
            _instance.addVariable(ProtobufProcessMarshaller.marshallVariable(context, variable.getKey(), variable.getValue()));
        }
    }
    List<Map.Entry<String, Integer>> iterationlevels = new ArrayList<Map.Entry<String, Integer>>(workFlow.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.getValue() != null) {
            _instance.addIterationLevels(JBPMMessages.IterationLevel.newBuilder().setId(level.getKey()).setLevel(level.getValue()));
        }
    }
    return _instance.build();
}
Also used : WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) ArrayList(java.util.ArrayList) 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) SwimlaneContextInstance(org.jbpm.process.instance.context.swimlane.SwimlaneContextInstance) ContextInstance(org.jbpm.process.instance.ContextInstance) SwimlaneContextInstance(org.jbpm.process.instance.context.swimlane.SwimlaneContextInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) Map(java.util.Map) HashMap(java.util.HashMap) 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)

Example 9 with ExclusiveGroupInstance

use of org.jbpm.process.instance.context.exclusive.ExclusiveGroupInstance 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

ExclusiveGroupInstance (org.jbpm.process.instance.context.exclusive.ExclusiveGroupInstance)9 NodeInstance (org.kie.api.runtime.process.NodeInstance)9 VariableScopeInstance (org.jbpm.process.instance.context.variable.VariableScopeInstance)8 SwimlaneContextInstance (org.jbpm.process.instance.context.swimlane.SwimlaneContextInstance)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)5 WorkflowProcessInstance (org.kie.api.runtime.process.WorkflowProcessInstance)5 MarshallerReaderContext (org.drools.core.marshalling.impl.MarshallerReaderContext)4 MarshallerWriteContext (org.drools.core.marshalling.impl.MarshallerWriteContext)4 Context (org.jbpm.process.core.Context)4 SwimlaneContext (org.jbpm.process.core.context.swimlane.SwimlaneContext)4 ContextInstance (org.jbpm.process.instance.ContextInstance)4 WorkflowProcessInstanceImpl (org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl)4 AsyncEventNodeInstance (org.jbpm.workflow.instance.node.AsyncEventNodeInstance)4 CompositeContextNodeInstance (org.jbpm.workflow.instance.node.CompositeContextNodeInstance)4 DynamicNodeInstance (org.jbpm.workflow.instance.node.DynamicNodeInstance)4 EventNodeInstance (org.jbpm.workflow.instance.node.EventNodeInstance)4 EventSubProcessNodeInstance (org.jbpm.workflow.instance.node.EventSubProcessNodeInstance)4