Search in sources :

Example 1 with RuleTerminalNodeLeftTuple

use of org.drools.core.reteoo.RuleTerminalNodeLeftTuple in project drools by kiegroup.

the class PhreakRuleTerminalNode method doLeftDelete.

public static void doLeftDelete(InternalAgenda agenda, RuleExecutor executor, Tuple leftTuple) {
    PropagationContext pctx = leftTuple.getPropagationContext();
    pctx = RuleTerminalNode.findMostRecentPropagationContext(leftTuple, pctx);
    RuleTerminalNodeLeftTuple rtnLt = (RuleTerminalNodeLeftTuple) leftTuple;
    rtnLt.setMatched(false);
    agenda.cancelActivation(leftTuple, pctx, rtnLt, rtnLt.getTerminalNode());
    if (leftTuple.getMemory() != null) {
        // Expiration propagations should not be removed from the list, as they still need to fire
        executor.removeLeftTuple(leftTuple);
    }
    leftTuple.setContextObject(null);
}
Also used : PropagationContext(org.drools.core.spi.PropagationContext) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple)

Example 2 with RuleTerminalNodeLeftTuple

use of org.drools.core.reteoo.RuleTerminalNodeLeftTuple in project drools by kiegroup.

the class PhreakRuleTerminalNode method doLeftTupleUpdate.

public static void doLeftTupleUpdate(TerminalNode rtnNode, RuleExecutor executor, InternalAgenda agenda, int salienceInt, Salience salience, LeftTuple leftTuple) {
    PropagationContext pctx = leftTuple.getPropagationContext();
    pctx = RuleTerminalNode.findMostRecentPropagationContext(leftTuple, pctx);
    boolean blocked = false;
    RuleTerminalNodeLeftTuple rtnLeftTuple = (RuleTerminalNodeLeftTuple) leftTuple;
    if (executor.isDeclarativeAgendaEnabled()) {
        if (rtnLeftTuple.getBlockers() != null && !rtnLeftTuple.getBlockers().isEmpty()) {
            // declarativeAgenda still blocking LeftTuple, so don't add back ot list
            blocked = true;
        }
    } else {
        blocked = rtnNode.getRule().isNoLoop() && rtnNode.equals(pctx.getTerminalNodeOrigin());
    }
    InternalWorkingMemory wm = agenda.getWorkingMemory();
    if (salience != null) {
        salienceInt = salience.getValue(new DefaultKnowledgeHelper(rtnLeftTuple, wm), rtnNode.getRule(), wm);
    }
    if (agenda.getActivationsFilter() != null && !agenda.getActivationsFilter().accept(rtnLeftTuple, wm, rtnNode)) {
        // only relevant for serialization, to not re-fire Matches already fired
        return;
    }
    if (!blocked) {
        boolean addToExector = true;
        if (rtnNode.getRule().isLockOnActive() && pctx.getType() != PropagationContext.Type.RULE_ADDITION) {
            long handleRecency = pctx.getFactHandle().getRecency();
            InternalAgendaGroup agendaGroup = executor.getRuleAgendaItem().getAgendaGroup();
            if (blockedByLockOnActive(rtnNode.getRule(), pctx, handleRecency, agendaGroup)) {
                addToExector = false;
            }
        }
        if (addToExector) {
            if (!rtnLeftTuple.isQueued()) {
                // not queued, so already fired, so it's effectively recreated
                EventSupport es = (EventSupport) wm;
                es.getAgendaEventSupport().fireActivationCreated(rtnLeftTuple, wm);
                rtnLeftTuple.update(salienceInt, pctx);
                executor.addLeftTuple(leftTuple);
                wm.getRuleEventSupport().onUpdateMatch(rtnLeftTuple);
            }
        }
    } else {
        // LeftTuple is blocked, and thus not queued, so just update it's values
        rtnLeftTuple.update(salienceInt, pctx);
    }
    if (!rtnNode.isFireDirect() && executor.isDeclarativeAgendaEnabled()) {
        agenda.modifyActivation(rtnLeftTuple, rtnLeftTuple.isQueued());
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) InternalAgendaGroup(org.drools.core.common.InternalAgendaGroup) PropagationContext(org.drools.core.spi.PropagationContext) EventSupport(org.drools.core.common.EventSupport) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple) DefaultKnowledgeHelper(org.drools.core.base.DefaultKnowledgeHelper)

