final RuleTerminalNodeLeftTuple tuple3_2 = new RuleTerminalNodeLeftTuple(new DefaultFactHandle(5, "cheese"), node3, true);
InternalWorkingMemory workingMemory = new AbstractWorkingMemory(0,
ruleBase);
final InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda();
final AgendaGroup agendaGroup1 = agenda.getAgendaGroup("agendaGroup1");
final AgendaGroup agendaGroup2 = agenda.getAgendaGroup("agendaGroup2");
// focus at this point is MAIN
assertEquals(0, agenda.focusStackSize());
node0.assertLeftTuple(tuple0,
context0,
workingMemory);
agenda.unstageActivations();
// check focus is main
final AgendaGroup main = agenda.getAgendaGroup(AgendaGroup.MAIN);
assertEquals(agenda.getFocus(), main);
// check main got the tuple
assertEquals(1, agenda.getFocus().size());
node2.assertLeftTuple(tuple2_1, context2, workingMemory);
agenda.unstageActivations();
// main is still focus and this tuple went to agendaGroup1
assertEquals(1, agenda.getFocus().size());
// check agendaGroup1 still got the tuple
assertEquals(1, agendaGroup1.size());
// make sure total agenda size reflects this
assertEquals(2, agenda.agendaSize());
// put another one on agendaGroup 1
node2.assertLeftTuple(tuple2_2,
context2,
workingMemory);
agenda.unstageActivations();
// main is still focus so shouldn't have increased
assertEquals(1,
agenda.getFocus().size());
// check agendaGroup2 still got the tuple
assertEquals(2,
agendaGroup1.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");
// agendaGroup2, the current focus, has no activations
assertEquals(0,
agenda.getFocus().size());
// add to agendaGroup2
node3.assertLeftTuple(tuple3_1,
context3,
workingMemory);
agenda.unstageActivations();
assertEquals(1,
agenda.getFocus().size());
node3.assertLeftTuple(tuple3_2,
context3,
workingMemory);
agenda.unstageActivations();
// agendaGroup2 now has 2 activations
assertEquals(2,
agenda.getFocus().size());
// check totalAgendaSize still works
assertEquals(5,
agenda.agendaSize());
// ok now lets check that stacks work with fireNextItem
agenda.fireNextItem(null, 0, -1);
// agendaGroup2 should still be the current agendaGroup
assertEquals(agendaGroup2,
agenda.getFocus());
// agendaGroup2 has gone from 2 to one activations
assertEquals(1,
agenda.getFocus().size());
// check totalAgendaSize has reduced too
assertEquals(4,
agenda.agendaSize());
// now repeat the process
agenda.fireNextItem(null, 0, -1);
// focus is still agendaGroup2, but now its empty
assertEquals(agendaGroup2,
agenda.getFocus());
assertEquals(0,
agenda.getFocus().size());
assertEquals(3,
agenda.agendaSize());
// repeat fire again
agenda.fireNextItem(null, 0, -1);
// agendaGroup2 is empty so it should be popped from the stack making agendaGroup1 the current agendaGroup
assertEquals(agendaGroup1,
agenda.getFocus());
// agendaGroup1 had 2 activations, now it only has 1
assertEquals(1,
agenda.getFocus().size());
assertEquals(2,
agenda.agendaSize());
// repeat fire again
agenda.fireNextItem(null, 0, -1);
assertEquals(agendaGroup1,
agenda.getFocus());
assertEquals(0,
agenda.getFocus().size());
assertEquals(1,
agenda.agendaSize());
// this last fire is more interesting as it demonstrates that
// agendaGroup1 on
// the stack before agendaGroup2 gets skipped as it has no activations
agenda.fireNextItem(null, 0, -1);
assertEquals(agenda.getFocus(),
main);
assertEquals(0,
agenda.getFocus().size());
assertEquals(0,
agenda.agendaSize());
}