001    /*
002     * Created on Sep 20, 2004
003     *
004     */
005    package aima.test.logictest.foltest;
006    
007    import java.util.LinkedHashMap;
008    import java.util.Map;
009    
010    import junit.framework.TestCase;
011    import aima.logic.fol.SubstVisitor;
012    import aima.logic.fol.domain.DomainFactory;
013    import aima.logic.fol.parsing.FOLParser;
014    import aima.logic.fol.parsing.ast.Constant;
015    import aima.logic.fol.parsing.ast.Sentence;
016    import aima.logic.fol.parsing.ast.Term;
017    import aima.logic.fol.parsing.ast.Variable;
018    
019    /**
020     * @author Ravi Mohan
021     * 
022     */
023    public class FOLSubstTest extends TestCase {
024            
025            private FOLParser parser;
026            private SubstVisitor sv;
027    
028            @Override
029            public void setUp() {
030                    parser = new FOLParser(DomainFactory.crusadesDomain());
031                    sv = new SubstVisitor();
032            }
033    
034            public void testSubstSingleVariableSucceedsWithPredicate() {
035                    Sentence beforeSubst = parser.parse("King(x)");
036                    Sentence expectedAfterSubst = parser.parse(" King(John) ");
037                    Sentence expectedAfterSubstCopy = (Sentence) expectedAfterSubst.copy();
038    
039                    assertEquals(expectedAfterSubst, expectedAfterSubstCopy);
040                    Map<Variable, Term> p = new LinkedHashMap<Variable, Term>();
041                    p.put(new Variable("x"), new Constant("John"));
042    
043                    Sentence afterSubst = sv.subst(p, beforeSubst);
044                    assertEquals(expectedAfterSubst, afterSubst);
045                    assertEquals(beforeSubst, parser.parse("King(x)"));
046            }
047    
048            public void testSubstSingleVariableFailsWithPredicate() {
049                    Sentence beforeSubst = parser.parse("King(x)");
050                    Sentence expectedAfterSubst = parser.parse(" King(x) ");
051                    
052                    Map<Variable, Term> p = new LinkedHashMap<Variable, Term>();
053                    p.put(new Variable("y"), new Constant("John"));
054    
055                    Sentence afterSubst = sv.subst(p, beforeSubst);
056                    assertEquals(expectedAfterSubst, afterSubst);
057                    assertEquals(beforeSubst, parser.parse("King(x)"));
058            }
059    
060            public void testMultipleVariableSubstitutionWithPredicate() {
061                    Sentence beforeSubst = parser.parse("King(x,y)");
062                    Sentence expectedAfterSubst = parser.parse(" King(John ,England) ");
063    
064                    Map<Variable, Term> p = new LinkedHashMap<Variable, Term>();
065                    p.put(new Variable("x"), new Constant("John"));
066                    p.put(new Variable("y"), new Constant("England"));
067    
068                    Sentence afterSubst = sv.subst(p, beforeSubst);
069                    assertEquals(expectedAfterSubst, afterSubst);
070                    assertEquals(beforeSubst, parser.parse("King(x,y)"));
071            }
072    
073            public void testMultipleVariablePartiallySucceedsWithPredicate() {
074                    Sentence beforeSubst = parser.parse("King(x,y)");
075                    Sentence expectedAfterSubst = parser.parse(" King(John ,y) ");
076    
077                    Map<Variable, Term> p = new LinkedHashMap<Variable, Term>();
078                    p.put(new Variable("x"), new Constant("John"));
079                    p.put(new Variable("z"), new Constant("England"));
080    
081                    Sentence afterSubst = sv.subst(p, beforeSubst);
082                    assertEquals(expectedAfterSubst, afterSubst);
083                    assertEquals(beforeSubst, parser.parse("King(x,y)"));
084            }
085    
086            public void testSubstSingleVariableSucceedsWithTermEquality() {
087                    Sentence beforeSubst = parser.parse("BrotherOf(x) = EnemyOf(y)");
088                    Sentence expectedAfterSubst = parser
089                                    .parse("BrotherOf(John) = EnemyOf(Saladin)");
090    
091                    Map<Variable, Term> p = new LinkedHashMap<Variable, Term>();
092                    p.put(new Variable("x"), new Constant("John"));
093                    p.put(new Variable("y"), new Constant("Saladin"));
094    
095                    Sentence afterSubst = sv.subst(p, beforeSubst);
096                    assertEquals(expectedAfterSubst, afterSubst);
097                    assertEquals(beforeSubst, parser.parse("BrotherOf(x) = EnemyOf(y)"));
098            }
099    
100            public void testSubstSingleVariableSucceedsWithTermEquality2() {
101                    Sentence beforeSubst = parser.parse("BrotherOf(John) = x)");
102                    Sentence expectedAfterSubst = parser.parse("BrotherOf(John) = Richard");
103    
104                    Map<Variable, Term> p = new LinkedHashMap<Variable, Term>();
105                    p.put(new Variable("x"), new Constant("Richard"));
106                    p.put(new Variable("y"), new Constant("Saladin"));
107    
108                    Sentence afterSubst = sv.subst(p, beforeSubst);
109                    assertEquals(expectedAfterSubst, afterSubst);
110                    assertEquals(parser.parse("BrotherOf(John) = x)"), beforeSubst);
111            }
112    
113            public void testSubstWithUniversalQuantifierAndSngleVariable() {
114                    Sentence beforeSubst = parser.parse("FORALL x King(x))");
115                    Sentence expectedAfterSubst = parser.parse("King(John)");
116    
117                    Map<Variable, Term> p = new LinkedHashMap<Variable, Term>();
118                    p.put(new Variable("x"), new Constant("John"));
119    
120                    Sentence afterSubst = sv.subst(p, beforeSubst);
121                    assertEquals(expectedAfterSubst, afterSubst);
122                    assertEquals(parser.parse("FORALL x King(x))"), beforeSubst);
123            }
124    
125            public void testSubstWithUniversalQuantifierAndZeroVariablesMatched() {
126                    Sentence beforeSubst = parser.parse("FORALL x King(x))");
127                    Sentence expectedAfterSubst = parser.parse("FORALL x King(x)");
128    
129                    Map<Variable, Term> p = new LinkedHashMap<Variable, Term>();
130                    p.put(new Variable("y"), new Constant("John"));
131    
132                    Sentence afterSubst = sv.subst(p, beforeSubst);
133                    assertEquals(expectedAfterSubst, afterSubst);
134                    assertEquals(parser.parse("FORALL x King(x))"), beforeSubst);
135            }
136    
137            public void testSubstWithUniversalQuantifierAndOneOfTwoVariablesMatched() {
138                    Sentence beforeSubst = parser.parse("FORALL x,y King(x,y))");
139                    Sentence expectedAfterSubst = parser.parse("FORALL x King(x,John)");
140    
141                    Map<Variable, Term> p = new LinkedHashMap<Variable, Term>();
142                    p.put(new Variable("y"), new Constant("John"));
143    
144                    Sentence afterSubst = sv.subst(p, beforeSubst);
145                    assertEquals(expectedAfterSubst, afterSubst);
146                    assertEquals(parser.parse("FORALL x,y King(x,y))"), beforeSubst);
147            }
148    
149            public void testSubstWithExistentialQuantifierAndSngleVariable() {
150                    Sentence beforeSubst = parser.parse("EXISTS x King(x))");
151                    Sentence expectedAfterSubst = parser.parse("King(John)");
152    
153                    Map<Variable, Term> p = new LinkedHashMap<Variable, Term>();
154                    p.put(new Variable("x"), new Constant("John"));
155    
156                    Sentence afterSubst = sv.subst(p, beforeSubst);
157                    
158                    assertEquals(expectedAfterSubst, afterSubst);
159                    assertEquals(parser.parse("EXISTS x King(x)"), beforeSubst);
160            }
161    
162            public void testSubstWithNOTSentenceAndSngleVariable() {
163                    Sentence beforeSubst = parser.parse("NOT King(x))");
164                    Sentence expectedAfterSubst = parser.parse("NOT King(John)");
165    
166                    Map<Variable, Term> p = new LinkedHashMap<Variable, Term>();
167                    p.put(new Variable("x"), new Constant("John"));
168    
169                    Sentence afterSubst = sv.subst(p, beforeSubst);
170                    assertEquals(expectedAfterSubst, afterSubst);
171                    assertEquals(parser.parse("NOT King(x))"), beforeSubst);
172            }
173    
174            public void testConnectiveANDSentenceAndSngleVariable() {
175                    Sentence beforeSubst = parser
176                                    .parse("EXISTS x ( King(x) AND BrotherOf(x) = EnemyOf(y) )");
177                    Sentence expectedAfterSubst = parser
178                                    .parse("( King(John) AND BrotherOf(John) = EnemyOf(Saladin) )");
179    
180                    Map<Variable, Term> p = new LinkedHashMap<Variable, Term>();
181                    p.put(new Variable("x"), new Constant("John"));
182                    p.put(new Variable("y"), new Constant("Saladin"));
183    
184                    Sentence afterSubst = sv.subst(p, beforeSubst);
185                    assertEquals(expectedAfterSubst, afterSubst);
186                    assertEquals(parser
187                                    .parse("EXISTS x ( King(x) AND BrotherOf(x) = EnemyOf(y) )"),
188                                    beforeSubst);
189            }
190    
191            public void testParanthisedSingleVariable() {
192                    Sentence beforeSubst = parser.parse("((( King(x))))");
193                    Sentence expectedAfterSubst = parser.parse("King(John) ");
194    
195                    Map<Variable, Term> p = new LinkedHashMap<Variable, Term>();
196                    p.put(new Variable("x"), new Constant("John"));
197    
198                    Sentence afterSubst = sv.subst(p, beforeSubst);
199                    assertEquals(expectedAfterSubst, afterSubst);
200                    assertEquals(parser.parse("((( King(x))))"), beforeSubst);
201            }
202    }