public void testAutoFocus() throws ConsequenceException {
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl)kBase.newStatefulKnowledgeSession();
final InternalAgenda agenda = (InternalAgenda) ksession.getAgenda();
// create the agendaGroup
final AgendaGroup agendaGroup = new AgendaGroupQueueImpl("agendaGroup",
kBase);
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";
}
};
// create a rule for the agendaGroup
final RuleImpl rule = new RuleImpl("test-rule",
"agendaGroup");
final RuleTerminalNode node = new RuleTerminalNode(2,
new MockTupleSource(2),
rule,
rule.getLhs(),
0,
buildContext);
final RuleTerminalNodeLeftTuple tuple = new RuleTerminalNodeLeftTuple(new DefaultFactHandle(1,
"cheese"),
node,
true);
rule.setConsequence(consequence);
final PropagationContext context = pctxFactory.createPropagationContext(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,
ksession);
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, 0, -1);
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,
ksession);
agenda.unstageActivations();
assertEquals(1,
agendaGroup.size());
agenda.fireNextItem(null, 0, -1);
assertEquals(0,
agendaGroup.size());
}