Search in sources :

Example 26 with PathMemory

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

the class SegmentCreationTest method testBranchCEMultipleSegments.

@Test
public void testBranchCEMultipleSegments() throws Exception {
    KieBase kbase = buildKnowledgeBase(// r1
    "   $a : A() \n", "   $a : A() \n" + "   if ( $a != null ) do[t1] \n" + // r2
    "   B() \n", "   $a : A() \n" + "   if ( $a != null ) do[t1] \n" + "   B() \n" + // r3
    "   C() \n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    ObjectTypeNode aotn = getObjectTypeNode(kbase, LinkingTest.A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    ConditionalBranchNode cen1Node = (ConditionalBranchNode) liaNode.getSinkPropagator().getSinks()[1];
    JoinNode bNode = (JoinNode) cen1Node.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn2 = (RuleTerminalNode) bNode.getSinkPropagator().getSinks()[0];
    JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[1];
    RuleTerminalNode rtn3 = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];
    FactHandle bFh = wm.insert(new LinkingTest.B());
    FactHandle cFh = wm.insert(new LinkingTest.C());
    wm.flushPropagations();
    BetaMemory bNodeBm = (BetaMemory) wm.getNodeMemory(bNode);
    SegmentMemory bNodeSmem = bNodeBm.getSegmentMemory();
    // no beta nodes before branch CE, so never unlinks
    assertEquals(0, bNodeSmem.getAllLinkedMaskTest());
    assertEquals(2, bNodeSmem.getLinkedNodeMask());
    PathMemory pmemr2 = (PathMemory) wm.getNodeMemory(rtn2);
    assertEquals(1, pmemr2.getAllLinkedMaskTest());
    assertEquals(2, pmemr2.getLinkedSegmentMask());
    assertEquals(3, pmemr2.getSegmentMemories().length);
    assertFalse(pmemr2.isRuleLinked());
    PathMemory pmemr3 = (PathMemory) wm.getNodeMemory(rtn3);
    // notice only the first segment links
    assertEquals(1, pmemr3.getAllLinkedMaskTest());
    assertEquals(3, pmemr3.getSegmentMemories().length);
    assertFalse(pmemr3.isRuleLinked());
    BetaMemory cNodeBm = (BetaMemory) wm.getNodeMemory(cNode);
    SegmentMemory cNodeSmem = cNodeBm.getSegmentMemory();
    assertEquals(1, cNodeSmem.getAllLinkedMaskTest());
    assertEquals(1, cNodeSmem.getLinkedNodeMask());
    wm.insert(new LinkingTest.A());
    wm.flushPropagations();
    assertTrue(pmemr2.isRuleLinked());
    assertTrue(pmemr3.isRuleLinked());
    // retract B does not unlink the rule
    wm.delete(bFh);
    // retract C does not unlink the rule
    wm.delete(cFh);
    wm.flushPropagations();
    // b segment never unlinks, as it has no impact on path unlinking anyway
    assertEquals(3, pmemr2.getLinkedSegmentMask());
    assertTrue(pmemr2.isRuleLinked());
    // b segment never unlinks, as it has no impact on path unlinking anyway
    assertEquals(3, pmemr3.getLinkedSegmentMask());
    assertTrue(pmemr3.isRuleLinked());
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) FactHandle(org.kie.api.runtime.rule.FactHandle) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ConditionalBranchNode(org.drools.core.reteoo.ConditionalBranchNode) BetaMemory(org.drools.core.reteoo.BetaMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Example 27 with PathMemory

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

the class AddRemoveRule method getPathEndMemories.

private static PathEndNodeMemories getPathEndMemories(InternalWorkingMemory wm, PathEndNodes pathEndNodes) {
    PathEndNodeMemories tnMems = new PathEndNodeMemories();
    for (LeftTupleNode node : pathEndNodes.otherEndNodes) {
        if (node.getType() == NodeTypeEnums.RightInputAdaterNode) {
            RiaNodeMemory riaMem = (RiaNodeMemory) wm.getNodeMemories().peekNodeMemory(node);
            if (riaMem != null) {
                tnMems.otherPmems.add(riaMem.getRiaPathMemory());
            }
        } else {
            PathMemory pmem = (PathMemory) wm.getNodeMemories().peekNodeMemory(node);
            if (pmem != null) {
                tnMems.otherPmems.add(pmem);
            }
        }
    }
    tnMems.subjectPmem = (PathMemory) wm.getNodeMemories().peekNodeMemory(pathEndNodes.subjectEndNode);
    if (tnMems.subjectPmem == null && !tnMems.otherPmems.isEmpty()) {
        // If "other pmem's are initialized, then the subject needs to be initialized too.
        tnMems.subjectPmem = (PathMemory) wm.getNodeMemory((MemoryFactory<Memory>) pathEndNodes.subjectEndNode);
    }
    for (LeftTupleNode node : pathEndNodes.subjectEndNodes) {
        if (node.getType() == NodeTypeEnums.RightInputAdaterNode) {
            RiaNodeMemory riaMem = (RiaNodeMemory) wm.getNodeMemories().peekNodeMemory(node);
            if (riaMem == null && !tnMems.otherPmems.isEmpty()) {
                riaMem = (RiaNodeMemory) wm.getNodeMemory((MemoryFactory<Memory>) node);
            }
            if (riaMem != null) {
                tnMems.subjectPmems.add(riaMem.getRiaPathMemory());
            }
        } else {
            PathMemory pmem = (PathMemory) wm.getNodeMemories().peekNodeMemory(node);
            if (pmem != null) {
                tnMems.subjectPmems.add(pmem);
            }
        }
    }
    return tnMems;
}
Also used : RiaNodeMemory(org.drools.core.reteoo.RightInputAdapterNode.RiaNodeMemory) 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) LeftTupleNode(org.drools.core.reteoo.LeftTupleNode) PathMemory(org.drools.core.reteoo.PathMemory)

