Search in sources :

Example 1 with BetaMemory

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

the class RuleNetworkEvaluator method doRiaNode2.

private void doRiaNode2(InternalWorkingMemory wm, TupleSets<LeftTuple> srcTuples, RightInputAdapterNode riaNode) {
    ObjectSink[] sinks = riaNode.getObjectSinkPropagator().getSinks();
    BetaNode betaNode = (BetaNode) sinks[0];
    BetaMemory bm;
    Memory nodeMem = wm.getNodeMemory(betaNode);
    if (NodeTypeEnums.AccumulateNode == betaNode.getType()) {
        bm = ((AccumulateMemory) nodeMem).getBetaMemory();
    } else {
        bm = (BetaMemory) nodeMem;
    }
    TupleSets<RightTuple> rightTuples = bm.getStagedRightTuples();
    // Build up iteration array for other sinks
    BetaNode[] bns = null;
    BetaMemory[] bms = null;
    int length = sinks.length;
    if (length > 1) {
        bns = new BetaNode[sinks.length - 1];
        bms = new BetaMemory[sinks.length - 1];
        for (int i = 1; i < length; i++) {
            bns[i - 1] = (BetaNode) sinks[i];
            Memory nodeMem2 = wm.getNodeMemory(bns[i - 1]);
            if (NodeTypeEnums.AccumulateNode == betaNode.getType()) {
                bms[i - 1] = ((AccumulateMemory) nodeMem2).getBetaMemory();
            } else {
                bms[i - 1] = (BetaMemory) nodeMem2;
            }
        }
    }
    // subtract one, as first is not in the array;
    length--;
    for (SubnetworkTuple subnetworkTuple = (SubnetworkTuple) srcTuples.getInsertFirst(); subnetworkTuple != null; ) {
        SubnetworkTuple next = (SubnetworkTuple) subnetworkTuple.getStagedNext();
        if (bm.getStagedRightTuples().isEmpty()) {
            bm.setNodeDirtyWithoutNotify();
        }
        subnetworkTuple.prepareStagingOnRight();
        rightTuples.addInsert(subnetworkTuple);
        if (bns != null) {
            for (int i = 0; i < length; i++) {
                if (bms[i].getStagedRightTuples().isEmpty()) {
                    bms[i].setNodeDirtyWithoutNotify();
                }
                subnetworkTuple = riaNode.createPeer(subnetworkTuple);
                bms[i].getStagedRightTuples().addInsert(subnetworkTuple);
            }
        }
        subnetworkTuple = next;
    }
    for (SubnetworkTuple subnetworkTuple = (SubnetworkTuple) srcTuples.getDeleteFirst(); subnetworkTuple != null; ) {
        SubnetworkTuple next = (SubnetworkTuple) subnetworkTuple.getStagedNext();
        if (rightTuples.isEmpty()) {
            bm.setNodeDirtyWithoutNotify();
        }
        switch(subnetworkTuple.getStagedTypeOnRight()) {
            // handle clash with already staged entries
            case Tuple.INSERT:
                rightTuples.removeInsert(subnetworkTuple);
                break;
            case Tuple.UPDATE:
                rightTuples.removeUpdate(subnetworkTuple);
                break;
        }
        subnetworkTuple.prepareStagingOnRight();
        rightTuples.addDelete(subnetworkTuple);
        if (bns != null) {
            for (int i = 0; i < length; i++) {
                subnetworkTuple = (SubnetworkTuple) subnetworkTuple.getPeer();
                if (bms[i].getStagedRightTuples().isEmpty()) {
                    bms[i].setNodeDirtyWithoutNotify();
                }
                bms[i].getStagedRightTuples().addDelete(subnetworkTuple);
                subnetworkTuple.setStagedOnRight();
            }
        }
        subnetworkTuple = next;
    }
    for (SubnetworkTuple subnetworkTuple = (SubnetworkTuple) srcTuples.getUpdateFirst(); subnetworkTuple != null; ) {
        SubnetworkTuple next = (SubnetworkTuple) subnetworkTuple.getStagedNext();
        if (rightTuples.isEmpty()) {
            bm.setNodeDirtyWithoutNotify();
        }
        subnetworkTuple.prepareStagingOnRight();
        rightTuples.addUpdate(subnetworkTuple);
        if (bns != null) {
            for (int i = 0; i < length; i++) {
                subnetworkTuple = (SubnetworkTuple) subnetworkTuple.getPeer();
                if (bms[i].getStagedRightTuples().isEmpty()) {
                    bms[i].setNodeDirtyWithoutNotify();
                }
                bms[i].getStagedRightTuples().addUpdate(subnetworkTuple);
                subnetworkTuple.setStagedOnRight();
            }
        }
        subnetworkTuple = next;
    }
    srcTuples.resetAll();
}
Also used : BetaNode(org.drools.core.reteoo.BetaNode) Memory(org.drools.core.common.Memory) QueryElementNodeMemory(org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory) PathMemory(org.drools.core.reteoo.PathMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ConditionalBranchMemory(org.drools.core.reteoo.ConditionalBranchNode.ConditionalBranchMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) TupleMemory(org.drools.core.reteoo.TupleMemory) TimerNodeMemory(org.drools.core.reteoo.TimerNode.TimerNodeMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) EvalMemory(org.drools.core.reteoo.EvalConditionNode.EvalMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) RiaPathMemory(org.drools.core.reteoo.RiaPathMemory) ObjectSink(org.drools.core.reteoo.ObjectSink) BetaMemory(org.drools.core.reteoo.BetaMemory) RightTuple(org.drools.core.reteoo.RightTuple) SubnetworkTuple(org.drools.core.reteoo.SubnetworkTuple)

Example 2 with BetaMemory

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

the class RuleNetworkEvaluator method evalBetaNode.

private boolean evalBetaNode(PathMemory pmem, NetworkNode node, Memory nodeMem, SegmentMemory[] smems, int smemIndex, TupleSets<LeftTuple> trgTuples, InternalAgenda agenda, LinkedList<StackEntry> stack, boolean processRian, RuleExecutor executor, TupleSets<LeftTuple> srcTuples, TupleSets<LeftTuple> stagedLeftTuples, LeftTupleSinkNode sink) {
    BetaNode betaNode = (BetaNode) node;
    BetaMemory bm;
    AccumulateMemory am = null;
    if (NodeTypeEnums.AccumulateNode == node.getType()) {
        am = (AccumulateMemory) nodeMem;
        bm = am.getBetaMemory();
    } else {
        bm = (BetaMemory) nodeMem;
    }
    if (processRian && betaNode.isRightInputIsRiaNode()) {
        // if the subnetwork is nested in this segment, it will create srcTuples containing
        // peer LeftTuples, suitable for the node in the main path.
        doRiaNode(agenda, pmem, srcTuples, betaNode, sink, smems, smemIndex, nodeMem, bm, stack, executor);
        // return here, doRiaNode queues the evaluation on the stack, which is necessary to handled nested query nodes
        return true;
    }
    switchOnDoBetaNode(node, trgTuples, agenda.getWorkingMemory(), srcTuples, stagedLeftTuples, sink, bm, am);
    return false;
}
Also used : AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) BetaNode(org.drools.core.reteoo.BetaNode) BetaMemory(org.drools.core.reteoo.BetaMemory)

