private static ProtobufMessages.NodeMemory writeRIANodeMemory(
      final int nodeId,
      final MarshallerWriteContext context,
      final BaseNode node,
      final NodeMemories memories,
      final Memory memory) {
    RightInputAdapterNode riaNode = (RightInputAdapterNode) node;

    ObjectSink[] sinks = riaNode.getSinkPropagator().getSinks();
    BetaNode betaNode = (BetaNode) sinks[0];

    Memory betaMemory = memories.peekNodeMemory(betaNode.getId());
    if (betaMemory == null) {
      return null;
    }
    BetaMemory bm;
    if (betaNode.getType() == NodeTypeEnums.AccumulateNode) {
      bm = ((AccumulateMemory) betaMemory).getBetaMemory();
    } else {
      bm = (BetaMemory) betaMemory;
    }

    // for RIA nodes, we need to store the ID of the created handles
    bm.getRightTupleMemory().iterator();
    if (bm.getRightTupleMemory().size() > 0) {
      ProtobufMessages.NodeMemory.RIANodeMemory.Builder _ria =
          ProtobufMessages.NodeMemory.RIANodeMemory.newBuilder();
      final org.drools.core.util.Iterator it = bm.getRightTupleMemory().iterator();

      // iterates over all propagated handles and assert them to the new sink
      for (RightTuple entry = (RightTuple) it.next();
          entry != null;
          entry = (RightTuple) it.next()) {
        LeftTuple leftTuple = (LeftTuple) entry.getFactHandle().getObject();
        InternalFactHandle handle = (InternalFactHandle) leftTuple.getObject();
        FactHandle _handle =
            ProtobufMessages.FactHandle.newBuilder()
                .setId(handle.getId())
                .setRecency(handle.getRecency())
                .build();
        _ria.addContext(
            ProtobufMessages.NodeMemory.RIANodeMemory.RIAContext.newBuilder()
                .setTuple(PersisterHelper.createTuple(leftTuple))
                .setResultHandle(_handle)
                .build());
      }

      return ProtobufMessages.NodeMemory.newBuilder()
          .setNodeId(nodeId)
          .setNodeType(ProtobufMessages.NodeMemory.NodeType.RIA)
          .setRia(_ria.build())
          .build();
    }
    return null;
  }
 private static void writeNodeMemories(
     MarshallerWriteContext context, ProtobufMessages.RuleData.Builder _ksb) throws IOException {
   InternalWorkingMemory wm = context.wm;
   NodeMemories memories = wm.getNodeMemories();
   // only some of the node memories require special serialization handling
   // so we iterate over all of them and process only those that require it
   for (int i = 0; i < memories.length(); i++) {
     Memory memory = memories.peekNodeMemory(i);
     // some nodes have no memory, so we need to check for nulls
     if (memory != null) {
       ProtobufMessages.NodeMemory _node = null;
       switch (memory.getNodeType()) {
         case NodeTypeEnums.AccumulateNode:
           {
             _node = writeAccumulateNodeMemory(i, memory);
             break;
           }
         case NodeTypeEnums.RightInputAdaterNode:
           {
             _node = writeRIANodeMemory(i, context, context.sinks.get(i), memories, memory);
             break;
           }
         case NodeTypeEnums.FromNode:
           {
             _node = writeFromNodeMemory(i, memory);
             break;
           }
         case NodeTypeEnums.QueryElementNode:
           {
             _node = writeQueryElementNodeMemory(i, memory, wm);
             break;
           }
       }
       if (_node != null) {
         // not all node memories require serialization
         _ksb.addNodeMemory(_node);
       }
     }
   }
 }