Example 3 with RuleTerminalNodeLeftTuple

use of org.drools.core.reteoo.RuleTerminalNodeLeftTuple in project drools by kiegroup.

the class RuleExecutor method cancel.

public void cancel(InternalWorkingMemory wm, EventSupport es) {
    while (!tupleList.isEmpty()) {
        RuleTerminalNodeLeftTuple rtnLt = (RuleTerminalNodeLeftTuple) tupleList.removeFirst();
        if (queue != null) {
            queue.dequeue(rtnLt);
        }
        es.getAgendaEventSupport().fireActivationCancelled(rtnLt, wm, MatchCancelledCause.CLEAR);
    }
}
Also used : RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple)

Example 4 with RuleTerminalNodeLeftTuple

use of org.drools.core.reteoo.RuleTerminalNodeLeftTuple in project drools by kiegroup.

the class TraitTest method testTupleIntegrityOnModification.

@Test
public void testTupleIntegrityOnModification() {
    String drl = "package test " + "import " + Entity.class.getName() + ";" + "global java.util.List list; " + "declare trait A @propertyReactive value : int end " + "rule Trait when " + "    $core: Entity( ) " + "then " + "    A o = don( $core, A.class ); " + "    System.out.println( 'Found ! ' + o ); " + "end " + "rule Test when " + "   $x: A( value == 0 ) " + "then " + "   list.add( 0 ); " + "end " + "rule Check when " + "   $x: A( value == 42 ) " + "then " + "   list.add( 42 ); " + "end " + "rule Mood when " + "  $x : A( value != 42 ) " + "then " + "  modify ( $x ) { setValue( 42 ); } " + "end ";
    KieBase kbase = getKieBaseFromString(drl);
    TraitFactory.setMode(mode, kbase);
    KieSession ksession = kbase.newKieSession();
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert(new Entity());
    ksession.fireAllRules();
    for (final Object o : ksession.getObjects(new ObjectFilter() {

        @Override
        public boolean accept(Object object) {
            return object.getClass().getName().contains("test.A");
        }
    })) {
        InternalFactHandle handle = (InternalFactHandle) ksession.getFactHandle(o);
        LeftTuple first = handle.getFirstLeftTuple();
        assertTrue(first instanceof RuleTerminalNodeLeftTuple);
        assertEquals("Check", ((RuleTerminalNodeLeftTuple) first).getRule().getName());
    }
    assertEquals(Arrays.asList(0, 42), list);
}
Also used : Entity(org.drools.core.factmodel.traits.Entity) KieBase(org.kie.api.KieBase) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) ObjectFilter(org.drools.core.ObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple) Test(org.junit.Test)

Example 5 with RuleTerminalNodeLeftTuple

use of org.drools.core.reteoo.RuleTerminalNodeLeftTuple 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)

Aggregations

RuleTerminalNodeLeftTuple (org.drools.core.reteoo.RuleTerminalNodeLeftTuple)6 PropagationContext (org.drools.core.spi.PropagationContext)3 DefaultKnowledgeHelper (org.drools.core.base.DefaultKnowledgeHelper)2 EventSupport (org.drools.core.common.EventSupport)2 InternalAgendaGroup (org.drools.core.common.InternalAgendaGroup)2 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ObjectFilter (org.drools.core.ObjectFilter)1 InternalFactHandle (org.drools.core.common.InternalFactHandle)1 Entity (org.drools.core.factmodel.traits.Entity)1 RuleAgendaItem (org.drools.core.phreak.RuleAgendaItem)1 RuleExecutor (org.drools.core.phreak.RuleExecutor)1 LeftTuple (org.drools.core.reteoo.LeftTuple)1 Activation (org.drools.core.spi.Activation)1 RuleFlowGroup (org.drools.core.spi.RuleFlowGroup)1 TupleList (org.drools.core.util.index.TupleList)1 Test (org.junit.Test)1 KieBase (org.kie.api.KieBase)1 ClassObjectFilter (org.kie.api.runtime.ClassObjectFilter)1