📄 agendatest.java
字号:
package org.drools.reteoo;
/*
* $Id: AgendaTest.java,v 1.4 2005/08/16 22:55:37 mproctor Exp $
*
* Copyright 2004-2005 (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 registered trademark of The Werken Company.
*
* 5. Due credit should be given to The Werken Company.
* (http://drools.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.util.HashMap;
import java.util.Map;
import org.drools.DroolsTestCase;
import org.drools.RuleBase;
import org.drools.rule.Rule;
import org.drools.spi.Activation;
import org.drools.spi.AgendaFilter;
import org.drools.spi.Consequence;
import org.drools.spi.ConsequenceException;
import org.drools.spi.Module;
import org.drools.spi.PropagationContext;
/**
* @author mproctor
*/
public class AgendaTest extends DroolsTestCase
{
PropagationContext initContext = new PropagationContextImpl( PropagationContext.ASSERTION,
null,
null );
public void testAddToAgenda() throws Exception
{
RuleBase ruleBase = new RuleBaseImpl( new Rete() );
WorkingMemoryImpl workingMemory = (WorkingMemoryImpl) ruleBase.newWorkingMemory();
final Agenda agenda = workingMemory.getAgenda();
final Rule rule1 = new Rule( "test-rule1" );
final Rule rule2 = new Rule( "test-rule2" );
final Map results = new HashMap();
final ReteTuple tuple = new ReteTuple( 0,
new FactHandleImpl( 1 ),
workingMemory );
final PropagationContext context1 = new PropagationContextImpl( PropagationContext.ASSERTION,
rule1,
new AgendaItem( tuple,
initContext,
rule1 ) );
final PropagationContext context2 = new PropagationContextImpl( PropagationContext.ASSERTION,
rule2,
new AgendaItem( tuple,
initContext,
rule2 ) );
/*
* Add consequence.
*/
rule1.setConsequence( new org.drools.spi.Consequence() {
public void invoke(Activation activation)
{
/*
* context1 one shows we are adding to the agenda where the rule is the same as its propogation context
*/
agenda.addToAgenda( (ReteTuple) tuple,
context1,
rule1 );
results.put( "fired",
new Boolean( true ) );
}
} );
/* make sure the focus is empty */
assertEquals( 0,
agenda.focusSize() );
/*
* This is not recursive so a rule should not be able to activate itself Notice here the context is the other rule, so should add this time.
*/
rule1.setNoLoop( true );
agenda.addToAgenda( tuple,
context2,
rule1 );
/* check tuple was added to the focus */
assertEquals( 1,
agenda.focusSize() );
agenda.fireNextItem( null );
/* make sure it fired */
assertEquals( new Boolean( true ),
results.get( "fired" ) );
/* the addToAgenda in the consequence should fail as the context is the same as the current rule */
assertEquals( 0,
agenda.focusSize() );
/* reset agenda and results map */
agenda.clearAgenda();
results.clear();
/*
* This is recursive so a rule should be able to activate itself
*/
rule1.setNoLoop( false );
agenda.addToAgenda( tuple,
context2,
rule1 );
assertEquals( 1,
agenda.focusSize() );
agenda.fireNextItem( null );
/* check rule fired */
assertEquals( new Boolean( true ),
results.get( "fired" ) );
/* check rule was able to add itself to the agenda */
assertEquals( 1,
agenda.focusSize() );
}
public void testClearAgenda()
{
RuleBase ruleBase = new RuleBaseImpl( new Rete() );
WorkingMemoryImpl workingMemory = (WorkingMemoryImpl) ruleBase.newWorkingMemory();
final Agenda agenda = workingMemory.getAgenda();
final Rule rule1 = new Rule( "test-rule1" );
ReteTuple tuple = new ReteTuple( 0,
new FactHandleImpl( 1 ),
workingMemory );
final PropagationContext context1 = new PropagationContextImpl( PropagationContext.ASSERTION,
rule1,
new AgendaItem( tuple,
initContext,
rule1 ) );
/*
* Add consequence. Notice here the context here for the add to agenda is itself
*/
rule1.setConsequence( new org.drools.spi.Consequence() {
public void invoke(Activation activation)
{
// do nothing
}
} );
assertEquals( 0,
agenda.focusSize() );
rule1.setNoLoop( false );
agenda.addToAgenda( tuple,
context1,
rule1 );
/* make sure we have an activation in the current focus */
assertEquals( 1,
agenda.focusSize() );
agenda.clearAgenda();
assertEquals( 0,
agenda.focusSize() );
}
public void testFilters() throws Exception
{
RuleBase ruleBase = new RuleBaseImpl( new Rete() );
WorkingMemoryImpl workingMemory = (WorkingMemoryImpl) ruleBase.newWorkingMemory();
final Agenda agenda = workingMemory.getAgenda();
final Rule rule = new Rule( "test-rule" );
final Map results = new HashMap();
// add consequence
rule.setConsequence( new org.drools.spi.Consequence() {
public void invoke(Activation activation)
{
results.put( "fired",
new Boolean( true ) );
}
} );
ReteTuple tuple = new ReteTuple( 0,
new FactHandleImpl( 1 ),
workingMemory );
final PropagationContext context = new PropagationContextImpl( PropagationContext.ASSERTION,
rule,
new AgendaItem( tuple,
initContext,
rule ) );
/* test agenda is empty */
assertEquals( 0,
agenda.focusSize() );
/*
* True filter, activations should always add
*/
AgendaFilter filterTrue = new AgendaFilter() {
public boolean accept(Activation item)
{
return true;
}
};
rule.setNoLoop( false );
agenda.addToAgenda( tuple,
context,
rule );
/* check there is an item to fire */
assertEquals( 1,
agenda.focusSize() );
agenda.fireNextItem( filterTrue );
/* check focus is empty */
assertEquals( 0,
agenda.focusSize() );
/* 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
*/
AgendaFilter filterFalse = new AgendaFilter() {
public boolean accept(Activation item)
{
return false;
}
};
rule.setNoLoop( false );
agenda.addToAgenda( tuple,
context,
rule );
/* check we have an item to fire */
assertEquals( 1,
agenda.focusSize() );
agenda.fireNextItem( filterFalse );
/* make sure the focus is empty */
assertEquals( 0,
agenda.focusSize() );
/* check the consequence never fired */
assertNull( results.get( "fired" ) );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -