use of org.drools.core.common.Memory in project drools by kiegroup.
the class AddRemoveRule method splitNodeMemories.
private static void splitNodeMemories(SegmentMemory sm1, SegmentMemory sm2, int pos) {
LinkedList<Memory> smNodeMemories1 = sm1.getNodeMemories();
LinkedList<Memory> smNodeMemories2 = sm2.getNodeMemories();
Memory mem = smNodeMemories1.getFirst();
int nodePosMask = 1;
for (int i = 0, length = smNodeMemories1.size(); i < length; i++) {
Memory next = mem.getNext();
if (i > pos) {
smNodeMemories1.remove(mem);
smNodeMemories2.add(mem);
mem.setSegmentMemory(sm2);
// correct the NodePosMaskBit
if (mem instanceof SegmentNodeMemory) {
((SegmentNodeMemory) mem).setNodePosMaskBit(nodePosMask);
}
nodePosMask = nodePosMask << 1;
}
mem = next;
}
}
use of org.drools.core.common.Memory in project drools by kiegroup.
the class AddRemoveRule method flushStagedTuples.
private static void flushStagedTuples(TerminalNode tn, PathMemory pmem, PathEndNodes pathEndNodes, InternalWorkingMemory wm) {
// first flush the subject rule, then flush any staging lists that are part of a merge
if (pmem.isInitialized()) {
new RuleNetworkEvaluator().evaluateNetwork(pmem, pmem.getRuleAgendaItem().getRuleExecutor(), wm);
}
// With the removing rules being flushed, we need to check any splits that will be merged, to see if they need flushing
// Beware that flushing a higher up node, might again cause lower nodes to have more staged items. So track flushed items
// incase they need to be reflushed
List<Flushed> flushed = new ArrayList<Flushed>();
for (LeftTupleNode node : pathEndNodes.subjectSplits) {
if (!isSplit(node, tn)) {
// check if the split is there even without the processed rule
Memory mem = wm.getNodeMemories().peekNodeMemory(node);
if (mem != null) {
SegmentMemory smem = mem.getSegmentMemory();
if (!smem.isEmpty()) {
for (SegmentMemory childSmem = smem.getFirst(); childSmem != null; childSmem = childSmem.getNext()) {
if (!childSmem.getStagedLeftTuples().isEmpty()) {
PathMemory childPmem = childSmem.getPathMemories().get(0);
flushed.add(new Flushed(childSmem, childPmem));
forceFlushLeftTuple(childPmem, childSmem, wm, childSmem.getStagedLeftTuples().takeAll());
}
}
}
}
}
}
// need to ensure that there is one full iteration, without any flushing. To avoid one flush causing populat of another already flushed segment
int flushCount = 1;
while (!flushed.isEmpty() && flushCount != 0) {
flushCount = 0;
for (Flushed path : flushed) {
if (!path.segmentMemory.getStagedLeftTuples().isEmpty()) {
flushCount++;
forceFlushLeftTuple(pmem, path.segmentMemory, wm, path.segmentMemory.getStagedLeftTuples().takeAll());
}
}
}
}
use of org.drools.core.common.Memory 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.common.Memory in project drools by kiegroup.
the class AddRemoveRule method insertPeerLeftTuple.
/**
* Create all missing peers
*/
private static LeftTuple insertPeerLeftTuple(LeftTuple lt, LeftTupleSinkNode node, InternalWorkingMemory wm) {
LeftInputAdapterNode.LiaNodeMemory liaMem = null;
if (node.getLeftTupleSource().getType() == NodeTypeEnums.LeftInputAdapterNode) {
liaMem = wm.getNodeMemory(((LeftInputAdapterNode) node.getLeftTupleSource()));
}
LeftTuple peer = node.createPeer(lt);
Memory memory = wm.getNodeMemories().peekNodeMemory(node);
if (memory == null || memory.getSegmentMemory() == null) {
throw new IllegalStateException("Defensive Programming: this should not be possilbe, as the addRule code should init child segments if they are needed ");
}
if (liaMem == null) {
memory.getSegmentMemory().getStagedLeftTuples().addInsert(peer);
} else {
// If parent is Lian, then this must be called, so that any linking or unlinking can be done.
LeftInputAdapterNode.doInsertSegmentMemory(wm, true, liaMem, memory.getSegmentMemory(), peer, node.getLeftTupleSource().isStreamMode());
}
return peer;
}
use of org.drools.core.common.Memory in project drools by kiegroup.
the class Misc2Test method testBetaMemoryLeakOnSegmentUnlinking.
@Test
@Ignore
public void testBetaMemoryLeakOnSegmentUnlinking() {
// DROOLS-915
String drl = "rule R1 when\n" + " $a : Integer(this == 1)\n" + " $b : String()\n" + " $c : Integer(this == 2)\n" + " $d : Integer(this == 3)\n" + "then \n" + "end\n" + "rule R2 when\n" + " $a : Integer(this == 1)\n" + " $b : String()\n" + "then \n" + "end\n";
KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
FactHandle fh1 = ksession.insert(1);
FactHandle fh2 = ksession.insert(2);
FactHandle fh3 = ksession.insert(3);
FactHandle fhtest = ksession.insert("test");
ksession.fireAllRules();
ksession.delete(fh3);
ksession.fireAllRules();
ksession.delete(fh1);
ksession.delete(fh2);
ksession.delete(fhtest);
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();
System.out.println(deleteFirst);
assertNull(deleteFirst);
}
}
}
Aggregations