use of org.drools.core.reteoo.JoinNode in project drools by kiegroup.
the class MemoryLeakTest method testBetaMemoryLeakOnFactDelete.
@Test
public void testBetaMemoryLeakOnFactDelete() {
// DROOLS-913
String drl = "rule R1 when\n" + " $a : Integer(this == 1)\n" + " $b : String()\n" + " $c : Integer(this == 2)\n" + "then \n" + "end\n" + "rule R2 when\n" + " $a : Integer(this == 1)\n" + " $b : String()\n" + " $c : Integer(this == 3)\n" + "then \n" + "end\n";
KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
FactHandle fh1 = ksession.insert(1);
FactHandle fh2 = ksession.insert(3);
FactHandle fh3 = ksession.insert("test");
ksession.fireAllRules();
ksession.delete(fh1);
ksession.delete(fh2);
ksession.delete(fh3);
ksession.fireAllRules();
NodeMemories nodeMemories = ((InternalWorkingMemory) ksession).getNodeMemories();
for (int i = 0; i < nodeMemories.length(); i++) {
Memory memory = nodeMemories.peekNodeMemory(i);
if (memory != null && memory.getSegmentMemory() != null) {
SegmentMemory segmentMemory = memory.getSegmentMemory();
System.out.println(memory);
LeftTuple deleteFirst = memory.getSegmentMemory().getStagedLeftTuples().getDeleteFirst();
if (segmentMemory.getRootNode() instanceof JoinNode) {
BetaMemory bm = (BetaMemory) segmentMemory.getNodeMemories().getFirst();
assertEquals(0, bm.getLeftTupleMemory().size());
}
System.out.println(deleteFirst);
assertNull(deleteFirst);
}
}
}
use of org.drools.core.reteoo.JoinNode in project drools by kiegroup.
the class LeftTupleIterator method getFirstLeftTuple.
public LeftTuple getFirstLeftTuple(LeftTupleSource source, LeftTupleSink sink, InternalWorkingMemory wm) {
if (source instanceof AccumulateNode) {
AccumulateMemory accmem = (AccumulateMemory) wm.getNodeMemory((MemoryFactory) source);
BetaMemory memory = accmem.getBetaMemory();
FastIterator localIt = memory.getLeftTupleMemory().fullFastIterator();
Tuple leftTuple = BetaNode.getFirstTuple(memory.getLeftTupleMemory(), localIt);
if (leftTuple != null) {
AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
return accctx.getResultLeftTuple();
}
return null;
}
if (source instanceof JoinNode || source instanceof NotNode || source instanceof FromNode || source instanceof AccumulateNode) {
BetaMemory memory;
FastIterator localIt;
if (source instanceof FromNode) {
memory = ((FromMemory) wm.getNodeMemory((MemoryFactory) source)).getBetaMemory();
} else if (source instanceof AccumulateNode) {
memory = ((AccumulateMemory) wm.getNodeMemory((MemoryFactory) source)).getBetaMemory();
} else {
memory = (BetaMemory) wm.getNodeMemory((MemoryFactory) source);
}
localIt = memory.getLeftTupleMemory().fullFastIterator();
Tuple leftTuple = BetaNode.getFirstTuple(memory.getLeftTupleMemory(), localIt);
while (leftTuple != null) {
for (LeftTuple childleftTuple = leftTuple.getFirstChild(); childleftTuple != null; childleftTuple = childleftTuple.getHandleNext()) {
if (childleftTuple.getTupleSink() == sink) {
return childleftTuple;
}
}
leftTuple = (LeftTuple) localIt.next(leftTuple);
}
}
if (source instanceof ExistsNode) {
BetaMemory memory = (BetaMemory) wm.getNodeMemory((MemoryFactory) source);
FastIterator localIt = memory.getRightTupleMemory().fullFastIterator();
RightTuple rightTuple = (RightTuple) BetaNode.getFirstTuple(memory.getRightTupleMemory(), localIt);
while (rightTuple != null) {
if (rightTuple.getBlocked() != null) {
for (LeftTuple leftTuple = rightTuple.getBlocked(); leftTuple != null; leftTuple = leftTuple.getBlockedNext()) {
for (LeftTuple childleftTuple = leftTuple.getFirstChild(); childleftTuple != null; childleftTuple = childleftTuple.getHandleNext()) {
if (childleftTuple.getTupleSink() == sink) {
return childleftTuple;
}
}
}
}
rightTuple = (RightTuple) localIt.next(rightTuple);
}
} else if (source instanceof LeftInputAdapterNode) {
ObjectSource os = ((LeftInputAdapterNode) source).getParentObjectSource();
while (!(os instanceof ObjectTypeNode)) {
os = os.getParentObjectSource();
}
ObjectTypeNode otn = (ObjectTypeNode) os;
otnIterator = wm.getNodeMemory(otn).iterator();
while (otnIterator.hasNext()) {
InternalFactHandle handle = otnIterator.next();
LeftTuple leftTuple = handle.findFirstLeftTuple(lt -> lt.getTupleSink() == sink);
if (leftTuple != null) {
return leftTuple;
}
}
} else if (source instanceof EvalConditionNode || source instanceof QueryElementNode) {
LeftTuple parentLeftTuple = getFirstLeftTuple(source.getLeftTupleSource(), (LeftTupleSink) source, wm);
while (parentLeftTuple != null) {
for (LeftTuple leftTuple = parentLeftTuple.getFirstChild(); leftTuple != null; leftTuple = leftTuple.getHandleNext()) {
if (leftTuple.getTupleSink() == sink) {
return leftTuple;
}
}
parentLeftTuple = getNextLeftTuple(source.getLeftTupleSource(), (LeftTupleSink) source, parentLeftTuple, wm);
}
}
return null;
}
use of org.drools.core.reteoo.JoinNode in project drools by kiegroup.
the class AddRuleTest method testPopulatedRuleMidwayShare.
@Test
public void testPopulatedRuleMidwayShare() throws Exception {
InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", " a : A() B() C(1;) D() E()\n");
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
List list = new ArrayList();
wm.setGlobal("list", list);
wm.insert(new A(1));
wm.insert(new A(2));
wm.insert(new A(3));
wm.insert(new B(1));
wm.insert(new C(1));
wm.insert(new C(2));
wm.insert(new D(1));
wm.insert(new E(1));
wm.fireAllRules();
assertEquals(3, list.size());
kbase1.addPackages(buildKnowledgePackage("r2", " a : A() B() C(2;) D() E()\n"));
ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class);
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();
JoinNode c1Node = (JoinNode) bNode.getSinkPropagator().getFirstLeftTupleSink();
JoinNode c2Node = (JoinNode) bNode.getSinkPropagator().getLastLeftTupleSink();
LiaNodeMemory lm = (LiaNodeMemory) wm.getNodeMemory(liaNode);
SegmentMemory sm = lm.getSegmentMemory();
BetaMemory c1Mem = (BetaMemory) wm.getNodeMemory(c1Node);
assertSame(sm.getFirst(), c1Mem.getSegmentMemory());
assertEquals(3, c1Mem.getLeftTupleMemory().size());
assertEquals(1, c1Mem.getRightTupleMemory().size());
BetaMemory c2Mem = (BetaMemory) wm.getNodeMemory(c2Node);
SegmentMemory c2Smem = sm.getFirst().getNext();
assertSame(c2Smem, c2Mem.getSegmentMemory());
assertEquals(0, c2Mem.getLeftTupleMemory().size());
assertEquals(0, c2Mem.getRightTupleMemory().size());
assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst());
assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext());
assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext());
assertNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext().getStagedNext());
wm.fireAllRules();
assertEquals(3, c2Mem.getLeftTupleMemory().size());
assertEquals(1, c2Mem.getRightTupleMemory().size());
assertNull(c2Smem.getStagedLeftTuples().getInsertFirst());
assertEquals(6, list.size());
assertEquals("r1", ((Match) list.get(0)).getRule().getName());
assertEquals("r1", ((Match) list.get(1)).getRule().getName());
assertEquals("r1", ((Match) list.get(2)).getRule().getName());
assertEquals("r2", ((Match) list.get(3)).getRule().getName());
assertEquals(3, ((A) ((Match) list.get(3)).getDeclarationValue("a")).getObject());
assertEquals("r2", ((Match) list.get(4)).getRule().getName());
assertEquals(2, ((A) ((Match) list.get(4)).getDeclarationValue("a")).getObject());
assertEquals("r2", ((Match) list.get(5)).getRule().getName());
assertEquals(1, ((A) ((Match) list.get(5)).getDeclarationValue("a")).getObject());
}
use of org.drools.core.reteoo.JoinNode in project drools by kiegroup.
the class AddRuleTest method testPopulatedSharedToRtn.
@Test
public void testPopulatedSharedToRtn() throws Exception {
InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", " A() B() C() D() E()\n");
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
List list = new ArrayList();
wm.setGlobal("list", list);
wm.insert(new A(1));
wm.insert(new A(2));
wm.insert(new B(1));
wm.insert(new C(1));
wm.insert(new D(1));
wm.insert(new E(1));
wm.fireAllRules();
assertEquals(2, list.size());
kbase1.addPackages(buildKnowledgePackage("r2", " A() B() C() D() E()\n"));
ObjectTypeNode eotn = getObjectTypeNode(kbase1, E.class);
JoinNode eNode = (JoinNode) eotn.getObjectSinkPropagator().getSinks()[0];
RuleTerminalNode rtn = (RuleTerminalNode) eNode.getSinkPropagator().getLastLeftTupleSink();
PathMemory pm = (PathMemory) wm.getNodeMemory(rtn);
SegmentMemory sm = pm.getSegmentMemory();
assertNotNull(sm.getStagedLeftTuples().getInsertFirst());
assertNotNull(sm.getStagedLeftTuples().getInsertFirst().getStagedNext());
assertNull(sm.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext());
wm.fireAllRules();
assertNull(sm.getStagedLeftTuples().getInsertFirst());
assertEquals(4, list.size());
assertEquals("r1", ((Match) list.get(0)).getRule().getName());
assertEquals("r1", ((Match) list.get(1)).getRule().getName());
assertEquals("r2", ((Match) list.get(2)).getRule().getName());
assertEquals("r2", ((Match) list.get(3)).getRule().getName());
}
use of org.drools.core.reteoo.JoinNode in project drools by kiegroup.
the class RemoveRuleTest method testPopulatedSharedLiaNode.
@Test
public void testPopulatedSharedLiaNode() throws Exception {
InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", " A() B(1;) C() D() E()\n");
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
List list = new ArrayList();
wm.setGlobal("list", list);
wm.insert(new A(1));
wm.insert(new A(2));
wm.insert(new A(3));
wm.insert(new B(1));
wm.insert(new B(2));
wm.insert(new C(1));
wm.insert(new D(1));
wm.insert(new E(1));
wm.fireAllRules();
assertEquals(3, list.size());
assertEquals(7, countNodeMemories(wm.getNodeMemories()));
kbase1.addPackages(buildKnowledgePackage("r2", " a : A() B(2;) C() D() E()\n"));
wm.fireAllRules();
assertEquals(17, countNodeMemories(wm.getNodeMemories()));
ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class);
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
JoinNode b1Node = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();
JoinNode b2Node = (JoinNode) liaNode.getSinkPropagator().getLastLeftTupleSink();
JoinNode c1Node = (JoinNode) b1Node.getSinkPropagator().getLastLeftTupleSink();
LiaNodeMemory lm = (LiaNodeMemory) wm.getNodeMemory(liaNode);
SegmentMemory sm = lm.getSegmentMemory();
BetaMemory b1Mem = (BetaMemory) wm.getNodeMemory(b1Node);
assertSame(sm.getFirst(), b1Mem.getSegmentMemory());
assertEquals(3, b1Mem.getLeftTupleMemory().size());
assertEquals(1, b1Mem.getRightTupleMemory().size());
BetaMemory b2Mem = (BetaMemory) wm.getNodeMemory(b2Node);
SegmentMemory b2Smem = sm.getFirst().getNext();
assertSame(b2Smem, b2Mem.getSegmentMemory());
assertEquals(3, b2Mem.getLeftTupleMemory().size());
assertEquals(1, b2Mem.getRightTupleMemory().size());
assertEquals(6, list.size());
BetaMemory c1Mem = (BetaMemory) wm.getNodeMemory(c1Node);
assertSame(b1Mem.getSegmentMemory(), c1Mem.getSegmentMemory());
assertNotSame(b1Mem.getSegmentMemory(), b2Mem.getSegmentMemory());
wm.fireAllRules();
assertEquals(6, list.size());
assertEquals(17, countNodeMemories(wm.getNodeMemories()));
kbase1.removeRule("org.kie", "r2");
assertEquals(12, countNodeMemories(wm.getNodeMemories()));
assertSame(sm, b1Mem.getSegmentMemory());
assertSame(sm, c1Mem.getSegmentMemory());
assertNull(sm.getFirst());
assertEquals(3, b1Mem.getLeftTupleMemory().size());
assertEquals(1, b1Mem.getRightTupleMemory().size());
// SegmentMemory b2Smem = sm.getFirst().remove();
assertSame(b2Smem, b2Mem.getSegmentMemory());
wm.insert(new A(1));
wm.fireAllRules();
assertEquals("r1", ((Match) list.get(6)).getRule().getName());
// only one more added, as second rule as removed
assertEquals(7, list.size());
}
Aggregations