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

📄 builder.java

📁 rule engine drools-2.0-beta-18
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.drools.reteoo;/* * $Id: Builder.java,v 1.62 2004/11/29 12:50:32 simon 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 org.drools.RuleBase;import org.drools.RuleIntegrationException;import org.drools.RuleSetIntegrationException;import org.drools.conflict.DefaultConflictResolver;import org.drools.rule.Declaration;import org.drools.rule.Rule;import org.drools.rule.RuleSet;import org.drools.spi.Condition;import org.drools.spi.ConflictResolver;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.Set;/** * Builds the Rete-OO network for a <code>RuleSet</code>. * * @see org.drools.rule.RuleSet * * @author <a href="mailto:bob@werken.com">bob mcwhirter </a> * * TODO Make joinForCondition actually be intelligent enough to build optimal * joins. Currently using forgy's original description of 2-input nodes, which I * feel (but don't know for sure, is sub-optimal. */public class Builder{    // ------------------------------------------------------------    // Instance members    // ------------------------------------------------------------    /** Rete network to build against. */    private Rete rete;    /** Rule-sets added. */    private List ruleSets;    private Map applicationData;    private FactHandleFactory factHandleFactory;    private ConflictResolver conflictResolver;    // ------------------------------------------------------------    // Constructors    // ------------------------------------------------------------    /**     * Construct a <code>Builder</code> against an existing <code>Rete</code>     * network.     */    public Builder()    {        reset( );    }    // ------------------------------------------------------------    // Instance methods    // ------------------------------------------------------------    /**     * Build the <code>RuleBase</code>.     *     * @return The rule-base.     */    public RuleBase buildRuleBase()    {        RuleBase ruleBase = new RuleBaseImpl( this.rete,                                              this.conflictResolver,                                              this.factHandleFactory,                                              this.ruleSets,                                              this.applicationData);        reset( );        return ruleBase;    }    /**     * Set the <code>FactHandleFactory</code>.     *     * @param factHandleFactory     *            The fact handle factory.     */    public void setFactHandleFactory(FactHandleFactory factHandleFactory)    {        this.factHandleFactory = factHandleFactory;    }    /**     * Set the <code>ConflictResolver</code>.     *     * @param conflictResolver     *            The conflict resolver.     */    public void setConflictResolver(ConflictResolver conflictResolver)    {        this.conflictResolver = conflictResolver;    }    /**     * Add a <code>RuleSet</code> to the network.     *     * @param ruleSet     *            The rule-set to add.     *     * @throws RuleIntegrationException     *             if an error prevents complete construction of the network for     *             the <code>Rule</code>.     */    public void addRuleSet(RuleSet ruleSet) throws RuleIntegrationException,                                           RuleSetIntegrationException    {        this.ruleSets.add( ruleSet );        Map newApplicationData = ruleSet.getApplicationData( );        Iterator it = newApplicationData.keySet( ).iterator( );        String identifier;        Class type;        while ( it.hasNext( ) )        {            identifier = (String) it.next( );            type = (Class) newApplicationData.get( identifier );            if ( this.applicationData.containsKey( identifier ) && !this.applicationData.get( identifier ).equals( type ) )            {                throw new RuleSetIntegrationException( ruleSet );            }        }        this.applicationData.putAll( newApplicationData );        Rule[] rules = ruleSet.getRules( );        for ( int i = 0; i < rules.length; ++i )        {            addRule( rules[i] );        }    }    /**     * Add a <code>Rule</code> to the network.     *     * @param rule     *            The rule to add.     *     * @throws RuleIntegrationException     *             if an error prevents complete construction of the network for     *             the <code>Rule</code>.     */    protected void addRule(Rule rule) throws RuleIntegrationException    {        List conds = new LinkedList( rule.getConditions( ) );        List leafNodes = createParameterNodes( rule );        boolean performedJoin;        boolean joinedForCondition;        while ( true )        {            joinedForCondition = false;            if ( !conds.isEmpty( ) )            {                attachConditions( rule,                                  conds,                                  leafNodes );            }            performedJoin = createJoinNodes( leafNodes );            if ( !performedJoin && !conds.isEmpty( ) )            {                joinedForCondition = joinForCondition( conds,                                                       leafNodes );            }            if ( joinedForCondition )            {                continue;            }            if ( leafNodes.size( ) > 1 )            {                if ( !performedJoin )                {                    joinArbitrary( leafNodes );                }            }            else            {                break;            }        }        if ( leafNodes.size( ) != 1 )        {            throw new RuleIntegrationException( rule );        }        TupleSource lastNode = (TupleSource) leafNodes.iterator( ).next( );        TerminalNode terminal = new TerminalNode( lastNode,                                                  rule );    }    /**     * Create the <code>ParameterNode</code> s for the <code>Rule</code>,     * and link into the network.     *     * @param rule     *            The rule.     *     * @return A <code>Set</code> of <code>ParameterNodes</code> created and     *         linked into the network.     */    List createParameterNodes(Rule rule)    {        List leafNodes = new LinkedList( );        Iterator declIter = rule.getParameterDeclarations( ).iterator( );        Declaration eachDecl;        while ( declIter.hasNext( ) )        {            eachDecl = (Declaration) declIter.next( );            leafNodes.add( new ParameterNode( this.rete.getOrCreateObjectTypeNode( eachDecl.getObjectType( ) ),                                              eachDecl ) );        }

⌨️ 快捷键说明

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