001    package aima.logic.fol.parsing;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    
006    import aima.logic.fol.parsing.ast.ConnectedSentence;
007    import aima.logic.fol.parsing.ast.Constant;
008    import aima.logic.fol.parsing.ast.Function;
009    import aima.logic.fol.parsing.ast.NotSentence;
010    import aima.logic.fol.parsing.ast.Predicate;
011    import aima.logic.fol.parsing.ast.QuantifiedSentence;
012    import aima.logic.fol.parsing.ast.Sentence;
013    import aima.logic.fol.parsing.ast.Term;
014    import aima.logic.fol.parsing.ast.TermEquality;
015    import aima.logic.fol.parsing.ast.Variable;
016    
017    /**
018     * @author Ravi Mohan
019     * 
020     */
021    public class AbstractFOLVisitor implements FOLVisitor {
022    
023            public AbstractFOLVisitor() {
024            }
025    
026            protected Sentence recreate(Object ast) {
027                    return (Sentence) ((Sentence) ast).copy();
028            }
029    
030            public Object visitVariable(Variable variable, Object arg) {
031                    return variable.copy();
032            }
033    
034            public Object visitQuantifiedSentence(QuantifiedSentence sentence,
035                            Object arg) {
036                    List<Variable> variables = new ArrayList<Variable>();
037                    for (Variable var : sentence.getVariables()) {
038                            variables.add((Variable) var.accept(this, arg));
039                    }
040    
041                    return new QuantifiedSentence(sentence.getQuantifier(), variables,
042                                    (Sentence) sentence.getQuantified().accept(this, arg));
043            }
044    
045            public Object visitPredicate(Predicate predicate, Object arg) {
046                    List<Term> terms = predicate.getTerms();
047                    List<Term> newTerms = new ArrayList<Term>();
048                    for (int i = 0; i < terms.size(); i++) {
049                            Term t = (Term) terms.get(i);
050                            Term subsTerm = (Term) t.accept(this, arg);
051                            newTerms.add(subsTerm);
052                    }
053                    return new Predicate(predicate.getPredicateName(), newTerms);
054    
055            }
056    
057            public Object visitTermEquality(TermEquality equality, Object arg) {
058                    Term newTerm1 = (Term) equality.getTerm1().accept(this, arg);
059                    Term newTerm2 = (Term) equality.getTerm2().accept(this, arg);
060                    return new TermEquality(newTerm1, newTerm2);
061            }
062    
063            public Object visitConstant(Constant constant, Object arg) {
064                    return constant;
065            }
066    
067            public Object visitFunction(Function function, Object arg) {
068                    List<Term> terms = function.getTerms();
069                    List<Term> newTerms = new ArrayList<Term>();
070                    for (int i = 0; i < terms.size(); i++) {
071                            Term t = terms.get(i);
072                            Term subsTerm = (Term) t.accept(this, arg);
073                            newTerms.add(subsTerm);
074                    }
075                    return new Function(function.getFunctionName(), newTerms);
076            }
077    
078            public Object visitNotSentence(NotSentence sentence, Object arg) {
079                    return new NotSentence((Sentence) sentence.getNegated().accept(this,
080                                    arg));
081            }
082    
083            public Object visitConnectedSentence(ConnectedSentence sentence, Object arg) {
084                    Sentence substFirst = (Sentence) sentence.getFirst().accept(this, arg);
085                    Sentence substSecond = (Sentence) sentence.getSecond()
086                                    .accept(this, arg);
087                    return new ConnectedSentence(sentence.getConnector(), substFirst,
088                                    substSecond);
089            }
090    }