Search in sources :

Example 11 with InternalAgenda

use of org.drools.core.common.InternalAgenda in project drools by kiegroup.

the class ExecutionFlowControlTest method testLockOnActive.

@Test
public void testLockOnActive() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_LockOnActive.drl");
    KieSession ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    // AgendaGroup "group1" is not active, so should receive activation
    final Cheese brie12 = new Cheese("brie", 12);
    ksession.insert(brie12);
    ((InternalWorkingMemory) ksession).flushPropagations();
    InternalAgenda agenda = ((InternalAgenda) ksession.getAgenda());
    final AgendaGroup group1 = agenda.getAgendaGroup("group1");
    assertEquals(1, group1.size());
    ksession.getAgenda().getAgendaGroup("group1").setFocus();
    // AgendaqGroup "group1" is now active, so should not receive activations
    final Cheese brie10 = new Cheese("brie", 10);
    ksession.insert(brie10);
    assertEquals(1, group1.size());
    final Cheese cheddar20 = new Cheese("cheddar", 20);
    ksession.insert(cheddar20);
    final AgendaGroup group2 = agenda.getAgendaGroup("group1");
    assertEquals(1, group2.size());
    agenda.setFocus(group2);
    final Cheese cheddar17 = new Cheese("cheddar", 17);
    ksession.insert(cheddar17);
    assertEquals(1, group2.size());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) InternalAgenda(org.drools.core.common.InternalAgenda) AgendaGroup(org.drools.core.spi.AgendaGroup) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.compiler.Cheese) Test(org.junit.Test)

Example 12 with InternalAgenda

use of org.drools.core.common.InternalAgenda in project drools by kiegroup.

the class ExecutionFlowControlTest method testRuleFlowGroupInActiveMode.

@Test(timeout = 10000)
public void testRuleFlowGroupInActiveMode() throws Exception {
    KieBase kbase = loadKnowledgeBase("ruleflowgroup.drl");
    final KieSession ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final AtomicBoolean fired = new AtomicBoolean(false);
    ksession.addEventListener(new org.kie.api.event.rule.DefaultAgendaEventListener() {

        @Override
        public void afterMatchFired(AfterMatchFiredEvent event) {
            synchronized (fired) {
                fired.set(true);
                fired.notifyAll();
            }
        }
    });
    new Thread(new Runnable() {

        public void run() {
            ksession.fireUntilHalt();
        }
    }).start();
    ksession.insert("Test");
    assertEquals(0, list.size());
    ((InternalAgenda) ksession.getAgenda()).activateRuleFlowGroup("Group1");
    synchronized (fired) {
        if (!fired.get()) {
            fired.wait();
        }
    }
    assertEquals(1, list.size());
    ksession.halt();
}
Also used : ArrayList(java.util.ArrayList) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) InternalAgenda(org.drools.core.common.InternalAgenda) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) Test(org.junit.Test)

Example 13 with InternalAgenda

use of org.drools.core.common.InternalAgenda in project drools by kiegroup.

the class ExecutionFlowControlTest method testLockOnActiveWithModify.

