Package org.drools.core.common

Examples of org.drools.core.common.BetaConstraints


     * @return
     */
    public BetaConstraints createBetaNodeConstraint(final BuildContext context,
                                                    final List<BetaNodeFieldConstraint> list,
                                                    final boolean disableIndexing) {
        BetaConstraints constraints;
        switch ( list.size() ) {
            case 0 :
                constraints = EmptyBetaConstraints.getInstance();
                break;
            case 1 :
View Full Code Here


     * @return
     */
    public BetaConstraints createBetaNodeConstraint(final BuildContext context,
                                                    final List<BetaNodeFieldConstraint> list,
                                                    final boolean disableIndexing) {
        BetaConstraints constraints;
        switch ( list.size() ) {
            case 0 :
                constraints = EmptyBetaConstraints.getInstance();
                break;
            case 1 :
View Full Code Here

                                  LeftTupleSets srcLeftTuples,
                                  LeftTupleSets trgLeftTuples) {

            BetaMemory bm = fm.getBetaMemory();
            ContextEntry[] context = bm.getContext();
            BetaConstraints betaConstraints = fromNode.getBetaConstraints();
            AlphaNodeFieldConstraint[] alphaConstraints = fromNode.getAlphaConstraints();
            DataProvider dataProvider = fromNode.getDataProvider();
            Class resultClass = fromNode.getResultClass();

            for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
                LeftTuple next = leftTuple.getStagedNext();

                PropagationContext propagationContext = leftTuple.getPropagationContext();

                Map<Object, RightTuple> matches = null;
                boolean useLeftMemory = useLeftMemory(fromNode, leftTuple);

                if (useLeftMemory) {
                    fm.betaMemory.getLeftTupleMemory().add(leftTuple);
                    matches = new LinkedHashMap<Object, RightTuple>();
                    leftTuple.setObject(matches);
                }

                betaConstraints.updateFromTuple(context,
                                                wm,
                                                leftTuple);

                for (final java.util.Iterator<?> it = dataProvider.getResults(leftTuple,
                                                                              wm,
                                                                              propagationContext,
                                                                              fm.providerContext); it.hasNext(); ) {
                    final Object object = it.next();
                    if ( (object == null) || !resultClass.isAssignableFrom( object.getClass() ) ) {
                        continue; // skip anything if it not assignable
                    }

                    RightTuple rightTuple = fromNode.createRightTuple(leftTuple,
                                                                      propagationContext,
                                                                      wm,
                                                                      object);

                    checkConstraintsAndPropagate(sink,
                                                 leftTuple,
                                                 rightTuple,
                                                 alphaConstraints,
                                                 betaConstraints,
                                                 propagationContext,
                                                 wm,
                                                 fm,
                                                 bm,
                                                 context,
                                                 useLeftMemory,
                                                 trgLeftTuples,
                                                 null);
                    if (useLeftMemory) {
                        fromNode.addToCreatedHandlesMap(matches,
                                                        rightTuple);
                    }
                }

                leftTuple.clearStaged();
                leftTuple = next;
            }
            betaConstraints.resetTuple(context);
        }
View Full Code Here

                                  LeftTupleSets trgLeftTuples,
                                  LeftTupleSets stagedLeftTuples) {
            BetaMemory bm = fm.getBetaMemory();
            LeftTupleMemory ltm = bm.getLeftTupleMemory();
            ContextEntry[] context = bm.getContext();
            BetaConstraints betaConstraints = fromNode.getBetaConstraints();
            AlphaNodeFieldConstraint[] alphaConstraints = fromNode.getAlphaConstraints();
            DataProvider dataProvider = fromNode.getDataProvider();
            Class resultClass = fromNode.getResultClass();

            for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
                LeftTuple next = leftTuple.getStagedNext();

                PropagationContext propagationContext = leftTuple.getPropagationContext();

                ltm.removeAdd(leftTuple);

                final Map<Object, RightTuple> previousMatches = (Map<Object, RightTuple>) leftTuple.getObject();
                final Map<Object, RightTuple> newMatches = new HashMap<Object, RightTuple>();
                leftTuple.setObject(newMatches);

                betaConstraints.updateFromTuple(context,
                                                wm,
                                                leftTuple);

                FastIterator rightIt = LinkedList.fastIterator;
                for (final java.util.Iterator<?> it = dataProvider.getResults(leftTuple,
                                                                              wm,
                                                                              propagationContext,
                                                                              fm.providerContext); it.hasNext(); ) {
                    final Object object = it.next();
                    if ( (object == null) || !resultClass.isAssignableFrom( object.getClass() ) ) {
                        continue; // skip anything if it not assignable
                    }

                    RightTuple rightTuple = previousMatches.remove(object);

                    if (rightTuple == null) {
                        // new match, propagate assert
                        rightTuple = fromNode.createRightTuple(leftTuple,
                                                               propagationContext,
                                                               wm,
                                                               object);
                    } else {
                        // previous match, so reevaluate and propagate modify
                        if (rightIt.next(rightTuple) != null) {
                            // handle the odd case where more than one object has the same hashcode/equals value
                            previousMatches.put(object,
                                                (RightTuple) rightIt.next(rightTuple));
                            rightTuple.setNext(null);
                        }
                    }

                    checkConstraintsAndPropagate(sink,
                                                 leftTuple,
                                                 rightTuple,
                                                 alphaConstraints,
                                                 betaConstraints,
                                                 propagationContext,
                                                 wm,
                                                 fm,
                                                 bm,
                                                 context,
                                                 true,
                                                 trgLeftTuples,
                                                 null);

                    fromNode.addToCreatedHandlesMap(newMatches,
                                                    rightTuple);
                }

                for (RightTuple rightTuple : previousMatches.values()) {
                    for (RightTuple current = rightTuple; current != null; current = (RightTuple) rightIt.next(current)) {
                        LeftTuple childLeftTuple = current.getFirstChild();
                        childLeftTuple.unlinkFromLeftParent();
                        childLeftTuple.unlinkFromRightParent();

                        switch (childLeftTuple.getStagedType()) {
                            // handle clash with already staged entries
                            case LeftTuple.INSERT:
                                stagedLeftTuples.removeInsert(childLeftTuple);
                                break;
                            case LeftTuple.UPDATE:
                                stagedLeftTuples.removeUpdate(childLeftTuple);
                                break;
                        }

                        childLeftTuple.setPropagationContext(propagationContext);
                        trgLeftTuples.addDelete(childLeftTuple);
                    }
                }

                leftTuple.clearStaged();
                leftTuple = next;
            }
            betaConstraints.resetTuple(context);
        }
