001    /*
002     * Created on Dec 4, 2004
003     *
004     */
005    package aima.logic.propositional.visitors;
006    
007    import java.util.HashSet;
008    import java.util.Set;
009    
010    import aima.logic.propositional.parsing.ast.Sentence;
011    import aima.logic.propositional.parsing.ast.Symbol;
012    import aima.logic.propositional.parsing.ast.UnarySentence;
013    import aima.util.SetOps;
014    
015    /**
016     * @author Ravi Mohan
017     * 
018     */
019    
020    public class PositiveSymbolCollector extends BasicTraverser {
021            @Override
022            public Object visitSymbol(Symbol symbol, Object arg) {
023                    Set<Symbol> s = (Set<Symbol>) arg;
024                    s.add(symbol);// add ALL symbols not discarded by the visitNotSentence
025                    // mathod
026                    return arg;
027            }
028    
029            @Override
030            public Object visitNotSentence(UnarySentence ns, Object arg) {
031                    Set<Symbol> s = (Set<Symbol>) arg;
032                    if (ns.getNegated() instanceof Symbol) {
033                            // do nothing .do NOT add a negated Symbol
034                    } else {
035                            s = new SetOps<Symbol>().union(s, (Set<Symbol>) ns.getNegated()
036                                            .accept(this, arg));
037                    }
038                    return s;
039            }
040    
041            public Set<Symbol> getPositiveSymbolsIn(Sentence sentence) {
042                    return (Set<Symbol>) sentence.accept(this, new HashSet<Symbol>());
043            }
044    }