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

📄 leapsworkingmemory.java

📁 jboss规则引擎
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                while ( !nothingToProcess ) {
                    // normal rules with required columns
                    while ( !this.mainStack.empty() ) {
                        final Token token = this.peekTokenOnTop();
                        boolean done = false;
                        while ( !done ) {
                            if ( !token.isResume() ) {
                                if ( token.hasNextRuleHandle() ) {
                                    token.nextRuleHandle();
                                } else {
                                    // we do not pop because something might get
                                    // asserted
                                    // and placed on hte top of the stack during
                                    // firing
                                    this.removeTokenFromStack( (LeapsFactHandle) token.getDominantFactHandle() );
                                    done = true;
                                }
                            }
                            if ( !done ) {
                                try {
                                    // ok. now we have tuple, dominant fact and
                                    // rules and ready to seek to checks if any
                                    // agendaItem
                                    // matches on current rule
                                    TokenEvaluator.evaluate( token );
                                    // something was found so set marks for
                                    // resume processing
                                    if ( token.getDominantFactHandle() != null ) {
                                        token.setResume( true );
                                        done = true;
                                    }
                                } catch ( final NoMatchesFoundException ex ) {
                                    token.setResume( false );
                                }
                            }
                            // we put everything on agenda
                            // and if there is no modules or anything like it
                            // it would fire just activated rule
                            while ( this.agenda.fireNextItem( agendaFilter ) ) {
                                ;
                            }
                        }
                    }
                    // pick activations generated by retraction or assert
                    // can generate activations off exists and not pending
                    // tuples
                    while ( this.agenda.fireNextItem( agendaFilter ) ) {
                        ;
                    }
                    if ( this.mainStack.empty() ) {
                        nothingToProcess = true;
                    }
                }
                // mark when method was called last time
                this.idLastFireAllAt = ( (LeapsFactHandleFactory) this.handleFactory ).getNextId( );
                // set all factTables to be reseeded
                for (final Iterator it = this.factTables.values( ).iterator( ); it.hasNext( );) {
                    ( (FactTable) it.next( ) ).setReseededStack( true );
                }
            }
            finally {
                this.firing = false;
            }
        }
    }

    protected final long getIdLastFireAllAt() {
        return this.idLastFireAllAt;
    }

    public String toString() {
        String ret = "";
        Object key;
        ret = ret + "\n" + "Working memory";
        ret = ret + "\n" + "Fact Tables by types:";
        for ( final Iterator it = this.factTables.keySet().iterator(); it.hasNext(); ) {
            key = it.next();
            ret = ret + "\n" + "******************   " + key;
            ret = ret + ((FactTable) this.factTables.get( key )).toString();
        }
        ret = ret + "\n" + "Stack:";
        for ( final Iterator it = this.mainStack.iterator(); it.hasNext(); ) {
            ret = ret + "\n" + "\t" + it.next();
        }
        return ret;
    }

    /**
     * Assert a new <code>Tuple</code>.
     * 
     * @param tuple
     *            The <code>Tuple</code> being asserted.
     * @param workingMemory
     *            The working memory seesion.
     * @throws AssertionException
     *             If an error occurs while asserting.
     */
    public final void assertTuple( final LeapsTuple tuple ) {
        final PropagationContext context = tuple.getContext( );
        final Rule rule = tuple.getLeapsRule( ).getRule( );
        // if the current Rule is no-loop and the origin rule is the same then
        // return
        if (rule.getNoLoop( ) && rule.equals( context.getRuleOrigin( ) )) {
            return;
        }
        //
        final Duration dur = rule.getDuration( );

        Activation agendaItem;
        if (dur != null && dur.getDuration( tuple ) > 0) {
            agendaItem = new ScheduledAgendaItem( context.getPropagationNumber( ),
                                                  tuple,
                                                  this.agenda,
                                                  context,
                                                  rule );
            this.agenda.scheduleItem( (ScheduledAgendaItem) agendaItem );
            tuple.setActivation( agendaItem );
            agendaItem.setActivated( true );
            this.getAgendaEventSupport().fireActivationCreated( agendaItem );
        } else {
            final LeapsRule leapsRule = tuple.getLeapsRule();
            AgendaGroupImpl agendaGroup = leapsRule.getAgendaGroup();
            if ( agendaGroup == null ) {
                if ( rule.getAgendaGroup() == null || rule.getAgendaGroup().equals( "" ) || rule.getAgendaGroup().equals( AgendaGroup.MAIN ) ) {
                    // Is the Rule AgendaGroup undefined? If it is use MAIN,
                    // which is added to the Agenda by default
                    agendaGroup = (AgendaGroupImpl) this.agenda.getAgendaGroup( AgendaGroup.MAIN );
                } else {
                    // AgendaGroup is defined, so try and get the AgendaGroup
                    // from the Agenda
                    agendaGroup = (AgendaGroupImpl) this.agenda.getAgendaGroup( rule.getAgendaGroup() );
                }

                if ( agendaGroup == null ) {
                    // The AgendaGroup is defined but not yet added to the
                    // Agenda, so create the AgendaGroup and add to the Agenda.
                    agendaGroup = new AgendaGroupImpl( rule.getAgendaGroup() );
                    this.agenda.addAgendaGroup( agendaGroup );
                }

                leapsRule.setAgendaGroup( agendaGroup );
            }

            // set the focus if rule autoFocus is true
            if ( rule.getAutoFocus() ) {
                this.agenda.setFocus( agendaGroup );
            }

            agendaItem = new AgendaItem( context.getPropagationNumber(),
                                         tuple,
                                         context,
                                         rule );

            agendaGroup.add( agendaItem );

            tuple.setActivation( agendaItem );
            agendaItem.setActivated( true );
            this.getAgendaEventSupport().fireActivationCreated( agendaItem );
        }

        // retract support
        final LeapsFactHandle[] factHandles = (LeapsFactHandle[]) tuple.getFactHandles();
        for ( int i = 0; i < factHandles.length; i++ ) {
            factHandles[i].addActivatedTuple( tuple );
        }
        
        // rules remove support
        FastMap activations = (FastMap) this.rulesActivationsMap.get( rule );
        if (activations == null) {
            activations = new FastMap( );
            this.rulesActivationsMap.put( rule, activations );
        }
        activations.put( agendaItem, agendaItem );
    }

    List getActivations() {
        List ret = new ArrayList( );
        for (final Iterator it = this.rulesActivationsMap.values( ).iterator( ); it.hasNext( );) {
            ret.addAll( ( (FastMap) it.next( ) ).values( ) );
        }

        return ret;
    }

    protected long nextPropagationIdCounter() {
        return ++this.propagationIdCounter;
    }

    public QueryResults getQueryResults( final String queryName ) {
        final IdentityMap map = (IdentityMap) this.queryResults.get( queryName );
        if (map == null) {
            return null;
        }

        final LinkedList list = new LinkedList( );
        for (final Iterator it = map.keySet( ).iterator( ); it.hasNext( );) {
            list.add( it.next( ) );
        }
        if (!list.isEmpty( )) {
            final Query queryRule = (Query) ( (LeapsTuple) list.get( 0 ) ).getLeapsRule( )
                                                                          .getRule( );
            return new LeapsQueryResults( list, queryRule, this );
        }
        else {
            return null;
        }
    }

    void addToQueryResults( final String query, final Tuple tuple ) {
        IdentityMap map = (IdentityMap) this.queryResults.get( query );
        if (map == null) {
            map = new IdentityMap( );
            this.queryResults.put( query, map );
        }
        map.put( tuple, tuple );
    }

    void removeFromQueryResults( final String query, final Tuple tuple ) {
        final IdentityMap map = (IdentityMap) this.queryResults.get( query );
        if (map != null) {
            map.remove( tuple );
        }
    }

    /**
     * to store facts to cursor over it
     */
    private final Map factTables = new FactTables(); 
    
    class FactTables implements Map, Serializable {
        private LinkedList tables = new LinkedList( );

        private HashMap    map    = new HashMap( );

        public int size() {
            return this.tables.size( );
        }

        public void clear() {
            this.tables.clear( );
            this.map.clear( );
        }

        public boolean isEmpty() {
            return this.tables.isEmpty( );
        }

        public boolean containsKey( Object key ) {
            return this.map.containsKey( key );
        }

        public boolean containsValue( Object value ) {
            return this.map.containsValue( value );
        }

        public Collection values() {
            return this.tables;
        }

        public void putAll( Map t ) {
            this.tables.addAll( t.values( ) );
            this.map.putAll( t );
        }

        public Set entrySet() {
            return this.map.entrySet( );
        }

        public Set keySet() {
            return this.map.keySet( );
        }

        public Object get( Object key ) {
            return this.map.get( key );

        }

        public Object remove( Object key ) {
            Object ret = this.map.remove( key );
            this.tables.remove( ret );
            return ret;
        }

        public Object put( Object key, Object value ) {
            this.tables.add( value );
            this.map.put( key, value );
            return value;
        }        
    }
}

⌨️ 快捷键说明

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