001    /*
002     * Created on Dec 4, 2004
003     *
004     */
005    package aima.logic.propositional.visitors;
006    
007    import java.util.Set;
008    
009    import aima.logic.propositional.parsing.PLVisitor;
010    import aima.logic.propositional.parsing.ast.BinarySentence;
011    import aima.logic.propositional.parsing.ast.FalseSentence;
012    import aima.logic.propositional.parsing.ast.MultiSentence;
013    import aima.logic.propositional.parsing.ast.Symbol;
014    import aima.logic.propositional.parsing.ast.TrueSentence;
015    import aima.logic.propositional.parsing.ast.UnarySentence;
016    import aima.util.SetOps;
017    
018    /**
019     * @author Ravi Mohan
020     * 
021     */
022    
023    /*
024     * Super class of Visitors that are "read only" and gather information from an
025     * existing parse tree .
026     */
027    
028    public class BasicTraverser implements PLVisitor {
029    
030            public Object visitSymbol(Symbol s, Object arg) {
031                    return arg;
032            }
033    
034            public Object visitTrueSentence(TrueSentence ts, Object arg) {
035                    return arg;
036            }
037    
038            public Object visitFalseSentence(FalseSentence fs, Object arg) {
039                    return arg;
040            }
041    
042            public Object visitNotSentence(UnarySentence ns, Object arg) {
043                    Set s = (Set) arg;
044                    return new SetOps().union(s, (Set) ns.getNegated().accept(this, arg));
045            }
046    
047            public Object visitBinarySentence(BinarySentence bs, Object arg) {
048                    Set s = (Set) arg;
049                    Set termunion = new SetOps().union((Set) bs.getFirst()
050                                    .accept(this, arg), (Set) bs.getSecond().accept(this, arg));
051                    return new SetOps().union(s, termunion);
052            }
053    
054            public Object visitMultiSentence(MultiSentence fs, Object arg) {
055                    throw new RuntimeException("Can't handle MultiSentence");
056            }
057    
058    }