aima.core.logic.fol.VariableCollector 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.
package aima.core.logic.fol;
import java.util.LinkedHashSet;
import java.util.Set;
import aima.core.logic.fol.kb.data.Chain;
import aima.core.logic.fol.kb.data.Clause;
import aima.core.logic.fol.kb.data.Literal;
import aima.core.logic.fol.parsing.FOLVisitor;
import aima.core.logic.fol.parsing.ast.ConnectedSentence;
import aima.core.logic.fol.parsing.ast.Constant;
import aima.core.logic.fol.parsing.ast.Function;
import aima.core.logic.fol.parsing.ast.NotSentence;
import aima.core.logic.fol.parsing.ast.Predicate;
import aima.core.logic.fol.parsing.ast.QuantifiedSentence;
import aima.core.logic.fol.parsing.ast.Sentence;
import aima.core.logic.fol.parsing.ast.Term;
import aima.core.logic.fol.parsing.ast.TermEquality;
import aima.core.logic.fol.parsing.ast.Variable;
/**
* @author Ravi Mohan
* @author Ciaran O'Reilly
*/
public class VariableCollector implements FOLVisitor {
public VariableCollector() {
}
// Note: The set guarantees the order in which they were
// found.
public Set collectAllVariables(Sentence sentence) {
Set variables = new LinkedHashSet();
sentence.accept(this, variables);
return variables;
}
public Set collectAllVariables(Term term) {
Set variables = new LinkedHashSet();
term.accept(this, variables);
return variables;
}
public Set collectAllVariables(Clause clause) {
Set variables = new LinkedHashSet();
for (Literal l : clause.getLiterals()) {
l.getAtomicSentence().accept(this, variables);
}
return variables;
}
public Set collectAllVariables(Chain chain) {
Set variables = new LinkedHashSet();
for (Literal l : chain.getLiterals()) {
l.getAtomicSentence().accept(this, variables);
}
return variables;
}
@SuppressWarnings("unchecked")
public Object visitVariable(Variable var, Object arg) {
Set variables = (Set) arg;
variables.add(var);
return var;
}
@SuppressWarnings("unchecked")
public Object visitQuantifiedSentence(QuantifiedSentence sentence,
Object arg) {
// Ensure I collect quantified variables too
Set variables = (Set) arg;
variables.addAll(sentence.getVariables());
sentence.getQuantified().accept(this, arg);
return sentence;
}
public Object visitPredicate(Predicate predicate, Object arg) {
for (Term t : predicate.getTerms()) {
t.accept(this, arg);
}
return predicate;
}
public Object visitTermEquality(TermEquality equality, Object arg) {
equality.getTerm1().accept(this, arg);
equality.getTerm2().accept(this, arg);
return equality;
}
public Object visitConstant(Constant constant, Object arg) {
return constant;
}
public Object visitFunction(Function function, Object arg) {
for (Term t : function.getTerms()) {
t.accept(this, arg);
}
return function;
}
public Object visitNotSentence(NotSentence sentence, Object arg) {
sentence.getNegated().accept(this, arg);
return sentence;
}
public Object visitConnectedSentence(ConnectedSentence sentence, Object arg) {
sentence.getFirst().accept(this, arg);
sentence.getSecond().accept(this, arg);
return sentence;
}
}