001    /*
002     * Created on Sep 22, 2004
003     *
004     */
005    package aima.logic.fol;
006    
007    import java.util.ArrayList;
008    import java.util.List;
009    
010    import aima.logic.fol.parsing.FOLVisitor;
011    import aima.logic.fol.parsing.ast.ConnectedSentence;
012    import aima.logic.fol.parsing.ast.Constant;
013    import aima.logic.fol.parsing.ast.Function;
014    import aima.logic.fol.parsing.ast.NotSentence;
015    import aima.logic.fol.parsing.ast.Predicate;
016    import aima.logic.fol.parsing.ast.QuantifiedSentence;
017    import aima.logic.fol.parsing.ast.Sentence;
018    import aima.logic.fol.parsing.ast.TermEquality;
019    import aima.logic.fol.parsing.ast.Variable;
020    
021    /**
022     * @author Ravi Mohan
023     * 
024     */
025    public class PredicateCollector implements FOLVisitor {
026    
027            public PredicateCollector() {
028    
029            }
030    
031            @SuppressWarnings("unchecked")
032            public List<Predicate> getPredicates(Sentence s) {
033                    return (List<Predicate>) s.accept(this, new ArrayList<Predicate>());
034            }
035    
036            @SuppressWarnings("unchecked")
037            public Object visitPredicate(Predicate p, Object arg) {
038                    List<Predicate> predicates = (List<Predicate>) arg;
039                    predicates.add(p);
040                    return predicates;
041            }
042    
043            public Object visitTermEquality(TermEquality equality, Object arg) {
044                    return arg;
045            }
046    
047            public Object visitVariable(Variable variable, Object arg) {
048                    return arg;
049            }
050    
051            public Object visitConstant(Constant constant, Object arg) {
052                    return arg;
053            }
054    
055            public Object visitFunction(Function function, Object arg) {
056                    return arg;
057            }
058    
059            public Object visitNotSentence(NotSentence sentence, Object arg) {
060                    sentence.getNegated().accept(this, arg);
061                    return arg;
062            }
063    
064            public Object visitConnectedSentence(ConnectedSentence sentence, Object arg) {
065                    sentence.getFirst().accept(this, arg);
066                    sentence.getSecond().accept(this, arg);
067                    return arg;
068            }
069    
070            public Object visitQuantifiedSentence(QuantifiedSentence sentence,
071                            Object arg) {
072                    sentence.getQuantified().accept(this, arg);
073                    return arg;
074            }
075    }