// find segment root
while ( BetaNode.parentInSameSegment(tupleSource) ) {
tupleSource = tupleSource.getLeftTupleSource();
}
LeftTupleSource segmentRoot = tupleSource;
SegmentMemory smem = new SegmentMemory(segmentRoot);
// Iterate all nodes on the same segment, assigning their position as a bit mask value
// allLinkedTestMask is the resulting mask used to test if all nodes are linked in
long nodePosMask = 1;
long allLinkedTestMask = 0;
while ( true ) {
if ( NodeTypeEnums.isBetaNode( tupleSource ) ) {
BetaMemory betaMemory;
BetaNode betaNode = ( BetaNode ) tupleSource;
if ( NodeTypeEnums.AccumulateNode == tupleSource.getType() ) {
betaMemory = (( AccumulateMemory ) smem.createNodeMemory( ( AccumulateNode ) tupleSource, wm )).getBetaMemory();
} else {
betaMemory = ( BetaMemory ) smem.createNodeMemory( betaNode, wm );
}
if ( betaNode.isRightInputIsRiaNode() ) {
// we need to iterate to find correct pair
// as there may be more than one set of sub networks, due to sharing.
LeftTupleSinkNode sinkNode = betaNode.getLeftTupleSource().getSinkPropagator().getFirstLeftTupleSink();
while ( sinkNode.getNextLeftTupleSinkNode() != betaNode ) {
sinkNode = sinkNode.getNextLeftTupleSinkNode();
}
SegmentMemory subNetworkSegmentMemory = createSegmentMemory( ( LeftTupleSource ) sinkNode, wm );
betaMemory.setSubnetworkSegmentMemor( subNetworkSegmentMemory );
}
betaMemory.setSegmentMemory( smem );
betaMemory.setNodePosMaskBit( nodePosMask );
allLinkedTestMask = allLinkedTestMask | nodePosMask;
if ( NodeTypeEnums.NotNode == tupleSource.getType() || NodeTypeEnums.AccumulateNode == tupleSource.getType()) {
// NotNode's and Accumulate are initialised as linkedin
smem.linkNode( nodePosMask, wm );
}
nodePosMask = nodePosMask << 1;
} else if ( tupleSource.getType() == NodeTypeEnums.LeftInputAdapterNode ) {
LiaNodeMemory liaMemory = ( LiaNodeMemory ) smem.createNodeMemory( ( LeftInputAdapterNode ) tupleSource, wm );
liaMemory.setSegmentMemory( smem );
liaMemory.setNodePosMaskBit( nodePosMask );
allLinkedTestMask = allLinkedTestMask | nodePosMask;
nodePosMask = nodePosMask << 1;
} else if ( tupleSource.getType() == NodeTypeEnums.EvalConditionNode ) {
EvalMemory evalMemory = ( EvalMemory ) smem.createNodeMemory( ( EvalConditionNode ) tupleSource, wm );
evalMemory.setSegmentMemory( smem );
} else if ( tupleSource.getType() == NodeTypeEnums.FromNode ) {
FromMemory fromMemory = ( FromMemory ) smem.createNodeMemory( ( FromNode ) tupleSource, wm );
fromMemory.getBetaMemory().setSegmentMemory( smem );
}
LeftTupleSinkPropagator sink = tupleSource.getSinkPropagator();
LeftTupleSinkNode firstSink = (LeftTupleSinkNode) sink.getFirstLeftTupleSink() ;
LeftTupleSinkNode secondSink = firstSink.getNextLeftTupleSinkNode();
if ( secondSink == null ) {
if ( NodeTypeEnums.isLeftTupleSource( firstSink ) ) {
tupleSource = ( LeftTupleSource ) firstSink;
} else {
// rtn or rian
break;
}
} else if ( sink.size() == 2 &&
NodeTypeEnums.isBetaNode( secondSink ) &&
((BetaNode)secondSink).isRightInputIsRiaNode() ) {
// must be a subnetwork split, always take the non riaNode path
tupleSource = ( LeftTupleSource )secondSink;
} else {
// not in same segment
break;
}
}
smem.setAllLinkedMaskTest( allLinkedTestMask );
smem.setTipNode( tupleSource );
// iterate to find root and determine the SegmentNodes position in the RuleSegment
LeftTupleSource parent = segmentRoot;
int ruleSegmentPosMask = 1;
int counter = 0;
while ( parent.getLeftTupleSource() != null ) {
if ( !BetaNode.parentInSameSegment( parent ) ) {
// for each new found segment, increase the mask bit position
ruleSegmentPosMask = ruleSegmentPosMask << 1;
counter++;
}
parent = parent.getLeftTupleSource();
}
smem.setSegmentPosMaskBit( ruleSegmentPosMask );
smem.setPos( counter );
SegmentUtilities.updateRiaAndTerminalMemory( 0, tupleSource, tupleSource, smem, wm );