public void testAutoFocus() throws ConsequenceException {
final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newStatefulSession();
final InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda();
// create the agendaGroup
final AgendaGroup agendaGroup = new BinaryHeapQueueAgendaGroup( "agendaGroup",
ruleBase );
agenda.addAgendaGroup( agendaGroup );
// create the consequence
final Consequence consequence = new Consequence() {
private static final long serialVersionUID = 510l;
public void evaluate(KnowledgeHelper knowledgeHelper,
WorkingMemory workingMemory) {
// do nothing
}
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public String getName() {
return "default";
}
};
final LeftTupleImpl tuple = new LeftTupleImpl( new DefaultFactHandle( 1,
"cheese" ),
null,
true );
// create a rule for the agendaGroup
final Rule rule = new Rule( "test-rule",
"agendaGroup" );
final RuleTerminalNode node = new RuleTerminalNode( 2,
new MockTupleSource( 2 ),
rule,
rule.getLhs(),
0,
buildContext );
rule.setConsequence( consequence );
final PropagationContext context = new PropagationContextImpl( 0,
PropagationContext.INSERTION,
rule,
null,
new DefaultFactHandle() );
// first test that autoFocus=false works. Here the rule should not fire
// as its agendaGroup does not have focus.
rule.setAutoFocus( false );
node.assertLeftTuple( tuple,
context,
workingMemory );
agenda.unstageActivations();
// check activation as added to the agendaGroup
assertEquals( 1,
agendaGroup.size() );
// fire next item, agendaGroup should not fire as its not on the focus stack
// and thus should retain its sinle activation
agenda.fireNextItem( null );
assertEquals( 1,
agendaGroup.size() );
// Clear the agenda we we can test again
agenda.clearAndCancel();
assertEquals( 0,
agendaGroup.size() );
// Now test that autoFocus=true works. Here the rule should fire as its
// agendaGroup gets the focus when the activation is created.
rule.setAutoFocus( true );
node.assertLeftTuple( tuple,
context,
workingMemory );
agenda.unstageActivations();
assertEquals( 1,
agendaGroup.size() );
agenda.fireNextItem( null );
assertEquals( 0,
agendaGroup.size() );
}