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

📄 agendatest.java

📁 jboss规则引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.drools.reteoo;

/*
 * Copyright 2005 JBoss Inc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.drools.Agenda;
import org.drools.DroolsTestCase;
import org.drools.RuleBase;
import org.drools.WorkingMemory;
import org.drools.common.DefaultAgenda;
import org.drools.common.AgendaGroupImpl;
import org.drools.common.DefaultFactHandle;
import org.drools.common.InternalAgenda;
import org.drools.common.PropagationContextImpl;
import org.drools.rule.Rule;
import org.drools.spi.Activation;
import org.drools.spi.ActivationGroup;
import org.drools.spi.AgendaFilter;
import org.drools.spi.AgendaGroup;
import org.drools.spi.Consequence;
import org.drools.spi.ConsequenceException;
import org.drools.spi.KnowledgeHelper;
import org.drools.spi.PropagationContext;

/**
 * @author mproctor
 */

public class AgendaTest extends DroolsTestCase {
    public void testClearAgenda() {
        final RuleBase ruleBase = new ReteooRuleBase();

        final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newWorkingMemory();

        final Agenda agenda = workingMemory.getAgenda();

        final Rule rule1 = new Rule( "test-rule1" );

        final TerminalNode node1 = new TerminalNode( 3,
                                                     new MockTupleSource( 2 ),
                                                     rule1 );

        final ReteTuple tuple = new ReteTuple( new DefaultFactHandle( 1,
                                                                      "cheese" ) );

        final PropagationContext context1 = new PropagationContextImpl( 0,
                                                                        PropagationContext.ASSERTION,
                                                                        rule1,
                                                                        null );

        // Add consequence. Notice here the context here for the add to ageyunda
        // is itself
        rule1.setConsequence( new org.drools.spi.Consequence() {
            /**
             * 
             */
            private static final long serialVersionUID = -130061018648367024L;

            public void evaluate(final KnowledgeHelper knowledgeHelper,
                                 final WorkingMemory workingMemory) {
                // do nothing
            }
        } );

        assertEquals( 0,
                      agenda.getFocus().size() );

        rule1.setNoLoop( false );
        node1.assertTuple( tuple,
                           context1,
                           workingMemory );

        // make sure we have an activation in the current focus
        assertEquals( 1,
                      agenda.getFocus().size() );

        agenda.clearAgenda();

        assertEquals( 0,
                      agenda.getFocus().size() );
    }

    public void testFilters() throws Exception {
        final RuleBase ruleBase = new ReteooRuleBase();

        final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newWorkingMemory();
        final InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda();

        final Rule rule = new Rule( "test-rule" );
        final TerminalNode node = new TerminalNode( 3,
                                                    new MockTupleSource( 2 ),
                                                    rule );

        final Map results = new HashMap();
        // add consequence
        rule.setConsequence( new org.drools.spi.Consequence() {
            /**
             * 
             */
            private static final long serialVersionUID = -5958911613493145604L;

            public void evaluate(final KnowledgeHelper knowledgeHelper,
                                 final WorkingMemory workingMemory) {
                results.put( "fired",
                             new Boolean( true ) );
            }
        } );

        final ReteTuple tuple = new ReteTuple( new DefaultFactHandle( 1,
                                                                      "cheese" ) );
        final PropagationContext context = new PropagationContextImpl( 0,
                                                                       PropagationContext.ASSERTION,
                                                                       rule,
                                                                       null );

        // test agenda is empty
        assertEquals( 0,
                      agenda.getFocus().size() );

        // True filter, activations should always add
        final AgendaFilter filterTrue = new AgendaFilter() {
            public boolean accept(Activation item) {
                return true;
            }
        };

        rule.setNoLoop( false );
        node.assertTuple( tuple,
                          context,
                          workingMemory );

        // check there is an item to fire
        assertEquals( 1,
                      agenda.getFocus().size() );
        agenda.fireNextItem( filterTrue );

        // check focus is empty
        assertEquals( 0,
                      agenda.getFocus().size() );

        // make sure it also fired
        assertEquals( new Boolean( true ),
                      results.get( "fired" ) );

        // clear the agenda and the result map
        agenda.clearAgenda();
        results.clear();

        // False filter, activations should always be denied
        final AgendaFilter filterFalse = new AgendaFilter() {
            public boolean accept(Activation item) {
                return false;
            }
        };

        rule.setNoLoop( false );
        node.assertTuple( tuple,
                          context,
                          workingMemory );

        // check we have an item to fire
        assertEquals( 1,
                      agenda.getFocus().size() );
        agenda.fireNextItem( filterFalse );

        // make sure the focus is empty
        assertEquals( 0,
                      agenda.getFocus().size() );

        // check the consequence never fired
        assertNull( results.get( "fired" ) );
    }