View Full Code Here

                                  LeftTupleSets srcLeftTuples,
                                  LeftTupleSets trgLeftTuples) {
            LeftTupleMemory ltm = bm.getLeftTupleMemory();
            RightTupleMemory rtm = bm.getRightTupleMemory();
            ContextEntry[] contextEntry = bm.getContext();
            BetaConstraints constraints = existsNode.getRawConstraints();

            for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
                LeftTuple next = leftTuple.getStagedNext();

                FastIterator it = existsNode.getRightIterator(rtm);
                PropagationContext context = leftTuple.getPropagationContext();

                boolean useLeftMemory = useLeftMemory(existsNode, leftTuple);

                constraints.updateFromTuple(contextEntry,
                                            wm,
                                            leftTuple);

                // This method will also remove rightTuples that are from subnetwork where no leftmemory use used
                findLeftTupleBlocker(existsNode, rtm, contextEntry, constraints, leftTuple, it, context, useLeftMemory);

                if (leftTuple.getBlocker() != null) {
                    // tuple is not blocked to propagate
                    trgLeftTuples.addInsert(sink.createLeftTuple(leftTuple,
                                                                 sink,
                                                                 leftTuple.getBlocker().getPropagationContext(), useLeftMemory));
                } else if (useLeftMemory) {
                    // LeftTuple is not blocked, so add to memory so other RightTuples can match
                    ltm.add(leftTuple);
                }
                leftTuple.clearStaged();
                leftTuple = next;
            }
            constraints.resetTuple(contextEntry);
        }