Example 3 with BetaMemory

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

the class AddRemoveRule method insertPeerRightTuple.

private static void insertPeerRightTuple(LeftTuple lt, InternalWorkingMemory wm, Rule rule, boolean insert) {
    // There's a shared RightInputAdaterNode, so check if one of its sinks is associated only to the new rule
    LeftTuple prevLt = null;
    RightInputAdapterNode rian = lt.getTupleSink();
    for (ObjectSink sink : rian.getObjectSinkPropagator().getSinks()) {
        if (lt != null) {
            if (prevLt != null && !insert && sink.isAssociatedWith(rule) && sink.getAssociationsSize() == 1) {
                prevLt.setPeer(null);
            }
            prevLt = lt;
            lt = lt.getPeer();
        } else if (insert) {
            BetaMemory bm = (BetaMemory) wm.getNodeMemory((BetaNode) sink);
            prevLt = rian.createPeer(prevLt);
            bm.linkNode((BetaNode) sink, wm);
            bm.getStagedRightTuples().addInsert((RightTuple) prevLt);
        }
    }
}
Also used : BetaNode(org.drools.core.reteoo.BetaNode) ObjectSink(org.drools.core.reteoo.ObjectSink) BetaMemory(org.drools.core.reteoo.BetaMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode)

Example 4 with BetaMemory

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

