use of org.drools.core.reteoo.LeftTuple in project drools by kiegroup.
the class AddRemoveRulesTest method testRemoveChildLeftTupleThatWasLastWithMultipleData.
@Test
public void testRemoveChildLeftTupleThatWasLastWithMultipleData() {
final String[] rules = getRules3Pattern();
final KieSession kieSession = TestUtil.createSession(rules[0], rules[1]);
try {
final List resultsList = new ArrayList();
kieSession.setGlobal("list", resultsList);
TestUtil.insertFacts(kieSession, 3, 4, 5);
kieSession.fireAllRules();
Assertions.assertThat(resultsList).containsOnly(TestUtil.RULE1_NAME, TestUtil.RULE2_NAME);
resultsList.clear();
TestUtil.removeRules(kieSession, TestUtil.RULES_PACKAGE_NAME, TestUtil.RULE2_NAME);
kieSession.fireAllRules();
final Map<String, Rule> rulesMap = rulestoMap(kieSession.getKieBase());
final InternalFactHandle fh1 = (InternalFactHandle) kieSession.getFactHandle(3);
final InternalFactHandle fh2 = (InternalFactHandle) kieSession.getFactHandle(4);
final InternalFactHandle fh3 = (InternalFactHandle) kieSession.getFactHandle(5);
final LeftTuple lt1 = fh1.getFirstLeftTuple();
final LeftTuple lt1_1 = lt1.getFirstChild();
final LeftTuple lt1_2 = lt1_1.getHandleNext();
final LeftTuple lt1_3 = lt1_2.getHandleNext();
assertNotNull(lt1_1);
assertNotNull(lt1_2);
assertNotNull(lt1_3);
assertSame(lt1_3, lt1.getLastChild());
assertSame(lt1_2, lt1_3.getHandlePrevious());
assertSame(lt1_1, lt1_2.getHandlePrevious());
assertEquals(1, lt1_1.getTupleSink().getAssociatedRuleSize());
assertTrue(lt1_1.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
assertNull(lt1_1.getPeer());
assertEquals(1, lt1_2.getTupleSink().getAssociatedRuleSize());
assertTrue(lt1_2.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
assertNull(lt1_2.getPeer());
assertEquals(1, lt1_3.getTupleSink().getAssociatedRuleSize());
assertTrue(lt1_3.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
assertNull(lt1_3.getPeer());
final RightTuple rt1 = fh3.getFirstRightTuple();
final LeftTuple rt1_1 = rt1.getLastChild();
assertSame(lt1_1, rt1_1);
final LeftTuple rt1_2 = rt1_1.getRightParentPrevious();
final LeftTuple rt1_3 = rt1_2.getRightParentPrevious();
assertNotNull(rt1_1);
assertNotNull(rt1_2);
assertNotNull(rt1_3);
assertSame(rt1_2, rt1_3.getRightParentNext());
assertSame(rt1_1, rt1_2.getRightParentNext());
assertEquals(1, rt1_1.getTupleSink().getAssociatedRuleSize());
assertTrue(rt1_1.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
assertNull(rt1_1.getPeer());
assertEquals(1, rt1_2.getTupleSink().getAssociatedRuleSize());
assertTrue(rt1_2.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
assertNull(rt1_2.getPeer());
assertEquals(1, rt1_3.getTupleSink().getAssociatedRuleSize());
assertTrue(rt1_3.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
assertNull(rt1_3.getPeer());
} finally {
kieSession.dispose();
}
}
use of org.drools.core.reteoo.LeftTuple in project drools by kiegroup.
the class MemoryLeakTest method testStagedLeftTupleLeak.
@Test
public void testStagedLeftTupleLeak() throws Exception {
// BZ-1058874
String str = "rule R1 when\n" + " String( this == \"this\" )\n" + " String( this == \"that\" )\n" + "then\n" + "end\n";
KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
KieSession ksession = kbase.newKieSession();
ksession.fireAllRules();
for (int i = 0; i < 10; i++) {
FactHandle fh = ksession.insert("this");
ksession.fireAllRules();
ksession.delete(fh);
ksession.fireAllRules();
}
Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
LeftInputAdapterNode liaNode = null;
for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
if (String.class == otn.getObjectType().getValueType().getClassType()) {
AlphaNode alphaNode = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
liaNode = (LeftInputAdapterNode) alphaNode.getObjectSinkPropagator().getSinks()[0];
break;
}
}
assertNotNull(liaNode);
InternalWorkingMemory wm = (InternalWorkingMemory) ksession;
LeftInputAdapterNode.LiaNodeMemory memory = (LeftInputAdapterNode.LiaNodeMemory) wm.getNodeMemory(liaNode);
TupleSets<LeftTuple> stagedLeftTuples = memory.getSegmentMemory().getStagedLeftTuples();
assertNull(stagedLeftTuples.getDeleteFirst());
assertNull(stagedLeftTuples.getInsertFirst());
}
use of org.drools.core.reteoo.LeftTuple 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.LeftTuple 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.LeftTuple in project drools by kiegroup.
the class LeftTupleIterator method next.
public LeftTuple next() {
LeftTuple leftTuple = null;
if (this.currentLeftTuple != null) {
leftTuple = currentLeftTuple;
setNextLeftTuple();
}
return leftTuple;
}
Aggregations