001    /*
002     * Created on Sep 15, 2003 by Ravi Mohan
003     *
004     */
005    package aima.test.logictest.prop.parser;
006    
007    import junit.framework.TestCase;
008    import aima.logic.propositional.parsing.PEParser;
009    import aima.logic.propositional.parsing.ast.AtomicSentence;
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    
017    /**
018     * @author Ravi Mohan
019     * 
020     */
021    
022    public class PEParserTest extends TestCase {
023            private PEParser parser;
024    
025            @Override
026            public void setUp() {
027                    parser = new PEParser();
028            }
029    
030            public void testAtomicSentenceTrueParse() {
031                    AtomicSentence sen = (AtomicSentence) parser.parse("true");
032                    assertEquals(TrueSentence.class, sen.getClass());
033                    sen = (AtomicSentence) parser.parse("(true)");
034                    assertEquals(TrueSentence.class, sen.getClass());
035                    sen = (AtomicSentence) parser.parse("((true))");
036                    assertEquals(TrueSentence.class, sen.getClass());
037            }
038    
039            public void testAtomicSentenceFalseParse() {
040                    AtomicSentence sen = (AtomicSentence) parser.parse("faLse");
041                    assertEquals(FalseSentence.class, sen.getClass());
042            }
043    
044            public void testAtomicSentenceSymbolParse() {
045                    AtomicSentence sen = (AtomicSentence) parser.parse("AIMA");
046                    assertEquals(Symbol.class, sen.getClass());
047            }
048    
049            public void testNotSentenceParse() {
050                    UnarySentence sen = (UnarySentence) parser.parse("NOT AIMA");
051                    assertEquals(UnarySentence.class, sen.getClass());
052            }
053    
054            public void testBinarySentenceParse() {
055                    BinarySentence sen = (BinarySentence) parser
056                                    .parse("(PETER  AND  NORVIG)");
057                    assertEquals(BinarySentence.class, sen.getClass());
058            }
059    
060            public void testMultiSentenceAndParse() {
061                    MultiSentence sen = (MultiSentence) parser
062                                    .parse("(AND  NORVIG AIMA LISP)");
063                    assertEquals(MultiSentence.class, sen.getClass());
064            }
065    
066            public void testMultiSentenceOrParse() {
067                    MultiSentence sen = (MultiSentence) parser
068                                    .parse("(OR  NORVIG AIMA LISP)");
069                    assertEquals(MultiSentence.class, sen.getClass());
070            }
071    
072            public void testMultiSentenceBracketedParse() {
073                    MultiSentence sen = (MultiSentence) parser
074                                    .parse("((OR  NORVIG AIMA LISP))");
075                    assertEquals(MultiSentence.class, sen.getClass());
076            }
077    
078            public void testComplexSentenceParse() {
079                    BinarySentence sen = (BinarySentence) parser
080                                    .parse("((OR  NORVIG AIMA LISP) AND TRUE)");
081                    assertEquals(BinarySentence.class, sen.getClass());
082    
083                    sen = (BinarySentence) parser
084                                    .parse("((OR  NORVIG AIMA LISP) AND (((LISP => COOL))))");
085                    assertEquals(BinarySentence.class, sen.getClass());
086                    assertEquals(" ( ( OR NORVIG AIMA LISP  )  AND  ( LISP => COOL ) )",
087                                    sen.toString());
088    
089                    String s = "((NOT (P AND Q ))  AND ((NOT (R AND S))))";
090                    sen = (BinarySentence) parser.parse(s);
091                    assertEquals(" (  ( NOT  ( P AND Q ) )  AND  ( NOT  ( R AND S ) )  )",
092                                    sen.toString());
093    
094                    s = "((P AND Q) OR (S AND T))";
095                    sen = (BinarySentence) parser.parse(s);
096                    assertEquals(" (  ( P AND Q ) OR  ( S AND T ) )", sen.toString());
097                    assertEquals("OR", sen.getOperator());
098    
099                    s = "(NOT ((P AND Q) => (S AND T)))";
100                    UnarySentence nsen = (UnarySentence) parser.parse(s);
101                    // assertEquals("=>",sen.getOperator());
102                    s = "(NOT (P <=> (S AND T)))";
103                    nsen = (UnarySentence) parser.parse(s);
104                    assertEquals(" ( NOT  ( P <=>  ( S AND T ) ) ) ", nsen.toString());
105    
106                    s = "(P <=> (S AND T))";
107                    sen = (BinarySentence) parser.parse(s);
108    
109                    s = "(P => Q)";
110                    sen = (BinarySentence) parser.parse(s);
111    
112                    s = "((P AND Q) => R)";
113                    sen = (BinarySentence) parser.parse(s);
114    
115            }
116    }