use of org.drools.core.common.NodeMemories 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";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
KieSession ksession = kbase.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.common.NodeMemories in project drools by kiegroup.
the class ProtobufOutputMarshaller method writeNodeMemories.
private static void writeNodeMemories(MarshallerWriteContext context, ProtobufMessages.RuleData.Builder _ksb) throws IOException {
InternalWorkingMemory wm = context.getWorkingMemory();
NodeMemories memories = wm.getNodeMemories();
// so we iterate over all of them and process only those that require it
for (BaseNode baseNode : context.getSinks().values()) {
Memory memory = memories.peekNodeMemory(baseNode);
if (memory != null) {
ProtobufMessages.NodeMemory _node = null;
switch(memory.getNodeType()) {
case NodeTypeEnums.QueryElementNode:
{
_node = writeQueryElementNodeMemory(baseNode.getId(), memory, wm);
break;
}
}
if (_node != null) {
// not all node memories require serialization
_ksb.addNodeMemory(_node);
}
}
}
}
use of org.drools.core.common.NodeMemories in project drools by kiegroup.
the class NotTest method getBlockerFactHandle.
private InternalFactHandle getBlockerFactHandle(KieSession ksession) {
ObjectTypeNode otn = getObjectTypeNode(ksession.getKieBase(), Person.class);
BetaNode notNode = (BetaNode) ((AlphaNode) otn.getSinks()[0]).getSinks()[0];
StatefulKnowledgeSessionImpl ksessionImpl = (StatefulKnowledgeSessionImpl) ksession;
NodeMemories nodeMemories = ksessionImpl.getNodeMemories();
BetaMemory betaMemory = (BetaMemory) nodeMemories.getNodeMemory(notNode, ksessionImpl);
TupleMemory rightTupleMemory = betaMemory.getRightTupleMemory();
Tuple[] tuples = (Tuple[]) rightTupleMemory.toArray();
for (int i = 0; i < tuples.length; i++) {
RightTupleImpl tuple = (RightTupleImpl) tuples[i];
if (tuple.getBlocked() != null) {
return tuple.getFactHandle();
}
}
fail("Cannot find blocker in BetaMemory");
return null;
}
use of org.drools.core.common.NodeMemories 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.common.NodeMemories in project drools by kiegroup.
the class RemoveRuleTest method testPopulatedSingleRuleNoSharing.
@Test
public void testPopulatedSingleRuleNoSharing() throws Exception {
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
wm.insert(new A(1));
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();
kbase.addPackages(buildKnowledgePackage("r1", " A() B() C(object == 2) D() E()\n"));
List list = new ArrayList();
wm.setGlobal("list", list);
ObjectTypeNode aotn = getObjectTypeNode(kbase, A.class);
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
LiaNodeMemory lm = (LiaNodeMemory) wm.getNodeMemory(liaNode);
SegmentMemory sm = lm.getSegmentMemory();
assertNotNull(sm.getStagedLeftTuples().getInsertFirst());
wm.fireAllRules();
BetaMemory bMem = (BetaMemory) sm.getNodeMemories().get(1);
assertEquals(1, bMem.getLeftTupleMemory().size());
assertEquals(1, bMem.getRightTupleMemory().size());
BetaMemory eMem = (BetaMemory) sm.getNodeMemories().get(4);
assertEquals(1, eMem.getLeftTupleMemory().size());
assertEquals(1, eMem.getRightTupleMemory().size());
NodeMemories nms = wm.getNodeMemories();
assertEquals(12, countNodeMemories(nms));
assertNull(sm.getStagedLeftTuples().getInsertFirst());
assertEquals(1, list.size());
assertEquals("r1", ((Match) list.get(0)).getRule().getName());
kbase.removeRule("org.kie", "r1");
// still has OTN
assertEquals(6, countNodeMemories(nms));
}
Aggregations