public void updateSink(
      final ObjectSink sink,
      final PropagationContext context,
      final InternalWorkingMemory workingMemory) {
    BetaNode betaNode = (BetaNode) this.sink.getSinks()[0];

    Memory betaMemory = workingMemory.getNodeMemory(betaNode);
    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) {
      final org.drools.core.util.Iterator it = bm.getRightTupleMemory().iterator();
      for (RightTuple entry = (RightTuple) it.next();
          entry != null;
          entry = (RightTuple) it.next()) {
        LeftTuple leftTuple = (LeftTuple) entry.getFactHandle().getObject();
        InternalFactHandle handle = (InternalFactHandle) leftTuple.getObject();
        sink.assertObject(handle, context, workingMemory);
      }
    }
  }
  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;
  }
  @Override
  protected void doVisit(
      NetworkNode node, Stack<NetworkNode> nodeStack, StatefulKnowledgeSessionInfo info) {
    RightInputAdapterNode an = (RightInputAdapterNode) node;
    DefaultNodeInfo ni = (DefaultNodeInfo) info.getNodeInfo(node);

    BetaNode betaNode = (BetaNode) an.getSinkPropagator().getSinks()[0];

    Memory childMemory = info.getSession().getNodeMemory(betaNode);

    BetaMemory bm;
    if (betaNode.getType() == NodeTypeEnums.AccumulateNode) {
      bm = ((AccumulateMemory) childMemory).getBetaMemory();
    } else {
      bm = (BetaMemory) childMemory;
    }

    ni.setMemoryEnabled(true);
    ni.setTupleMemorySize(bm.getRightTupleMemory().size());
    ni.setCreatedFactHandles(bm.getRightTupleMemory().size());
  }
  public void removeMemory(InternalWorkingMemory workingMemory) {
    BetaNode betaNode = (BetaNode) this.sink.getSinks()[0];

    Memory betaMemory = workingMemory.getNodeMemory(betaNode);
    BetaMemory bm;
    if (betaNode.getType() == NodeTypeEnums.AccumulateNode) {
      bm = ((AccumulateMemory) betaMemory).getBetaMemory();
    } else {
      bm = (BetaMemory) betaMemory;
    }

    if (bm.getRightTupleMemory().size() > 0) {
      final Iterator it = bm.getRightTupleMemory().iterator();
      for (RightTuple entry = (RightTuple) it.next();
          entry != null;
          entry = (RightTuple) it.next()) {
        LeftTuple leftTuple = (LeftTuple) entry.getFactHandle().getObject();
        leftTuple.unlinkFromLeftParent();
        leftTuple.unlinkFromRightParent();
      }
    }
    workingMemory.clearNodeMemory(this);
  }
  private void doRiaNode2(
      InternalWorkingMemory wm, TupleSets<LeftTuple> srcTuples, RightInputAdapterNode riaNode) {

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

    BetaNode betaNode = (BetaNode) sinks[0];
    BetaMemory bm;
    Memory nodeMem = wm.getNodeMemory(betaNode);
    if (NodeTypeEnums.AccumulateNode == betaNode.getType()) {
      bm = ((AccumulateMemory) nodeMem).getBetaMemory();
    } else {
      bm = (BetaMemory) nodeMem;
    }

    // Build up iteration array for other sinks
    BetaNode[] bns = null;
    BetaMemory[] bms = null;
    int length = sinks.length;
    if (length > 1) {
      bns = new BetaNode[sinks.length - 1];
      bms = new BetaMemory[sinks.length - 1];
      for (int i = 1; i < length; i++) {
        bns[i - 1] = (BetaNode) sinks[i];
        Memory nodeMem2 = wm.getNodeMemory(bns[i - 1]);
        if (NodeTypeEnums.AccumulateNode == betaNode.getType()) {
          bms[i - 1] = ((AccumulateMemory) nodeMem2).getBetaMemory();
        } else {
          bms[i - 1] = (BetaMemory) nodeMem2;
        }
      }
    }

    length--; // subtract one, as first is not in the array;
    for (LeftTuple leftTuple = srcTuples.getInsertFirst(); leftTuple != null; ) {
      LeftTuple next = leftTuple.getStagedNext();

      PropagationContext pctx = leftTuple.getPropagationContext();
      InternalFactHandle handle = riaNode.createFactHandle(leftTuple, pctx, wm);

      RightTuple rightTuple = new RightTupleImpl(handle, betaNode);
      leftTuple.setContextObject(handle);
      rightTuple.setPropagationContext(pctx);

      if (bm.getStagedRightTuples().isEmpty()) {
        bm.setNodeDirtyWithoutNotify();
      }
      bm.getStagedRightTuples().addInsert(rightTuple);

      if (bns != null) {
        // Add peered RightTuples, they are attached to FH - unlink LeftTuples that has a peer ref
        for (int i = 0; i < length; i++) {
          rightTuple = new RightTupleImpl(handle, bns[i]);
          rightTuple.setPropagationContext(pctx);

          if (bms[i].getStagedRightTuples().isEmpty()) {
            bms[i].setNodeDirtyWithoutNotify();
          }
          bms[i].getStagedRightTuples().addInsert(rightTuple);
        }
      }

      leftTuple.clearStaged();
      leftTuple = next;
    }

    for (LeftTuple leftTuple = srcTuples.getDeleteFirst(); leftTuple != null; ) {
      LeftTuple next = leftTuple.getStagedNext();

      InternalFactHandle handle = (InternalFactHandle) leftTuple.getContextObject();
      RightTuple rightTuple = handle.getFirstRightTuple();
      TupleSets<RightTuple> rightTuples = bm.getStagedRightTuples();

      if (rightTuples.isEmpty()) {
        bm.setNodeDirtyWithoutNotify();
      }
      rightTuples.addDelete(rightTuple);

      if (bns != null) {
        // Add peered RightTuples, they are attached to FH - unlink LeftTuples that has a peer ref
        for (int i = 0; i < length; i++) {
          rightTuple = rightTuple.getHandleNext();
          rightTuples = bms[i].getStagedRightTuples();
          if (rightTuples.isEmpty()) {
            bms[i].setNodeDirtyWithoutNotify();
          }
          rightTuples.addDelete(rightTuple);
        }
      }

      leftTuple.clearStaged();
      leftTuple = next;
    }

    for (LeftTuple leftTuple = srcTuples.getUpdateFirst(); leftTuple != null; ) {
      LeftTuple next = leftTuple.getStagedNext();

      InternalFactHandle handle = (InternalFactHandle) leftTuple.getContextObject();
      RightTuple rightTuple = handle.getFirstRightTuple();
      TupleSets<RightTuple> rightTuples = bm.getStagedRightTuples();

      if (rightTuples.isEmpty()) {
        bm.setNodeDirtyWithoutNotify();
      }
      rightTuples.addUpdate(rightTuple);

      if (bns != null) {
        // Add peered RightTuples, they are attached to FH - unlink LeftTuples that has a peer ref
        for (int i = 0; i < length; i++) {
          rightTuple = rightTuple.getHandleNext();
          rightTuples = bms[i].getStagedRightTuples();

          if (rightTuples.isEmpty()) {
            bms[i].setNodeDirtyWithoutNotify();
          }
          rightTuples.addUpdate(rightTuple);
        }
      }

      leftTuple.clearStaged();
      leftTuple = next;
    }

    srcTuples.resetAll();
  }