use of org.drools.core.common.Memory in project drools by kiegroup.
the class RightInputAdapterNodeVisitor method doVisit.
@Override
protected void doVisit(NetworkNode node, Stack<NetworkNode> nodeStack, StatefulKnowledgeSessionInfo info) {
RightInputAdapterNode an = (RightInputAdapterNode) node;
DefaultNodeInfo ni = info.getNodeInfo(node);
BetaNode betaNode = (BetaNode) an.getObjectSinkPropagator().getSinks()[0];
Memory childMemory = info.getSession().getNodeMemory(betaNode);
BetaMemory bm;
if (betaNode.getType() == NodeTypeEnums.AccumulateNode) {
bm = ((AccumulateMemory) childMemory).getBetaMemory();
} else {
bm = (BetaMemory) childMemory;
}
ni.setMemoryEnabled(true);
ni.setTupleMemorySize(bm.getRightTupleMemory().size());
ni.setCreatedFactHandles(bm.getRightTupleMemory().size());
}
use of org.drools.core.common.Memory in project drools by kiegroup.
the class BetaMemoryLeakOnDeleteTest method testBetaMemoryLeakOnFactDelete.
@Test
public void testBetaMemoryLeakOnFactDelete() {
final 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" + "then \n" + "end\n";
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
final FactHandle fh1 = ksession.insert(1);
final FactHandle fh2 = ksession.insert("test");
ksession.fireAllRules();
ksession.delete(fh1);
ksession.delete(fh2);
ksession.fireAllRules();
final NodeMemories nodeMemories = ((InternalWorkingMemory) ksession).getNodeMemories();
for (int i = 0; i < nodeMemories.length(); i++) {
final Memory memory = nodeMemories.peekNodeMemory(i);
if (memory != null && memory.getSegmentMemory() != null) {
final LeftTuple deleteFirst = memory.getSegmentMemory().getStagedLeftTuples().getDeleteFirst();
assertThat(deleteFirst).isNull();
}
}
}
use of org.drools.core.common.Memory in project drools by kiegroup.
the class PassiveQueryTest method testPassiveQueryUsingSegmentPropagator.
@Test
public void testPassiveQueryUsingSegmentPropagator() throws Exception {
String str = "global java.util.List list\n" + "query Q (Integer i)\n" + " String( this == i.toString() )\n" + "end\n" + "rule R1a @Propagation(IMMEDIATE) when\n" + " $i : Integer()\n" + " ?Q( $i; )\n" + "then\n" + " list.add( \"R1a\" );\n" + "end\n" + "rule R1b @Propagation(IMMEDIATE) when\n" + " $i : Integer()\n" + " ?Q( $i; )\n" + " Long( intValue == $i )\n" + "then\n" + " list.add( \"R1b\" );\n" + "end\n" + "rule R2 when\n" + " $i : Integer()\n" + " ?Q( $i; )\n" + "then\n" + " list.add( \"R2\" );\n" + "end\n";
KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
KieSession ksession = kbase.newKieSession();
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.insert(1L);
FactHandle fh = ksession.insert(1);
ksession.insert("1");
Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
LeftInputAdapterNode lia = null;
for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
if (Integer.class == otn.getObjectType().getValueType().getClassType()) {
lia = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
break;
}
}
LeftTupleSink[] sinks = lia.getSinkPropagator().getSinks();
QueryElementNode q1 = (QueryElementNode) sinks[0];
QueryElementNode q2 = (QueryElementNode) sinks[1];
InternalWorkingMemory wm = (InternalWorkingMemory) ksession;
wm.flushPropagations();
Memory memory1 = wm.getNodeMemory(q1);
assertTrue(memory1.getSegmentMemory().getStagedLeftTuples().isEmpty());
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals("R2", list.get(0));
list.clear();
ksession.delete(fh);
ksession.insert(1);
ksession.fireAllRules();
assertEquals(3, list.size());
}
use of org.drools.core.common.Memory 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.Memory in project drools by kiegroup.
the class ProtobufOutputMarshaller method writeRIANodeMemory.
private static ProtobufMessages.NodeMemory writeRIANodeMemory(final int nodeId, final BaseNode node, final NodeMemories memories) {
RightInputAdapterNode riaNode = (RightInputAdapterNode) node;
ObjectSink[] sinks = riaNode.getObjectSinkPropagator().getSinks();
BetaNode betaNode = (BetaNode) sinks[0];
Memory betaMemory = memories.peekNodeMemory(betaNode);
if (betaMemory == null) {
return null;
}
BetaMemory bm;
if (betaNode.getType() == NodeTypeEnums.AccumulateNode) {
bm = ((AccumulateMemory) betaMemory).getBetaMemory();
} else {
bm = (BetaMemory) betaMemory;
}
// for RIA nodes, we need to store the ID of the created handles
bm.getRightTupleMemory().iterator();
if (bm.getRightTupleMemory().size() > 0) {
ProtobufMessages.NodeMemory.RIANodeMemory.Builder _ria = ProtobufMessages.NodeMemory.RIANodeMemory.newBuilder();
final org.drools.core.util.Iterator it = bm.getRightTupleMemory().iterator();
// iterates over all propagated handles and assert them to the new sink
for (RightTuple entry = (RightTuple) it.next(); entry != null; entry = (RightTuple) it.next()) {
LeftTuple leftTuple = entry instanceof LeftTuple ? // with phreak the entry is always both a right and a left tuple
(LeftTuple) entry : // this is necessary only for reteoo
(LeftTuple) entry.getFactHandle().getObject();
InternalFactHandle handle = (InternalFactHandle) leftTuple.getFactHandle();
if (handle == null) {
continue;
}
FactHandle _handle = ProtobufMessages.FactHandle.newBuilder().setId(handle.getId()).setRecency(handle.getRecency()).build();
_ria.addContext(ProtobufMessages.NodeMemory.RIANodeMemory.RIAContext.newBuilder().setTuple(PersisterHelper.createTuple(leftTuple)).setResultHandle(_handle).build());
}
return ProtobufMessages.NodeMemory.newBuilder().setNodeId(nodeId).setNodeType(ProtobufMessages.NodeMemory.NodeType.RIA).setRia(_ria.build()).build();
}
return null;
}
Aggregations