private static ProtobufMessages.NodeMemory writeRIANodeMemory( final int nodeId, final Memory memory) { // for RIA nodes, we need to store the ID of the created handles RIAMemory mem = (RIAMemory) memory; if (!mem.memory.isEmpty()) { ProtobufMessages.NodeMemory.RIANodeMemory.Builder _ria = ProtobufMessages.NodeMemory.RIANodeMemory.newBuilder(); final org.drools.core.util.Iterator it = mem.memory.iterator(); // iterates over all propagated handles and assert them to the new sink for (ObjectEntry entry = (ObjectEntry) it.next(); entry != null; entry = (ObjectEntry) it.next()) { LeftTuple leftTuple = (LeftTuple) entry.getKey(); InternalFactHandle handle = (InternalFactHandle) entry.getValue(); 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 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 ProtobufMessages.NodeMemory writeQueryElementNodeMemory( final int nodeId, final Memory memory, final InternalWorkingMemory wm) { LeftTupleIterator it = LeftTupleIterator.iterator(wm, ((QueryElementNodeMemory) memory).node); ProtobufMessages.NodeMemory.QueryElementNodeMemory.Builder _query = ProtobufMessages.NodeMemory.QueryElementNodeMemory.newBuilder(); for (LeftTuple leftTuple = (LeftTuple) it.next(); leftTuple != null; leftTuple = (LeftTuple) it.next()) { InternalFactHandle handle = (InternalFactHandle) leftTuple.getObject(); FactHandle _handle = ProtobufMessages.FactHandle.newBuilder() .setId(handle.getId()) .setRecency(handle.getRecency()) .build(); ProtobufMessages.NodeMemory.QueryElementNodeMemory.QueryContext.Builder _context = ProtobufMessages.NodeMemory.QueryElementNodeMemory.QueryContext.newBuilder() .setTuple(PersisterHelper.createTuple(leftTuple)) .setHandle(_handle); LeftTuple childLeftTuple = leftTuple.getFirstChild(); while (childLeftTuple != null) { RightTuple rightParent = childLeftTuple.getRightParent(); _context.addResult( ProtobufMessages.FactHandle.newBuilder() .setId(rightParent.getFactHandle().getId()) .setRecency(rightParent.getFactHandle().getRecency()) .build()); while (childLeftTuple != null && childLeftTuple.getRightParent() == rightParent) { // skip to the next child that has a different right parent childLeftTuple = childLeftTuple.getLeftParentNext(); } } _query.addContext(_context.build()); } return _query.getContextCount() > 0 ? ProtobufMessages.NodeMemory.newBuilder() .setNodeId(nodeId) .setNodeType(ProtobufMessages.NodeMemory.NodeType.QUERY_ELEMENT) .setQueryElement(_query.build()) .build() : null; }
@SuppressWarnings("unchecked") private static ProtobufMessages.NodeMemory writeFromNodeMemory( final int nodeId, final Memory memory) { FromMemory fromMemory = (FromMemory) memory; if (fromMemory.betaMemory.getLeftTupleMemory().size() > 0) { ProtobufMessages.NodeMemory.FromNodeMemory.Builder _from = ProtobufMessages.NodeMemory.FromNodeMemory.newBuilder(); final org.drools.core.util.Iterator tupleIter = fromMemory.betaMemory.getLeftTupleMemory().iterator(); for (LeftTuple leftTuple = (LeftTuple) tupleIter.next(); leftTuple != null; leftTuple = (LeftTuple) tupleIter.next()) { Map<Object, RightTuple> matches = (Map<Object, RightTuple>) leftTuple.getObject(); ProtobufMessages.NodeMemory.FromNodeMemory.FromContext.Builder _context = ProtobufMessages.NodeMemory.FromNodeMemory.FromContext.newBuilder() .setTuple(PersisterHelper.createTuple(leftTuple)); for (RightTuple rightTuple : matches.values()) { FactHandle _handle = ProtobufMessages.FactHandle.newBuilder() .setId(rightTuple.getFactHandle().getId()) .setRecency(rightTuple.getFactHandle().getRecency()) .build(); _context.addHandle(_handle); } _from.addContext(_context.build()); } return ProtobufMessages.NodeMemory.newBuilder() .setNodeId(nodeId) .setNodeType(ProtobufMessages.NodeMemory.NodeType.FROM) .setFrom(_from.build()) .build(); } return null; }
private static ProtobufMessages.NodeMemory writeAccumulateNodeMemory( final int nodeId, final Memory memory) { // for accumulate nodes, we need to store the ID of created (result) handles AccumulateMemory accmem = (AccumulateMemory) memory; if (accmem.betaMemory.getLeftTupleMemory().size() > 0) { ProtobufMessages.NodeMemory.AccumulateNodeMemory.Builder _accumulate = ProtobufMessages.NodeMemory.AccumulateNodeMemory.newBuilder(); final org.drools.core.util.Iterator tupleIter = accmem.betaMemory.getLeftTupleMemory().iterator(); for (LeftTuple leftTuple = (LeftTuple) tupleIter.next(); leftTuple != null; leftTuple = (LeftTuple) tupleIter.next()) { AccumulateContext accctx = (AccumulateContext) leftTuple.getObject(); if (accctx.result != null) { FactHandle _handle = ProtobufMessages.FactHandle.newBuilder() .setId(accctx.result.getFactHandle().getId()) .setRecency(accctx.result.getFactHandle().getRecency()) .build(); _accumulate.addContext( ProtobufMessages.NodeMemory.AccumulateNodeMemory.AccumulateContext.newBuilder() .setTuple(PersisterHelper.createTuple(leftTuple)) .setResultHandle(_handle) .build()); } } return ProtobufMessages.NodeMemory.newBuilder() .setNodeId(nodeId) .setNodeType(ProtobufMessages.NodeMemory.NodeType.ACCUMULATE) .setAccumulate(_accumulate.build()) .build(); } return null; }
private static void readNodeMemories(MarshallerReaderContext context, RuleData _session) { for (ProtobufMessages.NodeMemory _node : _session.getNodeMemoryList()) { Object memory = null; switch (_node.getNodeType()) { case ACCUMULATE: { Map<TupleKey, ProtobufMessages.FactHandle> map = new HashMap<TupleKey, ProtobufMessages.FactHandle>(); for (ProtobufMessages.NodeMemory.AccumulateNodeMemory.AccumulateContext _ctx : _node.getAccumulate().getContextList()) { map.put(PersisterHelper.createTupleKey(_ctx.getTuple()), _ctx.getResultHandle()); } memory = map; break; } case RIA: { Map<TupleKey, ProtobufMessages.FactHandle> map = new HashMap<TupleKey, ProtobufMessages.FactHandle>(); for (ProtobufMessages.NodeMemory.RIANodeMemory.RIAContext _ctx : _node.getRia().getContextList()) { map.put(PersisterHelper.createTupleKey(_ctx.getTuple()), _ctx.getResultHandle()); } memory = map; break; } case FROM: { Map<TupleKey, List<ProtobufMessages.FactHandle>> map = new HashMap<TupleKey, List<ProtobufMessages.FactHandle>>(); for (ProtobufMessages.NodeMemory.FromNodeMemory.FromContext _ctx : _node.getFrom().getContextList()) { // have to instantiate a modifiable list map.put( PersisterHelper.createTupleKey(_ctx.getTuple()), new LinkedList<ProtobufMessages.FactHandle>(_ctx.getHandleList())); } memory = map; break; } case QUERY_ELEMENT: { Map<TupleKey, QueryElementContext> map = new HashMap<TupleKey, QueryElementContext>(); for (ProtobufMessages.NodeMemory.QueryElementNodeMemory.QueryContext _ctx : _node.getQueryElement().getContextList()) { // we have to use a "cloned" query element context as we need to write on it during // deserialization process and the // protobuf one is read-only map.put( PersisterHelper.createTupleKey(_ctx.getTuple()), new QueryElementContext(_ctx)); } memory = map; break; } default: { throw new IllegalArgumentException( "Unknown node type " + _node.getNodeType() + " while deserializing session."); } } context.nodeMemories.put(_node.getNodeId(), memory); } }