View Full Code Here

                                   RightTupleSets srcRightTuples,
                                   LeftTupleSets trgLeftTuples) {
            LeftTupleMemory ltm = bm.getLeftTupleMemory();
            RightTupleMemory rtm = bm.getRightTupleMemory();
            ContextEntry[] contextEntry = bm.getContext();
            BetaConstraints constraints = existsNode.getRawConstraints();

            for (RightTuple rightTuple = srcRightTuples.getInsertFirst(); rightTuple != null; ) {
                RightTuple next = rightTuple.getStagedNext();
                rtm.add(rightTuple);

                FastIterator it = existsNode.getLeftIterator(ltm);
                PropagationContext context = rightTuple.getPropagationContext();

                constraints.updateFromFactHandle(contextEntry,
                                                 wm,
                                                 rightTuple.getFactHandle());

                for (LeftTuple leftTuple = existsNode.getFirstLeftTuple(rightTuple, ltm, context, 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);

                        ltm.remove(leftTuple);

                        trgLeftTuples.addInsert(sink.createLeftTuple(leftTuple,
                                                                     sink,
                                                                     rightTuple.getPropagationContext(), true));
                    }

                    leftTuple = temp;
                }
                rightTuple.clearStaged();
                rightTuple = next;
            }
            constraints.resetFactHandle(contextEntry);
        }
View Full Code Here

                                  LeftTupleSets stagedLeftTuples) {
            boolean tupleMemory = true;
            LeftTupleMemory ltm = bm.getLeftTupleMemory();
            RightTupleMemory rtm = bm.getRightTupleMemory();
            ContextEntry[] contextEntry = bm.getContext();
            BetaConstraints constraints = existsNode.getRawConstraints();

            for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
                LeftTuple next = leftTuple.getStagedNext();
                PropagationContext context = leftTuple.getPropagationContext();

                FastIterator rightIt = existsNode.getRightIterator(rtm);

                RightTuple firstRightTuple = existsNode.getFirstRightTuple(leftTuple, rtm, null, rightIt);

                // If in memory, remove it, because we'll need to add it anyway if it's not blocked, to ensure iteration order
                RightTuple blocker = leftTuple.getBlocker();
                if (blocker == null) {
                    ltm.remove(leftTuple);
                } else {
                    // check if we changed bucket
                    if (rtm.isIndexed() && !rightIt.isFullIterator()) {
                        // if newRightTuple is null, we assume there was a bucket change and that bucket is empty               
                        if (firstRightTuple == null || firstRightTuple.getMemory() != blocker.getMemory()) {
                            // we changed bucket, so blocker no longer blocks
                            blocker.removeBlocked(leftTuple);
                            blocker = null;
                        }
                    }
                }

                constraints.updateFromTuple(contextEntry,
                                            wm,
                                            leftTuple);

                // if we where not blocked before (or changed buckets), or the previous blocker no longer blocks, then find the next blocker
                if (blocker == null || !constraints.isAllowedCachedLeft(contextEntry,
                                                                        blocker.getFactHandle())) {

                    if (blocker != null) {
                        // remove previous blocker if it exists, as we know it doesn't block any more
                        blocker.removeBlocked(leftTuple);
                    }

                    // find first blocker, because it's a modify, we need to start from the beginning again       
                    for (RightTuple newBlocker = firstRightTuple; newBlocker != null; newBlocker = (RightTuple) rightIt.next(newBlocker)) {
                        if (constraints.isAllowedCachedLeft(contextEntry,
                                                            newBlocker.getFactHandle())) {
                            leftTuple.setBlocker(newBlocker);
                            newBlocker.addBlocked(leftTuple);

                            break;
                        }
                    }
                }

                if (leftTuple.getBlocker() == null) {
                    // not blocked
                    ltm.add(leftTuple); // add to memory so other fact handles can attempt to match

                    if (leftTuple.getFirstChild() != null) {
                        // with previous children, delete
                        if (leftTuple.getFirstChild() != null) {
                            LeftTuple childLeftTuple = leftTuple.getFirstChild();

                            if (childLeftTuple != null) {
                                // no need to update pctx, as no right available, and pctx will exist on a parent LeftTuple anyway
                                childLeftTuple = deleteLeftChild(childLeftTuple, trgLeftTuples, stagedLeftTuples);
                            }
                        }
                    }
                    // with no previous children. do nothing.
                } else if (leftTuple.getFirstChild() == null) {
                    // blocked, with no previous children, insert
                    trgLeftTuples.addInsert(sink.createLeftTuple(leftTuple,
                                                                 sink,
                                                                 leftTuple.getBlocker().getPropagationContext(), tupleMemory));
                } else {
                    // blocked, with previous children, modify
                    if (leftTuple.getFirstChild() != null) {
                        LeftTuple childLeftTuple = leftTuple.getFirstChild();

                        while (childLeftTuple != null) {
                            switch (childLeftTuple.getStagedType()) {
                                // handle clash with already staged entries
                                case LeftTuple.INSERT:
                                    stagedLeftTuples.removeInsert(childLeftTuple);
                                    break;
                                case LeftTuple.UPDATE:
                                    stagedLeftTuples.removeUpdate(childLeftTuple);
                                    break;
                            }

                            // update, childLeftTuple is updated
                            childLeftTuple.setPropagationContext( leftTuple.getBlocker().getPropagationContext() );
                            trgLeftTuples.addUpdate(childLeftTuple);
                            childLeftTuple.reAddRight();
                            childLeftTuple = childLeftTuple.getLeftParentNext();
                        }
                    }
                }

                leftTuple.clearStaged();
                leftTuple = next;
            }
            constraints.resetTuple(contextEntry);
        }
