public NodeInstance readNodeInstance(
      MarshallerReaderContext context,
      NodeInstanceContainer nodeInstanceContainer,
      WorkflowProcessInstance processInstance)
      throws IOException {
    JBPMMessages.ProcessInstance.NodeInstance _node =
        (JBPMMessages.ProcessInstance.NodeInstance) context.parameterObject;

    NodeInstanceImpl nodeInstance = readNodeInstanceContent(_node, context, processInstance);

    nodeInstance.setNodeId(_node.getNodeId());
    nodeInstance.setNodeInstanceContainer(nodeInstanceContainer);
    nodeInstance.setProcessInstance(
        (org.jbpm.workflow.instance.WorkflowProcessInstance) processInstance);
    nodeInstance.setId(_node.getId());

    switch (_node.getContent().getType()) {
      case COMPOSITE_CONTEXT_NODE:
      case DYNAMIC_NODE:
        if (_node.getContent().getComposite().getVariableCount() > 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 (JBPMMessages.Variable _variable :
              _node.getContent().getComposite().getVariableList()) {
            try {
              Object _value = ProtobufProcessMarshaller.unmarshallVariableValue(context, _variable);
              variableScopeInstance.internalSetVariable(_variable.getName(), _value);
            } catch (ClassNotFoundException e) {
              throw new IllegalArgumentException(
                  "Could not reload variable " + _variable.getName());
            }
          }
        }
        for (JBPMMessages.ProcessInstance.NodeInstance _instance :
            _node.getContent().getComposite().getNodeInstanceList()) {
          context.parameterObject = _instance;
          readNodeInstance(context, (CompositeContextNodeInstance) nodeInstance, processInstance);
        }

        for (JBPMMessages.ProcessInstance.ExclusiveGroupInstance _excl :
            _node.getContent().getComposite().getExclusiveGroupList()) {
          ExclusiveGroupInstance exclusiveGroupInstance = new ExclusiveGroupInstance();
          ((org.jbpm.process.instance.ProcessInstance) processInstance)
              .addContextInstance(ExclusiveGroup.EXCLUSIVE_GROUP, exclusiveGroupInstance);
          for (Long nodeInstanceId : _excl.getGroupNodeInstanceIdList()) {
            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 FOR_EACH_NODE:
        for (JBPMMessages.ProcessInstance.NodeInstance _instance :
            _node.getContent().getForEach().getNodeInstanceList()) {
          context.parameterObject = _instance;
          readNodeInstance(context, (ForEachNodeInstance) nodeInstance, processInstance);
        }
        break;
      case EVENT_SUBPROCESS_NODE:
        for (JBPMMessages.ProcessInstance.NodeInstance _instance :
            _node.getContent().getComposite().getNodeInstanceList()) {
          context.parameterObject = _instance;
          readNodeInstance(context, (EventSubProcessNodeInstance) nodeInstance, processInstance);
        }
        break;
      default:
        // do nothing
    }

    return nodeInstance;
  }
  // Input methods
  public ProcessInstance readProcessInstance(MarshallerReaderContext context) throws IOException {
    InternalRuleBase ruleBase = context.ruleBase;
    InternalWorkingMemory wm = context.wm;

    JBPMMessages.ProcessInstance _instance =
        (org.jbpm.marshalling.impl.JBPMMessages.ProcessInstance) context.parameterObject;
    if (_instance == null) {
      // try to parse from the stream
      ExtensionRegistry registry = PersisterHelper.buildRegistry(context, null);
      Header _header;
      try {
        _header = PersisterHelper.readFromStreamWithHeader(context, registry);
      } catch (ClassNotFoundException e) {
        // Java 5 does not accept [new IOException(String, Throwable)]
        IOException ioe = new IOException("Error deserializing process instance.");
        ioe.initCause(e);
        throw ioe;
      }
      _instance = JBPMMessages.ProcessInstance.parseFrom(_header.getPayload(), registry);
    }

    WorkflowProcessInstanceImpl processInstance = createProcessInstance();
    processInstance.setId(_instance.getId());
    String processId = _instance.getProcessId();
    processInstance.setProcessId(processId);
    String processXml = _instance.getProcessXml();
    Process process = null;
    if (processXml != null && processXml.trim().length() > 0) {
      processInstance.setProcessXml(processXml);
      process = processInstance.getProcess();
    } else {
      process = ruleBase.getProcess(processId);
      processInstance.setProcess(process);
    }
    processInstance.setState(_instance.getState());
    long nodeInstanceCounter = _instance.getNodeInstanceCounter();
    processInstance.setKnowledgeRuntime(wm.getKnowledgeRuntime());

    if (_instance.getSwimlaneContextCount() > 0) {
      Context swimlaneContext =
          ((org.jbpm.process.core.Process) process)
              .getDefaultContext(SwimlaneContext.SWIMLANE_SCOPE);
      SwimlaneContextInstance swimlaneContextInstance =
          (SwimlaneContextInstance) processInstance.getContextInstance(swimlaneContext);
      for (JBPMMessages.ProcessInstance.SwimlaneContextInstance _swimlane :
          _instance.getSwimlaneContextList()) {
        swimlaneContextInstance.setActorId(_swimlane.getSwimlane(), _swimlane.getActorId());
      }
    }

    for (JBPMMessages.ProcessInstance.NodeInstance _node : _instance.getNodeInstanceList()) {
      context.parameterObject = _node;
      readNodeInstance(context, processInstance, processInstance);
    }

    for (JBPMMessages.ProcessInstance.ExclusiveGroupInstance _excl :
        _instance.getExclusiveGroupList()) {
      ExclusiveGroupInstance exclusiveGroupInstance = new ExclusiveGroupInstance();
      processInstance.addContextInstance(ExclusiveGroup.EXCLUSIVE_GROUP, exclusiveGroupInstance);
      for (Long nodeInstanceId : _excl.getGroupNodeInstanceIdList()) {
        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);
      }
    }

    if (_instance.getVariableCount() > 0) {
      Context variableScope =
          ((org.jbpm.process.core.Process) process).getDefaultContext(VariableScope.VARIABLE_SCOPE);
      VariableScopeInstance variableScopeInstance =
          (VariableScopeInstance) processInstance.getContextInstance(variableScope);
      for (JBPMMessages.Variable _variable : _instance.getVariableList()) {
        try {
          Object _value = ProtobufProcessMarshaller.unmarshallVariableValue(context, _variable);
          variableScopeInstance.internalSetVariable(_variable.getName(), _value);
        } catch (ClassNotFoundException e) {
          throw new IllegalArgumentException("Could not reload variable " + _variable.getName());
        }
      }
    }
    processInstance.internalSetNodeInstanceCounter(nodeInstanceCounter);
    if (wm != null) {
      processInstance.reconnect();
    }
    return processInstance;
  }