Skip to content

Instantly share code, notes, and snippets.

@AndreaCrotti
Created June 15, 2010 11:05
Show Gist options
  • Select an option

  • Save AndreaCrotti/438979 to your computer and use it in GitHub Desktop.

Select an option

Save AndreaCrotti/438979 to your computer and use it in GitHub Desktop.
@Override
public boolean equals(Object o) {
if (o instanceof PrologTerm) {
PrologTerm t = (PrologTerm) o;
return this.equals(t);
} else {
System.out.println("Strange");
return false;
}
}
public boolean equals(PrologTerm o) {
boolean res = true;
System.out.println("checking equality of " + this + " and " + o);
// TODO: throw exception if of another non related type?
// check if both variables
if ((o.isVariable() && this.isVariable()))
return this.equals(o);
// one is a variable and the other is not
if ((o.isVariable() && !(this.isVariable())) ||
(this.isVariable() && !(o.isVariable())))
return false;
// both back reference, TODO: possible infinite loops here
if ((this.isBackRef() && (o.isBackRef())))
((BackRef) this).equals((BackRef) o);
// just inverting the order
if ((this.isBackRef() && !(o.isBackRef())))
return o.equals(this);
// only the first one is a back reference
if ((o.isBackRef() && !(this.isBackRef()))) {
// equality with the thing pointed
return ((BackRef) o).getPointed().equals(this);
}
// in this last case they're both surely not Back Ref, so they have some children
if (!(this.name.equals(o.name)) || (this.getArity() != o.getArity()))
return false;
for (int i = 0; i < this.getArity(); i++) {
if (!(this.getArgument(i).equals(o.getArgument(i)))) {
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment