Search in sources :

Example 6 with InternalAgenda

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();
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ArrayList(java.util.ArrayList) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) InternalAgenda(org.drools.core.common.InternalAgenda) KieBase(org.kie.api.KieBase) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) DebugAgendaEventListener(org.kie.api.event.rule.DebugAgendaEventListener) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 7 with InternalAgenda

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());
}
Also used : InternalAgenda(org.drools.core.common.InternalAgenda) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 8 with InternalAgenda

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();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) InternalAgenda(org.drools.core.common.InternalAgenda) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 9 with InternalAgenda

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());
}
Also used : InternalAgenda(org.drools.core.common.InternalAgenda) ArrayList(java.util.ArrayList) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) Test(org.junit.Test)

Example 10 with InternalAgenda

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());
}
Also used : InternalAgenda(org.drools.core.common.InternalAgenda) ArrayList(java.util.ArrayList) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) Test(org.junit.Test)

Aggregations

InternalAgenda (org.drools.core.common.InternalAgenda)23 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)14 KieSession (org.kie.api.runtime.KieSession)11 List (java.util.List)9 KieBase (org.kie.api.KieBase)8 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)6 Cheese (org.drools.compiler.Cheese)5 AgendaItem (org.drools.core.common.AgendaItem)4 RuleAgendaItem (org.drools.core.phreak.RuleAgendaItem)4 Person (org.drools.compiler.Person)3 CommandBasedStatefulKnowledgeSession (org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 AgendaGroup (org.drools.core.spi.AgendaGroup)3 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)3 FactHandle (org.kie.api.runtime.rule.FactHandle)3 AgendaGroupQueueImpl (org.drools.core.common.AgendaGroupQueueImpl)2 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)2 MatchCreatedEvent (org.kie.api.event.rule.MatchCreatedEvent)2