    public void testFocusStack() throws ConsequenceException {
        final RuleBase ruleBase = new ReteooRuleBase();

        final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newWorkingMemory();

        final InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda();

        //       // create the AgendaGroups
        final AgendaGroupImpl agendaGroup1 = new AgendaGroupImpl( "agendaGroup1" );
        agenda.addAgendaGroup( agendaGroup1 );
        //        ActivationQueue queue1 = agendaGroup1.getActivationQueue( 0 );

        final AgendaGroupImpl agendaGroup2 = new AgendaGroupImpl( "agendaGroup2" );
        agenda.addAgendaGroup( agendaGroup2 );
        //        ActivationQueue queue2 = agendaGroup2.getActivationQueue( 0 );

        final AgendaGroupImpl agendaGroup3 = new AgendaGroupImpl( "agendaGroup3" );
        agenda.addAgendaGroup( agendaGroup3 );
        //        ActivationQueue queue3 = agendaGroup3.getActivationQueue( 0 );

        // create the consequence
        final Consequence consequence = new Consequence() {
            /**
             * 
             */
            private static final long serialVersionUID = -5828597682602481200L;

            public void evaluate(KnowledgeHelper knowledgeHelper,
                                 WorkingMemory workingMemory) {
                // do nothing
            }
        };

        final ReteTuple tuple = new ReteTuple( new DefaultFactHandle( 1,
                                                                      "cheese" ) );

        // create a rule for each agendaGroup
        final Rule rule0 = new Rule( "test-rule0" );
        final TerminalNode node0 = new TerminalNode( 3,
                                                     new MockTupleSource( 2 ),
                                                     rule0 );
        rule0.setConsequence( consequence );
        final PropagationContext context0 = new PropagationContextImpl( 0,
                                                                        PropagationContext.ASSERTION,
                                                                        rule0,
                                                                        null );

        final Rule rule1 = new Rule( "test-rule1",
                                     "agendaGroup1" );
        final TerminalNode node1 = new TerminalNode( 5,
                                                     new MockTupleSource( 4 ),
                                                     rule1 );
        rule1.setConsequence( consequence );
        final PropagationContext context1 = new PropagationContextImpl( 0,
                                                                        PropagationContext.ASSERTION,
                                                                        rule1,
                                                                        null );

        final Rule rule2 = new Rule( "test-rule2",
                                     "agendaGroup2" );
        final TerminalNode node2 = new TerminalNode( 7,
                                                     new MockTupleSource( 6 ),
                                                     rule2 );
        rule2.setConsequence( consequence );
        final PropagationContext context2 = new PropagationContextImpl( 0,
                                                                        PropagationContext.ASSERTION,
                                                                        rule2,
                                                                        null );

        final Rule rule3 = new Rule( "test-rule3",
                                     "agendaGroup3" );
        final TerminalNode node3 = new TerminalNode( 9,
                                                     new MockTupleSource( 8 ),
                                                     rule3 );
        rule3.setConsequence( consequence );
        final PropagationContext context3 = new PropagationContextImpl( 0,
                                                                        PropagationContext.ASSERTION,
                                                                        rule3,
                                                                        null );

        // focus at this point is MAIN
        assertEquals( 0,
                      agenda.focusStackSize() );

        node0.assertTuple( tuple,
                           context0,
                           workingMemory );

        // check focus is main
        final AgendaGroupImpl main = (AgendaGroupImpl) agenda.getAgendaGroup( AgendaGroup.MAIN );
        assertEquals( agenda.getFocus(),
                      main );
        // check main got the tuple
        assertEquals( 1,
                      agenda.getFocus().size() );
        node2.assertTuple( tuple,
                           context2,
                           workingMemory );

        // main is still focus and this tuple went to agendaGroup 2
        assertEquals( 1,
                      agenda.getFocus().size() );

        // check agendaGroup2 still got the tuple
        assertEquals( 1,
                      agendaGroup2.size() );

        // make sure total agenda size reflects this
        assertEquals( 2,
                      agenda.agendaSize() );

        // put another one on agendaGroup 2
        node2.assertTuple( tuple,
                           context2,
                           workingMemory );

        // main is still focus so shouldn't have increased
        assertEquals( 1,
                      agenda.getFocus().size() );

        // check agendaGroup2 still got the tuple
        assertEquals( 2,
                      agendaGroup2.size() );

        // make sure total agenda size reflects this
        assertEquals( 3,
                      agenda.agendaSize() );

        // set the focus to agendaGroup1, note agendaGroup1 has no activations
        agenda.setFocus( "agendaGroup1" );
        // add agendaGroup2 onto the focus stack
        agenda.setFocus( "agendaGroup2" );
        // finally add agendaGroup3 to the top of the focus stack
        agenda.setFocus( "agendaGroup3" );

        // agendaGroup3, the current focus, has no activations

⌨️ 快捷键说明

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