View Full Code Here

                                   LeftTupleSets trgLeftTuples,
                                   LeftTupleSets stagedLeftTuples) {
            LeftTupleMemory ltm = bm.getLeftTupleMemory();
            RightTupleMemory rtm = bm.getRightTupleMemory();
            ContextEntry[] contextEntry = bm.getContext();
            BetaConstraints constraints = existsNode.getRawConstraints();

            for (RightTuple rightTuple = srcRightTuples.getUpdateFirst(); rightTuple != null; ) {
                RightTuple next = rightTuple.getStagedNext();

                FastIterator leftIt = existsNode.getLeftIterator(ltm);
                PropagationContext context = rightTuple.getPropagationContext();

                LeftTuple firstLeftTuple = existsNode.getFirstLeftTuple(rightTuple, ltm, context, leftIt);

                LeftTuple firstBlocked = rightTuple.getBlocked();
                // we now have  reference to the first Blocked, so null it in the rightTuple itself, so we can rebuild
                rightTuple.nullBlocked();

                // 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);

                        // subclasses like ForallNotNode might override this propagation
                        trgLeftTuples.addInsert(sink.createLeftTuple(leftTuple,
                                                                     sink,
                                                                     rightTuple.getPropagationContext(), true));
                    }

                    leftTuple = temp;
                }

                if (firstBlocked != null) {
                    FastIterator rightIt = existsNode.getRightIterator(rtm);

                    boolean useComparisonIndex = rtm.getIndexType().isComparison();

                    // now process existing blocks, we only process existing and not new from above loop
                    RightTuple rootBlocker = useComparisonIndex ? null : (RightTuple) rightIt.next(rightTuple);

                    RightTupleList list = rightTuple.getMemory();

                    // we must do this after we have the next in memory
                    // We add to the end to give an opportunity to re-match if in same bucket
                    rtm.removeAdd(rightTuple);

                    if (!useComparisonIndex && rootBlocker == null && list == rightTuple.getMemory()) {
                        // we are at the end of the list, but still in same bucket, so set to self, to give self a chance to rematch
                        rootBlocker = rightTuple;
                    }

                    // iterate all the existing previous blocked LeftTuples
                    for (LeftTuple leftTuple = (LeftTuple) firstBlocked; leftTuple != null; ) {
                        LeftTuple temp = leftTuple.getBlockedNext();

                        leftTuple.clearBlocker(); // must null these as we are re-adding them to the list

                        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 (useComparisonIndex) {
                            rootBlocker = existsNode.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)) {
                            if (constraints.isAllowedCachedLeft(contextEntry,
                                                                newBlocker.getFactHandle())) {
                                leftTuple.setBlocker(newBlocker);
                                newBlocker.addBlocked(leftTuple);

                                break;
                            }
                        }

                        if (leftTuple.getBlocker() == null) {
                            // was previous blocked and not in memory, so add
                            ltm.add(leftTuple);

                            LeftTuple childLeftTuple = leftTuple.getFirstChild();
                            if (childLeftTuple != null) {
                                childLeftTuple.setPropagationContext(rightTuple.getPropagationContext());
                                childLeftTuple = deleteLeftChild(childLeftTuple, trgLeftTuples, stagedLeftTuples);
                            }
                        }

                        leftTuple = temp;
                    }
                } else {
                    // we had to do this at the end, rather than beginning as this 'if' block needs the next memory tuple
                    rtm.removeAdd(rightTuple);
                }

                rightTuple.clearStaged();
                rightTuple = next;
            }
            constraints.resetFactHandle(contextEntry);
        }
