Skip to content

Commit

Permalink
fix FunctionTermImpl#equals s.t. also subtypes can be equal
Browse files Browse the repository at this point in the history
  • Loading branch information
madmike200590 committed Dec 10, 2023
1 parent 782c338 commit c7ec493
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import at.ac.tuwien.kr.alpha.api.rules.heads.ActionHead;
import at.ac.tuwien.kr.alpha.api.terms.Term;
import at.ac.tuwien.kr.alpha.api.terms.VariableTerm;
import org.apache.commons.lang3.StringUtils;

class ActionHeadImpl implements ActionHead {

Expand Down Expand Up @@ -55,4 +56,8 @@ public VariableTerm getActionOutputTerm() {
return actionOutputTerm;
}

public String toString() {
return atom.toString() + " : @" + actionName + "(" + StringUtils.join(actionInputTerms, ", ") + ") = " + actionOutputTerm;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,19 @@ public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
if (o == null) {
return false;
}
if (!(o instanceof FunctionTerm)) {
return false;
}

FunctionTermImpl that = (FunctionTermImpl) o;
FunctionTerm that = (FunctionTerm) o;

if (!symbol.equals(that.symbol)) {
if (!symbol.equals(that.getSymbol())) {
return false;
}
return terms.equals(that.terms);
return terms.equals(that.getTerms());
}

@Override
Expand All @@ -121,24 +124,24 @@ public int compareTo(Term o) {
return 0;
}

if (!(o instanceof FunctionTermImpl)) {
if (!(o instanceof FunctionTerm)) {
return super.compareTo(o);
}

FunctionTermImpl other = (FunctionTermImpl) o;
FunctionTerm other = (FunctionTerm) o;

if (terms.size() != other.terms.size()) {
return terms.size() - other.terms.size();
if (terms.size() != other.getTerms().size()) {
return terms.size() - other.getTerms().size();
}

int result = symbol.compareTo(other.symbol);
int result = symbol.compareTo(other.getSymbol());

if (result != 0) {
return result;
}

for (int i = 0; i < terms.size(); i++) {
result = terms.get(i).compareTo(other.terms.get(i));
result = terms.get(i).compareTo(other.getTerms().get(i));
if (result != 0) {
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.List;

import at.ac.tuwien.kr.alpha.api.terms.FunctionTerm;
import org.junit.jupiter.api.Test;

import at.ac.tuwien.kr.alpha.api.terms.ConstantTerm;
Expand All @@ -30,4 +31,15 @@ public void stringsAsTermList() {
assertEquals("\"blubb\"", terms.get(1).toString());
}

/**
* Reproduction test for an error observed while testing evolog actions.
*/
@Test
public void functionTermVsActionSuccessTermHash() {
FunctionTerm funcTerm = Terms.newFunctionTerm("success", Terms.newFunctionTerm("stream", Terms.newConstant("outputStream_2")));
FunctionTerm actionSuccessTerm = Terms.actionSuccess(Terms.newFunctionTerm("stream", Terms.newConstant("outputStream_2")));
assertEquals(funcTerm, actionSuccessTerm);
assertEquals(funcTerm.hashCode(), actionSuccessTerm.hashCode());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public void removeIndexPosition(int position) {
* @return true if the instance is already contained in the storage.
*/
public boolean containsInstance(Instance instance) {
return instances.contains(instance);
boolean contains = instances.contains(instance);
return contains; // TODO remove extra variable, debugging only
}

public void addInstance(Instance instance) {
Expand Down

0 comments on commit c7ec493

Please sign in to comment.