aima.core.logic.fol.parsing.ast.QuantifiedSentence 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 QuantifiedSentence implements Sentence {
private String quantifier;
private List variables = new ArrayList();
private Sentence quantified;
private List args = new ArrayList();
private String stringRep = null;
private int hashCode = 0;
public QuantifiedSentence(String quantifier, List variables,
Sentence quantified) {
this.quantifier = quantifier;
this.variables.addAll(variables);
this.quantified = quantified;
this.args.addAll(variables);
this.args.add(quantified);
}
public String getQuantifier() {
return quantifier;
}
public List getVariables() {
return Collections.unmodifiableList(variables);
}
public Sentence getQuantified() {
return quantified;
}
//
// START-Sentence
public String getSymbolicName() {
return getQuantifier();
}
public boolean isCompound() {
return true;
}
public List getArgs() {
return Collections.unmodifiableList(args);
}
public Object accept(FOLVisitor v, Object arg) {
return v.visitQuantifiedSentence(this, arg);
}
public QuantifiedSentence copy() {
List copyVars = new ArrayList();
for (Variable v : variables) {
copyVars.add(v.copy());
}
return new QuantifiedSentence(quantifier, copyVars, quantified.copy());
}
// END-Sentence
//
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if ((o == null) || (this.getClass() != o.getClass())) {
return false;
}
QuantifiedSentence cs = (QuantifiedSentence) o;
return cs.quantifier.equals(quantifier)
&& cs.variables.equals(variables)
&& cs.quantified.equals(quantified);
}
@Override
public int hashCode() {
if (0 == hashCode) {
hashCode = 17;
hashCode = 37 * hashCode + quantifier.hashCode();
for (Variable v : variables) {
hashCode = 37 * hashCode + v.hashCode();
}
hashCode = hashCode * 37 + quantified.hashCode();
}
return hashCode;
}
@Override
public String toString() {
if (null == stringRep) {
StringBuilder sb = new StringBuilder();
sb.append(quantifier);
sb.append(" ");
for (Variable v : variables) {
sb.append(v.toString());
sb.append(" ");
}
sb.append(quantified.toString());
stringRep = sb.toString();
}
return stringRep;
}
}