public JBPMMessages.ProcessInstance.NodeInstance writeNodeInstance(
     MarshallerWriteContext context, NodeInstance nodeInstance) throws IOException {
   JBPMMessages.ProcessInstance.NodeInstance.Builder _node =
       JBPMMessages.ProcessInstance.NodeInstance.newBuilder()
           .setId(nodeInstance.getId())
           .setNodeId(nodeInstance.getNodeId());
   _node.setContent(writeNodeInstanceContent(_node, nodeInstance, context));
   return _node.build();
 }
  // 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());
    if (workFlow.getProcessXml() != null) {
      _instance.setProcessXml(workFlow.getProcessXml());
    }

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

    return _instance.build();
  }
  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 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 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<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 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));
        }
      }
      _content =
          JBPMMessages.ProcessInstance.NodeInstanceContent.newBuilder()
              .setType(NodeInstanceType.FOR_EACH_NODE)
              .setForEach(_foreach.build());
    } else {
      throw new IllegalArgumentException("Unknown node instance type: " + nodeInstance);
    }
    return _content.build();
  }