001    /*
002     * Created on Dec 4, 2004
003     *
004     */
005    package aima.test.logictest.prop.visitors;
006    
007    import java.util.Set;
008    
009    import junit.framework.TestCase;
010    import aima.logic.propositional.parsing.PEParser;
011    import aima.logic.propositional.parsing.ast.Sentence;
012    import aima.logic.propositional.visitors.SymbolClassifier;
013    
014    /**
015     * @author Ravi Mohan
016     * 
017     */
018    
019    public class SymbolClassifierTest extends TestCase {
020            private SymbolClassifier classifier;
021    
022            private PEParser parser;
023    
024            @Override
025            public void setUp() {
026                    classifier = new SymbolClassifier();
027                    parser = new PEParser();
028    
029            }
030    
031            public void testSimpleNegativeSymbol() {
032                    Sentence sentence = (Sentence) parser.parse("(NOT B)");
033    
034                    Set neg = classifier.getNegativeSymbolsIn(sentence);
035                    Set pos = classifier.getPositiveSymbolsIn(sentence);
036    
037                    Set pureNeg = classifier.getPureNegativeSymbolsIn(sentence);
038                    Set purePos = classifier.getPurePositiveSymbolsIn(sentence);
039    
040                    Set pure = classifier.getPureSymbolsIn(sentence);
041                    Set impure = classifier.getImpureSymbolsIn(sentence);
042    
043                    Sentence b = (Sentence) parser.parse("B");
044    
045                    assertEquals(1, neg.size());
046                    assertTrue(neg.contains(b));
047    
048                    assertEquals(0, pos.size());
049    
050                    assertEquals(1, pureNeg.size());
051                    assertTrue(pureNeg.contains(b));
052    
053                    assertEquals(0, purePos.size());
054    
055                    assertEquals(1, pure.size());
056                    assertTrue(pure.contains(b));
057    
058                    assertEquals(0, impure.size());
059            }
060    
061            public void testSimplePositiveSymbol() {
062                    Sentence sentence = (Sentence) parser.parse("B");
063                    Set neg = classifier.getNegativeSymbolsIn(sentence);
064                    Set pos = classifier.getPositiveSymbolsIn(sentence);
065    
066                    Set pureNeg = classifier.getPureNegativeSymbolsIn(sentence);
067                    Set purePos = classifier.getPurePositiveSymbolsIn(sentence);
068    
069                    Set pure = classifier.getPureSymbolsIn(sentence);
070                    Set impure = classifier.getImpureSymbolsIn(sentence);
071    
072                    assertEquals(0, neg.size());
073    
074                    assertEquals(1, pos.size());
075                    Sentence b = (Sentence) parser.parse("B");
076                    assertTrue(pos.contains(b));
077    
078                    assertEquals(1, purePos.size());
079                    assertTrue(purePos.contains(b));
080    
081                    assertEquals(0, pureNeg.size());
082                    assertEquals(1, pure.size());
083    
084                    assertTrue(pure.contains(b));
085    
086                    assertEquals(0, impure.size());
087            }
088    
089            public void testSingleSymbolPositiveAndNegative() {
090                    Sentence sentence = (Sentence) parser.parse("(B AND (NOT B))");
091                    Set neg = classifier.getNegativeSymbolsIn(sentence);
092                    Set pos = classifier.getPositiveSymbolsIn(sentence);
093    
094                    Set pureNeg = classifier.getPureNegativeSymbolsIn(sentence);
095                    Set purePos = classifier.getPurePositiveSymbolsIn(sentence);
096    
097                    Set pure = classifier.getPureSymbolsIn(sentence);
098                    Set impure = classifier.getImpureSymbolsIn(sentence);
099    
100                    Sentence b = (Sentence) parser.parse("B");
101    
102                    assertEquals(1, neg.size());
103                    assertTrue(neg.contains(b));
104    
105                    assertEquals(1, pos.size());
106                    assertTrue(pos.contains(b));
107    
108                    assertEquals(0, pureNeg.size());
109                    assertEquals(0, purePos.size());
110                    assertEquals(0, pure.size());
111                    assertEquals(1, impure.size());
112            }
113    
114            public void testAIMAExample() {
115                    // 2nd Edition Pg 221
116                    Sentence sentence = (Sentence) parser
117                                    .parse("(((A OR (NOT B)) AND ((NOT B) OR (NOT C))) AND (C OR A))");
118    
119                    Set neg = classifier.getNegativeSymbolsIn(sentence);
120                    Set pos = classifier.getPositiveSymbolsIn(sentence);
121    
122                    Set pureNeg = classifier.getPureNegativeSymbolsIn(sentence);
123                    Set purePos = classifier.getPurePositiveSymbolsIn(sentence);
124    
125                    Set pure = classifier.getPureSymbolsIn(sentence);
126                    Set impure = classifier.getImpureSymbolsIn(sentence);
127    
128                    Sentence a = (Sentence) parser.parse("A");
129                    Sentence b = (Sentence) parser.parse("B");
130                    Sentence c = (Sentence) parser.parse("C");
131    
132                    assertEquals(2, neg.size());
133                    assertTrue(neg.contains(b));
134                    assertTrue(neg.contains(c));
135    
136                    assertEquals(2, pos.size());
137                    assertTrue(pos.contains(a));
138                    assertTrue(pos.contains(c));
139    
140                    assertEquals(1, pureNeg.size());
141                    assertTrue(pureNeg.contains(b));
142    
143                    assertEquals(1, purePos.size());
144                    assertTrue(purePos.contains(a));
145    
146                    assertEquals(2, pure.size());
147                    assertTrue(pure.contains(a));
148                    assertTrue(pure.contains(b));
149    
150                    assertEquals(1, impure.size());
151                    assertTrue(impure.contains(c));
152            }
153    
154    }