Search in sources :

Example 26 with BetaConstraints

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

the class PhreakNotNode method doRightUpdates.

public void doRightUpdates(NotNode notNode, LeftTupleSink sink, BetaMemory bm, InternalWorkingMemory wm, TupleSets<RightTuple> srcRightTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    TupleMemory ltm = bm.getLeftTupleMemory();
    TupleMemory rtm = bm.getRightTupleMemory();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = notNode.getRawConstraints();
    boolean iterateFromStart = notNode.isIndexedUnificationJoin() || rtm.getIndexType().isComparison();
    for (RightTuple rightTuple = srcRightTuples.getUpdateFirst(); rightTuple != null; ) {
        RightTuple next = rightTuple.getStagedNext();
        if (ltm != null && ltm.size() > 0) {
            constraints.updateFromFactHandle(contextEntry, wm, rightTuple.getFactHandleForEvaluation());
            FastIterator leftIt = notNode.getLeftIterator(ltm);
            LeftTuple firstLeftTuple = notNode.getFirstLeftTuple(rightTuple, ltm, leftIt);
            // first process non-blocked tuples, as we know only those ones are in the left memory.
            for (LeftTuple leftTuple = firstLeftTuple; leftTuple != null; ) {
                // preserve next now, in case we remove this leftTuple
                LeftTuple temp = (LeftTuple) leftIt.next(leftTuple);
                if (leftTuple.getStagedType() == LeftTuple.UPDATE) {
                    // ignore, as it will get processed via left iteration. Children cannot be processed twice
                    leftTuple = temp;
                    continue;
                }
                // we know that only unblocked LeftTuples are  still in the memory
                if (constraints.isAllowedCachedRight(contextEntry, leftTuple)) {
                    leftTuple.setBlocker(rightTuple);
                    rightTuple.addBlocked(leftTuple);
                    // this is now blocked so remove from memory
                    ltm.remove(leftTuple);
                    LeftTuple childLeftTuple = leftTuple.getFirstChild();
                    if (childLeftTuple != null) {
                        childLeftTuple.setPropagationContext(rightTuple.getPropagationContext());
                        RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
                    }
                }
                leftTuple = temp;
            }
        }
        LeftTuple firstBlocked = rightTuple.getTempBlocked();
        if (firstBlocked != null) {
            RightTuple rootBlocker = rightTuple.getTempNextRightTuple();
            if (rootBlocker == null) {
                iterateFromStart = true;
            }
            FastIterator rightIt = notNode.getRightIterator(rtm);
            // iterate all the existing previous blocked LeftTuples
            for (LeftTuple leftTuple = firstBlocked; leftTuple != null; ) {
                LeftTuple temp = leftTuple.getBlockedNext();
                leftTuple.clearBlocker();
                if (leftTuple.getStagedType() == LeftTuple.UPDATE) {
                    // ignore, as it will get processed via left iteration. Children cannot be processed twice
                    // but need to add it back into list first
                    leftTuple.setBlocker(rightTuple);
                    rightTuple.addBlocked(leftTuple);
                    leftTuple = temp;
                    continue;
                }
                constraints.updateFromTuple(contextEntry, wm, leftTuple);
                if (iterateFromStart) {
                    rootBlocker = notNode.getFirstRightTuple(leftTuple, rtm, null, rightIt);
                }
                // we know that older tuples have been checked so continue next
                for (RightTuple newBlocker = rootBlocker; newBlocker != null; newBlocker = (RightTuple) rightIt.next(newBlocker)) {
                    // There may be UPDATE RightTuples too, but that's ok. They've already been re-added to the correct bucket, safe to be reprocessed.
                    if (leftTuple.getStagedType() != LeftTuple.DELETE && newBlocker.getStagedType() != LeftTuple.DELETE && constraints.isAllowedCachedLeft(contextEntry, newBlocker.getFactHandleForEvaluation())) {
                        leftTuple.setBlocker(newBlocker);
                        newBlocker.addBlocked(leftTuple);
                        break;
                    }
                }
                if (leftTuple.getBlocker() == null) {
                    insertChildLeftTuple(sink, trgLeftTuples, ltm, leftTuple, rightTuple.getPropagationContext(), true);
                }
                leftTuple = temp;
            }
        }
        rightTuple.clearStaged();
        rightTuple = next;
    }
    constraints.resetFactHandle(contextEntry);
    constraints.resetTuple(contextEntry);
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) FastIterator(org.drools.core.util.FastIterator) RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) PhreakJoinNode.updateChildLeftTuple(org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) ContextEntry(org.drools.core.rule.ContextEntry)

