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());
}
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;
}
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();
}
}
}
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;
}
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());
}
Aggregations