Search in sources :

Example 21 with BetaConstraints

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

the class PhreakAccumulateNode method evaluateResultConstraints.

private void evaluateResultConstraints(final AccumulateNode accNode, final LeftTupleSink sink, final Accumulate accumulate, final LeftTuple leftTuple, final PropagationContext context, final InternalWorkingMemory workingMemory, final AccumulateMemory memory, final AccumulateContext accctx, final TupleSets<LeftTuple> trgLeftTuples, final TupleSets<LeftTuple> stagedLeftTuples) {
    // get the actual result
    Object result = accumulate.getResult(memory.workingMemoryContext, accctx.context, leftTuple, workingMemory);
    if (result == null) {
        return;
    }
    if (accctx.getResultFactHandle() == null) {
        final InternalFactHandle handle = accNode.createResultFactHandle(context, workingMemory, leftTuple, result);
        accctx.setResultFactHandle(handle);
        accctx.setResultLeftTuple(sink.createLeftTuple(handle, leftTuple, sink));
    } else {
        accctx.getResultFactHandle().setObject(result);
    }
    // First alpha node filters
    AlphaNodeFieldConstraint[] resultConstraints = accNode.getResultConstraints();
    BetaConstraints resultBinder = accNode.getResultBinder();
    boolean isAllowed = true;
    for (AlphaNodeFieldConstraint resultConstraint : resultConstraints) {
        if (!resultConstraint.isAllowed(accctx.resultFactHandle, workingMemory)) {
            isAllowed = false;
            break;
        }
    }
    if (isAllowed) {
        resultBinder.updateFromTuple(memory.resultsContext, workingMemory, leftTuple);
        if (!resultBinder.isAllowedCachedLeft(memory.resultsContext, accctx.getResultFactHandle())) {
            isAllowed = false;
        }
        resultBinder.resetTuple(memory.resultsContext);
    }
    LeftTuple childLeftTuple = accctx.getResultLeftTuple();
    if (accctx.getPropagationContext() != null) {
        childLeftTuple.setPropagationContext(accctx.getPropagationContext());
        accctx.setPropagationContext(null);
    } else {
        childLeftTuple.setPropagationContext(leftTuple.getPropagationContext());
    }
    if (accctx.propagated) {
        normalizeStagedTuples(stagedLeftTuples, childLeftTuple);
        if (isAllowed) {
            // modify
            trgLeftTuples.addUpdate(childLeftTuple);
        } else {
            // retract
            trgLeftTuples.addDelete(childLeftTuple);
            accctx.propagated = false;
        }
    } else if (isAllowed) {
        // assert
        trgLeftTuples.addInsert(childLeftTuple);
        accctx.propagated = true;
    }
}
Also used : AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) BetaConstraints(org.drools.core.common.BetaConstraints) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple)

Example 22 with BetaConstraints

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

the class PhreakFromNode method doLeftInserts.

public void doLeftInserts(FromNode fromNode, FromMemory fm, LeftTupleSink sink, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> 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 = RuleNetworkEvaluator.useLeftMemory(fromNode, leftTuple);
        if (useLeftMemory) {
            fm.getBetaMemory().getLeftTupleMemory().add(leftTuple);
            matches = new LinkedHashMap<Object, RightTuple>();
            leftTuple.setContextObject(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())) {
                // skip anything if it not assignable
                continue;
            }
            RightTuple rightTuple = fromNode.createRightTuple(leftTuple, propagationContext, wm, object);
            checkConstraintsAndPropagate(sink, leftTuple, rightTuple, alphaConstraints, betaConstraints, propagationContext, wm, fm, context, useLeftMemory, trgLeftTuples, null);
            if (useLeftMemory) {
                fromNode.addToCreatedHandlesMap(matches, rightTuple);
            }
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
    betaConstraints.resetTuple(context);
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) PropagationContext(org.drools.core.spi.PropagationContext) BetaMemory(org.drools.core.reteoo.BetaMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) PhreakJoinNode.updateChildLeftTuple(org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) ContextEntry(org.drools.core.rule.ContextEntry) DataProvider(org.drools.core.spi.DataProvider) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint)

Example 23 with BetaConstraints

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

the class PhreakJoinNode method doLeftInserts.

