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 void writeFactHandle( MarshallerWriteContext context, ObjectOutputStream stream, ObjectMarshallingStrategyStore objectMarshallingStrategyStore, int type, InternalFactHandle handle) throws IOException { stream.writeInt(type); stream.writeInt(handle.getId()); stream.writeLong(handle.getRecency()); if (type == 2) { // is event EventFactHandle efh = (EventFactHandle) handle; stream.writeLong(efh.getStartTimestamp()); stream.writeLong(efh.getDuration()); stream.writeBoolean(efh.isExpired()); stream.writeLong(efh.getActivationsCount()); } // context.out.println( "Object : int:" + handle.getId() + " long:" + handle.getRecency() ); // context.out.println( handle.getObject() ); Object object = handle.getObject(); // Old versions wrote -1 and tested >= 0 to see if there was a strategy available // Now, we write -2 to indicate that we write the strategy class name to the stream stream.writeInt(-2); if (object != null) { ObjectMarshallingStrategy strategy = objectMarshallingStrategyStore.getStrategyObject(object); String strategyClassName = strategy.getClass().getName(); stream.writeUTF(strategyClassName); strategy.write(stream, object); } else { stream.writeUTF(""); } if (handle.getEntryPoint() instanceof InternalWorkingMemoryEntryPoint) { String entryPoint = ((InternalWorkingMemoryEntryPoint) handle.getEntryPoint()) .getEntryPoint() .getEntryPointId(); if (entryPoint != null && !entryPoint.equals("")) { stream.writeBoolean(true); stream.writeUTF(entryPoint); } else { stream.writeBoolean(false); } } else { stream.writeBoolean(false); } }
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; }
// ------------------------------------------------------------ // Constructors // ------------------------------------------------------------ public LeftTuple( final InternalFactHandle factHandle, LeftTupleSink sink, boolean leftTupleMemoryEnabled) { this.handle = factHandle; this.recency = factHandle.getRecency(); this.hashCode = handle.hashCode(); if (leftTupleMemoryEnabled) { LeftTuple currentFirst = handle.getLeftTuple(); if (currentFirst != null) { currentFirst.leftParentPrevious = this; this.leftParentNext = currentFirst; } handle.setLeftTuple(this); } this.sink = sink; }
private static ProtobufMessages.FactHandle writeFactHandle( MarshallerWriteContext context, ObjectMarshallingStrategyStore objectMarshallingStrategyStore, InternalFactHandle handle) throws IOException { ProtobufMessages.FactHandle.Builder _handle = ProtobufMessages.FactHandle.newBuilder(); _handle.setType(getHandleType(handle)); _handle.setId(handle.getId()); _handle.setRecency(handle.getRecency()); if (_handle.getType() == ProtobufMessages.FactHandle.HandleType.EVENT) { // is event EventFactHandle efh = (EventFactHandle) handle; _handle.setTimestamp(efh.getStartTimestamp()); _handle.setDuration(efh.getDuration()); _handle.setIsExpired(efh.isExpired()); _handle.setActivationsCount(efh.getActivationsCount()); } Object object = handle.getObject(); if (object != null) { ObjectMarshallingStrategy strategy = objectMarshallingStrategyStore.getStrategyObject(object); String strategyClassName = strategy.getClass().getName(); Integer index = context.usedStrategies.get(strategyClassName); if (index == null) { index = Integer.valueOf(context.usedStrategies.size()); context.usedStrategies.put(strategyClassName, index); } _handle.setStrategyIndex(index.intValue()); _handle.setObject(ByteString.copyFrom(strategy.marshal(context, object))); } return _handle.build(); }
public static void writeLeftTuple( LeftTuple leftTuple, MarshallerWriteContext context, boolean recurse) throws IOException { ObjectOutputStream stream = context.stream; InternalRuleBase ruleBase = context.ruleBase; InternalWorkingMemory wm = context.wm; LeftTupleSink sink = leftTuple.getLeftTupleSink(); switch (sink.getType()) { case NodeTypeEnums.JoinNode: { // context.out.println( "JoinNode" ); for (LeftTuple childLeftTuple = leftTuple.getFirstChild(); childLeftTuple != null; childLeftTuple = (LeftTuple) childLeftTuple.getLeftParentNext()) { stream.writeShort(PersisterEnums.RIGHT_TUPLE); int childSinkId = childLeftTuple.getLeftTupleSink().getId(); stream.writeInt(childSinkId); stream.writeInt(childLeftTuple.getRightParent().getFactHandle().getId()); // context.out.println( "RightTuple int:" + childLeftTuple.getLeftTupleSink().getId() + // " int:" + childLeftTuple.getRightParent().getFactHandle().getId() ); writeLeftTuple(childLeftTuple, context, recurse); } stream.writeShort(PersisterEnums.END); // context.out.println( "JoinNode --- END" ); break; } case NodeTypeEnums.QueryRiaFixerNode: case NodeTypeEnums.EvalConditionNode: { // context.out.println( ".... EvalConditionNode" ); for (LeftTuple childLeftTuple = leftTuple.getFirstChild(); childLeftTuple != null; childLeftTuple = (LeftTuple) childLeftTuple.getLeftParentNext()) { stream.writeShort(PersisterEnums.LEFT_TUPLE); stream.writeInt(childLeftTuple.getLeftTupleSink().getId()); writeLeftTuple(childLeftTuple, context, recurse); } stream.writeShort(PersisterEnums.END); // context.out.println( "---- EvalConditionNode --- END" ); break; } case NodeTypeEnums.NotNode: case NodeTypeEnums.ForallNotNode: { if (leftTuple.getBlocker() == null) { // is not blocked so has children stream.writeShort(PersisterEnums.LEFT_TUPLE_NOT_BLOCKED); for (LeftTuple childLeftTuple = leftTuple.getFirstChild(); childLeftTuple != null; childLeftTuple = (LeftTuple) childLeftTuple.getLeftParentNext()) { stream.writeShort(PersisterEnums.LEFT_TUPLE); stream.writeInt(childLeftTuple.getLeftTupleSink().getId()); writeLeftTuple(childLeftTuple, context, recurse); } stream.writeShort(PersisterEnums.END); } else { stream.writeShort(PersisterEnums.LEFT_TUPLE_BLOCKED); stream.writeInt(leftTuple.getBlocker().getFactHandle().getId()); } break; } case NodeTypeEnums.ExistsNode: { if (leftTuple.getBlocker() == null) { // is blocked so has children stream.writeShort(PersisterEnums.LEFT_TUPLE_NOT_BLOCKED); } else { stream.writeShort(PersisterEnums.LEFT_TUPLE_BLOCKED); stream.writeInt(leftTuple.getBlocker().getFactHandle().getId()); for (LeftTuple childLeftTuple = leftTuple.getFirstChild(); childLeftTuple != null; childLeftTuple = (LeftTuple) childLeftTuple.getLeftParentNext()) { stream.writeShort(PersisterEnums.LEFT_TUPLE); stream.writeInt(childLeftTuple.getLeftTupleSink().getId()); writeLeftTuple(childLeftTuple, context, recurse); } stream.writeShort(PersisterEnums.END); } break; } case NodeTypeEnums.AccumulateNode: { // context.out.println( ".... AccumulateNode" ); // accumulate nodes generate new facts on-demand and need special procedures when // serializing to persistent storage AccumulateMemory memory = (AccumulateMemory) context.wm.getNodeMemory((BetaNode) sink); AccumulateContext accctx = (AccumulateContext) leftTuple.getObject(); // first we serialize the generated fact handle writeFactHandle( context, stream, context.objectMarshallingStrategyStore, accctx.result.getFactHandle()); // then we serialize the associated accumulation context stream.writeObject(accctx.context); // then we serialize the boolean propagated flag stream.writeBoolean(accctx.propagated); // then we serialize all the propagated tuples for (LeftTuple childLeftTuple = leftTuple.getFirstChild(); childLeftTuple != null; childLeftTuple = (LeftTuple) childLeftTuple.getLeftParentNext()) { if (leftTuple.getLeftTupleSink().getId() == childLeftTuple.getLeftTupleSink().getId()) { // this is a matching record, so, associate the right tuples // context.out.println( "RightTuple(match) int:" + // childLeftTuple.getLeftTupleSink().getId() + " int:" + // childLeftTuple.getRightParent().getFactHandle().getId() ); stream.writeShort(PersisterEnums.RIGHT_TUPLE); stream.writeInt(childLeftTuple.getRightParent().getFactHandle().getId()); } else { // this is a propagation record // context.out.println( "RightTuple(propagation) int:" + // childLeftTuple.getLeftTupleSink().getId() + " int:" + // childLeftTuple.getRightParent().getFactHandle().getId() ); stream.writeShort(PersisterEnums.LEFT_TUPLE); int sinkId = childLeftTuple.getLeftTupleSink().getId(); stream.writeInt(sinkId); writeLeftTuple(childLeftTuple, context, recurse); } } stream.writeShort(PersisterEnums.END); // context.out.println( "---- AccumulateNode --- END" ); break; } case NodeTypeEnums.RightInputAdaterNode: { // context.out.println( ".... RightInputAdapterNode" ); // RIANs generate new fact handles on-demand to wrap tuples and need special procedures // when serializing to persistent storage ObjectHashMap memory = (ObjectHashMap) context.wm.getNodeMemory((NodeMemory) sink); InternalFactHandle ifh = (InternalFactHandle) memory.get(leftTuple); // first we serialize the generated fact handle ID // context.out.println( "FactHandle id:"+ifh.getId() ); stream.writeInt(ifh.getId()); stream.writeLong(ifh.getRecency()); writeRightTuples(ifh, context); stream.writeShort(PersisterEnums.END); // context.out.println( "---- RightInputAdapterNode --- END" ); break; } case NodeTypeEnums.FromNode: { // context.out.println( ".... FromNode" ); // FNs generate new fact handles on-demand to wrap objects and need special procedures // when serializing to persistent storage FromMemory memory = (FromMemory) context.wm.getNodeMemory((NodeMemory) sink); Map<Object, RightTuple> matches = (Map<Object, RightTuple>) leftTuple.getObject(); for (RightTuple rightTuples : matches.values()) { // first we serialize the generated fact handle ID stream.writeShort(PersisterEnums.FACT_HANDLE); writeFactHandle( context, stream, context.objectMarshallingStrategyStore, rightTuples.getFactHandle()); writeRightTuples(rightTuples.getFactHandle(), context); } stream.writeShort(PersisterEnums.END); for (LeftTuple childLeftTuple = leftTuple.getFirstChild(); childLeftTuple != null; childLeftTuple = (LeftTuple) childLeftTuple.getLeftParentNext()) { stream.writeShort(PersisterEnums.RIGHT_TUPLE); stream.writeInt(childLeftTuple.getLeftTupleSink().getId()); stream.writeInt(childLeftTuple.getRightParent().getFactHandle().getId()); // context.out.println( "RightTuple int:" + childLeftTuple.getLeftTupleSink().getId() + // " int:" + childLeftTuple.getRightParent().getFactHandle().getId() ); writeLeftTuple(childLeftTuple, context, recurse); } stream.writeShort(PersisterEnums.END); // context.out.println( "---- FromNode --- END" ); break; } case NodeTypeEnums.UnificationNode: { // context.out.println( ".... UnificationNode" ); QueryElementNode node = (QueryElementNode) sink; boolean isOpen = node.isOpenQuery(); context.writeBoolean(isOpen); if (isOpen) { InternalFactHandle factHandle = (InternalFactHandle) leftTuple.getObject(); DroolsQuery query = (DroolsQuery) factHandle.getObject(); // context.out.println( "factHandle:" + factHandle ); factHandle.setObject(null); writeFactHandle(context, stream, context.objectMarshallingStrategyStore, 0, factHandle); factHandle.setObject(query); writeLeftTuples(context, new InternalFactHandle[] {factHandle}); } else { for (LeftTuple childLeftTuple = leftTuple.getFirstChild(); childLeftTuple != null; childLeftTuple = (LeftTuple) childLeftTuple.getLeftParentNext()) { stream.writeShort(PersisterEnums.LEFT_TUPLE); stream.writeInt(childLeftTuple.getLeftTupleSink().getId()); InternalFactHandle factHandle = childLeftTuple.getLastHandle(); writeFactHandle( context, stream, context.objectMarshallingStrategyStore, 1, factHandle); writeLeftTuple(childLeftTuple, context, recurse); } stream.writeShort(PersisterEnums.END); } // context.out.println( "---- EvalConditionNode --- END" ); break; } case NodeTypeEnums.RuleTerminalNode: { // context.out.println( "RuleTerminalNode" ); int pos = context.terminalTupleMap.size(); context.terminalTupleMap.put(leftTuple, pos); break; } case NodeTypeEnums.QueryTerminalNode: { // context.out.println( ".... QueryTerminalNode" ); // LeftTuple entry = leftTuple; // // // find the DroolsQuery object // while ( entry.getParent() != null ) { // entry = entry.getParent(); // } // // // Now output all the child tuples in the caller network // DroolsQuery query = (DroolsQuery) entry.getLastHandle().getObject(); // if ( query.getQueryResultCollector() instanceof // UnificationNodeViewChangedEventListener ) { // context.writeBoolean( true ); // UnificationNodeViewChangedEventListener collector = // (UnificationNodeViewChangedEventListener) query.getQueryResultCollector(); // leftTuple = collector.getLeftTuple(); // context.writeBoolean(true); RightTuple rightTuple = (RightTuple) leftTuple.getObject(); // context.out.println( "rightTuple:" + rightTuple.getFactHandle() ); writeFactHandle( context, stream, context.objectMarshallingStrategyStore, 1, rightTuple.getFactHandle()); for (LeftTuple childLeftTuple = rightTuple.firstChild; childLeftTuple != null; childLeftTuple = (LeftTuple) childLeftTuple.getRightParentNext()) { stream.writeShort(PersisterEnums.LEFT_TUPLE); stream.writeInt(childLeftTuple.getLeftTupleSink().getId()); writeLeftTuple(childLeftTuple, context, recurse); } // for ( LeftTuple childLeftTuple = leftTuple.getFirstChild(); // childLeftTuple != null; childLeftTuple = (LeftTuple) childLeftTuple.getLeftParentNext() // ) { // stream.writeShort( PersisterEnums.LEFT_TUPLE ); // stream.writeInt( childLeftTuple.getLeftTupleSink().getId() ); // writeFactHandle( context, // stream, // context.objectMarshallingStrategyStore, // 1, // childLeftTuple.getLastHandle() ); // writeLeftTuple( childLeftTuple, // context, // recurse ); // } // } else { // context.writeBoolean( false ); // } stream.writeShort(PersisterEnums.END); // context.out.println( "---- QueryTerminalNode --- END" ); break; } } }
public static void writeSession(MarshallerWriteContext context) throws IOException { // context.out.println( "... write session"); ReteooWorkingMemory wm = (ReteooWorkingMemory) context.wm; wm.getAgenda().unstageActivations(); final boolean multithread = wm.isPartitionManagersActive(); // is multi-thread active? if (multithread) { context.writeBoolean(true); wm.stopPartitionManagers(); } else { context.writeBoolean(false); } long time = 0; if (context.wm.getTimerService() instanceof PseudoClockScheduler) { time = context.clockTime; } context.writeLong(time); context.writeInt(wm.getFactHandleFactory().getId()); context.writeLong(wm.getFactHandleFactory().getRecency()); //// context.out.println( "FactHandleFactory int:" + wm.getFactHandleFactory().getId() + " // long:" + wm.getFactHandleFactory().getRecency() ); context.writeLong(wm.getPropagationIdCounter()); // context.out.println( "PropagationCounter long:" + wm.getPropagationIdCounter() ); InternalFactHandle handle = context.wm.getInitialFactHandle(); context.writeInt(handle.getId()); context.writeLong(handle.getRecency()); // context.out.println( "InitialFact int:" + handle.getId() + " long:" + handle.getRecency() ); writeAgenda(context); writeInitialFactHandleRightTuples(context); for (WorkingMemoryEntryPoint wmep : wm.getEntryPoints().values()) { context.stream.writeShort(PersisterEnums.ENTRY_POINT); context.stream.writeUTF(wmep.getEntryPointId()); writeFactHandles(context, ((NamedEntryPoint) wmep).getObjectStore()); } context.stream.writeShort(PersisterEnums.END); writeInitialFactHandleLeftTuples(context); writePropagationContexts(context); writeActivations(context); writeActionQueue(context); writeTruthMaintenanceSystem(context); if (context.marshalProcessInstances && processMarshaller != null) { processMarshaller.writeProcessInstances(context); } else { context.stream.writeShort(PersisterEnums.END); } if (context.marshalWorkItems && processMarshaller != null) { processMarshaller.writeWorkItems(context); } else { context.stream.writeShort(PersisterEnums.END); } if (processMarshaller != null) { // this now just assigns the writer, it will not write out any timer information processMarshaller.writeProcessTimers(context); } else { context.stream.writeShort(PersisterEnums.END); } // Only works for JpaJDKTimerService writeTimers(context.wm.getTimerService().getTimerJobInstances(), context); if (multithread) { wm.startPartitionManagers(); } // context.out.println( "--- write session --- END"); }
private static ProtobufMessages.KnowledgeSession serializeSession(MarshallerWriteContext context) throws IOException { ReteooWorkingMemory wm = (ReteooWorkingMemory) context.wm; wm.getAgenda().unstageActivations(); ProtobufMessages.RuleData.Builder _ruleData = ProtobufMessages.RuleData.newBuilder(); final boolean multithread = wm.isPartitionManagersActive(); if (multithread) { wm.stopPartitionManagers(); } long time = 0; if (context.wm.getTimerService() instanceof PseudoClockScheduler) { time = context.clockTime; } _ruleData.setLastId(wm.getFactHandleFactory().getId()); _ruleData.setLastRecency(wm.getFactHandleFactory().getRecency()); InternalFactHandle handle = context.wm.getInitialFactHandle(); ProtobufMessages.FactHandle _ifh = ProtobufMessages.FactHandle.newBuilder() .setType(ProtobufMessages.FactHandle.HandleType.INITIAL_FACT) .setId(handle.getId()) .setRecency(handle.getRecency()) .build(); _ruleData.setInitialFact(_ifh); writeAgenda(context, _ruleData); writeNodeMemories(context, _ruleData); for (WorkingMemoryEntryPoint wmep : wm.getEntryPoints().values()) { org.drools.marshalling.impl.ProtobufMessages.EntryPoint.Builder _epb = ProtobufMessages.EntryPoint.newBuilder(); _epb.setEntryPointId(wmep.getEntryPointId()); writeFactHandles(context, _epb, ((NamedEntryPoint) wmep).getObjectStore()); _ruleData.addEntryPoint(_epb.build()); } writeActionQueue(context, _ruleData); writeTruthMaintenanceSystem(context, _ruleData); ProtobufMessages.KnowledgeSession.Builder _session = ProtobufMessages.KnowledgeSession.newBuilder() .setMultithread(multithread) .setTime(time) .setRuleData(_ruleData.build()); if (processMarshaller != null) { Builder _pdata = ProtobufMessages.ProcessData.newBuilder(); if (context.marshalProcessInstances) { context.parameterObject = _pdata; processMarshaller.writeProcessInstances(context); } if (context.marshalWorkItems) { context.parameterObject = _pdata; processMarshaller.writeWorkItems(context); } // this now just assigns the writer, it will not write out any timer information context.parameterObject = _pdata; processMarshaller.writeProcessTimers(context); _session.setProcessData(_pdata.build()); } Timers _timers = writeTimers(context.wm.getTimerService().getTimerJobInstances(), context); if (_timers != null) { _session.setTimers(_timers); } if (multithread) { wm.startPartitionManagers(); } return _session.build(); }