Search in sources :

Example 6 with LeftInputAdapterNode

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

the class SegmentUtilities method getQuerySegmentMemory.

public static SegmentMemory getQuerySegmentMemory(InternalWorkingMemory wm, LeftTupleSource segmentRoot, QueryElementNode queryNode) {
    LeftInputAdapterNode liaNode = getQueryLiaNode(queryNode.getQueryElement().getQueryName(), getQueryOtn(segmentRoot));
    LiaNodeMemory liam = wm.getNodeMemory(liaNode);
    SegmentMemory querySmem = liam.getSegmentMemory();
    if (querySmem == null) {
        querySmem = createSegmentMemory(liaNode, liam, wm);
    }
    return querySmem;
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 7 with LeftInputAdapterNode

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

the class SegmentUtilities method getQueryLiaNode.

private static LeftInputAdapterNode getQueryLiaNode(String queryName, ObjectTypeNode queryOtn) {
    for (ObjectSink sink : queryOtn.getObjectSinkPropagator().getSinks()) {
        AlphaNode alphaNode = (AlphaNode) sink;
        QueryNameConstraint nameConstraint = (QueryNameConstraint) alphaNode.getConstraint();
        if (queryName.equals(nameConstraint.getQueryName())) {
            return (LeftInputAdapterNode) alphaNode.getObjectSinkPropagator().getSinks()[0];
        }
    }
    throw new RuntimeException("Unable to find query '" + queryName + "'");
}
Also used : QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) ObjectSink(org.drools.core.reteoo.ObjectSink) AlphaNode(org.drools.core.reteoo.AlphaNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 8 with LeftInputAdapterNode

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

the class SegmentUtilities method createSegmentMemory.

public static SegmentMemory createSegmentMemory(LeftTupleSource tupleSource, Memory mem, InternalWorkingMemory wm) {
    // find segment root
    while (!SegmentUtilities.isRootNode(tupleSource, null)) {
        tupleSource = tupleSource.getLeftTupleSource();
    }
    LeftTupleSource segmentRoot = tupleSource;
    int nodeTypesInSegment = 0;
    SegmentMemory smem = restoreSegmentFromPrototype(wm, segmentRoot, nodeTypesInSegment);
    if (smem != null) {
        if (NodeTypeEnums.isBetaNode(segmentRoot) && ((BetaNode) segmentRoot).isRightInputIsRiaNode()) {
            createRiaSegmentMemory((BetaNode) segmentRoot, wm);
        }
        return smem;
    }
    smem = new SegmentMemory(segmentRoot);
    // Iterate all nodes on the same segment, assigning their position as a bit mask value
    // allLinkedTestMask is the resulting mask used to test if all nodes are linked in
    long nodePosMask = 1;
    long allLinkedTestMask = 0;
    // nodes after a branch CE can notify, but they cannot impact linking
    boolean updateNodeBit = true;
    while (true) {
        nodeTypesInSegment = updateNodeTypesMask(tupleSource, nodeTypesInSegment);
        if (NodeTypeEnums.isBetaNode(tupleSource)) {
            allLinkedTestMask = processBetaNode((BetaNode) tupleSource, wm, smem, nodePosMask, allLinkedTestMask, updateNodeBit);
        } else {
            switch(tupleSource.getType()) {
                case NodeTypeEnums.LeftInputAdapterNode:
                    allLinkedTestMask = processLiaNode((LeftInputAdapterNode) tupleSource, wm, smem, nodePosMask, allLinkedTestMask);
                    break;
                case NodeTypeEnums.EvalConditionNode:
                    processEvalNode((EvalConditionNode) tupleSource, wm, smem);
                    break;
                case NodeTypeEnums.ConditionalBranchNode:
                    updateNodeBit = processBranchNode((ConditionalBranchNode) tupleSource, wm, smem);
                    break;
                case NodeTypeEnums.FromNode:
                    processFromNode((FromNode) tupleSource, wm, smem);
                    break;
                case NodeTypeEnums.ReactiveFromNode:
                    processReactiveFromNode((MemoryFactory) tupleSource, wm, smem, nodePosMask);
                    break;
                case NodeTypeEnums.TimerConditionNode:
                    processTimerNode((TimerNode) tupleSource, wm, smem, nodePosMask);
                    break;
                case NodeTypeEnums.QueryElementNode:
                    updateNodeBit = processQueryNode((QueryElementNode) tupleSource, wm, segmentRoot, smem, nodePosMask);
                    break;
            }
        }
        nodePosMask = nodePosMask << 1;
        if (tupleSource.getSinkPropagator().size() == 1) {
            LeftTupleSinkNode sink = tupleSource.getSinkPropagator().getFirstLeftTupleSink();
            if (NodeTypeEnums.isLeftTupleSource(sink)) {
                tupleSource = (LeftTupleSource) sink;
            } else {
                // rtn or rian
                // While not technically in a segment, we want to be able to iterate easily from the last node memory to the ria/rtn memory
                // we don't use createNodeMemory, as these may already have been created by, but not added, by the method updateRiaAndTerminalMemory
                Memory memory = wm.getNodeMemory((MemoryFactory) sink);
                if (sink.getType() == NodeTypeEnums.RightInputAdaterNode) {
                    PathMemory riaPmem = ((RiaNodeMemory) memory).getRiaPathMemory();
                    smem.getNodeMemories().add(riaPmem);
                    RightInputAdapterNode rian = (RightInputAdapterNode) sink;
                    ObjectSink[] nodes = rian.getObjectSinkPropagator().getSinks();
                    for (ObjectSink node : nodes) {
                        if (NodeTypeEnums.isLeftTupleSource(node)) {
                            createSegmentMemory((LeftTupleSource) node, wm);
                        }
                    }
                } else if (NodeTypeEnums.isTerminalNode(sink)) {
                    smem.getNodeMemories().add(memory);
                }
                memory.setSegmentMemory(smem);
                smem.setTipNode(sink);
                break;
            }
        } else {
            // not in same segment
            smem.setTipNode(tupleSource);
            break;
        }
    }
    smem.setAllLinkedMaskTest(allLinkedTestMask);
    // iterate to find root and determine the SegmentNodes position in the RuleSegment
    LeftTupleSource pathRoot = segmentRoot;
    int ruleSegmentPosMask = 1;
    int counter = 0;
    while (pathRoot.getType() != NodeTypeEnums.LeftInputAdapterNode) {
        LeftTupleSource leftTupleSource = pathRoot.getLeftTupleSource();
        if (SegmentUtilities.isNonTerminalTipNode(leftTupleSource, null)) {
            // for each new found segment, increase the mask bit position
            ruleSegmentPosMask = ruleSegmentPosMask << 1;
            counter++;
        }
        pathRoot = leftTupleSource;
    }
    smem.setSegmentPosMaskBit(ruleSegmentPosMask);
    smem.setPos(counter);
    nodeTypesInSegment = updateRiaAndTerminalMemory(tupleSource, tupleSource, smem, wm, false, nodeTypesInSegment);
    ((KnowledgeBaseImpl) wm.getKnowledgeBase()).registerSegmentPrototype(segmentRoot, smem);
    return smem;
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) BetaNode(org.drools.core.reteoo.BetaNode) Memory(org.drools.core.common.Memory) TimerNodeMemory(org.drools.core.reteoo.TimerNode.TimerNodeMemory) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) EvalMemory(org.drools.core.reteoo.EvalConditionNode.EvalMemory) QueryElementNodeMemory(org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) PathMemory(org.drools.core.reteoo.PathMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ConditionalBranchMemory(org.drools.core.reteoo.ConditionalBranchNode.ConditionalBranchMemory) RiaNodeMemory(org.drools.core.reteoo.RightInputAdapterNode.RiaNodeMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) QueryElementNode(org.drools.core.reteoo.QueryElementNode) ConditionalBranchNode(org.drools.core.reteoo.ConditionalBranchNode) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) RiaNodeMemory(org.drools.core.reteoo.RightInputAdapterNode.RiaNodeMemory) LeftTupleSinkNode(org.drools.core.reteoo.LeftTupleSinkNode) ObjectSink(org.drools.core.reteoo.ObjectSink) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) PathMemory(org.drools.core.reteoo.PathMemory)

