List<KnowledgePackage> pkgs = new ArrayList<KnowledgePackage>();
pkgs.add( new KnowledgePackageImp( pkg ) );
kbase.addKnowledgePackages( pkgs );
// create a new working memory and add an AgendaEventListener
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
final List agendaList = new ArrayList();
final AgendaEventListener agendaEventListener = new AgendaEventListener() {
public void matchCancelled(MatchCancelledEvent event) {
assertNotNull( event.getKieRuntime() );
agendaList.add( event );
}
public void matchCreated(MatchCreatedEvent event) {
assertNotNull( event.getKieRuntime() );
agendaList.add( event );
}
public void afterMatchFired(AfterMatchFiredEvent event) {
assertNotNull( event.getKieRuntime() );
agendaList.add( event );
}
public void agendaGroupPopped(AgendaGroupPoppedEvent event) {
assertNotNull( event.getKieRuntime() );
agendaList.add( event );
}
public void agendaGroupPushed(AgendaGroupPushedEvent event) {
assertNotNull( event.getKieRuntime() );
agendaList.add( event );
}
public void beforeMatchFired(BeforeMatchFiredEvent event) {
assertNotNull( event.getKieRuntime() );
agendaList.add( event );
}
public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
assertNotNull( event.getKieRuntime() );
agendaList.add( event );
}
public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
assertNotNull( event.getKieRuntime() );
agendaList.add( event );
}
public void beforeRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
assertNotNull( event.getKieRuntime() );
agendaList.add( event );
}
public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
assertNotNull( event.getKieRuntime() );
agendaList.add( event );
}
};
ksession.addEventListener( agendaEventListener );
assertEquals( 1, ksession.getAgendaEventListeners().size() );
// assert the cheese fact
final Cheese cheddar = new Cheese( "cheddar",
15 );
FactHandle cheddarHandle = ksession.insert( cheddar );
// should be one ActivationCreatedEvent
assertEquals( 1,
agendaList.size() );
MatchCreatedEvent createdEvent = (MatchCreatedEvent) agendaList.get( 0 );
assertSame( cheddarHandle,
createdEvent.getMatch().getFactHandles().toArray()[0] );
// clear the agenda to check CLEAR events occur
ksession.getAgenda().clear();
MatchCancelledEvent cancelledEvent = (MatchCancelledEvent) agendaList.get( 1 );
assertEquals( MatchCancelledCause.CLEAR,
cancelledEvent.getCause() );
agendaList.clear();
// update results in an ActivationCreatedEvent
cheddar.setPrice( 14 );
ksession.update( cheddarHandle,
cheddar );
assertEquals( 1,
agendaList.size() );
createdEvent = (MatchCreatedEvent) agendaList.get( 0 );
assertSame( cheddarHandle,
createdEvent.getMatch().getFactHandles().toArray()[0] );
agendaList.clear();
// update should not result in cancelation+activation events
cheddar.setPrice( 14 );
ksession.update( cheddarHandle,
cheddar );
assertEquals( 0,
agendaList.size() );
//cancelledEvent = (ActivationCancelledEvent) agendaList.get( 0 );
//assertEquals( ActivationCancelledCause.WME_MODIFY, cancelledEvent.getCause() );
// assertSame( cheddarHandle,
// cancelledEvent.getActivation().getFactHandles().toArray()[0] );
// createdEvent = (ActivationCreatedEvent) agendaList.get( 1 );
// assertSame( cheddarHandle,
// createdEvent.getActivation().getFactHandles().toArray()[0] );
// agendaList.clear();
// retract results in a ActivationCancelledEvent, note the object is not resolveable now as it no longer exists
ksession.retract( cheddarHandle );
assertEquals( 1,
agendaList.size() );
cancelledEvent = (MatchCancelledEvent) agendaList.get( 0 );
assertNull( ((InternalFactHandle) cancelledEvent.getMatch().getFactHandles().toArray()[0]).getObject() );
// re-assert the fact so we can test the agenda group events
cheddarHandle = ksession.insert( cheddar );
agendaList.clear();
// setFocus results in an AgendaGroupPushedEvent
ksession.getAgenda().getAgendaGroup( "test group" ).setFocus();
assertEquals( 1,
agendaList.size() );
final AgendaGroupPushedEvent pushedEvent = (AgendaGroupPushedEvent) agendaList.get( 0 );
assertEquals( "test group",
pushedEvent.getAgendaGroup().getName() );
agendaList.clear();
// fireAllRules results in a BeforeActivationFiredEvent and an AfterActivationFiredEvent
// the AgendaGroup becomes empty, which results in a popped event.
ksession.fireAllRules();
assertEquals( 3,
agendaList.size() );
final BeforeMatchFiredEvent beforeEvent = (BeforeMatchFiredEvent) agendaList.get( 0 );
assertSame( cheddarHandle,
beforeEvent.getMatch().getFactHandles().toArray()[0] );