Example 27 with BetaConstraints

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

the class PhreakNotNode method doRightInserts.

public void doRightInserts(NotNode notNode, BetaMemory bm, InternalWorkingMemory wm, TupleSets<RightTuple> srcRightTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    TupleMemory ltm = bm.getLeftTupleMemory();
    TupleMemory rtm = bm.getRightTupleMemory();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = notNode.getRawConstraints();
    // this must be processed here, rather than initial insert, as we need to link the blocker
    unlinkNotNodeOnRightInsert(notNode, bm, wm);
    for (RightTuple rightTuple = srcRightTuples.getInsertFirst(); rightTuple != null; ) {
        RightTuple next = rightTuple.getStagedNext();
        rtm.add(rightTuple);
        if (ltm != null && ltm.size() > 0) {
            FastIterator it = notNode.getLeftIterator(ltm);
            constraints.updateFromFactHandle(contextEntry, wm, rightTuple.getFactHandleForEvaluation());
            for (LeftTuple leftTuple = notNode.getFirstLeftTuple(rightTuple, ltm, it); leftTuple != null; ) {
                // preserve next now, in case we remove this leftTuple
                LeftTuple temp = (LeftTuple) it.next(leftTuple);
                if (leftTuple.getStagedType() == LeftTuple.UPDATE) {
                    // ignore, as it will get processed via left iteration. Children cannot be processed twice
                    leftTuple = temp;
                    continue;
                }
                // we know that only unblocked LeftTuples are  still in the memory
                if (constraints.isAllowedCachedRight(contextEntry, leftTuple)) {
                    leftTuple.setBlocker(rightTuple);
                    rightTuple.addBlocked(leftTuple);
                    // this is now blocked so remove from memory
                    ltm.remove(leftTuple);
                    // subclasses like ForallNotNode might override this propagation
                    // ** @TODO (mdp) need to not break forall
                    LeftTuple childLeftTuple = leftTuple.getFirstChild();
                    if (childLeftTuple != null) {
                        // NotNode only has one child
                        childLeftTuple.setPropagationContext(rightTuple.getPropagationContext());
                        RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
                    }
                }
                leftTuple = temp;
            }
        }
        rightTuple.clearStaged();
        rightTuple = next;
    }
    constraints.resetFactHandle(contextEntry);
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) FastIterator(org.drools.core.util.FastIterator) RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) PhreakJoinNode.updateChildLeftTuple(org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) ContextEntry(org.drools.core.rule.ContextEntry)

Example 28 with BetaConstraints

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

the class ReactiveFromBuilder method build.

public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
    final From from = (From) rce;
    context.pushRuleComponent(from);
    @SuppressWarnings("unchecked") BetaConstraints betaConstraints = utils.createBetaNodeConstraint(context, context.getBetaconstraints(), true);
    AlphaNodeFieldConstraint[] alphaNodeFieldConstraints = context.getAlphaConstraints() != null ? context.getAlphaConstraints().toArray(new AlphaNodeFieldConstraint[context.getAlphaConstraints().size()]) : new AlphaNodeFieldConstraint[0];
    ReactiveFromNode node = context.getComponentFactory().getNodeFactoryService().buildReactiveFromNode(context.getNextId(), from.getDataProvider(), context.getTupleSource(), alphaNodeFieldConstraints, betaConstraints, context.isTupleMemoryEnabled(), context, from);
    context.setTupleSource(utils.attachNode(context, node));
    context.setAlphaConstraints(null);
    context.setBetaconstraints(null);
    context.incrementCurrentPatternOffset();
    int patternOffset = context.getCurrentPatternOffset();
    List<XpathConstraint> xpathConstraints = context.getXpathConstraints();
    for (XpathConstraint xpathConstraint : xpathConstraints) {
        for (XpathConstraint.XpathChunk chunk : xpathConstraint.getChunks()) {
            context.setAlphaConstraints(chunk.getAlphaConstraints());
            context.setBetaconstraints(chunk.getBetaConstraints());
            context.setXpathConstraints(chunk.getXpathConstraints());
            build(context, utils, chunk.asFrom());
        }
    }
    context.setCurrentPatternOffset(patternOffset);
}
Also used : XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) BetaConstraints(org.drools.core.common.BetaConstraints) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) ReactiveFromNode(org.drools.core.reteoo.ReactiveFromNode) From(org.drools.core.rule.From) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint)

Example 29 with BetaConstraints

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

the class AccumulateBuilder method build.

/**
 * @inheritDoc
 */
