use of org.drools.core.common.InternalAgenda in project drools by kiegroup.
the class Misc2Test method testReuseAgendaAfterException.
@Test
public void testReuseAgendaAfterException() throws Exception {
// JBRULES-3677
String str = "import org.drools.compiler.Person;\n" + "global java.util.List results;" + "rule R1\n" + "ruleflow-group \"test\"\n" + "when\n" + " Person( $age : age ) \n" + "then\n" + " if ($age > 40) throw new RuntimeException(\"Too old\");\n" + " results.add(\"OK\");" + "end";
KieBase kbase = loadKnowledgeBaseFromString(str);
final KieSession ksession = kbase.newKieSession();
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();
}
use of org.drools.core.common.InternalAgenda in project drools by kiegroup.
the class ExecutionFlowControlTest method testRuleFlowGroup.
@Test
public void testRuleFlowGroup() throws Exception {
KieBase kbase = loadKnowledgeBase("ruleflowgroup.drl");
KieSession ksession = kbase.newKieSession();
final List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert("Test");
ksession.fireAllRules();
assertEquals(0, list.size());
((InternalAgenda) ksession.getAgenda()).activateRuleFlowGroup("Group1");
ksession.fireAllRules();
assertEquals(1, list.size());
}
use of org.drools.core.common.InternalAgenda in project drools by kiegroup.
the class DynamicRulesTest method testJBRULES_2206.
@Test(timeout = 10000)
public void testJBRULES_2206() {
KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
((RuleBaseConfiguration) config).setRuleBaseUpdateHandler(null);
InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase(config);
KieSession session = createKnowledgeSession(kbase);
AgendaEventListener ael = mock(AgendaEventListener.class);
session.addEventListener(ael);
for (int i = 0; i < 5; i++) {
session.insert(new Cheese());
}
kbase.addPackages(loadKnowledgePackages("test_JBRULES_2206_1.drl"));
((InternalAgenda) session.getAgenda()).evaluateEagerList();
// two matching rules were added, so 2 activations should have been created
verify(ael, times(2)).matchCreated(any(MatchCreatedEvent.class));
int fireCount = session.fireAllRules();
// both should have fired
assertEquals(2, fireCount);
kbase.addPackages(loadKnowledgePackages("test_JBRULES_2206_2.drl"));
((InternalAgenda) session.getAgenda()).evaluateEagerList();
// one rule was overridden and should activate
verify(ael, times(3)).matchCreated(any(MatchCreatedEvent.class));
fireCount = session.fireAllRules();
// that rule should fire again
assertEquals(1, fireCount);
session.dispose();
}
use of org.drools.core.common.InternalAgenda in project drools by kiegroup.
the class AgendaRuleFlowGroupsTest method testRuleFlowGroupOnly.
@Test
public void testRuleFlowGroupOnly() throws Exception {
CommandBasedStatefulKnowledgeSession ksession = createSession(-1, "ruleflow-groups.drl");
org.drools.core.spi.AgendaGroup[] groups = ((InternalAgenda) stripSession(ksession).getAgenda()).getAgendaGroups();
// only main is available
assertEquals(1, groups.length);
assertEquals("MAIN", groups[0].getName());
long id = ksession.getIdentifier();
List<String> list = new ArrayList<String>();
list.add("Test");
ksession.insert(list);
ksession.execute(new ActivateRuleFlowCommand("ruleflow-group"));
ksession.dispose();
ksession = createSession(id, "ruleflow-groups.drl");
groups = ((InternalAgenda) stripSession(ksession).getAgenda()).getAgendaGroups();
// main and rule flow is now on the agenda
assertEquals(2, groups.length);
assertEquals("MAIN", groups[0].getName());
assertEquals("ruleflow-group", groups[1].getName());
}
use of org.drools.core.common.InternalAgenda in project drools by kiegroup.
the class AgendaRuleFlowGroupsTest method testAgendaGroupOnly.
@Test
public void testAgendaGroupOnly() throws Exception {
CommandBasedStatefulKnowledgeSession ksession = createSession(-1, "agenda-groups.drl");
org.drools.core.spi.AgendaGroup[] groups = ((InternalAgenda) stripSession(ksession).getAgenda()).getAgendaGroups();
// only main is available
assertEquals(1, groups.length);
assertEquals("MAIN", groups[0].getName());
long id = ksession.getIdentifier();
List<String> list = new ArrayList<String>();
list.add("Test");
ksession.insert(list);
ksession.execute(new ActivateAgendaGroupCommand("agenda-group"));
ksession.dispose();
ksession = createSession(id, "agenda-groups.drl");
groups = ((InternalAgenda) stripSession(ksession).getAgenda()).getAgendaGroups();
// main and agenda group is now on the agenda
assertEquals(2, groups.length);
assertEquals("MAIN", groups[0].getName());
assertEquals("agenda-group", groups[1].getName());
}
Aggregations