001    package aima.test.logictest.foltest;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    
006    import junit.framework.TestCase;
007    import aima.logic.fol.domain.FOLDomain;
008    import aima.logic.fol.inference.Demodulation;
009    import aima.logic.fol.kb.data.Clause;
010    import aima.logic.fol.kb.data.Literal;
011    import aima.logic.fol.parsing.FOLParser;
012    import aima.logic.fol.parsing.ast.Predicate;
013    import aima.logic.fol.parsing.ast.TermEquality;
014    
015    /**
016     * @author Ciaran O'Reilly
017     * 
018     */
019    public class DemodulationTest extends TestCase {
020            
021            private Demodulation demodulation = null;
022    
023            public void setUp() {
024                    demodulation = new Demodulation();
025            }
026            
027            // Note: Based on:
028            // http://logic.stanford.edu/classes/cs157/2008/lectures/lecture15.pdf
029            // Slide 22.
030            public void testSimpleAtomicExamples() {
031                    FOLDomain domain = new FOLDomain();
032                    domain.addConstant("A");
033                    domain.addConstant("B");
034                    domain.addConstant("C");
035                    domain.addConstant("D");
036                    domain.addConstant("E");
037                    domain.addPredicate("P");
038                    domain.addFunction("F");
039                    domain.addFunction("G");
040                    domain.addFunction("H");
041                    domain.addFunction("J");
042    
043                    FOLParser parser = new FOLParser(domain);
044                    
045                    Predicate expression = (Predicate) parser
046                                    .parse("P(A,F(B,G(A,H(B)),C),D)");
047                    TermEquality assertion = (TermEquality) parser.parse("B = E");
048                    
049                    Predicate altExpression = (Predicate) demodulation.apply(assertion,
050                                    expression);
051                    
052                    assertFalse(expression.equals(altExpression));
053                    assertEquals("P(A,F(E,G(A,H(B)),C),D)", altExpression.toString());
054                    
055                    altExpression = (Predicate) demodulation
056                                    .apply(assertion, altExpression);
057    
058                    assertEquals("P(A,F(E,G(A,H(E)),C),D)", altExpression.toString());
059    
060                    assertion = (TermEquality) parser.parse("G(x,y) = J(x)");
061    
062                    altExpression = (Predicate) demodulation.apply(assertion, expression);
063    
064                    assertEquals("P(A,F(B,J(A),C),D)", altExpression.toString());
065            }
066            
067            // Note: Based on:
068            // http://logic.stanford.edu/classes/cs157/2008/lectures/lecture15.pdf
069            // Slide 23.
070            public void testSimpleAtomicNonExample() {
071                    FOLDomain domain = new FOLDomain();
072                    domain.addConstant("A");
073                    domain.addConstant("B");
074                    domain.addConstant("C");
075                    domain.addConstant("D");
076                    domain.addConstant("E");
077                    domain.addPredicate("P");
078                    domain.addFunction("F");
079                    domain.addFunction("G");
080                    domain.addFunction("H");
081                    domain.addFunction("J");
082    
083                    FOLParser parser = new FOLParser(domain);
084    
085                    Predicate expression = (Predicate) parser.parse("P(A,G(x,B),C)");
086                    TermEquality assertion = (TermEquality) parser.parse("G(A,y) = J(y)");
087    
088                    Predicate altExpression = (Predicate) demodulation.apply(assertion,
089                                    expression);
090    
091                    assertNull(altExpression);
092            }
093            
094            public void testSimpleClauseExamples() {
095                    FOLDomain domain = new FOLDomain();
096                    domain.addConstant("A");
097                    domain.addConstant("B");
098                    domain.addConstant("C");
099                    domain.addConstant("D");
100                    domain.addConstant("E");
101                    domain.addPredicate("P");
102                    domain.addPredicate("Q");
103                    domain.addPredicate("W");
104                    domain.addFunction("F");
105                    domain.addFunction("G");
106                    domain.addFunction("H");
107                    domain.addFunction("J");
108    
109                    FOLParser parser = new FOLParser(domain);
110    
111                    List<Literal> lits = new ArrayList<Literal>();
112                    Predicate p1 = (Predicate) parser.parse("Q(z, G(D,B))");
113                    Predicate p2 = (Predicate) parser.parse("P(x, G(A,C))");
114                    Predicate p3 = (Predicate) parser.parse("W(z,x,u,w,y)");
115                    lits.add(new Literal(p1));
116                    lits.add(new Literal(p2));
117                    lits.add(new Literal(p3));
118    
119                    Clause clExpression = new Clause(lits);
120    
121                    TermEquality assertion = (TermEquality) parser.parse("G(x,y) = x");
122    
123                    Clause altClExpression = demodulation.apply(assertion, clExpression);
124                    
125                    assertEquals("[P(x,G(A,C)), Q(z,D), W(z,x,u,w,y)]", altClExpression
126                                    .toString());
127    
128                    altClExpression = demodulation.apply(assertion, altClExpression);
129    
130                    assertEquals("[P(x,A), Q(z,D), W(z,x,u,w,y)]", altClExpression
131                                    .toString());
132            }
133    
134            public void testSimpleClauseNonExample() {
135                    FOLDomain domain = new FOLDomain();
136                    domain.addConstant("A");
137                    domain.addConstant("B");
138                    domain.addConstant("C");
139                    domain.addPredicate("P");
140                    domain.addFunction("F");
141    
142                    FOLParser parser = new FOLParser(domain);
143    
144                    List<Literal> lits = new ArrayList<Literal>();
145                    Predicate p1 = (Predicate) parser.parse("P(y, F(A,y))");
146                    lits.add(new Literal(p1));
147    
148                    Clause clExpression = new Clause(lits);
149    
150                    TermEquality assertion = (TermEquality) parser.parse("F(x,B) = C");
151    
152                    Clause altClExpression = demodulation.apply(assertion, clExpression);
153    
154                    assertNull(altClExpression);
155            }
156            
157            public void testBypassReflexivityAxiom() {
158                    FOLDomain domain = new FOLDomain();
159                    domain.addConstant("A");
160                    domain.addConstant("B");
161                    domain.addConstant("C");
162                    domain.addPredicate("P");
163                    domain.addFunction("F");
164    
165                    FOLParser parser = new FOLParser(domain);
166    
167                    List<Literal> lits = new ArrayList<Literal>();
168                    Predicate p1 = (Predicate) parser.parse("P(y, F(A,y))");
169                    lits.add(new Literal(p1));
170    
171                    Clause clExpression = new Clause(lits);
172    
173                    TermEquality assertion = (TermEquality) parser.parse("x = x");
174    
175                    Clause altClExpression = demodulation.apply(assertion, clExpression);
176    
177                    assertNull(altClExpression);
178            }
179    }