Skip to content

Commit

Permalink
[DROOLS-7462] fix tuples traversal while removing an exists node duri…
Browse files Browse the repository at this point in the history
…ng incremental compilation (#5335)
  • Loading branch information
mariofusco authored Jun 21, 2023
1 parent fa40ea2 commit 8598a3d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -827,24 +827,23 @@ private static void processLeftTuples(LeftTupleNode node, InternalWorkingMemory
AccumulateContext accctx = (AccumulateContext) lt.getContextObject();
visitChild( (LeftTuple) accctx.getResultLeftTuple(), insert, wm, rule);
}
} else if (NodeTypeEnums.ExistsNode == node.getType() &&
!((BetaNode)node).isRightInputIsRiaNode()) { // do not process exists with subnetworks
} else if (NodeTypeEnums.ExistsNode == node.getType() && !node.isRightInputIsRiaNode()) { // do not process exists with subnetworks
// If there is a subnetwork, then there is no populated RTM, but the LTM is populated,
// so this would be procsssed in the "else".
// so this would be processed in the "else".

bm = (BetaMemory) wm.getNodeMemory((MemoryFactory) node);
FastIterator it = bm.getRightTupleMemory().fullFastIterator(); // done off the RightTupleMemory, as exists only have unblocked tuples on the left side
RightTuple rt = (RightTuple) BetaNode.getFirstTuple(bm.getRightTupleMemory(), it);
for (; rt != null; rt = (RightTuple) it.next(rt)) {
for (RightTuple rt = (RightTuple) BetaNode.getFirstTuple(bm.getRightTupleMemory(), it); rt != null; rt = (RightTuple) it.next(rt)) {
for (LeftTuple lt = rt.getBlocked(); lt != null; lt = lt.getBlockedNext()) {
visitChild(wm, insert, rule, it, lt);
visitLeftTuple(wm, insert, rule, lt);
}
}
} else {
bm = (BetaMemory) wm.getNodeMemory((MemoryFactory) node);
FastIterator it = bm.getLeftTupleMemory().fullFastIterator();
Tuple lt = BetaNode.getFirstTuple(bm.getLeftTupleMemory(), it);
visitChild(wm, insert, rule, it, lt);
for (LeftTuple lt = (LeftTuple)BetaNode.getFirstTuple(bm.getLeftTupleMemory(), it); lt != null; lt = (LeftTuple) it.next(lt)) {
visitLeftTuple(wm, insert, rule, lt);
}
}
return;
} else if (NodeTypeEnums.FromNode == node.getType()) {
Expand Down Expand Up @@ -893,14 +892,12 @@ private static void processLeftTuplesOnLian( InternalWorkingMemory wm, boolean i
}
}

private static void visitChild(InternalWorkingMemory wm, boolean insert, Rule rule, FastIterator it, Tuple lt) {
for (; lt != null; lt = (LeftTuple) it.next(lt)) {
LeftTuple childLt = lt.getFirstChild();
while (childLt != null) {
LeftTuple nextLt = childLt.getHandleNext();
visitChild(childLt, insert, wm, rule);
childLt = nextLt;
}
private static void visitLeftTuple(InternalWorkingMemory wm, boolean insert, Rule rule, LeftTuple lt) {
LeftTuple childLt = lt.getFirstChild();
while (childLt != null) {
LeftTuple nextLt = childLt.getHandleNext();
visitChild(childLt, insert, wm, rule);
childLt = nextLt;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5165,4 +5165,62 @@ public void testReaddAllRulesWithComplexNodeSharing() {
assertThat(ksession.fireAllRules()).isEqualTo(1);
assertThat(fired).containsExactly("R6");
}

@Test
public void testReaddAllRulesWithIdenticalRules() {
// DROOLS-7462

final String drl1 =
"import " + Message.class.getCanonicalName() + ";\n" +
"\n" +
"rule R1 when\n" +
" $m: Message()\n" +
" exists String(toString == $m.message)\n" +
"then\n" +
"end\n" +
"\n" +
"rule R2 when\n" +
" $m: Message()\n" +
" exists String(toString == $m.message)\n" +
"then\n" +
"end\n";

final String drl2 =
"import " + Message.class.getCanonicalName() + ";\n" +
"\n" +
"rule R3 when\n" +
" $m: Message()\n" +
" exists String(toString == $m.message)\n" +
"then\n" +
"end\n" +
"\n" +
"rule R4 when\n" +
" $m: Message()\n" +
" exists String(toString == $m.message)\n" +
"then\n" +
"end\n";

final KieServices ks = KieServices.Factory.get();

final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
KieUtil.getKieModuleFromDrls(releaseId1, kieBaseTestConfiguration, drl1);

final KieContainer kc = ks.newKieContainer(releaseId1);
KieSession ksession = kc.newKieSession();

ksession.insert(new Message("test1"));
ksession.insert("test1");
ksession.insert(new Message("test2"));
ksession.insert("test2");

int fired = ksession.fireAllRules();
assertThat(fired).isEqualTo(4);

final ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
KieUtil.getKieModuleFromDrls(releaseId2, kieBaseTestConfiguration, drl2);

kc.updateToVersion(releaseId2);
fired = ksession.fireAllRules();
assertThat(fired).isEqualTo(4);
}
}

0 comments on commit 8598a3d

Please sign in to comment.