View Full Code Here

                                   LeftTupleSets trgLeftTuples,
                                   LeftTupleSets stagedLeftTuples) {
            RightTupleMemory rtm = bm.getRightTupleMemory();
            LeftTupleMemory ltm = bm.getLeftTupleMemory();
            ContextEntry[] contextEntry = bm.getContext();
            BetaConstraints constraints = existsNode.getRawConstraints();

            for (RightTuple rightTuple = srcRightTuples.getDeleteFirst(); rightTuple != null; ) {
                RightTuple next = rightTuple.getStagedNext();

                FastIterator it = existsNode.getRightIterator(rtm);

                boolean useComparisonIndex = rtm.getIndexType().isComparison();
                RightTuple rootBlocker = useComparisonIndex ? null : (RightTuple) it.next(rightTuple);

                if (rightTuple.getMemory() != null) {
                    // it may have been staged and never actually added
                    rtm.remove(rightTuple);
                }

                if (rightTuple.getBlocked() != null) {

                    PropagationContext context = rightTuple.getPropagationContext();

                    for (LeftTuple leftTuple = rightTuple.getBlocked(); 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
                            leftTuple = temp;
                            continue;
                        }

                        constraints.updateFromTuple(contextEntry,
                                                    wm,
                                                    leftTuple);

                        if (useComparisonIndex) {
                            rootBlocker = rtm.getFirst(leftTuple, null, it);
                        }

                        // we know that older tuples have been checked so continue previously
                        for (RightTuple newBlocker = rootBlocker; newBlocker != null; newBlocker = (RightTuple) it.next(newBlocker)) {
                            if (constraints.isAllowedCachedLeft(contextEntry,
                                                                newBlocker.getFactHandle())) {
                                leftTuple.setBlocker(newBlocker);
                                newBlocker.addBlocked(leftTuple);

                                break;
View Full Code Here

            Accumulate accumulate = accNode.getAccumulate();
            BetaMemory bm = am.getBetaMemory();
            LeftTupleMemory ltm = bm.getLeftTupleMemory();
            RightTupleMemory rtm = bm.getRightTupleMemory();
            ContextEntry[] contextEntry = bm.getContext();
            BetaConstraints constraints = accNode.getRawConstraints();


            for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
                LeftTuple next = leftTuple.getStagedNext();

                boolean useLeftMemory = useLeftMemory(accNode, leftTuple);

                if (useLeftMemory) {
                    ltm.add(leftTuple);
                }

                PropagationContext context = leftTuple.getPropagationContext();

                AccumulateContext accresult = new AccumulateContext();


                leftTuple.setObject(accresult);

                accresult.context = accumulate.createContext();

                accumulate.init(am.workingMemoryContext,
                                accresult.context,
                                leftTuple,
                                wm);

                constraints.updateFromTuple(contextEntry,
                                            wm,
                                            leftTuple);

                FastIterator rightIt = accNode.getRightIterator(rtm);

                for (RightTuple rightTuple = accNode.getFirstRightTuple(leftTuple,
                                                                        rtm,
                                                                        null,
                                                                        rightIt); rightTuple != null; ) {
                    RightTuple nextRightTuple = (RightTuple) rightIt.next(rightTuple);

                    InternalFactHandle handle = rightTuple.getFactHandle();
                    if (constraints.isAllowedCachedLeft(contextEntry,
                                                        handle)) {
                        // add a match
                        addMatch(accNode,
                                 accumulate,
                                 leftTuple,
                                 rightTuple,
                                 null,
                                 null,
                                 wm,
                                 am,
                                 accresult,
                                 useLeftMemory);

                        if (!useLeftMemory && accNode.isRightInputIsRiaNode()) {
                            // RIAN with no left memory must have their right tuples removed
                            rtm.remove(rightTuple);
                        }
                    }

                    rightTuple = nextRightTuple;
                }

                leftTuple.clearStaged();
                trgLeftTuples.addInsert(leftTuple);

                constraints.resetTuple(contextEntry);

                leftTuple = next;
            }
            constraints.resetTuple(contextEntry);
        }
View Full Code Here

TOP

Related Classes of org.drools.core.common.BetaConstraints

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.