use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.
the class RemoveRuleTest method testPopulatedMultipleSharesRemoveFirst.
@Test
public void testPopulatedMultipleSharesRemoveFirst() throws Exception {
InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", " A(1;) A(2;) B(1;) B(2;) C(1;) X() E()\n");
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
List list = new ArrayList();
wm.setGlobal("list", list);
InternalFactHandle fh1 = (InternalFactHandle) wm.insert(new A(1));
InternalFactHandle fh2 = (InternalFactHandle) wm.insert(new A(2));
InternalFactHandle fh3 = (InternalFactHandle) wm.insert(new A(2));
InternalFactHandle fh4 = (InternalFactHandle) wm.insert(new A(3));
InternalFactHandle fh5 = (InternalFactHandle) wm.insert(new B(1));
InternalFactHandle fh6 = (InternalFactHandle) wm.insert(new B(2));
InternalFactHandle fh7 = (InternalFactHandle) wm.insert(new C(1));
InternalFactHandle fh8 = (InternalFactHandle) wm.insert(new C(2));
InternalFactHandle fh9 = (InternalFactHandle) wm.insert(new X(1));
InternalFactHandle fh10 = (InternalFactHandle) wm.insert(new E(1));
wm.fireAllRules();
assertEquals(2, list.size());
kbase1.addPackages(buildKnowledgePackage("r2", " A(1;) A(2;) B(1;) B(2;) C(2;) X() E()\n"));
kbase1.addPackages(buildKnowledgePackage("r3", " A(1;) A(3;) B(1;) B(2;) C(2;) X() E()\n"));
wm.fireAllRules();
assertEquals(5, list.size());
kbase1.removeRule("org.kie", "r1");
list.clear();
wm.update(fh1, fh1.getObject());
wm.update(fh2, fh2.getObject());
wm.update(fh3, fh3.getObject());
wm.update(fh4, fh4.getObject());
wm.update(fh5, fh5.getObject());
wm.update(fh6, fh6.getObject());
wm.update(fh7, fh7.getObject());
wm.update(fh8, fh8.getObject());
wm.update(fh9, fh9.getObject());
wm.update(fh10, fh10.getObject());
wm.fireAllRules();
assertEquals(3, list.size());
}
use of org.drools.core.common.InternalWorkingMemory 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.InternalWorkingMemory in project drools by kiegroup.
the class TraitTest method testTMSConsistencyWithNonTraitableBeans.
@Test
public void testTMSConsistencyWithNonTraitableBeans() {
String s1 = "package org.drools.test;\n" + "import org.drools.traits.compiler.Person; \n" + "import org.drools.core.factmodel.traits.Traitable; \n" + "" + "declare Person @Traitable end \n" + "" + "rule \"Init\"\n" + "when\n" + "then\n" + " insertLogical( new Person( \"x\", 18 ) );\n" + "end\n" + "\n" + "declare trait Student\n" + " age : int\n" + " name : String\n" + "end\n" + "\n" + "rule \"Trait\"\n" + "when\n" + " $p : Person( )\n" + "then\n" + " don( $p, Student.class, true );\n" + "end\n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSession ksession = kbase.newKieSession();
TraitFactoryImpl.setMode(mode, ksession.getKieBase());
ksession.fireAllRules();
FactHandle personHandle = ksession.getFactHandles(new ClassObjectFilter(Person.class)).iterator().next();
InternalFactHandle h = ((InternalFactHandle) personHandle);
ObjectTypeConfigurationRegistry reg = h.getEntryPoint((InternalWorkingMemory) ksession).getObjectTypeConfigurationRegistry();
ObjectTypeConf conf = reg.getOrCreateObjectTypeConf(h.getEntryPointId(), ((InternalFactHandle) personHandle).getObject());
assertTrue(conf.isTMSEnabled());
ksession.dispose();
}
use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.
the class AddRemoveRule method removeRule.
/**
* This method is called before the rule nodes are removed from the network.
* For remove tuples are processed before the segments and pmems have been adjusted
*/
public static void removeRule(TerminalNode tn, Collection<InternalWorkingMemory> wms, RuleBase kBase) {
if (log.isTraceEnabled()) {
log.trace("Removing Rule {}", tn.getRule().getName());
}
boolean hasProtos = kBase.hasSegmentPrototypes();
boolean hasWms = !wms.isEmpty();
if (!hasProtos && !hasWms) {
return;
}
RuleImpl rule = tn.getRule();
LeftTupleNode firstSplit = getNetworkSplitPoint(tn);
PathEndNodes pathEndNodes = getPathEndNodes(kBase, firstSplit, tn, rule, hasProtos, hasWms);
for (InternalWorkingMemory wm : wms) {
wm.flushPropagations();
PathEndNodeMemories tnms = getPathEndMemories(wm, pathEndNodes);
if (!tnms.subjectPmems.isEmpty()) {
if (NodeTypeEnums.LeftInputAdapterNode == firstSplit.getType() && firstSplit.getAssociationsSize() == 1) {
if (tnms.subjectPmem != null) {
flushStagedTuples(firstSplit, tnms.subjectPmem, wm);
}
processLeftTuples(firstSplit, wm, false, tn.getRule());
removeNewPaths(wm, tnms.subjectPmems);
} else {
flushStagedTuples(tn, tnms.subjectPmem, pathEndNodes, wm);
processLeftTuples(firstSplit, wm, false, tn.getRule());
removeNewPaths(wm, tnms.subjectPmems);
Map<PathMemory, SegmentMemory[]> prevSmemsLookup = reInitPathMemories(tnms.otherPmems, tn);
// must collect all visited SegmentMemories, for link notification
Set<SegmentMemory> smemsToNotify = handleExistingPaths(tn, prevSmemsLookup, tnms.otherPmems, wm, ExistingPathStrategy.REMOVE_STRATEGY);
notifySegments(smemsToNotify, wm);
}
}
if (tnms.subjectPmem != null && tnms.subjectPmem.isInitialized() && tnms.subjectPmem.getRuleAgendaItem().isQueued()) {
// SubjectPmem can be null, if it was never initialized
tnms.subjectPmem.getRuleAgendaItem().dequeue();
}
}
if (!hasWms) {
for (PathEndNode node : pathEndNodes.otherEndNodes) {
node.resetPathMemSpec(null);
}
}
}
use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.
the class AddRemoveRule method removeNewPaths.
private static void removeNewPaths(InternalWorkingMemory wm, List<PathMemory> pmems) {
Set<Integer> visitedNodes = new HashSet<>();
for (PathMemory pmem : pmems) {
LeftTupleSink tipNode = 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();
}
}
}
Aggregations