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

📄 agenda.java

📁 drools 一个开放源码的规则引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                                                       eachItem.getKey( ),
                                                       eachItem );
                }
                else
                {
                    this.workingMemory.getEventSupport( ).fireActivationCancelled( rule,
                                                                                   eachItem.getTuple( ) );
                }
            }
        }

        itemIter = this.scheduledItems.values( ).iterator( );

        while ( itemIter.hasNext( ) )
        {
            eachItem = (AgendaItem) itemIter.next( );

            if ( eachItem.getRule( ) == rule && eachItem.getKey( ).containsAll( key ) )
            {
                if ( (this.mode == Agenda.MODIFY) && !this.workingMemory.getEventSupport( ).isEmpty( ) )
                {
                    this.scheduledItemsToRetract.putAgendaItem( rule,
                                                                eachItem.getKey( ),
                                                                eachItem );
                }
                else
                {
                    tuple = eachItem.getTuple( );

                    cancelItem( eachItem );

                    itemIter.remove( );

                    this.workingMemory.getEventSupport( ).fireActivationCancelled( rule,
                                                                                   tuple );
                }
            }
        }
    }
    
    /**
     * Remove a tuple from the agenda which has the specified xor group
     * 
     * @param key
     *            The key to the tuple to be removed.
     * @param xorGroup
     *            The xorGroup to remove.
     */
    void removeXorGroupFromAgenda(TupleKey key,
                                  String xorGroup)
    {
        if ( xorGroup == null )
        {
            return;
        }
        
        AgendaItem eachItem;
        Tuple tuple;
        Iterator itemIter = this.activationQueue.iterator();

        while ( itemIter.hasNext( ) )
        {
            eachItem = (AgendaItem) itemIter.next();

            if ( xorGroup.equals( eachItem.getRule().getXorGroup() ) )
            {
                itemIter.remove( );
                // need to restart iterator as priority queue could place elements before
                // current iterator position
                itemIter = this.activationQueue.iterator( );

                this.workingMemory.getEventSupport( ).fireActivationCancelled( eachItem.getRule(),
                                                                               eachItem.getTuple( ) );
            }
        }

        itemIter = this.scheduledItems.values( ).iterator( );

        while ( itemIter.hasNext( ) )
        {
            eachItem = (AgendaItem) itemIter.next( );

            if ( xorGroup.equals( eachItem.getRule().getXorGroup() ) )
            {
                    tuple = eachItem.getTuple( );

                    cancelItem( eachItem );

                    itemIter.remove( );

                    this.workingMemory.getEventSupport( ).fireActivationCancelled( eachItem.getRule(),
                                                                                   tuple );
            }
        }
    }    

    void removeMarkedItemsFromAgenda()
    {
        AgendaItemMap.RemoveDelegate delegate = new AgendaItemMap.RemoveDelegate( ) {
            public void processRemove(Object obj)
            {
                AgendaItem eachItem = (AgendaItem) obj;
                workingMemory.getEventSupport( ).fireActivationCancelled( eachItem.getRule( ),
                                                                          eachItem.getTuple( ) );
            }
        };
        itemsToRetract.removeAll( delegate );

        AgendaItemMap.RemoveDelegate scheduledDelegate = new AgendaItemMap.RemoveDelegate( ) {
            public void processRemove(Object obj)
            {
                AgendaItem eachItem = (AgendaItem) obj;
                cancelItem( eachItem );

                workingMemory.getEventSupport( ).fireActivationCancelled( eachItem.getRule( ),
                                                                          eachItem.getTuple( ) );
            }
        };
        scheduledItemsToRetract.removeAll( scheduledDelegate );

    }

    /**
     * Clears all Activations from the Agenda
     * 
     */
    void clearAgenda()
    {
        AgendaItem eachItem;

        // Remove all items in the Agenda and fire a Cancelled event for each
        Iterator iter = this.activationQueue.iterator( );
        while ( iter.hasNext( ) )
        {
            eachItem = (AgendaItem) iter.next( );

            iter.remove( );

            this.workingMemory.getEventSupport( ).fireActivationCancelled( eachItem.getRule( ),
                                                                           eachItem.getTuple( ) );
        }

        iter = this.scheduledItems.values( ).iterator( );

        // Cancel all items in the Schedule and fire a Cancelled event for each
        while ( iter.hasNext( ) )
        {
            eachItem = (AgendaItem) iter.next( );

            cancelItem( eachItem );

            iter.remove( );

            this.workingMemory.getEventSupport( ).fireActivationCancelled( eachItem.getRule( ),
                                                                           eachItem.getTuple( ) );
        }
    }

    /**
     * Schedule an agenda item for delayed firing.
     * 
     * @param item
     *            The item to schedule.
     */
    void scheduleItem(AgendaItem item)
    {
        Scheduler.getInstance( ).scheduleAgendaItem( item,
                                                     this.workingMemory );
    }

    /**
     * Cancel a scheduled agenda item for delayed firing.
     * 
     * @param item
     *            The item to cancel.
     */
    void cancelItem(AgendaItem item)
    {
        Scheduler.getInstance( ).cancelAgendaItem( item );
    }

    /**
     * Determine if this <code>Agenda</code> has any scheduled items.
     * 
     * @return <code>true<code> if the agenda is empty, otherwise
     *          <code>false</code>.
     */
    public boolean isEmpty()
    {
        return this.activationQueue.isEmpty( );
    }

    public int size()
    {
        return activationQueue.size( );
    }

    /**
     * Fire the next scheduled <code>Agenda</code> item.
     * 
     * @throws ConsequenceException
     *             If an error occurs while firing an agenda item.
     */
    public void fireNextItem(AgendaFilter filter) throws ConsequenceException
    {
        if ( isEmpty( ) )
        {
            return;
        }

        item = (AgendaItem) this.activationQueue.remove( );
        
        //if its in an xorGroup remove all other conflicts
        String xorGroup = item.getRule().getXorGroup();
        if ( xorGroup != null)
        {
            removeXorGroupFromAgenda( item.getKey(),
                                      xorGroup );
        }

        try
        {
            if ( filter == null || filter.accept( item ) )
            {
                item.fire( this.workingMemory );
            }
        }
        finally
        {
            item = null;
        }
    }

    /**
     * Sets the AsyncExceptionHandler to handle exceptions thrown by the Agenda
     * Scheduler used for duration rules.
     * 
     * @param handler
     */
    void setAsyncExceptionHandler(AsyncExceptionHandler handler)
    {
        Scheduler.getInstance( ).setAsyncExceptionHandler( handler );
    }

    void setMode(int mode)
    {
        this.mode = mode;
    }
}

⌨️ 快捷键说明

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