001    /*
002     * Created on Dec 5, 2004
003     *
004     */
005    package aima.logic.propositional.visitors;
006    
007    import aima.logic.propositional.parsing.PLVisitor;
008    import aima.logic.propositional.parsing.ast.BinarySentence;
009    import aima.logic.propositional.parsing.ast.FalseSentence;
010    import aima.logic.propositional.parsing.ast.MultiSentence;
011    import aima.logic.propositional.parsing.ast.Sentence;
012    import aima.logic.propositional.parsing.ast.Symbol;
013    import aima.logic.propositional.parsing.ast.TrueSentence;
014    import aima.logic.propositional.parsing.ast.UnarySentence;
015    
016    /**
017     * @author Ravi Mohan
018     * 
019     */
020    
021    public class AndDetector implements PLVisitor {
022    
023            public Object visitSymbol(Symbol s, Object arg) {
024    
025                    return new Boolean(false);
026            }
027    
028            public Object visitTrueSentence(TrueSentence ts, Object arg) {
029                    return new Boolean(false);
030            }
031    
032            public Object visitFalseSentence(FalseSentence fs, Object arg) {
033                    return new Boolean(false);
034            }
035    
036            public Object visitNotSentence(UnarySentence fs, Object arg) {
037                    return fs.getNegated().accept(this, null);
038            }
039    
040            public Object visitBinarySentence(BinarySentence fs, Object arg) {
041                    if (fs.isAndSentence()) {
042                            return new Boolean(true);
043                    } else {
044                            boolean first = ((Boolean) fs.getFirst().accept(this, null))
045                                            .booleanValue();
046                            boolean second = ((Boolean) fs.getSecond().accept(this, null))
047                                            .booleanValue();
048                            return new Boolean((first || second));
049                    }
050            }
051    
052            public Object visitMultiSentence(MultiSentence fs, Object arg) {
053                    throw new RuntimeException("can't handle multisentences");
054            }
055    
056            public boolean containsEmbeddedAnd(Sentence s) {
057                    return ((Boolean) s.accept(this, null)).booleanValue();
058            }
059    
060    }