private InternalFactHandle getMostRecentFact(final InternalFactHandle[] handles) { InternalFactHandle mostRecent = handles[0]; for ( int i = 1; i < handles.length; i++ ) { final InternalFactHandle eachHandle = handles[i]; if ( eachHandle.getRecency() > mostRecent.getRecency() ) { mostRecent = eachHandle; } } return mostRecent; }
private InternalFactHandle getMostRecentFact(final InternalFactHandle[] handles, final InternalFactHandle handle) { InternalFactHandle mostRecent = null; for ( int i = 0; i < handles.length; i++ ) { final InternalFactHandle eachHandle = handles[i]; if ( mostRecent == null && eachHandle.getRecency() < handle.getRecency() ) { mostRecent = eachHandle; } if ( mostRecent != null && eachHandle.getRecency() > mostRecent.getRecency() && eachHandle.getRecency() < handle.getRecency() ) { mostRecent = eachHandle; } } return mostRecent; }
/** * @see ConflictResolver */ public int compare(final Activation lhs, final Activation rhs) { final InternalFactHandle[] lFacts = lhs.getTuple().getFactHandles(); final InternalFactHandle[] rFacts = rhs.getTuple().getFactHandles(); InternalFactHandle leftMostRecent = getMostRecentFact( lFacts ); InternalFactHandle rightMostRecent = getMostRecentFact( rFacts ); final int lastIndex = (lFacts.length < rFacts.length) ? lFacts.length : rFacts.length; if ( leftMostRecent.getRecency() == rightMostRecent.getRecency() && lastIndex > 1 ) { for ( int i = 0; i < lastIndex; i++ ) { leftMostRecent = getMostRecentFact( lFacts, leftMostRecent ); rightMostRecent = getMostRecentFact( rFacts, rightMostRecent ); if ( leftMostRecent == null || rightMostRecent == null ) { if ( leftMostRecent == null && rightMostRecent != null ) { return (int) rightMostRecent.getRecency(); } } else if ( leftMostRecent.getRecency() != rightMostRecent.getRecency() ) { return (int) (rightMostRecent.getRecency() - leftMostRecent.getRecency()); } } } else { return (int) (rightMostRecent.getRecency() - leftMostRecent.getRecency()); } return rFacts.length - lFacts.length; }
stream.writeInt( type ); stream.writeInt( handle.getId() ); stream.writeLong( handle.getRecency() );
public static DisconnectedFactHandle newFrom( FactHandle handle ) { if( handle instanceof DisconnectedFactHandle ) { return (DisconnectedFactHandle) handle; } else { InternalFactHandle ifh = (InternalFactHandle) handle; return new DisconnectedFactHandle(ifh.getId(), ifh.getIdentityHashCode(), ifh.getObjectHashCode(), ifh.getRecency(), ifh.getEntryPoint() != null ? ifh.getEntryPoint().getEntryPointId() : null, null, ifh.isValid(), ifh.isTraitOrTraitable() ); } }
public void rowAdded(final Rule rule, final LeftTuple tuple, final PropagationContext context, final InternalWorkingMemory workingMemory) { InternalFactHandle[] handles = new InternalFactHandle[tuple.getIndex() + 1]; LeftTuple entry = tuple; // Add all the FactHandles while ( entry != null) { InternalFactHandle handle = entry.getLastHandle(); handles[entry.getIndex()] = new DefaultFactHandle( handle.getId(), ( handle.getEntryPoint() != null ) ? handle.getEntryPoint().getEntryPointId() : null, handle.getIdentityHashCode(), handle.getObjectHashCode(), handle.getRecency(), handle.getObject() ); entry = entry.getParent(); } QueryTerminalNode node = ( QueryTerminalNode ) tuple.getLeftTupleSink(); this.results.add( new QueryRowWithSubruleIndex(handles, node.getSubruleIndex()) ); }
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()); _handle.setEntryPoint( handle.getEntryPoint().getEntryPointId() ); 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); Integer index = context.getStrategyIndex(strategy); _handle.setStrategyIndex(index.intValue()); _handle.setObject(ByteString.copyFrom(strategy.marshal(context.strategyContext.get(strategy), context, object))); } return _handle.build(); }
@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; }
FactHandle _handle = ProtobufMessages.FactHandle.newBuilder() .setId(handle.getId()) .setRecency(handle.getRecency()) .build(); _context.addResult(ProtobufMessages.FactHandle.newBuilder() .setId(rightParent.getFactHandle().getId()) .setRecency(rightParent.getFactHandle().getRecency()) .build()); while (childLeftTuple != null && childLeftTuple.getRightParent() == rightParent) {
stream.writeLong( ifh.getRecency() );
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 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; }
context.writeLong( handle.getRecency() );
.setType(ProtobufMessages.FactHandle.HandleType.INITIAL_FACT) .setId(handle.getId()) .setRecency(handle.getRecency()) .build(); _ruleData.setInitialFact(_ifh);