Example 28 with PathMemory

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

the class AddRemoveRule method addNewPaths.

private static void addNewPaths(InternalWorkingMemory wm, Set<SegmentMemory> smemsToNotify, List<PathMemory> pmems) {
    // Multiple paths may be renetrant, in the case of a second subnetwork on the same rule.
    // Must make sure we don't duplicate the child smem, and when found, just update it with new pmem.
    Set<LeftTupleNode> visited = new HashSet<LeftTupleNode>();
    for (PathMemory pmem : pmems) {
        LeftTupleSink tipNode = (LeftTupleSink) pmem.getPathEndNode();
        LeftTupleNode child = tipNode;
        LeftTupleNode parent = tipNode.getLeftTupleSource();
        while (true) {
            if (visited.add(child)) {
                if (parent != null && parent.getAssociationsSize() != 1 && child.getAssociationsSize() == 1) {
                    // This is the split point that the new path enters an existing path.
                    // If the parent has other child SegmentMemorys then it must create a new child SegmentMemory
                    // If the parent is a query node, then it's internal data structure needs changing
                    // all right input data must be propagated
                    Memory mem = wm.getNodeMemories().peekNodeMemory(parent);
                    if (mem != null && mem.getSegmentMemory() != null) {
                        SegmentMemory sm = mem.getSegmentMemory();
                        if (sm.getFirst() != null && sm.size() < parent.getSinkPropagator().size()) {
                            LeftTupleSink[] sinks = parent.getSinkPropagator().getSinks();
                            for (int i = sm.size(); i < sinks.length; i++) {
                                SegmentMemory childSmem = SegmentUtilities.createChildSegment(wm, sinks[i]);
                                sm.add(childSmem);
                                pmem.setSegmentMemory(childSmem.getPos(), childSmem);
                                smemsToNotify.add(childSmem);
                            }
                        }
                        correctMemoryOnSplitsChanged(parent, null, wm);
                    }
                } else {
                    Memory mem = wm.getNodeMemories().peekNodeMemory(child);
                    // The root of each segment
                    if (mem != null) {
                        SegmentMemory sm = mem.getSegmentMemory();
                        if (sm != null && !sm.getPathMemories().contains(pmem)) {
                            sm.addPathMemory(pmem);
                            pmem.setSegmentMemory(sm.getPos(), sm);
                            sm.notifyRuleLinkSegment(wm, pmem);
                        }
                    }
                }
            } else {
                Memory mem = wm.getNodeMemories().peekNodeMemory(child);
                if (mem != null) {
                    mem.getSegmentMemory().notifyRuleLinkSegment(wm, pmem);
                }
            }
            if (parent == null) {
                break;
            }
            child = parent;
            parent = parent.getLeftTupleSource();
        }
    }
}
Also used : 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) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) LeftTupleNode(org.drools.core.reteoo.LeftTupleNode) HashSet(java.util.HashSet) PathMemory(org.drools.core.reteoo.PathMemory)

Example 29 with PathMemory

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

the class AddRemoveRule method handleExistingPaths.