the class PhreakAccumulateNode method doRightInserts.

public void doRightInserts(AccumulateNode accNode, AccumulateMemory am, InternalWorkingMemory wm, TupleSets<RightTuple> srcRightTuples, TupleSets<LeftTuple> trgLeftTuples) {
    Accumulate accumulate = accNode.getAccumulate();
    BetaMemory bm = am.getBetaMemory();
    TupleMemory ltm = bm.getLeftTupleMemory();
    TupleMemory rtm = bm.getRightTupleMemory();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = accNode.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) {
            constraints.updateFromFactHandle(contextEntry, wm, rightTuple.getFactHandleForEvaluation());
            FastIterator leftIt = accNode.getLeftIterator(ltm);
            for (LeftTuple leftTuple = accNode.getFirstLeftTuple(rightTuple, ltm, leftIt); leftTuple != null; leftTuple = (LeftTuple) leftIt.next(leftTuple)) {
                if (constraints.isAllowedCachedRight(contextEntry, leftTuple)) {
                    final AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
                    addMatch(accNode, accumulate, leftTuple, rightTuple, null, null, wm, am, accctx, true);
                    // so any existing leftTuples we know are updates, but only add if not already added
                    if (leftTuple.getStagedType() == LeftTuple.NONE) {
                        trgLeftTuples.addUpdate(leftTuple);
                    }
                }
            }
        }
        rightTuple.clearStaged();
        rightTuple = next;
    }
    constraints.resetFactHandle(contextEntry);
}
Also used : AbstractHashTable(org.drools.core.util.AbstractHashTable) BetaConstraints(org.drools.core.common.BetaConstraints) BetaMemory(org.drools.core.reteoo.BetaMemory) 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) AccumulateContext(org.drools.core.reteoo.AccumulateNode.AccumulateContext) Accumulate(org.drools.core.rule.Accumulate)

Example 5 with BetaMemory

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

the class PhreakAccumulateNode method doLeftDeletes.

public void doLeftDeletes(AccumulateNode accNode, AccumulateMemory am, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    BetaMemory bm = am.getBetaMemory();
    TupleMemory ltm = bm.getLeftTupleMemory();
    Accumulate accumulate = accNode.getAccumulate();
    for (LeftTuple leftTuple = srcLeftTuples.getDeleteFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        if (leftTuple.getMemory() != null) {
            // it may have been staged and never actually added
            ltm.remove(leftTuple);
            final AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
            leftTuple.setContextObject(null);
            removePreviousMatchesForLeftTuple(accumulate, leftTuple, wm, am, accctx, false);
            if (accctx.propagated) {
                normalizeStagedTuples(stagedLeftTuples, accctx.resultLeftTuple);
                trgLeftTuples.addDelete(accctx.resultLeftTuple);
            }
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : BetaMemory(org.drools.core.reteoo.BetaMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) AccumulateContext(org.drools.core.reteoo.AccumulateNode.AccumulateContext) Accumulate(org.drools.core.rule.Accumulate)

Aggregations

BetaMemory (org.drools.core.reteoo.BetaMemory)49 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)25 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)24 Test (org.junit.Test)24 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)23 RightTuple (org.drools.core.reteoo.RightTuple)20 JoinNode (org.drools.core.reteoo.JoinNode)19 LeftTuple (org.drools.core.reteoo.LeftTuple)19 ArrayList (java.util.ArrayList)16 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)14 SegmentMemory (org.drools.core.reteoo.SegmentMemory)13 TupleMemory (org.drools.core.reteoo.TupleMemory)13 BetaNode (org.drools.core.reteoo.BetaNode)12 List (java.util.List)11 FastIterator (org.drools.core.util.FastIterator)11 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)10 KieBase (org.kie.api.KieBase)10 InternalFactHandle (org.drools.core.common.InternalFactHandle)9 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)9 BetaConstraints (org.drools.core.common.BetaConstraints)8