Search in sources :

Example 1 with AlphaTerminalNode

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

the class AddRemoveRule method processLeftTuples.

/**
 * Populates the SegmentMemory with staged LeftTuples. If the parent is not a Beta or From node, it iterates up to find the first node with memory. If necessary
 * It traverses to the LiaNode's ObjectTypeNode. It then iterates the LeftTuple chains, where an existing LeftTuple is staged
 * as delete. Or a new LeftTuple is created and staged as an insert.
 */
private static void processLeftTuples(LeftTupleNode node, InternalWorkingMemory wm, boolean insert, Rule rule) {
    if (node instanceof AlphaTerminalNode) {
        processLeftTuplesOnLian(wm, insert, rule, (LeftInputAdapterNode) node);
        return;
    }
    Memory memory = wm.getNodeMemories().peekNodeMemory(node);
    if (memory == null || memory.getSegmentMemory() == null) {
        // segment has never been initialized, which means the rule(s) have never been linked and thus no Tuples to fix
        return;
    }
    SegmentMemory sm = memory.getSegmentMemory();
    while (NodeTypeEnums.LeftInputAdapterNode != node.getType()) {
        if (NodeTypeEnums.isBetaNode(node)) {
            BetaMemory bm;
            if (NodeTypeEnums.AccumulateNode == node.getType()) {
                AccumulateMemory am = (AccumulateMemory) memory;
                bm = am.getBetaMemory();
                FastIterator it = bm.getLeftTupleMemory().fullFastIterator();
                Tuple lt = BetaNode.getFirstTuple(bm.getLeftTupleMemory(), it);
                for (; lt != null; lt = (LeftTuple) it.next(lt)) {
                    AccumulateContext accctx = (AccumulateContext) lt.getContextObject();
                    visitChild(accctx.getResultLeftTuple(), insert, wm, rule);
                }
            } else if (NodeTypeEnums.ExistsNode == node.getType() && !((BetaNode) node).isRightInputIsRiaNode()) {
                // do not process exists with subnetworks
                // If there is a subnetwork, then there is no populated RTM, but the LTM is populated,
                // so this would be procsssed in the "else".
                bm = (BetaMemory) wm.getNodeMemory((MemoryFactory) node);
                // done off the RightTupleMemory, as exists only have unblocked tuples on the left side
                FastIterator it = bm.getRightTupleMemory().fullFastIterator();
                RightTuple rt = (RightTuple) BetaNode.getFirstTuple(bm.getRightTupleMemory(), it);
                for (; rt != null; rt = (RightTuple) it.next(rt)) {
                    for (LeftTuple lt = rt.getBlocked(); lt != null; lt = lt.getBlockedNext()) {
                        visitChild(wm, insert, rule, it, lt);
                    }
                }
            } else {
                bm = (BetaMemory) wm.getNodeMemory((MemoryFactory) node);
                FastIterator it = bm.getLeftTupleMemory().fullFastIterator();
                Tuple lt = BetaNode.getFirstTuple(bm.getLeftTupleMemory(), it);
                visitChild(wm, insert, rule, it, lt);
            }
            return;
        } else if (NodeTypeEnums.FromNode == node.getType()) {
            FromMemory fm = (FromMemory) wm.getNodeMemory((MemoryFactory) node);
            TupleMemory ltm = fm.getBetaMemory().getLeftTupleMemory();
            FastIterator it = ltm.fullFastIterator();
            for (LeftTuple lt = (LeftTuple) ltm.getFirst(null); lt != null; lt = (LeftTuple) it.next(lt)) {
                visitChild(lt, insert, wm, rule);
            }
            return;
        }
        if (sm.getRootNode() == node) {
            sm = wm.getNodeMemory((MemoryFactory<Memory>) node.getLeftTupleSource()).getSegmentMemory();
        }
        node = node.getLeftTupleSource();
    }
    // No beta or from nodes, so must retrieve LeftTuples from the LiaNode.
    // This is done by scanning all the LeftTuples referenced from the FactHandles in the ObjectTypeNode
    processLeftTuplesOnLian(wm, insert, rule, (LeftInputAdapterNode) node);
}
Also used : AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) Memory(org.drools.core.common.Memory) PathMemory(org.drools.core.reteoo.PathMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentNodeMemory(org.drools.core.reteoo.SegmentNodeMemory) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) RiaNodeMemory(org.drools.core.reteoo.RightInputAdapterNode.RiaNodeMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) TupleMemory(org.drools.core.reteoo.TupleMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) MemoryFactory(org.drools.core.common.MemoryFactory) RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) AlphaTerminalNode(org.drools.core.reteoo.AlphaTerminalNode) FastIterator(org.drools.core.util.FastIterator) Tuple(org.drools.core.spi.Tuple) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) AccumulateContext(org.drools.core.reteoo.AccumulateNode.AccumulateContext)

