Search in sources :

Example 1 with RuleExecutor

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

the class RuleTerminalNode method cancelMatch.

public void cancelMatch(AgendaItem match, InternalWorkingMemoryActions workingMemory) {
    match.cancel();
    if (match.isQueued()) {
        Tuple leftTuple = match.getTuple();
        if (match.getRuleAgendaItem() != null) {
            // phreak must also remove the LT from the rule network evaluator
            if (leftTuple.getMemory() != null) {
                leftTuple.getMemory().remove(leftTuple);
            }
        }
        RuleExecutor ruleExecutor = ((RuleTerminalNodeLeftTuple) leftTuple).getRuleAgendaItem().getRuleExecutor();
        PhreakRuleTerminalNode.doLeftDelete(ruleExecutor.getPathMemory().getActualAgenda(workingMemory), ruleExecutor, leftTuple);
    }
}
Also used : RuleExecutor(org.drools.core.phreak.RuleExecutor) Tuple(org.drools.core.spi.Tuple)

Example 2 with RuleExecutor

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

Example 3 with RuleExecutor

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

the class DefaultAgenda method isRuleInstanceAgendaItem.

public boolean isRuleInstanceAgendaItem(String ruleflowGroupName, String ruleName, long processInstanceId) {
    propagationList.flush();
    RuleFlowGroup systemRuleFlowGroup = this.getRuleFlowGroup(ruleflowGroupName);
    Match[] matches = ((InternalAgendaGroup) systemRuleFlowGroup).getActivations();
    for (Match match : matches) {
        Activation act = (Activation) match;
        if (act.isRuleAgendaItem()) {
            // The lazy RuleAgendaItem must be fully evaluated, to see if there is a rule match
            RuleExecutor ruleExecutor = ((RuleAgendaItem) act).getRuleExecutor();
            ruleExecutor.evaluateNetwork(this);
            TupleList list = ruleExecutor.getLeftTupleList();
            for (RuleTerminalNodeLeftTuple lt = (RuleTerminalNodeLeftTuple) list.getFirst(); lt != null; lt = (RuleTerminalNodeLeftTuple) lt.getNext()) {
                if (ruleName.equals(lt.getRule().getName())) {
                    if (checkProcessInstance(lt, processInstanceId)) {
                        return true;
                    }
                }
            }
        } else {
            if (ruleName.equals(act.getRule().getName())) {
                if (checkProcessInstance(act, processInstanceId)) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : RuleFlowGroup(org.drools.core.spi.RuleFlowGroup) TupleList(org.drools.core.util.index.TupleList) RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple) RuleExecutor(org.drools.core.phreak.RuleExecutor) Activation(org.drools.core.spi.Activation) Match(org.kie.api.runtime.rule.Match)

Example 4 with RuleExecutor

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

the class DefaultAgenda method evaluateEagerList.

public void evaluateEagerList() {
    while (!eager.isEmpty()) {
        RuleAgendaItem item = eager.removeFirst();
        if (item.isRuleInUse()) {
            // this rule could have been removed by an incremental compilation
            evaluateQueriesForRule(item);
            RuleExecutor ruleExecutor = item.getRuleExecutor();
            ruleExecutor.evaluateNetwork(this);
        }
    }
}
Also used : RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem) RuleExecutor(org.drools.core.phreak.RuleExecutor)

Example 5 with RuleExecutor

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

the class DefaultAgenda method clearAndCancelActivationGroup.

/*
     * (non-Javadoc)
     *
     * @see org.kie.common.AgendaI#clearActivationGroup(org.kie.spi.ActivationGroup)
     */
public void clearAndCancelActivationGroup(final InternalActivationGroup activationGroup) {
    final EventSupport eventsupport = this.workingMemory;
    activationGroup.setTriggeredForRecency(this.workingMemory.getFactHandleFactory().getRecency());
    for (final Iterator it = activationGroup.iterator(); it.hasNext(); ) {
        final ActivationGroupNode node = (ActivationGroupNode) it.next();
        final Activation activation = node.getActivation();
        activation.setActivationGroupNode(null);
        if (activation.isQueued()) {
            activation.setQueued(false);
            activation.remove();
            RuleExecutor ruleExec = ((RuleTerminalNodeLeftTuple) activation).getRuleAgendaItem().getRuleExecutor();
            ruleExec.removeLeftTuple((LeftTuple) activation);
            eventsupport.getAgendaEventSupport().fireActivationCancelled(activation, this.workingMemory, MatchCancelledCause.CLEAR);
        }
    }
    activationGroup.reset();
}
Also used : RuleExecutor(org.drools.core.phreak.RuleExecutor) Iterator(java.util.Iterator) Activation(org.drools.core.spi.Activation)

Aggregations

RuleExecutor (org.drools.core.phreak.RuleExecutor)5 RuleAgendaItem (org.drools.core.phreak.RuleAgendaItem)3 Activation (org.drools.core.spi.Activation)2 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 AgendaItem (org.drools.core.common.AgendaItem)1 InternalAgenda (org.drools.core.common.InternalAgenda)1 InternalAgendaGroup (org.drools.core.common.InternalAgendaGroup)1 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)1 RuleTerminalNodeLeftTuple (org.drools.core.reteoo.RuleTerminalNodeLeftTuple)1 RuleFlowGroup (org.drools.core.spi.RuleFlowGroup)1 Tuple (org.drools.core.spi.Tuple)1 TupleList (org.drools.core.util.index.TupleList)1 Test (org.junit.Test)1 Match (org.kie.api.runtime.rule.Match)1 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)1