Skip to content

Commit

Permalink
[DROOLS-7554] retain all events till expiration when multiple matches…
Browse files Browse the repository at this point in the history
… is enabled (#83)
  • Loading branch information
mariofusco authored Sep 22, 2023
1 parent 5da912f commit 7e87676
Show file tree
Hide file tree
Showing 2 changed files with 396 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

import org.drools.ansible.rulebook.integration.api.domain.RuleMatch;
import org.drools.core.common.InternalFactHandle;
import org.drools.core.reteoo.LeftTuple;
import org.drools.core.reteoo.RightTuple;
import org.drools.core.reteoo.RuleTerminalNodeLeftTuple;
import org.drools.core.reteoo.Tuple;
import org.kie.api.runtime.rule.AgendaFilter;
import org.kie.api.runtime.rule.FactHandle;
import org.kie.api.runtime.rule.Match;
Expand Down Expand Up @@ -59,45 +55,17 @@ public boolean accept(Match match) {
matchedRules.add( matchTransformers.getOrDefault(metadata.get(RULE_TYPE_TAG), Function.identity()).apply(match) );
}

for (InternalFactHandle fh : fhs) {
if (fh.isEvent() && discardMatchedEvent(fh)) {
eventsToBeDeleted.add(fh);
if (!rulesExecutorSession.isMatchMultipleRules()) {
for (InternalFactHandle fh : fhs) {
if (fh.isEvent()) {
eventsToBeDeleted.add(fh);
}
}
}

return validMatch;
}

private boolean discardMatchedEvent(InternalFactHandle fh) {
// if an event can match multiple rules and is still part of a partial match it shouldn't be discarded yet
return !(rulesExecutorSession.isMatchMultipleRules() && hasPartialMatch(fh));
}

private static boolean hasPartialMatch(InternalFactHandle fh) {
// check if all the tuples created from this fact reached the terminal node, otherwise it is a partial match
for (RightTuple rt = fh.getLinkedTuples().getFirstRightTuple(0); rt != null; rt = rt.getHandleNext()) {
if (isPartialMatch(rt)) {
return true;
}
}
for (LeftTuple lt = fh.getLinkedTuples().getFirstLeftTuple(0); lt != null; lt = lt.getHandleNext()) {
if (isPartialMatch(lt)) {
return true;
}
}
return false;
}

private static boolean isPartialMatch(Tuple tuple) {
// the tuple is a partial match if never reached a terminal node
for (; tuple != null; tuple = tuple.getFirstChild()) {
if (tuple instanceof RuleTerminalNodeLeftTuple) {
return false;
}
}
return true;
}

public List<Match> finalizeAndGetResults(boolean event) {
rulesExecutorSession.registerMatchedEvents(eventsToBeDeleted);
for (FactHandle toBeDeleted : eventsToBeDeleted) {
Expand Down
Loading

0 comments on commit 7e87676

Please sign in to comment.