@Test
public void testLockOnActiveWithModify() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_LockOnActiveWithUpdate.drl");
    KieSession ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final Cheese brie = new Cheese("brie", 13);
    final Person bob = new Person("bob");
    bob.setCheese(brie);
    final Person mic = new Person("mic");
    mic.setCheese(brie);
    final Person mark = new Person("mark");
    mark.setCheese(brie);
    final FactHandle brieHandle = (FactHandle) ksession.insert(brie);
    ksession.insert(bob);
    ksession.insert(mic);
    ksession.insert(mark);
    InternalWorkingMemory wm = (InternalWorkingMemory) ksession;
    wm.flushPropagations();
    final InternalAgenda agenda = (InternalAgenda) ksession.getAgenda();
    final AgendaGroup group1 = agenda.getAgendaGroup("group1");
    agenda.setFocus(group1);
    assertEquals(1, group1.size());
    RuleAgendaItem ruleItem1 = (RuleAgendaItem) group1.getActivations()[0];
    ruleItem1.getRuleExecutor().evaluateNetwork(wm.getAgenda());
    assertEquals(3, ruleItem1.getRuleExecutor().getLeftTupleList().size());
    agenda.fireNextItem(null, 0, 0);
    assertEquals(1, group1.size());
    assertEquals(2, ruleItem1.getRuleExecutor().getLeftTupleList().size());
    ksession.update(brieHandle, brie);
    assertEquals(1, group1.size());
    ruleItem1.getRuleExecutor().evaluateNetwork(wm.getAgenda());
    assertEquals(2, ruleItem1.getRuleExecutor().getLeftTupleList().size());
    AgendaGroup group2 = agenda.getAgendaGroup("group2");
    agenda.setFocus(group2);
    assertEquals(1, group2.size());
    RuleAgendaItem ruleItem2 = (RuleAgendaItem) group2.getActivations()[0];
    ruleItem2.getRuleExecutor().evaluateNetwork(wm.getAgenda());
    assertEquals(3, ruleItem2.getRuleExecutor().getLeftTupleList().size());
    agenda.fireNextItem(null, 0, 0);
    assertEquals(1, group2.size());
    assertEquals(2, ruleItem2.getRuleExecutor().getLeftTupleList().size());
    ksession.update(brieHandle, brie);
    assertEquals(1, group2.size());
    assertEquals(2, ruleItem2.getRuleExecutor().getLeftTupleList().size());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) InternalAgenda(org.drools.core.common.InternalAgenda) AgendaGroup(org.drools.core.spi.AgendaGroup) RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 14 with InternalAgenda

use of org.drools.core.common.InternalAgenda in project drools by kiegroup.

the class ExecutionFlowControlTest method testRuleFlowGroupDeactivate.

@Test
public void testRuleFlowGroupDeactivate() throws Exception {
    // need to make eager, for cancel to work, (mdp)
    KieBase kbase = loadKnowledgeBase("ruleflowgroup2.drl");
    KieSession ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert("Test");
    ksession.fireAllRules();
    assertEquals(0, list.size());
    assertEquals(2, ((InternalAgenda) ksession.getAgenda()).getRuleFlowGroup("Group1").size());
    ((InternalAgenda) ksession.getAgenda()).activateRuleFlowGroup("Group1");
    ksession.fireAllRules();
    assertEquals(0, 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 15 with InternalAgenda

use of org.drools.core.common.InternalAgenda in project drools by kiegroup.

the class LinkingTest method testSubnetwork.

@Test
public void testSubnetwork() throws Exception {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + D.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "import " + F.class.getCanonicalName() + "\n";
    str += "import " + G.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule rule1 when \n";
    str += "   $a : A() \n";
    str += "   exists ( B() and C() ) \n";
    str += "   $e : D() \n";
    str += "then \n";
    str += "  list.add( 'x' ); \n";
    str += "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    ObjectTypeNode aotn = getObjectTypeNode(kbase, A.class);
    ObjectTypeNode botn = getObjectTypeNode(kbase, A.class);
    ObjectTypeNode cotn = getObjectTypeNode(kbase, A.class);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.insert(new A());
    wm.insert(new B());
    for (int i = 0; i < 28; i++) {
        wm.insert(new C());
    }
    wm.insert(new D());
    wm.flushPropagations();
    InternalAgenda agenda = (InternalAgenda) wm.getAgenda();
    InternalAgendaGroup group = (InternalAgendaGroup) agenda.getNextFocus();
    AgendaItem item = (AgendaItem) group.remove();
    RuleExecutor ruleExecutor = ((RuleAgendaItem) item).getRuleExecutor();
    int count = ruleExecutor.evaluateNetworkAndFire(wm, null, 0, -1);
    // assertEquals(3, count );
    wm.fireAllRules();
    assertEquals(1, list.size());
    wm.fireAllRules();
    // check it doesn't double fire
    assertEquals(1, list.size());
}
Also used : InternalAgendaGroup(org.drools.core.common.InternalAgendaGroup) RuleExecutor(org.drools.core.phreak.RuleExecutor) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) AgendaItem(org.drools.core.common.AgendaItem) RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) InternalAgenda(org.drools.core.common.InternalAgenda) RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) 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