public TupleImpl getNextLeftTuple()

in drools-serialization-protobuf/src/main/java/org/drools/serialization/protobuf/iterators/LeftTupleIterator.java [194:354]


    public TupleImpl getNextLeftTuple(LeftTupleSource source,
                                      LeftTupleSink sink,
                                      TupleImpl leftTuple,
                                      InternalWorkingMemory wm) {

        if ( otnIterator != null ) {
            TupleImpl leftParent = leftTuple.getLeftParent();
            
            while ( leftTuple != null ) {
                leftTuple = leftTuple.getHandleNext();
                
                for ( ; leftTuple != null; leftTuple = leftTuple.getHandleNext() ) {
                    // Iterate to find the next left tuple for this sink, skip tuples for other sinks due to sharing split
                    if (leftTuple.getSink() == sink ) {
                        return leftTuple;
                    }
                }
            }
                                    
            // We have a parent LeftTuple so try there next
            if ( leftParent != null ) {
                // we know it has to be evalNode query element node
                while ( leftParent != null ) {
                    leftParent = getNextLeftTuple( source.getLeftTupleSource(),
                                                   (LeftTupleSink) source,
                                                   leftParent,
                                                   wm );

                    if ( leftParent != null ) {
                        for ( leftTuple = leftParent.getFirstChild(); leftTuple != null; leftTuple = leftTuple.getHandleNext() ) {
                            if (leftTuple.getSink() == sink ) {
                                return leftTuple;
                            }
                        }
                    }
                }
                return null;
            }

            // We have exhausted the current FactHandle, now try the next
            while ( otnIterator.hasNext() ) {
                InternalFactHandle handle = otnIterator.next();
                leftTuple = handle.findFirstLeftTuple( lt -> lt.getSink() == sink);
                if ( leftTuple != null ) {
                    return leftTuple;
                }
            }
            // We've exhausted this OTN so set the iterator to null
            otnIterator = null;

        } else if ( source instanceof AccumulateNode ) {
            // when using phreak, accumulate result tuples will not link to leftParent, but to parent instead 
            BetaMemory memory = ((AccumulateMemory) wm.getNodeMemory( (MemoryFactory) source )).getBetaMemory();
            FastIterator localIt = memory.getLeftTupleMemory().fullFastIterator( leftTuple.getParent() );

            TupleImpl childLeftTuple = leftTuple;
            leftTuple = childLeftTuple.getParent();

            while ( leftTuple != null ) {
                if ( childLeftTuple == null ) {
                    childLeftTuple = leftTuple.getFirstChild();
                } else {
                    childLeftTuple = childLeftTuple.getHandleNext();
                }
                for ( ; childLeftTuple != null; childLeftTuple = childLeftTuple.getHandleNext() ) {
                    if (childLeftTuple.getSink() == sink ) {
                        return childLeftTuple;
                    }
                }
                leftTuple = (TupleImpl) localIt.next(leftTuple );
            }

        } else if ( source instanceof JoinNode || source instanceof NotNode|| source instanceof FromNode || source instanceof AccumulateNode ) {
            BetaMemory memory;
            FastIterator localIt;
            if ( source instanceof FromNode ) {
                memory = ((FromMemory) wm.getNodeMemory( (MemoryFactory) source )).getBetaMemory();
            } else if ( source instanceof AccumulateNode ) {
                memory = ((AccumulateMemory) wm.getNodeMemory( (MemoryFactory) source )).getBetaMemory();
            } else {
                memory = (BetaMemory) wm.getNodeMemory( (MemoryFactory) source );
            }
            
            localIt = memory.getLeftTupleMemory().fullFastIterator( leftTuple.getLeftParent() );

            TupleImpl childLeftTuple = leftTuple;
            leftTuple = childLeftTuple.getLeftParent();

            while ( leftTuple != null ) {
                if ( childLeftTuple == null ) {
                    childLeftTuple = leftTuple.getFirstChild();
                } else {
                    childLeftTuple = childLeftTuple.getHandleNext();
                }
                for ( ; childLeftTuple != null; childLeftTuple = childLeftTuple.getHandleNext() ) {
                    if (childLeftTuple.getSink() == sink ) {
                        return childLeftTuple;
                    }
                }
                leftTuple = (TupleImpl) localIt.next(leftTuple );
            }
        }
        if ( source instanceof ExistsNode ) {
            BetaMemory memory = (BetaMemory) wm.getNodeMemory( (MemoryFactory) source );
            if (leftTuple != null) {
                RightTuple   rightTuple = (RightTuple) leftTuple.getLeftParent().getBlocker();
                FastIterator localIt    = memory.getRightTupleMemory().fullFastIterator( rightTuple );

                for ( TupleImpl childleftTuple = leftTuple.getHandleNext(); childleftTuple != null; childleftTuple = childleftTuple.getHandleNext() ) {
                    if (childleftTuple.getSink() == sink ) {
                        return childleftTuple;
                    }
                }

                leftTuple = leftTuple.getLeftParent();

                // now move onto next RightTuple
                while ( rightTuple != null ) {
                    if ( rightTuple.getBlocked() != null ) {
                        if ( leftTuple != null ) {
                            leftTuple = leftTuple.getBlockedNext();
                        } else {
                            leftTuple = rightTuple.getBlocked();
                        }
                        for ( ; leftTuple != null; leftTuple = leftTuple.getBlockedNext() ) {
                            for ( TupleImpl childleftTuple = leftTuple.getFirstChild(); childleftTuple != null; childleftTuple = childleftTuple.getHandleNext() ) {
                                if (childleftTuple.getSink() == sink ) {
                                    return childleftTuple;
                                }
                            }
                        }

                    }
                    rightTuple = (RightTuple) localIt.next(rightTuple);
                }
            }
        } else if ( source instanceof EvalConditionNode || source instanceof QueryElementNode ) {
            TupleImpl childLeftTuple = leftTuple;
            if ( leftTuple != null ) {
                leftTuple = leftTuple.getLeftParent();

                while ( leftTuple != null ) {
                    if ( childLeftTuple != null ) {
                        childLeftTuple = childLeftTuple.getHandleNext();
                    } else {
                        childLeftTuple = leftTuple.getFirstChild();
                    }
                    for ( ; childLeftTuple != null; childLeftTuple = childLeftTuple.getHandleNext() ) {
                        if (childLeftTuple.getSink() == sink ) {
                            return childLeftTuple;
                        }
                    }
                    leftTuple = getNextLeftTuple( source.getLeftTupleSource(),
                                                  (LeftTupleSink) source,
                                                  leftTuple,
                                                  wm );
                }
            }
        }
        return null;
    }