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

📄 builder.java

📁 drools 一个开放源码的规则引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     *            The node to attach.
     * @param leafNodes
     *            The list to which the newly added node will be added.
     */
    private void attachNode(TupleSource candidate,
                            List leafNodes)
    {
        TupleSource node = (TupleSource) this.attachedNodes.get( candidate );

        if ( node == null )
        {
            candidate.attach( );

            this.attachedNodes.put( candidate,
                                    candidate );

            node = candidate;
        }

        leafNodes.add( node );
    }

    /**
     * Create and attach <code>Condition</code> s to the network.
     * 
     * <p>
     * It may not be possible to satisfy all filder conditions on the first
     * pass. This method removes satisfied conditions from the
     * <code>Condition</code> parameter, and leaves unsatisfied ones in the
     * <code>Set</code>.
     * </p>
     * 
     * @param rule
     *            The rule.
     * @param conds
     *            Set of <code>Conditions</code> to attempt attaching.
     * @param leafNodes
     *            The leaf node.
     */
    private void attachConditions(Rule rule,
                                  List conds,
                                  List leafNodes)
    {
        Iterator condIter = conds.iterator( );
        Condition eachCond;
        TupleSource tupleSource;

        while ( condIter.hasNext( ) )
        {
            eachCond = (Condition) condIter.next( );

            tupleSource = findMatchingTupleSourceForCondition( eachCond,
                                                               leafNodes );

            if ( tupleSource == null )
            {
                continue;
            }

            condIter.remove( );

            attachNode( new ConditionNode( rule,
                                           tupleSource,
                                           eachCond ),
                        leafNodes );
        }
    }

    /**
     * Join two arbitrary leaves in order to satisfy a filter that currently
     * cannot be applied.
     * 
     * @param conds
     *            The filter conditions remaining.
     * @param leafNodes
     *            Available leaf nodes.
     * 
     * @return <code>true</code> if a join was possible, otherwise,
     *         <code>false</code>.
     */
    private boolean joinForCondition(List conds,
                                     List leafNodes)
    {
        return joinArbitrary( leafNodes );
    }

    /**
     * Join two arbitrary leaves in order to satisfy a filter that currently
     * cannot be applied.
     * 
     * @param leafNodes
     *            Available leaf nodes.
     * 
     * @return <code>true</code> if successfully joined some nodes, otherwise
     *         <code>false</code>.
     */
    private boolean joinArbitrary(List leafNodes)
    {
        Iterator leafIter = leafNodes.iterator( );

        TupleSource left = (TupleSource) leafIter.next( );

        if ( !leafIter.hasNext( ) )
        {
            return false;
        }

        leafIter.remove( );

        TupleSource right = (TupleSource) leafIter.next( );

        leafIter.remove( );

        attachNode( new JoinNode( left,
                                  right ),
                    leafNodes );

        return true;
    }

    /**
     * Create and attach <code>JoinNode</code> s to the network.
     * 
     * <p>
     * It may not be possible to join all <code>leafNodes</code>.
     * </p>
     * 
     * <p>
     * Any <code>leafNodes</code> member that particiates in a <i>join </i> is
     * removed from the <code>leafNodes</code> collection, and replaced by the
     * joining <code>JoinNode</code>.
     * </p>
     * 
     * @param leafNodes
     *            The current attachable leaf nodes of the network.
     * 
     * @return <code>true</code> if at least one <code>JoinNode</code> was
     *         created, else <code>false</code>.
     */
    private boolean createJoinNodes(List leafNodes)
    {
        boolean performedJoin = false;

        Object[] nodesArray = leafNodes.toArray( );

        TupleSource left;
        TupleSource right;

        for ( int i = 0; i < nodesArray.length; ++i )
        {
            left = (TupleSource) nodesArray[i];

            if ( leafNodes.contains( left ) )
            {
                for ( int j = i + 1; j < nodesArray.length; ++j )
                {
                    right = (TupleSource) nodesArray[j];

                    if ( leafNodes.contains( right ) && canBeJoined( left,
                                                                     right ) )
                    {
                        leafNodes.remove( left );
                        leafNodes.remove( right );

                        attachNode( new JoinNode( left,
                                                  right ),
                                    leafNodes );

                        performedJoin = true;

                        break;
                    }
                }
            }
        }

        return performedJoin;
    }

    /**
     * Determine if two <code>TupleSource</code> s can be joined.
     * 
     * @param left
     *            The left tuple source
     * @param right
     *            The right tuple source
     * 
     * @return <code>true</code> if they can be joined (they share at least
     *         one common member declaration), else <code>false</code>.
     */
    private boolean canBeJoined(TupleSource left,
                                TupleSource right)
    {
        Set leftDecls = left.getTupleDeclarations( );
        Iterator rightDeclIter = right.getTupleDeclarations( ).iterator( );

        while ( rightDeclIter.hasNext( ) )
        {
            if ( leftDecls.contains( rightDeclIter.next( ) ) )
            {
                return true;
            }
        }

        return false;
    }

    /**
     * Locate a <code>TupleSource</code> suitable for attaching the
     * <code>Condition</code> and remove it.
     * 
     * @param condition
     *            The <code>Condition</code> to attach.
     * @param sources
     *            Candidate <code>TupleSources</code>.
     * 
     * @return Matching <code>TupleSource</code> if a suitable one can be
     *         found, else <code>null</code>.
     */
    TupleSource findMatchingTupleSourceForCondition(Condition condition,
                                                    List sources)
    {
        Iterator sourceIter = sources.iterator( );
        TupleSource eachSource;

        while ( sourceIter.hasNext( ) )
        {
            eachSource = (TupleSource) sourceIter.next( );

            if ( matches( condition,
                          eachSource.getTupleDeclarations( ) ) )
            {
                sourceIter.remove( );
                return eachSource;
            }
        }

        return null;
    }

    /**
     * Determine if a set of <code>Declarations</code> match those required by
     * a <code>Condition</code>.
     * 
     * @param condition
     *            The <code>Condition</code>.
     * @param declarations
     *            The set of <code>Declarations</code> to compare against.
     * 
     * @return <code>true</code> if the set of <code>Declarations</code> is
     *         a super-set of the <code>Declarations</code> required by the
     *         <code>Condition</code>.
     */
    boolean matches(Condition condition,
                    Set declarations)
    {
        return containsAll( declarations,
                            condition.getRequiredTupleMembers( ) );
    }

    /**
     * Determine if a set of <code>Declarations</code> is a super set of
     * required <code>Declarations</code>
     * 
     * @param declarations
     *            The set of <code>Declarations</code> to compare against.
     * 
     * @param requiredDecls
     *            The required <code>Declarations</code>.
     * @return <code>true</code> if the set of <code>Declarations</code> is
     *         a super-set of the <code>Declarations</code> required by the
     *         <code>Condition</code>.
     */
    private boolean containsAll(Set declarations,
                                Declaration[] requiredDecls)
    {
        for ( int i = requiredDecls.length - 1; i >= 0; i-- )
        {
            if ( !declarations.contains( requiredDecls[i] ) )
            {
                return false;
            }

        }

        return true;
    }

    /**
     * Reset the internal state.
     */
    private void reset()
    {
        this.rete = new Rete( );
        this.ruleSets = new ArrayList( );
        this.attachedNodes = new HashMap( );
        this.applicationData = new HashMap( );
        this.factHandleFactory = new DefaultFactHandleFactory( );
        this.conflictResolver = DefaultConflictResolver.getInstance( );
    }
}

⌨️ 快捷键说明

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