public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
    final Accumulate accumulate = (Accumulate) rce;
    boolean existSubNetwort = false;
    context.pushRuleComponent(accumulate);
    final List<BetaNodeFieldConstraint> resultBetaConstraints = context.getBetaconstraints();
    final List<AlphaNodeFieldConstraint> resultAlphaConstraints = context.getAlphaConstraints();
    RuleConditionElement source = accumulate.getSource();
    if (source instanceof GroupElement) {
        GroupElement ge = (GroupElement) source;
        if (ge.isAnd() && ge.getChildren().size() == 1) {
            source = ge.getChildren().get(0);
        }
    }
    // get builder for the pattern
    final ReteooComponentBuilder builder = utils.getBuilderFor(source);
    // save tuple source and current pattern offset for later if needed
    LeftTupleSource tupleSource = context.getTupleSource();
    final int currentPatternIndex = context.getCurrentPatternOffset();
    // builds the source pattern
    builder.build(context, utils, source);
    // if object source is null, then we need to adapt tuple source into a subnetwork
    if (context.getObjectSource() == null) {
        // attach right input adapter node to convert tuple source into an object source
        RightInputAdapterNode riaNode = context.getComponentFactory().getNodeFactoryService().buildRightInputNode(context.getNextId(), context.getTupleSource(), tupleSource, context);
        // attach right input adapter node to convert tuple source into an object source
        context.setObjectSource(utils.attachNode(context, riaNode));
        // restore tuple source from before the start of the sub network
        context.setTupleSource(tupleSource);
        // create a tuple start equals constraint and set it in the context
        final TupleStartEqualsConstraint constraint = TupleStartEqualsConstraint.getInstance();
        final List<BetaNodeFieldConstraint> betaConstraints = new ArrayList<BetaNodeFieldConstraint>();
        betaConstraints.add(constraint);
        context.setBetaconstraints(betaConstraints);
        existSubNetwort = true;
    }
    NodeFactory nfactory = context.getComponentFactory().getNodeFactoryService();
    final BetaConstraints resultsBinder = utils.createBetaNodeConstraint(context, resultBetaConstraints, true);
    final BetaConstraints sourceBinder = utils.createBetaNodeConstraint(context, context.getBetaconstraints(), false);
    AccumulateNode accNode = nfactory.buildAccumulateNode(context.getNextId(), context.getTupleSource(), context.getObjectSource(), resultAlphaConstraints.toArray(new AlphaNodeFieldConstraint[resultAlphaConstraints.size()]), sourceBinder, resultsBinder, accumulate, existSubNetwort, context);
    context.setTupleSource(utils.attachNode(context, accNode));
    // source pattern was bound, so nulling context
    context.setObjectSource(null);
    context.setCurrentPatternOffset(currentPatternIndex);
    context.popRuleComponent();
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) AccumulateNode(org.drools.core.reteoo.AccumulateNode) GroupElement(org.drools.core.rule.GroupElement) ArrayList(java.util.ArrayList) TupleStartEqualsConstraint(org.drools.core.common.TupleStartEqualsConstraint) RuleConditionElement(org.drools.core.rule.RuleConditionElement) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) TupleStartEqualsConstraint(org.drools.core.common.TupleStartEqualsConstraint) Accumulate(org.drools.core.rule.Accumulate) LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode)

Aggregations

BetaConstraints (org.drools.core.common.BetaConstraints)29 LeftTuple (org.drools.core.reteoo.LeftTuple)21 ContextEntry (org.drools.core.rule.ContextEntry)21 RightTuple (org.drools.core.reteoo.RightTuple)19 TupleMemory (org.drools.core.reteoo.TupleMemory)18 FastIterator (org.drools.core.util.FastIterator)18 PhreakJoinNode.updateChildLeftTuple (org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple)12 BetaMemory (org.drools.core.reteoo.BetaMemory)8 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)7 Accumulate (org.drools.core.rule.Accumulate)6 ArrayList (java.util.ArrayList)5 InternalFactHandle (org.drools.core.common.InternalFactHandle)4 BetaNodeFieldConstraint (org.drools.core.spi.BetaNodeFieldConstraint)4 SingleBetaConstraints (org.drools.core.common.SingleBetaConstraints)3 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)3 List (java.util.List)2 RuleBaseConfiguration (org.drools.core.RuleBaseConfiguration)2 TupleStartEqualsConstraint (org.drools.core.common.TupleStartEqualsConstraint)2 AccumulateNode (org.drools.core.reteoo.AccumulateNode)2 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)2