Package org.drools.reteoo

Examples of org.drools.reteoo.SegmentMemory


    public int evaluateNetwork(InternalWorkingMemory wm) {
        SegmentMemory[] smems = rmem.getSegmentMemories();

        int smemIndex = 0;
        SegmentMemory smem = smems[smemIndex]; // 0
        LeftInputAdapterNode liaNode = (LeftInputAdapterNode) smem.getRootNode();

        LinkedList<Memory> nodeMemories = smem.getNodeMemories();

        LiaNodeMemory liaNodeMemory = (LiaNodeMemory) nodeMemories.getFirst();

        StagedLeftTuples trgTuples = new StagedLeftTuples();
        StagedLeftTuples srcTuples = smem.getStagedLeftTuples();
        pLiaNode.doNode( liaNode, liaNodeMemory, wm, srcTuples, trgTuples );

        LeftTupleSource node = null;
        Memory nodeMem = null;
        if ( liaNode == smem.getTipNode() ) {
            // Segment only contains LiaNode, so need to propagate peers
            SegmentPropagator.propagate( smem, trgTuples, wm );
            smem = smems[smemIndex]; // 1
            nodeMem = smem.getNodeMemories().getFirst();
            node = smem.getRootNode();
        } else {
            node = (LeftTupleSource) liaNode.getSinkPropagator().getFirstLeftTupleSink(); // we know it can only have one child
            nodeMem = liaNodeMemory.getNext();
        }
View Full Code Here


    public StagedLeftTuples eval( LeftTupleSource node, Memory nodeMem, SegmentMemory[] smems, int smemIndex, StagedLeftTuples trgTuples, StagedLeftTuples stagedLeftTuples, InternalWorkingMemory wm) {
        StagedLeftTuples srcTuples = null;
       
        boolean foundSegmentTip = false;
       
        SegmentMemory smem = smems[smemIndex];
        while ( true ) {
            srcTuples = trgTuples; // previous target, is now the source
           
            if ( NodeTypeEnums.isTerminalNode( node ) ) {
                RuleTerminalNode rtn = rmem.getRuleTerminalNode();
                pRtnNode.doNode( rtn, wm, srcTuples );
                break; // returns null;
            } else if ( NodeTypeEnums.RightInputAdaterNode == node.getType() ) {
                return trgTuples;
            } else if ( nodeMem == null ) {
                // Reached end of segment, start on new segment.
                SegmentPropagator.propagate( smem, trgTuples, wm );
                smem = smems[smemIndex++];
                node = smem.getRootNode();
                nodeMem = smem.getNodeMemories().getFirst();
            }
         
           
            if ( node == smem.getTipNode() && smem.getFirst() != null) {
                // we are about to process the segment tip, so allow it to merge insert/update/delete clashes
                // can be null if next sink is RTN, or RiaNode
                stagedLeftTuples = smem.getFirst().getStagedLeftTuples();
            } else {
                stagedLeftTuples = null;
            }         

            trgTuples = new StagedLeftTuples();
           
            if ( NodeTypeEnums.isBetaNode( node ) ) {
                BetaNode betaNode = ( BetaNode )node;
                BetaMemory bm = (BetaMemory) nodeMem;
                if ( betaNode.isRightInputIsRiaNode() ) {
                    StagedLeftTuples peerTuples = new StagedLeftTuples();
                    SegmentPropagator.processPeers( srcTuples, peerTuples, betaNode );
                   
                    // Make sure subnetwork Segment has tuples to process
                    SegmentMemory subSmem = bm.getSubnetworkSegmentMemory();
                    StagedLeftTuples subnetworkStaged =  subSmem.getStagedLeftTuples();
                    subnetworkStaged.addAllDeletes( srcTuples.getDeleteFirst() );
                    subnetworkStaged.addAllUpdates( srcTuples.getUpdateFirst() );
                    subnetworkStaged.addAllInserts( srcTuples.getInsertFirst() );
                  
                    List<RuleMemory> ruleMemories = subSmem.getRuleMemories();
                   
                    RiaRuleMemory rm = null;
                    if ( ruleMemories.size() == 1 ) {
                        rm = ( RiaRuleMemory) ruleMemories.get( 0 ) ;
                    } else {
                        for ( int i = 0, size = ruleMemories.size(); i < size; i++ ) {
                            if ( ruleMemories.get( i ) instanceof RiaRuleMemory ) {
                                rm = ( RiaRuleMemory) ruleMemories.get( i ) ;
                            }
                        }
                    }
                   
                    RightInputAdapterNode riaNode = ( RightInputAdapterNode ) betaNode.getRightInput();
                    // At this point we have the tuples to apply to the left and to the right of the subnetwork root node
                    StagedLeftTuples riaStagedTuples = eval( subSmem.getRootNode(), subSmem.getNodeMemories().getFirst(), rm.getSegmentMemories(), 0, srcTuples, null, wm );
                    StagedRightTuples riaStageRight = bm.getStagedRightTuples();
                    for ( LeftTuple leftTuple = riaStagedTuples.getInsertFirst(); leftTuple != null; leftTuple = leftTuple.getStagedNext() ) {
                        InternalFactHandle handle = riaNode.createFactHandle( leftTuple, leftTuple.getPropagationContext(), wm );
                        RightTuple rightTuple = betaNode.createRightTuple( handle, betaNode, leftTuple.getPropagationContext() );
                        //riaStagedTuples.
View Full Code Here

        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 );
View Full Code Here

        if ( sourceSegment.isEmpty() ) {
            // We know it's a Composite 
            LeftTupleSinkNodeList list = ((CompositeLeftTupleSinkAdapter)source.getSinkPropagator()).getRawSinks();
            for (LeftTupleSinkNode sink = list.getFirst(); sink != null; sink = sink.getNextLeftTupleSinkNode() ) {
                Memory memory = wm.getNodeMemories().getNodeMemory( ( MemoryFactory ) sink );
                SegmentMemory smem = memory.getSegmentMemory();
                sourceSegment.add( smem );
            }
        }
               
        processPeers(sourceSegment, stagedLeftTuples);
View Full Code Here

    }
   
    public static void processPeers(SegmentMemory sourceSegment, StagedLeftTuples leftTuples) {   
       
        // Process Deletes
        SegmentMemory firstSmem = sourceSegment.getFirst();
        if ( leftTuples.getDeleteFirst() != null ) {
            for ( LeftTuple leftTuple = leftTuples.getDeleteFirst(); leftTuple != null; leftTuple = leftTuple.getStagedNext()) {                       
                SegmentMemory smem = firstSmem.getNext();
                if ( smem != null ) {
                    for ( LeftTuple peer = leftTuple.getPeer(); peer != null; peer = peer.getPeer() ) {
                        StagedLeftTuples stagedLeftTuples = smem.getStagedLeftTuples();
                        switch ( peer.getStagedType() ) {
                            // handle clash with already staged entries
                            case LeftTuple.INSERT:
                                stagedLeftTuples.removeInsert( peer );
                                break;
                            case LeftTuple.UPDATE:
                                stagedLeftTuples.removeUpdate( peer );
                                break;
                       
                        stagedLeftTuples.addDelete( peer );
                        smem = smem.getNext();
                    }
                }
            }
            firstSmem.getStagedLeftTuples().addAllDeletes( leftTuples.getDeleteFirst() );
        }
       
        // Process Updates       
        if ( leftTuples.getUpdateFirst() != null ) {
            firstSmem = sourceSegment.getFirst();
            for ( LeftTuple leftTuple = leftTuples.getUpdateFirst(); leftTuple != null; leftTuple = leftTuple.getStagedNext()) {           
                SegmentMemory smem = firstSmem.getNext();
                if ( smem != null ) {               
                    for ( LeftTuple peer = leftTuple.getPeer(); peer != null; peer = peer.getPeer() ) {
                        StagedLeftTuples stagedLeftTuples = smem.getStagedLeftTuples();
                        switch ( peer.getStagedType() ) {
                            // handle clash with already staged entries
                            case LeftTuple.INSERT:
                                stagedLeftTuples.removeInsert( peer );
                                break;
                       
                        stagedLeftTuples.addUpdate( peer );
                        smem = smem.getNext();
                    }
                }           
            }  
            firstSmem.getStagedLeftTuples().addAllUpdates( leftTuples.getUpdateFirst() );
        }
       
        // Process Inserts
        if ( leftTuples.getInsertFirst() != null ) {
            firstSmem = sourceSegment.getFirst();
            for ( LeftTuple leftTuple = leftTuples.getInsertFirst(); leftTuple != null; leftTuple =  leftTuple.getStagedNext()) {           
                SegmentMemory smem = firstSmem.getNext();
                if ( smem != null ) {
                    LeftTuple peer = leftTuple;
                    for (; smem != null; smem = smem.getNext() ) {
                        peer  = smem.getRootNode().createPeer( peer );
                        smem.getStagedLeftTuples().addInsert( peer );
                    }
                }          
            }
            firstSmem.getStagedLeftTuples().addAllInserts( leftTuples.getInsertFirst() );
        }
View Full Code Here

        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, this happens if betaNode is rootNode.
                    // 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();
                    }
                   
                    Memory mem = wm.getNodeMemory( (MemoryFactory) sinkNode );
                    SegmentMemory subNetworkSegmentMemory = mem.getSegmentMemory();
                    if ( subNetworkSegmentMemory == null ) {
                        // we need to stop recursion here
                        subNetworkSegmentMemory = createSegmentMemory( ( LeftTupleSource ) sinkNode, wm );
                    }
                    betaMemory.setSubnetworkSegmentMemory( subNetworkSegmentMemory );
View Full Code Here

                    SegmentUtilities.createSegmentMemory( (LeftTupleSource ) sink, wm );
                }
            } else {
                // RTNS and RiaNode's have their own segment, if they are the child of a split.
                if ( memory.getSegmentMemory() == null ) {
                    SegmentMemory childSmem = new SegmentMemory(sink);
                    RuleMemory rmem;
                    if ( NodeTypeEnums.isTerminalNode( sink  ) ) {
                        rmem = ( RuleMemory ) memory;
                    } else {
                        rmem =  ((RiaNodeMemory) memory ).getRiaRuleMemory();
                    }
                    rmem.getSegmentMemories()[ rmem.getSegmentMemories().length -1 ] = childSmem;
                    rmem.setSegmentMemory( childSmem );
                   
                    childSmem.setTipNode( sink );
                    childSmem.setSinkFactory( sink );                   
                }               
            }
            smem.add( memory.getSegmentMemory() );
        }
    }
View Full Code Here

    }   
   
    public static void processPeers(SegmentMemory sourceSegment, LeftTupleSets leftTuples) {   
       
        // Process Deletes
        SegmentMemory firstSmem = sourceSegment.getFirst();
        if ( leftTuples.getDeleteFirst() != null ) {
            for ( LeftTuple leftTuple = leftTuples.getDeleteFirst(); leftTuple != null; leftTuple = leftTuple.getStagedNext()) {                       
                SegmentMemory smem = firstSmem.getNext();
                if ( smem != null ) {
                    for ( LeftTuple peer = leftTuple.getPeer(); peer != null; peer = peer.getPeer() ) {
                        LeftTupleSets stagedLeftTuples = smem.getStagedLeftTuples();
                        switch ( peer.getStagedType() ) {
                            // handle clash with already staged entries
                            case LeftTuple.INSERT:
                                stagedLeftTuples.removeInsert( peer );
                                break;
                            case LeftTuple.UPDATE:
                                stagedLeftTuples.removeUpdate( peer );
                                break;
                       
                        stagedLeftTuples.addDelete( peer );
                        smem = smem.getNext();
                    }
                }
            }
            firstSmem.getStagedLeftTuples().addAllDeletes( leftTuples );
        }
       
        // Process Updates       
        if ( leftTuples.getUpdateFirst() != null ) {
            firstSmem = sourceSegment.getFirst();
            for ( LeftTuple leftTuple = leftTuples.getUpdateFirst(); leftTuple != null; leftTuple = leftTuple.getStagedNext()) {           
                SegmentMemory smem = firstSmem.getNext();
                if ( smem != null ) {               
                    for ( LeftTuple peer = leftTuple.getPeer(); peer != null; peer = peer.getPeer() ) {
                        if ( peer.getStagedType() == LeftTuple.NONE ) {
                            // only stage, if not already staged, if insert, leave as insert
                            LeftTupleSets stagedLeftTuples = smem.getStagedLeftTuples();
                            stagedLeftTuples.addUpdate( peer );
                        }
                       
                        smem = smem.getNext();
                    }
                }           
            }  
            firstSmem.getStagedLeftTuples().addAllUpdates( leftTuples );
        }
       
        // Process Inserts
        if ( leftTuples.getInsertFirst() != null ) {
            firstSmem = sourceSegment.getFirst();
            for ( LeftTuple leftTuple = leftTuples.getInsertFirst(); leftTuple != null; leftTuple =  leftTuple.getStagedNext()) {           
                SegmentMemory smem = firstSmem.getNext();
                if ( smem != null ) {
                    LeftTuple peer = leftTuple;
                    for (; smem != null; smem = smem.getNext() ) {
                        peer  =  ((LeftTupleSink)smem.getRootNode()).createPeer( peer );
                        smem.getStagedLeftTuples().addInsert( peer );
                    }
                }          
            }
            firstSmem.getStagedLeftTuples().addAllInserts( leftTuples );
        }
View Full Code Here

    public int evaluateNetwork(InternalWorkingMemory wm) {
        SegmentMemory[] smems = rmem.getSegmentMemories();

        int smemIndex = 0;
        SegmentMemory smem = smems[smemIndex]; // 0
        LeftInputAdapterNode liaNode = (LeftInputAdapterNode) smem.getRootNode();

        NetworkNode node;
        Memory nodeMem;
        if ( liaNode == smem.getTipNode() ) {
            // segment only has liaNode in it
            // nothing is staged in the liaNode, so skip to next segment          
            smem = smems[++smemIndex]; // 1
            node = smem.getRootNode();
            nodeMem = smem.getNodeMemories().getFirst();
        } else {
            // lia is in shared segment, so point to next node
            LeftTupleSinkPropagator sink = liaNode.getSinkPropagator();
            LeftTupleSinkNode firstSink = (LeftTupleSinkNode) sink.getFirstLeftTupleSink() ;
            LeftTupleSinkNode secondSink = firstSink.getNextLeftTupleSinkNode();
            if ( sink.size() == 2 ) {
                // As we check above for segment splits, if the sink size is 2, it must be a subnetwork.
                // Always take the non riaNode path
                node = secondSink;
            } else {
                node = firstSink;
            }  
            nodeMem = smem.getNodeMemories().getFirst().getNext(); // skip the liaNode memory
        }
       
        LeftTupleSets srcTuples = smem.getStagedLeftTuples();
       
        if ( log.isTraceEnabled() ) {
        //     log.trace( "Start Rule[name={}] {}", getRule().getName(), srcTuples.toStringSizes() );
        }
View Full Code Here

    }
   
    public LeftTupleSets eval( NetworkNode node, Memory nodeMem, SegmentMemory[] smems, int smemIndex, LeftTupleSets trgTuples, LeftTupleSets stagedLeftTuples, InternalWorkingMemory wm) {
        LeftTupleSets srcTuples;
       
        SegmentMemory smem = smems[smemIndex];
        while ( true ) {
//            if ( log.isTraceEnabled() ) {
//                log.trace( "Start Node[offset={}, type={}] Rule[name={}]", getRule().getName() );
//            }
            srcTuples = trgTuples; // previous target, is now the source
           
            if ( NodeTypeEnums.isTerminalNode( node ) ) {
                RuleTerminalNode rtn = rmem.getRuleTerminalNode();
                pRtnNode.doNode( rtn, wm, srcTuples );
                return null;
            } else if ( NodeTypeEnums.RightInputAdaterNode == node.getType() ) {
                return trgTuples;
            }          
           
            if ( node == smem.getTipNode() && smem.getFirst() != null) {
                // we are about to process the segment tip, allow it to merge insert/update/delete clashes
                // Can happen if the next segments have not yet been initialized
                stagedLeftTuples = smem.getFirst().getStagedLeftTuples();
            } else {
                stagedLeftTuples = null;
            }         
           
            LeftTupleSinkNode sink = ((LeftTupleSource)node).getSinkPropagator().getFirstLeftTupleSink();

            trgTuples = new LeftTupleSets();
           
            if ( NodeTypeEnums.isBetaNode( node ) ) {
                BetaNode betaNode = ( BetaNode )node;
               
                BetaMemory bm = null;
                AccumulateMemory am = null;
                if ( NodeTypeEnums.AccumulateNode == node.getType() ) {
                    am = (AccumulateMemory) nodeMem;
                    bm = am.getBetaMemory();
                } else {
                    bm = (BetaMemory) nodeMem;   
                }               
               
                if ( betaNode.isRightInputIsRiaNode() ) {
                    // if the subnetwork is nested in this segment, it will create srcTuples containing
                    // peer LeftTuples, suitable for the node in the main path.
                    srcTuples = doRiaNode( wm,
                                           srcTuples,
                                           betaNode,
                                           bm );                   
                }
               
                switch( node.getType() ) {
                    case  NodeTypeEnums.JoinNode:                   
                        pJoinNode.doNode( (JoinNode) node, sink,
                                          bm, wm, srcTuples, trgTuples, stagedLeftTuples );
                        break;
                    case  NodeTypeEnums.NotNode:
                        pNotNode.doNode( (NotNode) node, sink,
                                         bm, wm, srcTuples, trgTuples, stagedLeftTuples );
                        break;
                    case  NodeTypeEnums.ExistsNode:
                        pExistsNode.doNode( (ExistsNode) node, sink,
                                            bm, wm, srcTuples, trgTuples, stagedLeftTuples );
                        break;
                    case  NodeTypeEnums.AccumulateNode:
                        pAccNode.doNode( (AccumulateNode) node, sink,
                                         am, wm, srcTuples, trgTuples, stagedLeftTuples );
                        break;                          
                }
            } else if ( node.getType() == NodeTypeEnums.EvalConditionNode ) {
                pEvalNode.doNode( (EvalConditionNode) node, (EvalMemory) nodeMem, sink,
                                   wm, srcTuples, trgTuples, stagedLeftTuples );               
            } else if ( node.getType() == NodeTypeEnums.FromNode ) {
                pFromNode.doNode( (FromNode) node, (FromMemory) nodeMem, sink,
                                  wm, srcTuples, trgTuples, stagedLeftTuples );               
           } else if (true) {
               pBranchNode.doNode( (ConditionalBranchNode) node, (ConditionalBranchMemory) nodeMem, sink,
                                 wm, srcTuples, trgTuples, stagedLeftTuples );
           }
           
            if ( node != smem.getTipNode() ) {
                // get next node and node memory in the segment
                LeftTupleSink nextSink = sink.getNextLeftTupleSinkNode();
                if ( nextSink == null ) {
                    node = sink;
                } else {
                    // there is a nested subnetwork, take out path
                    node = nextSink;
                }
               
                nodeMem = nodeMem.getNext();
            } else {                           
                // Reached end of segment, start on new segment.
                SegmentPropagator.propagate( smem, trgTuples, wm );
                smem = smems[++smemIndex];
                trgTuples = smem.getStagedLeftTuples();
                node = ( LeftTupleSink ) smem.getRootNode();
                nodeMem = smem.getNodeMemories().getFirst();
            }
        }
    }
View Full Code Here

TOP

Related Classes of org.drools.reteoo.SegmentMemory

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.