⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 notnode.java

📁 jboss规则引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * <code>ReteTuple<code>, any resulting proppagated joins are also retracted. 
     * 
     * @param key
     *            The tuple key.
     * @param context
     *            The <code>PropagationContext</code>
     * @param workingMemory
     *            The working memory seesion.
     */
    public void retractTuple(final ReteTuple leftTuple,
                             final PropagationContext context,
                             final ReteooWorkingMemory workingMemory) {
        final BetaMemory memory = (BetaMemory) workingMemory.getNodeMemory( this );
        memory.remove( workingMemory,
                       leftTuple );

        final Map matches = leftTuple.getTupleMatches();

        if ( !matches.isEmpty() ) {
            for ( final Iterator it = matches.values().iterator(); it.hasNext(); ) {
                final TupleMatch tupleMatch = (TupleMatch) it.next();
                tupleMatch.getObjectMatches().remove( tupleMatch );
            }
        }

        propagateRetractTuple( leftTuple,
                               context,
                               workingMemory );
    }

    public void modifyTuple(final ReteTuple leftTuple,
                            final PropagationContext context,
                            final ReteooWorkingMemory workingMemory) {
        final BetaMemory memory = (BetaMemory) workingMemory.getNodeMemory( this );

        // We remove the tuple as now its modified it needs to go to the top of
        // the stack, which is added back in else where
        memory.remove( workingMemory,
                       leftTuple );
        // ensure the tuple is at the top of the memory
        memory.add( workingMemory,
                    leftTuple );

        // TIRELLI's NOTE: the following is necessary because in case memory  
        // indexing is enabled, the loop over right objects may skip some of the
        // previously matched objects
        final Map oldMatches = new HashMap(leftTuple.getTupleMatches());
        leftTuple.getTupleMatches().clear();

        final int previous = oldMatches.size();
        final BetaNodeBinder binder = getJoinNodeBinder();

        for ( final Iterator rightIterator = memory.rightObjectIterator( workingMemory,
                                                                         leftTuple ); rightIterator.hasNext(); ) {
            final ObjectMatches objectMatches = (ObjectMatches) rightIterator.next();
            final DefaultFactHandle handle = objectMatches.getFactHandle();

            if ( binder.isAllowed( handle,
                                   leftTuple,
                                   workingMemory ) ) {
                // test passes
                TupleMatch tupleMatch = (TupleMatch) oldMatches.remove( handle );
                if ( tupleMatch == null ) {
                    // no previous matches so add a match now
                    tupleMatch = objectMatches.add( leftTuple );
                }
                leftTuple.addTupleMatch( handle,
                                         tupleMatch );
            } else {
                final TupleMatch tupleMatch = (TupleMatch) oldMatches.remove( handle );
                if ( tupleMatch != null ) {
                    // use to match and doesn't any more, so remove match
                    objectMatches.remove( tupleMatch );
                }
            }
        }

        // TIRELLI's NOTE: the following is necessary because in case memory  
        // indexing is enabled, the loop over right objects may skip some of the
        // previously matched objects
        for ( final Iterator oldMatchesIt = oldMatches.values().iterator(); oldMatchesIt.hasNext(); ) {
            final TupleMatch tupleMatch = (TupleMatch) oldMatchesIt.next();
            tupleMatch.getObjectMatches().remove( tupleMatch );
        }
        
        if ( previous == 0 && leftTuple.matchesSize() == 0 ) {
            propagateModifyTuple( leftTuple,
                                  context,
                                  workingMemory );
        } else if ( previous != 0 && leftTuple.matchesSize() == 0 ) {
            propagateAssertTuple( leftTuple,
                                  context,
                                  workingMemory );
        } else if ( previous == 0 && leftTuple.matchesSize() != 0 ) {
            propagateRetractTuple( leftTuple,
                                   context,
                                   workingMemory );
        }
    }

    public void modifyObject(final DefaultFactHandle handle,
                             final PropagationContext context,
                             final ReteooWorkingMemory workingMemory) {
        final BetaMemory memory = (BetaMemory) workingMemory.getNodeMemory( this );

        // Remove the FactHandle from memory
        final ObjectMatches objectMatches = memory.remove( workingMemory,
                                                           handle );
        memory.add( workingMemory,
                    objectMatches );

        TupleMatch tupleMatch = objectMatches.getFirstTupleMatch();
        final BetaNodeBinder binder = getJoinNodeBinder();
        
        for ( final Iterator it = memory.leftTupleIterator( workingMemory,
                                                            handle ); it.hasNext(); ) {
            final ReteTuple leftTuple = (ReteTuple) it.next();
            
            if ( tupleMatch != null && tupleMatch.getTuple() == leftTuple ) {
                // has previous match so need to decide whether to continue
                // modify or retract
                final int previous = leftTuple.getTupleMatches().size();
                TupleMatch nextTupleMatch = (TupleMatch) tupleMatch.getNext();
                if ( !binder.isAllowed( handle,
                                        leftTuple,
                                        workingMemory ) ) {
                    leftTuple.removeMatch( handle );
                    objectMatches.remove( tupleMatch );
                } 
                if ( previous == 0 && leftTuple.matchesSize() == 0 ) {
                    propagateModifyTuple( leftTuple,
                                          context,
                                          workingMemory );
                } else if ( previous != 0 && leftTuple.matchesSize() == 0 ) {
                    propagateAssertTuple( leftTuple,
                                          context,
                                          workingMemory );
                } else if ( previous == 0 && leftTuple.matchesSize() != 0 ) {
                    propagateRetractTuple( leftTuple,
                                           context,
                                           workingMemory );
                }

                tupleMatch = (TupleMatch) nextTupleMatch;
            } else {
                // no previous join, so attempt join now
                final int previousSize = leftTuple.matchesSize();
                attemptJoin( leftTuple,
                             handle,
                             objectMatches,
                             binder,
                             workingMemory );
                if ( previousSize == 0 && leftTuple.matchesSize() != 0 ) {
                    propagateRetractTuple( leftTuple,
                                           context,
                                           workingMemory );
                }
            }
        }
    }

    /* (non-Javadoc)
     * @see org.drools.reteoo.BaseNode#updateNewNode(org.drools.reteoo.WorkingMemoryImpl, org.drools.spi.PropagationContext)
     */
    public void updateNewNode(final ReteooWorkingMemory workingMemory,
                              final PropagationContext context) {
        this.attachingNewNode = true;

        final BetaMemory memory = (BetaMemory) workingMemory.getNodeMemory( this );
        for ( final Iterator it = memory.getLeftTupleMemory().iterator(); it.hasNext(); ) {
            final ReteTuple leftTuple = (ReteTuple) it.next();
            if ( leftTuple.matchesSize() == 0 ) {
                final ReteTuple child = new ReteTuple( leftTuple );
                // no TupleMatch so instead add as a linked tuple
                leftTuple.addLinkedTuple( new LinkedListObjectWrapper( child ) );
                ((TupleSink) getTupleSinks().get( getTupleSinks().size() - 1 )).assertTuple( child,
                                                                                             context,
                                                                                             workingMemory );
            }
        }

        this.attachingNewNode = true;
    }

    /**
     * @inheritDoc
     */
    public List getPropagatedTuples(final ReteooWorkingMemory workingMemory,
                                    final TupleSink sink) {
        final BetaMemory memory = (BetaMemory) workingMemory.getNodeMemory( this );
        final int index = this.getTupleSinks().indexOf( sink );
        final List propagatedTuples = new ArrayList();

        for ( final Iterator it = memory.getLeftTupleMemory().iterator(); it.hasNext(); ) {
            final ReteTuple leftTuple = (ReteTuple) it.next();
            final LinkedList linkedTuples = leftTuple.getLinkedTuples();

            LinkedListObjectWrapper wrapper = (LinkedListObjectWrapper) linkedTuples.getFirst();
            for ( int i = 0; i < index; i++ ) {
                wrapper = (LinkedListObjectWrapper) wrapper.getNext();
            }
            propagatedTuples.add( wrapper.getObject() );
        }
        return propagatedTuples;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -