Search in sources :

Example 1 with Tuple

use of org.drools.core.spi.Tuple in project drools by kiegroup.

the class PhreakQueryTerminalNode method doLeftInserts.

public void doLeftInserts(QueryTerminalNode qtnNode, InternalAgenda agenda, TupleSets<LeftTuple> srcLeftTuples, LinkedList<StackEntry> stack) {
    for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        // qtnNode.assertLeftTuple( leftTuple, leftTuple.getPropagationContext(), wm );
        PropagationContext pCtx = RuleTerminalNode.findMostRecentPropagationContext(leftTuple, leftTuple.getPropagationContext());
        // find the DroolsQuery object
        Tuple rootEntry = leftTuple.getRootTuple();
        DroolsQuery dquery = (DroolsQuery) rootEntry.getFactHandle().getObject();
        dquery.setQuery(qtnNode.getQuery());
        if (dquery.getStackEntry() != null) {
            checkAndTriggerQueryReevaluation(agenda, stack, rootEntry, dquery);
        }
        // Add results to the adapter
        dquery.getQueryResultCollector().rowAdded(qtnNode.getQuery(), leftTuple, pCtx, agenda.getWorkingMemory());
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : PropagationContext(org.drools.core.spi.PropagationContext) LeftTuple(org.drools.core.reteoo.LeftTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) Tuple(org.drools.core.spi.Tuple) DroolsQuery(org.drools.core.base.DroolsQuery)

Example 2 with Tuple

use of org.drools.core.spi.Tuple in project drools by kiegroup.

the class ReactiveList method set.

@Override
public T set(int index, T element) {
    T previous = wrapped.set(index, element);
    if (previous != element) {
        // this is indeed intended != to check by reference
        ReactiveObjectUtil.notifyModification(element, getLeftTuples(), ModificationType.ADD);
        if (element instanceof ReactiveObject) {
            for (Tuple lts : getLeftTuples()) {
                ((ReactiveObject) element).addLeftTuple(lts);
            }
        }
        if (previous instanceof ReactiveObject) {
            for (Tuple lts : getLeftTuples()) {
                ((ReactiveObject) previous).removeLeftTuple(lts);
            }
        }
        ReactiveObjectUtil.notifyModification(previous, getLeftTuples(), ModificationType.REMOVE);
    }
    return previous;
}
Also used : Tuple(org.drools.core.spi.Tuple)

Example 3 with Tuple

use of org.drools.core.spi.Tuple in project drools by kiegroup.

the class ReactiveList method add.

@Override
public void add(int index, T element) {
    wrapped.add(index, element);
    ReactiveObjectUtil.notifyModification(element, getLeftTuples(), ModificationType.ADD);
    if (element instanceof ReactiveObject) {
        for (Tuple lts : getLeftTuples()) {
            ((ReactiveObject) element).addLeftTuple(lts);
        }
    }
}
Also used : Tuple(org.drools.core.spi.Tuple)

Example 4 with Tuple

use of org.drools.core.spi.Tuple in project drools by kiegroup.

the class ReactiveList method remove.

@Override
public T remove(int index) {
    T result = wrapped.remove(index);
    if (result instanceof ReactiveObject) {
        for (Tuple lts : getLeftTuples()) {
            ((ReactiveObject) result).removeLeftTuple(lts);
        }
    }
    ReactiveObjectUtil.notifyModification(result, getLeftTuples(), ModificationType.REMOVE);
    return result;
}
Also used : Tuple(org.drools.core.spi.Tuple)

Example 5 with Tuple

use of org.drools.core.spi.Tuple in project drools by kiegroup.

the class RuleExecutor method fire.

private int fire(InternalWorkingMemory wm, InternalAgenda agenda, AgendaFilter filter, int fireCount, int fireLimit) {
    int localFireCount = 0;
    if (!tupleList.isEmpty()) {
        if (!fireExitedEarly && isDeclarativeAgendaEnabled()) {
            // Network Evaluation can notify meta rules, which should be given a chance to fire first
            RuleAgendaItem nextRule = agenda.peekNextRule();
            if (!isHigherSalience(nextRule)) {
                fireExitedEarly = true;
                return localFireCount;
            }
        }
        RuleTerminalNode rtn = (RuleTerminalNode) pmem.getPathEndNode();
        RuleImpl rule = rtn.getRule();
        Tuple tuple = getNextTuple();
        if (rule.isAllMatches()) {
            fireConsequenceEvent(wm, agenda, (AgendaItem) tuple, DefaultAgenda.ON_BEFORE_ALL_FIRES_CONSEQUENCE_NAME);
        }
        Tuple lastTuple = null;
        for (; tuple != null; lastTuple = tuple, tuple = getNextTuple()) {
            // if the current Rule is no-loop and the origin rule is the same then return
            if (cancelAndContinue(wm, rtn, rule, tuple, filter)) {
                continue;
            }
            AgendaItem item = (AgendaItem) tuple;
            if (agenda.getActivationsFilter() != null && !agenda.getActivationsFilter().accept(item, wm, rtn)) {
                // only relevant for seralization, to not refire Matches already fired
                continue;
            }
            fireActivation(wm, agenda, item);
            localFireCount++;
            if (rtn.getLeftTupleSource() == null) {
                // The activation firing removed this rule from the rule base
                break;
            }
            agenda.flushPropagations();
            // dyanmic salience may have updated it, so get again.
            int salience = ruleAgendaItem.getSalience();
            if (queue != null && !queue.isEmpty() && salience != queue.peek().getSalience()) {
                ruleAgendaItem.dequeue();
                ruleAgendaItem.setSalience(queue.peek().getSalience());
                ruleAgendaItem.getAgendaGroup().add(ruleAgendaItem);
            }
            if (!rule.isAllMatches()) {
                // if firing rule is @All don't give way to other rules
                if (haltRuleFiring(fireCount, fireLimit, localFireCount, agenda)) {
                    // another rule has high priority and is on the agenda, so evaluate it first
                    break;
                }
                if (!wm.isSequential()) {
                    reEvaluateNetwork(agenda);
                }
            }
        }
        if (rule.isAllMatches()) {
            fireConsequenceEvent(wm, agenda, (AgendaItem) lastTuple, DefaultAgenda.ON_AFTER_ALL_FIRES_CONSEQUENCE_NAME);
        }
    }
    removeRuleAgendaItemWhenEmpty(wm);
    fireExitedEarly = false;
    return localFireCount;
}
Also used : RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Tuple(org.drools.core.spi.Tuple) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple) AgendaItem(org.drools.core.common.AgendaItem) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode)

Aggregations

Tuple (org.drools.core.spi.Tuple)54 InternalFactHandle (org.drools.core.common.InternalFactHandle)17 LeftTuple (org.drools.core.reteoo.LeftTuple)16 RightTuple (org.drools.core.reteoo.RightTuple)14 FastIterator (org.drools.core.util.FastIterator)14 Declaration (org.drools.core.rule.Declaration)9 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)8 WorkingMemory (org.drools.core.WorkingMemory)7 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)7 Cheese (org.drools.core.test.model.Cheese)7 Test (org.junit.Test)7 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)6 FieldIndex (org.drools.core.util.AbstractHashTable.FieldIndex)6 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)5 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)5 BetaMemory (org.drools.core.reteoo.BetaMemory)5 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)5 TupleMemory (org.drools.core.reteoo.TupleMemory)5 MethodVisitor (org.mvel2.asm.MethodVisitor)5 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)4