" if ($age > 40) throw new RuntimeException(\"Too old\");\n" +
" results.add(\"OK\");" +
"end";
KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
List<String> res = new ArrayList<String>();
ksession.setGlobal( "results", res );
AgendaEventListener agendaEventListener = new AgendaEventListener() {
public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) {
}
public void matchCancelled(org.kie.api.event.rule.MatchCancelledEvent event) {
}
public void beforeMatchFired(org.kie.api.event.rule.BeforeMatchFiredEvent event) {
}
public void afterMatchFired(org.kie.api.event.rule.AfterMatchFiredEvent event) {
}
public void agendaGroupPopped(org.kie.api.event.rule.AgendaGroupPoppedEvent event) {
}
public void agendaGroupPushed(org.kie.api.event.rule.AgendaGroupPushedEvent event) {
}
public void beforeRuleFlowGroupActivated(org.kie.api.event.rule.RuleFlowGroupActivatedEvent event) {
}
public void afterRuleFlowGroupActivated(org.kie.api.event.rule.RuleFlowGroupActivatedEvent event) {
ksession.fireAllRules();
}
public void beforeRuleFlowGroupDeactivated(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent event) {
}
public void afterRuleFlowGroupDeactivated(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent event) {
}
};
ksession.addEventListener(agendaEventListener);
FactHandle fact1 = ksession.insert(new Person("Mario", 38));
((InternalAgenda)ksession.getAgenda()).activateRuleFlowGroup("test");
ksession.fireAllRules();
assertEquals(1, res.size());
res.clear();
ksession.delete(fact1);
FactHandle fact2 = ksession.insert(new Person("Mario", 48));
try {
((InternalAgenda)ksession.getAgenda()).activateRuleFlowGroup("test");
ksession.fireAllRules();
fail("should throw an Exception");
} catch (Exception e) { }
ksession.delete(fact2);
assertEquals(0, res.size());
// try to reuse the ksession after the Exception
FactHandle fact3 = ksession.insert(new Person("Mario", 38));
((InternalAgenda)ksession.getAgenda()).activateRuleFlowGroup("test");
ksession.fireAllRules();
assertEquals(1, res.size());
ksession.delete(fact3);
ksession.dispose();
}