Skip to content

Commit

Permalink
Merge pull request #507 from TNO/440-prevent-the-same-knowledge-base-…
Browse files Browse the repository at this point in the history
…to-be-contacted-multiple-times-in-a-single-interaction

Fix applying a rule multiple times.
  • Loading branch information
bnouwt authored May 29, 2024
2 parents 8be6632 + 1fae3ed commit 115a923
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,10 @@ protected void setName(String aName) {
this.name = aName;
}

protected String getName() {
return name;
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ public TaskBoard execute(BindingSet bindingSet) {

// Ready, and current version of input has not been scheduled on taskboard? ->
// Add to taskboard otherwise -> Do not add to taskboard
if (current.readyForApplyRule() && !current.isResultBindingSetInputScheduled()) {
if (current.readyForApplyRule() && !current.isResultBindingSetInputAlreadyScheduledOrDone()) {
this.scheduleOrDoTask(current, taskBoard);
current.setResultBindingSetInputScheduled(true);
current.setResultBindingSetInputAlreadyScheduledOrDone(true);
}

if (current.shouldPropagateFilterBindingSetOutput()) {
Expand All @@ -145,10 +145,18 @@ public TaskBoard execute(BindingSet bindingSet) {
((ConsSide) current).getConsequentNeighbours().forEach((n, matches) -> {
var translated = toBeResultPropagated.translate(n.getRule().getAntecedent(),
Match.invertAll(matches));

TripleVarBindingSet beforeBindingSet = n.getResultBindingSetInput();
boolean itChanged = ((AntSide) n).addResultBindingSetInput(current, translated);
TripleVarBindingSet afterBindingSet = n.getResultBindingSetInput();
if (itChanged) {
changed.add(n);
n.setResultBindingSetInputScheduled(false);

// We should only set this to false if the actual binding set of the
// BindngSetStore.get() method changes. Otherwise rules get applied multiple
// times with the same binding set.
if (!beforeBindingSet.equals(afterBindingSet))
n.setResultBindingSetInputAlreadyScheduledOrDone(false);
}
});
current.setResultBindingSetOutputPropagated();
Expand Down Expand Up @@ -287,7 +295,6 @@ private RuleNode createOrGetReasonerNode(BaseRule aRule, BaseRule aParent) {
private void scheduleOrDoTask(RuleNode current, TaskBoard taskBoard) {
if (this.useTaskBoard) {
taskBoard.addTask(current);
current.setResultBindingSetInputScheduled(true);
} else {
try {
current.applyRule().get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public BaseRule getRule() {

public abstract Set<RuleNode> getAllSameLoopNeighbors();

public void setResultBindingSetInputScheduled(boolean b) {
public void setResultBindingSetInputAlreadyScheduledOrDone(boolean b) {
this.resultBindingSetOutputScheduled = b;
}

public boolean isResultBindingSetInputScheduled() {
public boolean isResultBindingSetInputAlreadyScheduledOrDone() {
return this.resultBindingSetOutputScheduled;
}

Expand Down

0 comments on commit 115a923

Please sign in to comment.