Search in sources :

Example 1 with RuleAgendaItem

use of org.drools.core.phreak.RuleAgendaItem in project drools by kiegroup.

the class DefaultAgenda method clearAndCancelAgendaGroup.

/*
     * (non-Javadoc)
     *
     * @see org.kie.common.AgendaI#clearAgendaGroup(org.kie.common.AgendaGroupImpl)
     */
public void clearAndCancelAgendaGroup(InternalAgendaGroup agendaGroup) {
    // enforce materialization of all activations of this group before removing them
    for (Activation activation : agendaGroup.getActivations()) {
        ((RuleAgendaItem) activation).getRuleExecutor().reEvaluateNetwork(this);
    }
    final EventSupport eventsupport = this.workingMemory;
    agendaGroup.setClearedForRecency(this.workingMemory.getFactHandleFactory().getRecency());
    // this is thread safe for BinaryHeapQueue
    // Binary Heap locks while it returns the array and reset's it's own internal array. Lock is released afer getAndClear()
    List<RuleAgendaItem> lazyItems = new ArrayList<RuleAgendaItem>();
    for (Activation aQueueable : agendaGroup.getAndClear()) {
        final AgendaItem item = (AgendaItem) aQueueable;
        if (item.isRuleAgendaItem()) {
            lazyItems.add((RuleAgendaItem) item);
            ((RuleAgendaItem) item).getRuleExecutor().cancel(workingMemory, eventsupport);
            continue;
        }
        // this must be set false before removal from the activationGroup.
        // Otherwise the activationGroup will also try to cancel the Actvation
        // Also modify won't work properly
        item.setQueued(false);
        if (item.getActivationGroupNode() != null) {
            item.getActivationGroupNode().getActivationGroup().removeActivation(item);
        }
        eventsupport.getAgendaEventSupport().fireActivationCancelled(item, this.workingMemory, MatchCancelledCause.CLEAR);
    }
    // restore lazy items
    for (RuleAgendaItem lazyItem : lazyItems) {
        agendaGroup.add(lazyItem);
    }
}
Also used : RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem) ArrayList(java.util.ArrayList) Activation(org.drools.core.spi.Activation) RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem)

Example 2 with RuleAgendaItem

use of org.drools.core.phreak.RuleAgendaItem in project drools by kiegroup.

the class DefaultAgenda method createRuleAgendaItem.

public RuleAgendaItem createRuleAgendaItem(final int salience, final PathMemory rs, final TerminalNode rtn) {
    String agendaGroupName = rtn.getRule().getAgendaGroup();
    String ruleFlowGroupName = rtn.getRule().getRuleFlowGroup();
    RuleAgendaItem lazyAgendaItem;
    if (!StringUtils.isEmpty(ruleFlowGroupName)) {
        lazyAgendaItem = new RuleAgendaItem(activationCounter++, null, salience, null, rs, rtn, isDeclarativeAgenda(), (InternalAgendaGroup) getAgendaGroup(ruleFlowGroupName));
    } else {
        lazyAgendaItem = new RuleAgendaItem(activationCounter++, null, salience, null, rs, rtn, isDeclarativeAgenda(), (InternalAgendaGroup) getRuleFlowGroup(agendaGroupName));
    }
    return lazyAgendaItem;
}
Also used : RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem)

Example 3 with RuleAgendaItem

use of org.drools.core.phreak.RuleAgendaItem in project drools by kiegroup.

the class DefaultKnowledgeHelper method cancelRemainingPreviousLogicalDependencies.

public void cancelRemainingPreviousLogicalDependencies() {
    if (this.previousJustified != null) {
        for (LogicalDependency<T> dep = this.previousJustified.getFirst(); dep != null; dep = dep.getNext()) {
            TruthMaintenanceSystemHelper.removeLogicalDependency(dep, activation.getPropagationContext());
        }
    }
    if (this.previousBlocked != null) {
        for (LogicalDependency<SimpleMode> dep = this.previousBlocked.getFirst(); dep != null; ) {
            LogicalDependency<SimpleMode> tmp = dep.getNext();
            this.previousBlocked.remove(dep);
            AgendaItem justified = (AgendaItem) dep.getJustified();
            justified.getBlockers().remove((SimpleMode) dep.getMode());
            if (justified.getBlockers().isEmpty()) {
                RuleAgendaItem ruleAgendaItem = justified.getRuleAgendaItem();
                workingMemory.getAgenda().stageLeftTuple(ruleAgendaItem, justified);
            }
            dep = tmp;
        }
    }
}
Also used : RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem) RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem) AgendaItem(org.drools.core.common.AgendaItem) SimpleMode(org.drools.core.beliefsystem.simple.SimpleMode)

Example 4 with RuleAgendaItem

use of org.drools.core.phreak.RuleAgendaItem 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 5 with RuleAgendaItem

use of org.drools.core.phreak.RuleAgendaItem 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

RuleAgendaItem (org.drools.core.phreak.RuleAgendaItem)12 Activation (org.drools.core.spi.Activation)5 ArrayList (java.util.ArrayList)3 AgendaItem (org.drools.core.common.AgendaItem)3 InternalAgenda (org.drools.core.common.InternalAgenda)3 RuleExecutor (org.drools.core.phreak.RuleExecutor)3 List (java.util.List)2 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)2 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)2 TupleList (org.drools.core.util.index.TupleList)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Cheese (org.drools.compiler.Cheese)1 Person (org.drools.compiler.Person)1 SimpleLogicalDependency (org.drools.core.beliefsystem.simple.SimpleLogicalDependency)1 SimpleMode (org.drools.core.beliefsystem.simple.SimpleMode)1 InternalAgendaGroup (org.drools.core.common.InternalAgendaGroup)1