Example 2 with AlphaTerminalNode

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

the class AddRemoveRule method insertPeerLeftTuple.

/**
 * Create all missing peers
 */
private static LeftTuple insertPeerLeftTuple(LeftTuple lt, LeftTupleSinkNode node, InternalWorkingMemory wm, boolean insert) {
    LeftTuple peer = node.createPeer(lt);
    if (node.getLeftTupleSource() instanceof AlphaTerminalNode) {
        if (insert) {
            TerminalNode rtn = (TerminalNode) node;
            InternalAgenda agenda = wm.getAgenda();
            RuleAgendaItem agendaItem = AlphaTerminalNode.getRuleAgendaItem(wm, agenda, rtn, insert);
            PhreakRuleTerminalNode.doLeftTupleInsert(rtn, agendaItem.getRuleExecutor(), agenda, agendaItem, peer);
        }
        return peer;
    }
    LeftInputAdapterNode.LiaNodeMemory liaMem = null;
    if (node.getLeftTupleSource().getType() == NodeTypeEnums.LeftInputAdapterNode) {
        liaMem = wm.getNodeMemory(((LeftInputAdapterNode) node.getLeftTupleSource()));
    }
    Memory memory = wm.getNodeMemories().peekNodeMemory(node);
    if (memory == null || memory.getSegmentMemory() == null) {
        throw new IllegalStateException("Defensive Programming: this should not be possilbe, as the addRule code should init child segments if they are needed ");
    }
    if (liaMem == null) {
        memory.getSegmentMemory().getStagedLeftTuples().addInsert(peer);
    } else {
        // If parent is Lian, then this must be called, so that any linking or unlinking can be done.
        LeftInputAdapterNode.doInsertSegmentMemoryWithFlush(wm, true, liaMem, memory.getSegmentMemory(), peer, node.getLeftTupleSource().isStreamMode());
    }
    return peer;
}
Also used : InternalAgenda(org.drools.core.common.InternalAgenda) Memory(org.drools.core.common.Memory) PathMemory(org.drools.core.reteoo.PathMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentNodeMemory(org.drools.core.reteoo.SegmentNodeMemory) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) RiaNodeMemory(org.drools.core.reteoo.RightInputAdapterNode.RiaNodeMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) TupleMemory(org.drools.core.reteoo.TupleMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) AlphaTerminalNode(org.drools.core.reteoo.AlphaTerminalNode) AlphaTerminalNode(org.drools.core.reteoo.AlphaTerminalNode) AbstractTerminalNode(org.drools.core.reteoo.AbstractTerminalNode) TerminalNode(org.drools.core.reteoo.TerminalNode) LeftTuple(org.drools.core.reteoo.LeftTuple) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Aggregations

InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)2 Memory (org.drools.core.common.Memory)2 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)2 AlphaTerminalNode (org.drools.core.reteoo.AlphaTerminalNode)2 BetaMemory (org.drools.core.reteoo.BetaMemory)2 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)2 LeftTuple (org.drools.core.reteoo.LeftTuple)2 ObjectTypeNodeMemory (org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory)2 PathMemory (org.drools.core.reteoo.PathMemory)2 RiaNodeMemory (org.drools.core.reteoo.RightInputAdapterNode.RiaNodeMemory)2 SegmentMemory (org.drools.core.reteoo.SegmentMemory)2 SegmentNodeMemory (org.drools.core.reteoo.SegmentNodeMemory)2 TupleMemory (org.drools.core.reteoo.TupleMemory)2 InternalAgenda (org.drools.core.common.InternalAgenda)1 MemoryFactory (org.drools.core.common.MemoryFactory)1 AbstractTerminalNode (org.drools.core.reteoo.AbstractTerminalNode)1 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)1 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)1 RightTuple (org.drools.core.reteoo.RightTuple)1 TerminalNode (org.drools.core.reteoo.TerminalNode)1