private static Set<SegmentMemory> handleExistingPaths(TerminalNode tn, Map<PathMemory, SegmentMemory[]> prevSmemsLookup, List<PathMemory> pmems, InternalWorkingMemory wm, ExistingPathStrategy strategy) {
    Set<SegmentMemory> smemsToNotify = new HashSet<SegmentMemory>();
    Set<SegmentMemory> visitedSegments = new HashSet<SegmentMemory>();
    Set<LeftTupleNode> visitedNodes = new HashSet<LeftTupleNode>();
    Map<LeftTupleNode, SegmentMemory> nodeToSegmentMap = new HashMap<LeftTupleNode, SegmentMemory>();
    for (PathMemory pmem : pmems) {
        LeftTupleNode[] nodes = pmem.getPathEndNode().getPathNodes();
        SegmentMemory[] prevSmems = prevSmemsLookup.get(pmem);
        SegmentMemory[] smems = strategy.getSegmenMemories(pmem);
        LeftTupleNode node;
        int prevSmemIndex = 0;
        int smemIndex = 0;
        int smemSplitAdjustAmount = 0;
        int nodeIndex = 0;
        int nodeTypesInSegment = 0;
        // excluding the rule just added iterate while not split (i.e. find the next split, prior to this rule being added)
        // note it's checking for when the parent is the split, and thus node is the next root root.
        smems[smemIndex] = prevSmems[prevSmemIndex];
        do {
            node = nodes[nodeIndex++];
            LeftTupleNode parentNode = node.getLeftTupleSource();
            nodeTypesInSegment = SegmentUtilities.updateNodeTypesMask(parentNode, nodeTypesInSegment);
            if (isSplit(parentNode)) {
                smemIndex = strategy.incSmemIndex1(smemIndex);
                prevSmemIndex = strategy.incPrevSmemIndex1(prevSmemIndex);
                if (isSplit(parentNode, tn)) {
                    // check if the split is there even without the processed rule
                    smemIndex = strategy.incSmemIndex2(smemIndex);
                    prevSmemIndex = strategy.incPrevSmemIndex2(prevSmemIndex);
                    smems[smemIndex] = prevSmems[prevSmemIndex];
                    if (smems[smemIndex] != null && smemSplitAdjustAmount > 0 && visitedSegments.add(smems[smemIndex])) {
                        strategy.adjustSegment(wm, smemsToNotify, smems[smemIndex], smemSplitAdjustAmount);
                    }
                } else {
                    strategy.handleSplit(pmem, prevSmems, smems, smemIndex, prevSmemIndex, parentNode, node, tn, visitedNodes, smemsToNotify, nodeToSegmentMap, wm);
                    smemSplitAdjustAmount++;
                }
                SegmentUtilities.checkEagerSegmentCreation((LeftTupleSource) parentNode, wm, nodeTypesInSegment);
                nodeTypesInSegment = 0;
            }
        } while (!NodeTypeEnums.isEndNode(node));
        strategy.processSegmentMemories(smems, pmem);
    }
    return smemsToNotify;
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) HashMap(java.util.HashMap) LeftTupleNode(org.drools.core.reteoo.LeftTupleNode) HashSet(java.util.HashSet) PathMemory(org.drools.core.reteoo.PathMemory)

Example 30 with PathMemory

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

the class AddRemoveRule method forceFlushLeftTuple.

public static void forceFlushLeftTuple(PathMemory pmem, SegmentMemory sm, InternalWorkingMemory wm, TupleSets<LeftTuple> leftTupleSets) {
    SegmentMemory[] smems = pmem.getSegmentMemories();
    LeftTupleNode node;
    Memory mem;
    long bit = 1;
    if (sm.getRootNode().getType() == NodeTypeEnums.LeftInputAdapterNode && sm.getTipNode().getType() != NodeTypeEnums.LeftInputAdapterNode) {
        // The segment is the first and it has the lian shared with other nodes, the lian must be skipped, so adjust the bit and sink
        node = sm.getRootNode().getSinkPropagator().getFirstLeftTupleSink();
        mem = sm.getNodeMemories().get(1);
        // adjust bit to point to next node
        bit = 2;
    } else {
        node = sm.getRootNode();
        mem = sm.getNodeMemories().get(0);
    }
    PathMemory rtnPmem = NodeTypeEnums.isTerminalNode(pmem.getPathEndNode()) ? pmem : wm.getNodeMemory((AbstractTerminalNode) pmem.getPathEndNode().getPathEndNodes()[0]);
    InternalAgenda agenda = pmem.getActualAgenda(wm);
    new RuleNetworkEvaluator().outerEval(pmem, node, bit, mem, smems, sm.getPos(), leftTupleSets, agenda, new LinkedList<StackEntry>(), true, rtnPmem.getOrCreateRuleAgendaItem(agenda).getRuleExecutor());
}
Also used : AbstractTerminalNode(org.drools.core.reteoo.AbstractTerminalNode) InternalAgenda(org.drools.core.common.InternalAgenda) 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) LeftTupleNode(org.drools.core.reteoo.LeftTupleNode) PathMemory(org.drools.core.reteoo.PathMemory)

Aggregations

PathMemory (org.drools.core.reteoo.PathMemory)34 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)26 SegmentMemory (org.drools.core.reteoo.SegmentMemory)26 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)16 Test (org.junit.Test)16 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)14 BetaMemory (org.drools.core.reteoo.BetaMemory)13 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)13 JoinNode (org.drools.core.reteoo.JoinNode)12 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)11 LeftTupleNode (org.drools.core.reteoo.LeftTupleNode)9 Memory (org.drools.core.common.Memory)8 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)8 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)8 ArrayList (java.util.ArrayList)7 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)7 RightInputAdapterNode (org.drools.core.reteoo.RightInputAdapterNode)7 RiaNodeMemory (org.drools.core.reteoo.RightInputAdapterNode.RiaNodeMemory)7 TupleMemory (org.drools.core.reteoo.TupleMemory)7 List (java.util.List)6