001 /* 002 * Created on Sep 20, 2004 003 * 004 */ 005 package aima.logic.fol; 006 007 import java.util.LinkedHashSet; 008 import java.util.Set; 009 010 import aima.logic.fol.kb.data.Chain; 011 import aima.logic.fol.kb.data.Clause; 012 import aima.logic.fol.kb.data.Literal; 013 import aima.logic.fol.parsing.FOLVisitor; 014 import aima.logic.fol.parsing.ast.ConnectedSentence; 015 import aima.logic.fol.parsing.ast.Constant; 016 import aima.logic.fol.parsing.ast.Function; 017 import aima.logic.fol.parsing.ast.NotSentence; 018 import aima.logic.fol.parsing.ast.Predicate; 019 import aima.logic.fol.parsing.ast.QuantifiedSentence; 020 import aima.logic.fol.parsing.ast.Sentence; 021 import aima.logic.fol.parsing.ast.Term; 022 import aima.logic.fol.parsing.ast.TermEquality; 023 import aima.logic.fol.parsing.ast.Variable; 024 025 /** 026 * @author Ravi Mohan 027 * @author Ciaran O'Reilly 028 */ 029 public class VariableCollector implements FOLVisitor { 030 031 public VariableCollector() { 032 } 033 034 // Note: The set guarantees the order in which they were 035 // found. 036 public Set<Variable> collectAllVariables(Sentence sentence) { 037 Set<Variable> variables = new LinkedHashSet<Variable>(); 038 039 sentence.accept(this, variables); 040 041 return variables; 042 } 043 044 public Set<Variable> collectAllVariables(Term aTerm) { 045 Set<Variable> variables = new LinkedHashSet<Variable>(); 046 047 aTerm.accept(this, variables); 048 049 return variables; 050 } 051 052 public Set<Variable> collectAllVariables(Clause aClause) { 053 Set<Variable> variables = new LinkedHashSet<Variable>(); 054 055 for (Literal l : aClause.getLiterals()) { 056 l.getAtomicSentence().accept(this, variables); 057 } 058 059 return variables; 060 } 061 062 public Set<Variable> collectAllVariables(Chain aChain) { 063 Set<Variable> variables = new LinkedHashSet<Variable>(); 064 065 for (Literal l : aChain.getLiterals()) { 066 l.getAtomicSentence().accept(this, variables); 067 } 068 069 return variables; 070 } 071 072 @SuppressWarnings("unchecked") 073 public Object visitVariable(Variable var, Object arg) { 074 Set<Variable> variables = (Set<Variable>) arg; 075 variables.add(var); 076 return var; 077 } 078 079 @SuppressWarnings("unchecked") 080 public Object visitQuantifiedSentence(QuantifiedSentence sentence, 081 Object arg) { 082 // Ensure I collect quantified variables too 083 Set<Variable> variables = (Set<Variable>) arg; 084 variables.addAll(sentence.getVariables()); 085 086 sentence.getQuantified().accept(this, arg); 087 088 return sentence; 089 } 090 091 public Object visitPredicate(Predicate predicate, Object arg) { 092 for (Term t : predicate.getTerms()) { 093 t.accept(this, arg); 094 } 095 return predicate; 096 } 097 098 public Object visitTermEquality(TermEquality equality, Object arg) { 099 equality.getTerm1().accept(this, arg); 100 equality.getTerm2().accept(this, arg); 101 return equality; 102 } 103 104 public Object visitConstant(Constant constant, Object arg) { 105 return constant; 106 } 107 108 public Object visitFunction(Function function, Object arg) { 109 for (Term t : function.getTerms()) { 110 t.accept(this, arg); 111 } 112 return function; 113 } 114 115 public Object visitNotSentence(NotSentence sentence, Object arg) { 116 sentence.getNegated().accept(this, arg); 117 return sentence; 118 } 119 120 public Object visitConnectedSentence(ConnectedSentence sentence, Object arg) { 121 sentence.getFirst().accept(this, arg); 122 sentence.getSecond().accept(this, arg); 123 return sentence; 124 } 125 }