Example 9 with LeftInputAdapterNode

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

the class AddRemoveRule method insertPeerLeftTuple.

/**
 * Create all missing peers
 */
private static LeftTuple insertPeerLeftTuple(LeftTuple lt, LeftTupleSinkNode node, InternalWorkingMemory wm) {
    LeftInputAdapterNode.LiaNodeMemory liaMem = null;
    if (node.getLeftTupleSource().getType() == NodeTypeEnums.LeftInputAdapterNode) {
        liaMem = wm.getNodeMemory(((LeftInputAdapterNode) node.getLeftTupleSource()));
    }
    LeftTuple peer = node.createPeer(lt);
    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.doInsertSegmentMemory(wm, true, liaMem, memory.getSegmentMemory(), peer, node.getLeftTupleSource().isStreamMode());
    }
    return peer;
}
Also used : 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) LeftTuple(org.drools.core.reteoo.LeftTuple) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 10 with LeftInputAdapterNode

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

the class AddRemoveRule method insertLiaFacts.

private static void insertLiaFacts(LeftTupleNode startNode, InternalWorkingMemory wm) {
    // rule added with no sharing
    PropagationContextFactory pctxFactory = wm.getKnowledgeBase().getConfiguration().getComponentFactory().getPropagationContextFactory();
    final PropagationContext pctx = pctxFactory.createPropagationContext(wm.getNextPropagationIdCounter(), PropagationContext.Type.RULE_ADDITION, null, null, null);
    LeftInputAdapterNode lian = (LeftInputAdapterNode) startNode;
    RightTupleSinkAdapter liaAdapter = new RightTupleSinkAdapter(lian);
    lian.getObjectSource().updateSink(liaAdapter, pctx, wm);
}
Also used : PropagationContextFactory(org.drools.core.common.PropagationContextFactory) RightTupleSinkAdapter(org.drools.core.reteoo.LeftInputAdapterNode.RightTupleSinkAdapter) PropagationContext(org.drools.core.spi.PropagationContext) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Aggregations

LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)72 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)60 Test (org.junit.Test)56 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)48 KieBase (org.kie.api.KieBase)35 JoinNode (org.drools.core.reteoo.JoinNode)28 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)28 SegmentMemory (org.drools.core.reteoo.SegmentMemory)28 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)27 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)25 BetaMemory (org.drools.core.reteoo.BetaMemory)25 ArrayList (java.util.ArrayList)17 PathMemory (org.drools.core.reteoo.PathMemory)15 List (java.util.List)12 FactHandle (org.kie.api.runtime.rule.FactHandle)12 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)12 ClassObjectType (org.drools.core.base.ClassObjectType)11 AlphaNode (org.drools.core.reteoo.AlphaNode)11 NotNode (org.drools.core.reteoo.NotNode)11 ExistsNode (org.drools.core.reteoo.ExistsNode)10