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

📄 agenda.java

📁 drools 一个开放源码的规则引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.drools.reteoo;

/*
 * $Id: Agenda.java,v 1.59 2005/09/25 17:57:26 mproctor Exp $
 *
 * Copyright 2001-2003 (C) The Werken Company. All Rights Reserved.
 *
 * Redistribution and use of this software and associated documentation
 * ("Software"), with or without modification, are permitted provided that the
 * following conditions are met:
 *
 * 1. Redistributions of source code must retain copyright statements and
 * notices. Redistributions must also contain a copy of this document.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3. The name "drools" must not be used to endorse or promote products derived
 * from this Software without prior written permission of The Werken Company.
 * For written permission, please contact bob@werken.com.
 *
 * 4. Products derived from this Software may not be called "drools" nor may
 * "drools" appear in their names without prior written permission of The Werken
 * Company. "drools" is a trademark of The Werken Company.
 *
 * 5. Due credit should be given to The Werken Company. (http://werken.com/)
 *
 * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.drools.rule.Rule;
import org.drools.spi.AgendaFilter;
import org.drools.spi.AsyncExceptionHandler;
import org.drools.spi.ConflictResolver;
import org.drools.spi.ConsequenceException;
import org.drools.spi.Duration;
import org.drools.spi.Tuple;
import org.drools.util.PriorityQueue;

/**
 * Rule-firing Agenda.
 * 
 * <p>
 * Since many rules may be matched by a single assertObject(...) all scheduled
 * actions are placed into the <code>Agenda</code>.
 * </p>
 * 
 * <p>
 * While processing a scheduled action, it may modify or retract objects in
 * other scheduled actions, which must then be removed from the agenda.
 * Non-invalidated actions are left on the agenda, and are executed in turn.
 * </p>
 * 
 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter </a>
 * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris </a>
 */
class Agenda
    implements
    Serializable
{
    // ------------------------------------------------------------
    // Instance members
    // ------------------------------------------------------------

    public static int               NONE    = 0;
    public static int               ASSERT  = 1;
    public static int               MODIFY  = 2;
    public static int               RETRACT = 3;

    /** Working memory of this Agenda. */
    private final WorkingMemoryImpl workingMemory;

    /** Items in the agenda. */
    private final PriorityQueue     activationQueue;

    /** Items time-delayed. */
    private final Map               scheduledItems;

    /** retract maps for event normalisation */
    private final AgendaItemMap     itemsToRetract;

    private final AgendaItemMap     scheduledItemsToRetract;

    /** The current agenda item being fired; or null if none. */
    private AgendaItem              item;

    private int                     mode    = 0;

    // ------------------------------------------------------------
    // Constructors
    // ------------------------------------------------------------

    /**
     * Construct.
     * 
     * @param workingMemory
     *            The <code>WorkingMemory</code> of this agenda.
     * @param conflictResolver
     *            The conflict resolver.
     */
    public Agenda(WorkingMemoryImpl workingMemory,
                  ConflictResolver conflictResolver)
    {
        this.workingMemory = workingMemory;
        this.activationQueue = new PriorityQueue( conflictResolver );
        this.scheduledItems = new HashMap( );
        this.itemsToRetract = new AgendaItemMap( );
        this.scheduledItemsToRetract = new AgendaItemMap( );

    }
    
    public List getActivations()
    {
        List activations = new ArrayList();
        activations.addAll( this.activationQueue );
        activations.addAll( this.scheduledItems.values() );
        return activations;
    }

    // ------------------------------------------------------------
    // Instance methods
    // ------------------------------------------------------------

    /**
     * Schedule a rule action invokation on this <code>Agenda</code>. Rules
     * specified with noNoop=true that are active should not be added to the
     * agenda
     * 
     * @param tuple
     *            The matching <code>Tuple</code>.
     * @param rule
     *            The rule to fire.
     */
    void addToAgenda(ReteTuple tuple,
                     Rule rule)
    {
        
        
        /*
         * if no-loop is true for this rule and the current rule is active then
         * do not not re-add to the agenda
         * NOTE: this only applies for the same Rule/TupleKey combination.
         * For a different TupleKey (ie different facts to the rule currently firing) 
         * no-loop does not apply.
         */
        if ( this.item != null && rule.isNoLoop( ) && rule.equals( this.item.getRule( ) ) 
                && this.item.getKey().equals(tuple.getKey()) )
        {            
            return;
        }  
        
        
        

        Duration dur = rule.getDuration( );

        if ( dur != null && dur.getDuration( tuple ) > 0 )
        {
            // check if item has been retracted as part of a modify
            AgendaItem item = null;
            if ( !this.scheduledItemsToRetract.isEmpty( ) )
            {
                item = this.scheduledItemsToRetract.removeAgendaItem( rule,
                                                                      tuple.getKey( ) );
            }

            if ( item == null )
            {
                item = new AgendaItem( tuple,
                                       rule );
                this.scheduledItems.put( item.getKey( ),
                                         item );
                scheduleItem( item );
                this.workingMemory.getEventSupport( ).fireActivationCreated( rule,
                                                                             tuple );
            }
        }
        else
        {
            // check if item has been retracted as part of a modify
            AgendaItem item = null;
            if ( !this.itemsToRetract.isEmpty( ) )
            {
                item = this.itemsToRetract.removeAgendaItem( rule,
                                                             tuple.getKey( ) );
            }

            if ( item == null )
            {
                item = new AgendaItem( tuple,
                                       rule );
                this.activationQueue.add( item );
                this.workingMemory.getEventSupport( ).fireActivationCreated( rule,
                                                                             tuple );
            }
            else
            {
                this.activationQueue.add( item );
            }
        }
    }

    /**
     * Remove a tuple from the agenda.
     * 
     * @param key
     *            The key to the tuple to be removed.
     * @param rule
     *            The rule to remove.
     */
    void removeFromAgenda(TupleKey key,
                          Rule rule)
    {
        
        AgendaItem eachItem;
        Tuple tuple;
        Iterator itemIter = this.activationQueue.iterator( );

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

            if ( eachItem.getRule( ) == rule && eachItem.getKey( ).containsAll( key ) )
            {
                itemIter.remove( );
                // need to restart iterator as heap could place elements before
                // current iterator position
                itemIter = this.activationQueue.iterator( );

                if ( (this.mode == Agenda.MODIFY) && !this.workingMemory.getEventSupport( ).isEmpty( ) )
                {
                    this.itemsToRetract.putAgendaItem( rule,

⌨️ 快捷键说明

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