public void doLeftInserts(JoinNode joinNode, LeftTupleSink sink, BetaMemory bm, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples) {
    TupleMemory ltm = bm.getLeftTupleMemory();
    TupleMemory rtm = bm.getRightTupleMemory();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = joinNode.getRawConstraints();
    for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        boolean useLeftMemory = RuleNetworkEvaluator.useLeftMemory(joinNode, leftTuple);
        if (useLeftMemory) {
            ltm.add(leftTuple);
        }
        FastIterator it = joinNode.getRightIterator(rtm);
        constraints.updateFromTuple(contextEntry, wm, leftTuple);
        for (RightTuple rightTuple = joinNode.getFirstRightTuple(leftTuple, rtm, null, it); rightTuple != null; rightTuple = (RightTuple) it.next(rightTuple)) {
            if (constraints.isAllowedCachedLeft(contextEntry, rightTuple.getFactHandle())) {
                insertChildLeftTuple(trgLeftTuples, leftTuple, rightTuple, null, null, sink, useLeftMemory);
            }
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
    constraints.resetTuple(contextEntry);
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) FastIterator(org.drools.core.util.FastIterator) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) ContextEntry(org.drools.core.rule.ContextEntry)

Example 24 with BetaConstraints

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

the class PhreakJoinNode method doRightInserts.

public void doRightInserts(JoinNode joinNode, LeftTupleSink sink, BetaMemory bm, InternalWorkingMemory wm, TupleSets<RightTuple> srcRightTuples, TupleSets<LeftTuple> trgLeftTuples) {
    TupleMemory ltm = bm.getLeftTupleMemory();
    TupleMemory rtm = bm.getRightTupleMemory();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = joinNode.getRawConstraints();
    if (srcRightTuples.getInsertSize() > 32 && rtm instanceof AbstractHashTable) {
        ((AbstractHashTable) rtm).ensureCapacity(srcRightTuples.getInsertSize());
    }
    for (RightTuple rightTuple = srcRightTuples.getInsertFirst(); rightTuple != null; ) {
        RightTuple next = rightTuple.getStagedNext();
        rtm.add(rightTuple);
        if (ltm != null && ltm.size() > 0) {
            FastIterator it = joinNode.getLeftIterator(ltm);
            constraints.updateFromFactHandle(contextEntry, wm, rightTuple.getFactHandleForEvaluation());
            for (LeftTuple leftTuple = joinNode.getFirstLeftTuple(rightTuple, ltm, it); leftTuple != null; leftTuple = (LeftTuple) it.next(leftTuple)) {
                if (leftTuple.getStagedType() == LeftTuple.UPDATE) {
                    // ignore, as it will get processed via left iteration. Children cannot be processed twice
                    continue;
                }
                if (constraints.isAllowedCachedRight(contextEntry, leftTuple)) {
                    insertChildLeftTuple(trgLeftTuples, leftTuple, rightTuple, null, null, sink, true);
                }
            }
        }
        rightTuple.clearStaged();
        rightTuple = next;
    }
    constraints.resetFactHandle(contextEntry);
}
Also used : AbstractHashTable(org.drools.core.util.AbstractHashTable) BetaConstraints(org.drools.core.common.BetaConstraints) FastIterator(org.drools.core.util.FastIterator) RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) ContextEntry(org.drools.core.rule.ContextEntry)

Example 25 with BetaConstraints

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

the class PhreakJoinNode method doRightUpdates.

public void doRightUpdates(JoinNode joinNode, LeftTupleSink sink, BetaMemory bm, InternalWorkingMemory wm, TupleSets<RightTuple> srcRightTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    TupleMemory ltm = bm.getLeftTupleMemory();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = joinNode.getRawConstraints();
    for (RightTuple rightTuple = srcRightTuples.getUpdateFirst(); rightTuple != null; ) {
        RightTuple next = rightTuple.getStagedNext();
        if (ltm != null && ltm.size() > 0) {
            FastIterator it = joinNode.getLeftIterator(ltm);
            LeftTuple leftTuple = joinNode.getFirstLeftTuple(rightTuple, ltm, it);
            constraints.updateFromFactHandle(contextEntry, wm, rightTuple.getFactHandleForEvaluation());
            // first check our index (for indexed nodes only) hasn't changed and we are returning the same bucket
            // We assume a bucket change if leftTuple == null
            LeftTuple childLeftTuple = rightTuple.getFirstChild();
            if (childLeftTuple != null && ltm.isIndexed() && !it.isFullIterator() && (leftTuple == null || (leftTuple.getMemory() != childLeftTuple.getLeftParent().getMemory()))) {
                // our index has changed, so delete all the previous propagations
                while (childLeftTuple != null) {
                    childLeftTuple.setPropagationContext(rightTuple.getPropagationContext());
                    LeftTuple nextChild = childLeftTuple.getRightParentNext();
                    RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
                    childLeftTuple = nextChild;
                }
            // childLeftTuple is now null, so the next check will attempt matches for new bucket
            }
            // we can't do anything if LeftTupleMemory is empty
            if (leftTuple != null) {
                doRightUpdatesProcessChildren(childLeftTuple, leftTuple, rightTuple, stagedLeftTuples, contextEntry, constraints, sink, it, trgLeftTuples);
            }
        }
        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) TupleMemory(org.drools.core.reteoo.TupleMemory) ContextEntry(org.drools.core.rule.ContextEntry)

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