use of org.drools.core.reteoo.PathMemory in project drools by kiegroup.
the class AddRemoveRule method removeNewPaths.
private static void removeNewPaths(InternalWorkingMemory wm, List<PathMemory> pmems) {
Set<Integer> visitedNodes = new HashSet<Integer>();
for (PathMemory pmem : pmems) {
LeftTupleSink tipNode = (LeftTupleSink) pmem.getPathEndNode();
LeftTupleNode child = tipNode;
LeftTupleNode parent = tipNode.getLeftTupleSource();
while (true) {
if (child.getAssociationsSize() == 1 && NodeTypeEnums.isBetaNode(child)) {
// If this is a beta node, it'll delete all the right input data
deleteRightInputData((LeftTupleSink) child, wm);
}
if (parent != null && parent.getAssociationsSize() != 1 && child.getAssociationsSize() == 1) {
// all right input data must be propagated
if (!visitedNodes.contains(child.getId())) {
Memory mem = wm.getNodeMemories().peekNodeMemory(parent);
if (mem != null && mem.getSegmentMemory() != null) {
SegmentMemory sm = mem.getSegmentMemory();
if (sm.getFirst() != null) {
SegmentMemory childSm = wm.getNodeMemories().peekNodeMemory(child).getSegmentMemory();
sm.remove(childSm);
}
}
}
} 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)) {
mem.getSegmentMemory().removePathMemory(pmem);
}
}
}
if (parent == null) {
break;
}
visitedNodes.add(child.getId());
child = parent;
parent = parent.getLeftTupleSource();
}
}
}
use of org.drools.core.reteoo.PathMemory in project drools by kiegroup.
the class AddRemoveRule method addRule.
/**
* This method is called after the rule nodes have been added to the network
* For add tuples are processed after the segments and pmems have been adjusted
*/
public static void addRule(TerminalNode tn, InternalWorkingMemory[] wms, InternalKnowledgeBase kBase) {
if (log.isTraceEnabled()) {
log.trace("Adding Rule {}", tn.getRule().getName());
}
boolean hasProtos = kBase.hasSegmentPrototypes();
boolean hasWms = wms.length > 0;
if (!hasProtos && !hasWms) {
return;
}
RuleImpl rule = tn.getRule();
LeftTupleNode firstSplit = getNetworkSplitPoint(tn);
PathEndNodes pathEndNodes = getPathEndNodes(kBase, firstSplit, tn, rule, hasProtos, hasWms);
for (InternalWorkingMemory wm : wms) {
wm.flushPropagations();
if (NodeTypeEnums.LeftInputAdapterNode == firstSplit.getType() && firstSplit.getAssociationsSize() == 1) {
// rule added with no sharing
insertLiaFacts(firstSplit, wm);
} else {
PathEndNodeMemories tnms = getPathEndMemories(wm, pathEndNodes);
if (tnms.subjectPmem == null) {
// If the existing PathMemories are not yet initialized there are no Segments or tuples to process
continue;
}
Map<PathMemory, SegmentMemory[]> prevSmemsLookup = reInitPathMemories(tnms.otherPmems, null);
// must collect all visited SegmentMemories, for link notification
Set<SegmentMemory> smemsToNotify = handleExistingPaths(tn, prevSmemsLookup, tnms.otherPmems, wm, ExistingPathStrategy.ADD_STRATEGY);
addNewPaths(wm, smemsToNotify, tnms.subjectPmems);
processLeftTuples(firstSplit, wm, true, rule);
notifySegments(smemsToNotify, wm);
}
}
if (hasWms) {
insertFacts(pathEndNodes, wms);
} else {
for (PathEndNode node : pathEndNodes.otherEndNodes) {
node.resetPathMemSpec(null);
}
}
}
use of org.drools.core.reteoo.PathMemory in project drools by kiegroup.
the class AddRemoveRule method flushLeftTupleIfNecessary.
public static boolean flushLeftTupleIfNecessary(InternalWorkingMemory wm, SegmentMemory sm, LeftTuple leftTuple, boolean streamMode, short stagedType) {
PathMemory pmem = streamMode ? sm.getPathMemories().get(0) : sm.getFirstDataDrivenPathMemory();
if (pmem == null) {
return false;
}
TupleSets<LeftTuple> leftTupleSets = new TupleSetsImpl<LeftTuple>();
if (leftTuple != null) {
switch(stagedType) {
case Tuple.INSERT:
leftTupleSets.addInsert(leftTuple);
break;
case Tuple.DELETE:
leftTupleSets.addDelete(leftTuple);
break;
case Tuple.UPDATE:
leftTupleSets.addUpdate(leftTuple);
break;
}
}
forceFlushLeftTuple(pmem, sm, wm, leftTupleSets);
if (pmem.isDataDriven() && pmem.getNodeType() == NodeTypeEnums.RightInputAdaterNode) {
for (PathEndNode pnode : pmem.getPathEndNode().getPathEndNodes()) {
if (pnode instanceof TerminalNode) {
PathMemory outPmem = wm.getNodeMemory((TerminalNode) pnode);
if (outPmem.isDataDriven()) {
SegmentMemory outSmem = outPmem.getSegmentMemories()[0];
if (outSmem != null) {
forceFlushLeftTuple(outPmem, outSmem, wm, new TupleSetsImpl<LeftTuple>());
}
}
}
}
}
return true;
}
use of org.drools.core.reteoo.PathMemory in project drools by kiegroup.
the class SegmentCreationTest method testBranchCESingleSegment.
@Test
public void testBranchCESingleSegment() throws Exception {
KieBase kbase = buildKnowledgeBase(" $a : A() \n" + " if ( $a != null ) do[t1] \n" + " B() \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()[0];
JoinNode bNode = (JoinNode) cen1Node.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn1 = (RuleTerminalNode) bNode.getSinkPropagator().getSinks()[0];
FactHandle bFh = wm.insert(new LinkingTest.B());
wm.flushPropagations();
LiaNodeMemory liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
SegmentMemory smem = liaMem.getSegmentMemory();
assertEquals(1, smem.getAllLinkedMaskTest());
// B links, but it will not trigger mask
assertEquals(4, smem.getLinkedNodeMask());
assertFalse(smem.isSegmentLinked());
PathMemory pmem = (PathMemory) wm.getNodeMemory(rtn1);
assertEquals(1, pmem.getAllLinkedMaskTest());
assertEquals(0, pmem.getLinkedSegmentMask());
assertFalse(pmem.isRuleLinked());
wm.insert(new LinkingTest.A());
wm.flushPropagations();
// A links in segment
assertEquals(5, smem.getLinkedNodeMask());
assertTrue(smem.isSegmentLinked());
assertEquals(1, pmem.getLinkedSegmentMask());
assertTrue(pmem.isRuleLinked());
// retract B does not unlink the rule
wm.delete(bFh);
wm.flushPropagations();
assertEquals(1, pmem.getLinkedSegmentMask());
assertTrue(pmem.isRuleLinked());
}
use of org.drools.core.reteoo.PathMemory in project drools by kiegroup.
the class AddRuleTest method testSplitTwoBeforeCreatedSegment.
@Test
public void testSplitTwoBeforeCreatedSegment() throws Exception {
InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", " A(1;) A(2;) B(1;) B(2;) C(1;) C(2;) D(1;) D(2;) E(1;) E(2;)\n");
kbase1.addPackages(buildKnowledgePackage("r2", " A(1;) A(2;) B(1;) B(2;) C(1;) C(2;) D(1;) D(2;) E(1;) E(2;)\n"));
kbase1.addPackages(buildKnowledgePackage("r3", " A(1;) A(2;) B(1;) B(2;) C(1;) C(2;) D(1;) D(2;)\n"));
kbase1.addPackages(buildKnowledgePackage("r4", " A(1;) A(2;) B(1;) B(2;) C(1;) C(2;) \n"));
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
List list = new ArrayList();
wm.setGlobal("list", list);
wm.insert(new E(1));
wm.insert(new E(2));
wm.flushPropagations();
RuleTerminalNode rtn1 = getRtn("org.kie.r1", kbase1);
RuleTerminalNode rtn2 = getRtn("org.kie.r2", kbase1);
RuleTerminalNode rtn3 = getRtn("org.kie.r3", kbase1);
RuleTerminalNode rtn4 = getRtn("org.kie.r4", kbase1);
PathMemory pm1 = (PathMemory) wm.getNodeMemory(rtn1);
SegmentMemory[] smems = pm1.getSegmentMemories();
assertEquals(4, smems.length);
assertNull(smems[0]);
assertNull(smems[1]);
assertNull(smems[3]);
SegmentMemory sm = smems[2];
assertEquals(2, sm.getPos());
assertEquals(4, sm.getSegmentPosMaskBit());
assertEquals(4, pm1.getLinkedSegmentMask());
kbase1.addPackages(buildKnowledgePackage("r5", " A(1;) A(2;) B(1;) B(2;) \n"));
smems = pm1.getSegmentMemories();
assertEquals(5, smems.length);
assertNull(smems[0]);
assertNull(smems[1]);
assertNull(smems[2]);
sm = smems[3];
assertEquals(3, sm.getPos());
assertEquals(8, sm.getSegmentPosMaskBit());
assertEquals(8, pm1.getLinkedSegmentMask());
RuleTerminalNode rtn5 = getRtn("org.kie.r5", kbase1);
PathMemory pm5 = (PathMemory) wm.getNodeMemory(rtn5);
smems = pm5.getSegmentMemories();
assertEquals(2, smems.length);
assertNull(smems[0]);
assertNull(smems[1]);
}
Aggregations