aima.core.logic.fol.parsing.ast.Predicate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aima-core Show documentation
Show all versions of aima-core Show documentation
AIMA-Java Core Algorithms from the book Artificial Intelligence a Modern Approach 3rd Ed.
The newest version!
package aima.core.logic.fol.parsing.ast;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import aima.core.logic.fol.parsing.FOLVisitor;
/**
* @author Ravi Mohan
* @author Ciaran O'Reilly
*/
public class Predicate implements AtomicSentence {
private String predicateName;
private List terms = new ArrayList();
private String stringRep = null;
private int hashCode = 0;
public Predicate(String predicateName, List terms) {
this.predicateName = predicateName;
this.terms.addAll(terms);
}
public String getPredicateName() {
return predicateName;
}
public List getTerms() {
return Collections.unmodifiableList(terms);
}
//
// START-AtomicSentence
public String getSymbolicName() {
return getPredicateName();
}
public boolean isCompound() {
return true;
}
public List getArgs() {
return getTerms();
}
public Object accept(FOLVisitor v, Object arg) {
return v.visitPredicate(this, arg);
}
public Predicate copy() {
List copyTerms = new ArrayList();
for (Term t : terms) {
copyTerms.add(t.copy());
}
return new Predicate(predicateName, copyTerms);
}
// END-AtomicSentence
//
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Predicate)) {
return false;
}
Predicate p = (Predicate) o;
return p.getPredicateName().equals(getPredicateName())
&& p.getTerms().equals(getTerms());
}
@Override
public int hashCode() {
if (0 == hashCode) {
hashCode = 17;
hashCode = 37 * hashCode + predicateName.hashCode();
for (Term t : terms) {
hashCode = 37 * hashCode + t.hashCode();
}
}
return hashCode;
}
@Override
public String toString() {
if (null == stringRep) {
StringBuilder sb = new StringBuilder();
sb.append(predicateName);
sb.append("(");
boolean first = true;
for (Term t : terms) {
if (first) {
first = false;
} else {
sb.append(",");
}
sb.append(t.toString());
}
sb.append(")");
stringRep = sb.toString();
}
return stringRep;
}
}