001    /*
002     * Created on Sep 22, 2004
003     *
004     */
005    package aima.logic.fol.demos;
006    
007    import java.util.ArrayList;
008    import java.util.Hashtable;
009    import java.util.List;
010    import java.util.Map;
011    import java.util.Set;
012    
013    import aima.logic.fol.CNFConverter;
014    import aima.logic.fol.StandardizeApartIndexicalFactory;
015    import aima.logic.fol.Unifier;
016    import aima.logic.fol.domain.DomainFactory;
017    import aima.logic.fol.domain.FOLDomain;
018    import aima.logic.fol.inference.Demodulation;
019    import aima.logic.fol.inference.FOLBCAsk;
020    import aima.logic.fol.inference.FOLFCAsk;
021    import aima.logic.fol.inference.FOLModelElimination;
022    import aima.logic.fol.inference.FOLOTTERLikeTheoremProver;
023    import aima.logic.fol.inference.FOLTFMResolution;
024    import aima.logic.fol.inference.InferenceProcedure;
025    import aima.logic.fol.inference.InferenceResult;
026    import aima.logic.fol.inference.Paramodulation;
027    import aima.logic.fol.inference.proof.Proof;
028    import aima.logic.fol.inference.proof.ProofPrinter;
029    import aima.logic.fol.kb.FOLKnowledgeBase;
030    import aima.logic.fol.kb.FOLKnowledgeBaseFactory;
031    import aima.logic.fol.kb.data.CNF;
032    import aima.logic.fol.kb.data.Clause;
033    import aima.logic.fol.kb.data.Literal;
034    import aima.logic.fol.parsing.FOLParser;
035    import aima.logic.fol.parsing.ast.AtomicSentence;
036    import aima.logic.fol.parsing.ast.Constant;
037    import aima.logic.fol.parsing.ast.Predicate;
038    import aima.logic.fol.parsing.ast.Sentence;
039    import aima.logic.fol.parsing.ast.Term;
040    import aima.logic.fol.parsing.ast.TermEquality;
041    import aima.logic.fol.parsing.ast.Variable;
042    
043    /**
044     * @author Ravi Mohan
045     * @author Ciaran O'Reilly
046     */
047    public class FolDemo {
048            public static void main(String[] args) {
049                    unifierDemo();
050                    fOL_fcAskDemo();
051                    fOL_bcAskDemo();
052                    fOL_CNFConversion();
053                    fOL_TFMResolutionDemo();
054                    fOL_Demodulation();
055                    fOL_Paramodulation();
056                    fOL_OTTERDemo();
057                    fOL_ModelEliminationDemo();
058            }
059    
060            private static void unifierDemo() {
061                    FOLParser parser = new FOLParser(DomainFactory.knowsDomain());
062                    Unifier unifier = new Unifier();
063                    Map<Variable, Term> theta = new Hashtable<Variable, Term>();
064    
065                    Sentence query = parser.parse("Knows(John,x)");
066                    Sentence johnKnowsJane = parser.parse("Knows(y,Mother(y))");
067    
068                    System.out.println("------------");
069                    System.out.println("Unifier Demo");
070                    System.out.println("------------");
071                    Map<Variable, Term> subst = unifier.unify(query, johnKnowsJane, theta);
072                    System.out.println("Unify '" + query + "' with '" + johnKnowsJane
073                                    + "' to get the substitution " + subst + ".");
074                    System.out.println("");
075            }
076    
077            private static void fOL_fcAskDemo() {
078                    System.out.println("---------------------------");
079                    System.out.println("Forward Chain, Kings Demo 1");
080                    System.out.println("---------------------------");
081                    kingsDemo1(new FOLFCAsk());
082                    System.out.println("---------------------------");
083                    System.out.println("Forward Chain, Kings Demo 2");
084                    System.out.println("---------------------------");
085                    kingsDemo2(new FOLFCAsk());
086                    System.out.println("---------------------------");
087                    System.out.println("Forward Chain, Weapons Demo");
088                    System.out.println("---------------------------");
089                    weaponsDemo(new FOLFCAsk());
090            }
091    
092            private static void fOL_bcAskDemo() {
093                    System.out.println("----------------------------");
094                    System.out.println("Backward Chain, Kings Demo 1");
095                    System.out.println("----------------------------");
096                    kingsDemo1(new FOLBCAsk());
097                    System.out.println("----------------------------");
098                    System.out.println("Backward Chain, Kings Demo 2");
099                    System.out.println("----------------------------");
100                    kingsDemo2(new FOLBCAsk());
101                    System.out.println("----------------------------");
102                    System.out.println("Backward Chain, Weapons Demo");
103                    System.out.println("----------------------------");
104                    weaponsDemo(new FOLBCAsk());
105            }
106            
107            private static void fOL_CNFConversion() {
108                    System.out.println("-------------------------------------------------");
109                    System.out.println("Conjuctive Normal Form for First Order Logic Demo");
110                    System.out.println("-------------------------------------------------");
111                    FOLDomain domain = DomainFactory.lovesAnimalDomain();
112                    FOLParser parser = new FOLParser(domain);
113    
114                    Sentence origSentence = parser
115                                    .parse("FORALL x (FORALL y (Animal(y) => Loves(x, y)) => EXISTS y Loves(y, x))");
116    
117                    CNFConverter cnfConv = new CNFConverter(parser);
118    
119                    CNF cnf = cnfConv.convertToCNF(origSentence);
120    
121                    System.out.println("Convert '" + origSentence + "' to CNF.");
122                    System.out.println("CNF=" + cnf.toString());
123                    System.out.println("");
124            }
125    
126            private static void fOL_TFMResolutionDemo() {
127                    System.out.println("----------------------------");
128                    System.out.println("TFM Resolution, Kings Demo 1");
129                    System.out.println("----------------------------");
130                    kingsDemo1(new FOLTFMResolution());
131                    System.out.println("----------------------------");
132                    System.out.println("TFM Resolution, Kings Demo 2");
133                    System.out.println("----------------------------");
134                    kingsDemo2(new FOLTFMResolution());
135                    System.out.println("----------------------------");
136                    System.out.println("TFM Resolution, Weapons Demo");
137                    System.out.println("----------------------------");
138                    weaponsDemo(new FOLTFMResolution());
139                    System.out.println("---------------------------------");
140                    System.out.println("TFM Resolution, Loves Animal Demo");
141                    System.out.println("---------------------------------");
142                    lovesAnimalDemo(new FOLTFMResolution());
143                    System.out.println("---------------------------------------");
144                    System.out.println("TFM Resolution, ABC Equality Axiom Demo");
145                    System.out.println("---------------------------------------");
146                    abcEqualityAxiomDemo(new FOLTFMResolution());
147            }
148            
149            private static void fOL_Demodulation() {
150                    System.out.println("-----------------");
151                    System.out.println("Demodulation Demo");
152                    System.out.println("-----------------");
153                    FOLDomain domain = new FOLDomain();
154                    domain.addConstant("A");
155                    domain.addConstant("B");
156                    domain.addConstant("C");
157                    domain.addConstant("D");
158                    domain.addConstant("E");
159                    domain.addPredicate("P");
160                    domain.addFunction("F");
161                    domain.addFunction("G");
162                    domain.addFunction("H");
163                    domain.addFunction("J");
164    
165                    FOLParser parser = new FOLParser(domain);
166    
167                    Predicate expression = (Predicate) parser
168                                    .parse("P(A,F(B,G(A,H(B)),C),D)");
169                    TermEquality assertion = (TermEquality) parser.parse("B = E");
170    
171                    Demodulation demodulation = new Demodulation();
172                    Predicate altExpression = (Predicate) demodulation.apply(assertion,
173                                    expression);
174    
175                    System.out.println("Demodulate '" + expression + "' with '" + assertion
176                                    + "' to give");
177                    System.out.println(altExpression.toString());
178                    System.out.println("and again to give");
179                    System.out.println(demodulation.apply(assertion, altExpression)
180                                    .toString());
181                    System.out.println("");
182            }
183    
184            private static void fOL_Paramodulation() {
185                    System.out.println("-------------------");
186                    System.out.println("Paramodulation Demo");
187                    System.out.println("-------------------");
188                    
189                    FOLDomain domain = new FOLDomain();
190                    domain.addConstant("A");
191                    domain.addConstant("B");
192                    domain.addPredicate("P");
193                    domain.addPredicate("Q");
194                    domain.addPredicate("R");
195                    domain.addFunction("F");
196    
197                    FOLParser parser = new FOLParser(domain);
198    
199                    List<Literal> lits = new ArrayList<Literal>();
200                    AtomicSentence a1 = (AtomicSentence) parser.parse("P(F(x,B),x)");
201                    AtomicSentence a2 = (AtomicSentence) parser.parse("Q(x)");
202                    lits.add(new Literal(a1));
203                    lits.add(new Literal(a2));
204    
205                    Clause c1 = new Clause(lits);
206    
207                    lits.clear();
208                    a1 = (AtomicSentence) parser.parse("F(A,y) = y");
209                    a2 = (AtomicSentence) parser.parse("R(y)");
210                    lits.add(new Literal(a1));
211                    lits.add(new Literal(a2));
212    
213                    Clause c2 = new Clause(lits);
214    
215                    Paramodulation paramodulation = new Paramodulation();
216                    Set<Clause> paras = paramodulation.apply(c1, c2);
217    
218                    System.out.println("Paramodulate '" + c1 + "' with '" + c2
219                                    + "' to give");
220                    System.out.println(paras.toString());
221                    System.out.println("");
222            }
223            
224            private static void fOL_OTTERDemo() {
225                    System.out.println("---------------------------------------");
226                    System.out.println("OTTER Like Theorem Prover, Kings Demo 1");
227                    System.out.println("---------------------------------------");
228                    kingsDemo1(new FOLOTTERLikeTheoremProver());
229                    System.out.println("---------------------------------------");
230                    System.out.println("OTTER Like Theorem Prover, Kings Demo 2");
231                    System.out.println("---------------------------------------");
232                    kingsDemo2(new FOLOTTERLikeTheoremProver());
233                    System.out.println("---------------------------------------");
234                    System.out.println("OTTER Like Theorem Prover, Weapons Demo");
235                    System.out.println("---------------------------------------");
236                    weaponsDemo(new FOLOTTERLikeTheoremProver());
237                    System.out.println("--------------------------------------------");
238                    System.out.println("OTTER Like Theorem Prover, Loves Animal Demo");
239                    System.out.println("--------------------------------------------");
240                    lovesAnimalDemo(new FOLOTTERLikeTheoremProver());
241                    System.out
242                                    .println("--------------------------------------------------");
243                    System.out
244                                    .println("OTTER Like Theorem Prover, ABC Equality Axiom Demo");
245                    System.out
246                                    .println("--------------------------------------------------");
247                    abcEqualityAxiomDemo(new FOLOTTERLikeTheoremProver(false));
248                    System.out
249                                    .println("-----------------------------------------------------");
250                    System.out
251                                    .println("OTTER Like Theorem Prover, ABC Equality No Axiom Demo");
252                    System.out
253                                    .println("-----------------------------------------------------");
254                    abcEqualityNoAxiomDemo(new FOLOTTERLikeTheoremProver(true));
255            }
256            
257            private static void fOL_ModelEliminationDemo() {
258                    System.out.println("-------------------------------");
259                    System.out.println("Model Elimination, Kings Demo 1");
260                    System.out.println("-------------------------------");
261                    kingsDemo1(new FOLModelElimination());
262                    System.out.println("-------------------------------");
263                    System.out.println("Model Elimination, Kings Demo 2");
264                    System.out.println("-------------------------------");
265                    kingsDemo2(new FOLModelElimination());
266                    System.out.println("-------------------------------");
267                    System.out.println("Model Elimination, Weapons Demo");
268                    System.out.println("-------------------------------");
269                    weaponsDemo(new FOLModelElimination());
270                    System.out.println("------------------------------------");
271                    System.out.println("Model Elimination, Loves Animal Demo");
272                    System.out.println("------------------------------------");
273                    lovesAnimalDemo(new FOLModelElimination());
274                    System.out.println("------------------------------------------");
275                    System.out.println("Model Elimination, ABC Equality Axiom Demo");
276                    System.out.println("-------------------------------------------");
277                    abcEqualityAxiomDemo(new FOLModelElimination());
278            }
279    
280    
281            private static void kingsDemo1(InferenceProcedure ip) {
282                    StandardizeApartIndexicalFactory.flush();
283    
284                    FOLKnowledgeBase kb = FOLKnowledgeBaseFactory
285                                    .createKingsKnowledgeBase(ip);
286    
287                    String kbStr = kb.toString();
288    
289                    List<Term> terms = new ArrayList<Term>();
290                    terms.add(new Constant("John"));
291                    Predicate query = new Predicate("Evil", terms);
292    
293                    InferenceResult answer = kb.ask(query);
294    
295                    System.out.println("Kings Knowledge Base:");
296                    System.out.println(kbStr);
297                    System.out.println("Query: " + query);
298                    for (Proof p : answer.getProofs()) {
299                            System.out.print(ProofPrinter.printProof(p));
300                            System.out.println("");
301                    }
302            }
303            
304            private static void kingsDemo2(InferenceProcedure ip) {
305                    StandardizeApartIndexicalFactory.flush();
306    
307                    FOLKnowledgeBase kb = FOLKnowledgeBaseFactory
308                                    .createKingsKnowledgeBase(ip);
309    
310                    String kbStr = kb.toString();
311    
312                    List<Term> terms = new ArrayList<Term>();
313                    terms.add(new Variable("x"));
314                    Predicate query = new Predicate("King", terms);
315    
316                    InferenceResult answer = kb.ask(query);
317    
318                    System.out.println("Kings Knowledge Base:");
319                    System.out.println(kbStr);
320                    System.out.println("Query: " + query);
321                    for (Proof p : answer.getProofs()) {
322                            System.out.print(ProofPrinter.printProof(p));
323                    }
324            }
325    
326            private static void weaponsDemo(InferenceProcedure ip) {
327                    StandardizeApartIndexicalFactory.flush();
328    
329                    FOLKnowledgeBase kb = FOLKnowledgeBaseFactory
330                                    .createWeaponsKnowledgeBase(ip);
331    
332                    String kbStr = kb.toString();
333    
334                    List<Term> terms = new ArrayList<Term>();
335                    terms.add(new Variable("x"));
336                    Predicate query = new Predicate("Criminal", terms);
337    
338                    InferenceResult answer = kb.ask(query);
339    
340                    System.out.println("Weapons Knowledge Base:");
341                    System.out.println(kbStr);
342                    System.out.println("Query: " + query);
343                    for (Proof p : answer.getProofs()) {
344                            System.out.print(ProofPrinter.printProof(p));
345                            System.out.println("");
346                    }
347            }
348    
349            private static void lovesAnimalDemo(InferenceProcedure ip) {
350                    StandardizeApartIndexicalFactory.flush();
351    
352                    FOLKnowledgeBase kb = FOLKnowledgeBaseFactory
353                                    .createLovesAnimalKnowledgeBase(ip);
354    
355                    String kbStr = kb.toString();
356    
357                    List<Term> terms = new ArrayList<Term>();
358                    terms.add(new Constant("Curiosity"));
359                    terms.add(new Constant("Tuna"));
360                    Predicate query = new Predicate("Kills", terms);
361    
362                    InferenceResult answer = kb.ask(query);
363    
364                    System.out.println("Loves Animal Knowledge Base:");
365                    System.out.println(kbStr);
366                    System.out.println("Query: " + query);
367                    for (Proof p : answer.getProofs()) {
368                            System.out.print(ProofPrinter.printProof(p));
369                            System.out.println("");
370                    }
371            }
372    
373            private static void abcEqualityAxiomDemo(InferenceProcedure ip) {
374                    StandardizeApartIndexicalFactory.flush();
375    
376                    FOLKnowledgeBase kb = FOLKnowledgeBaseFactory
377                                    .createABCEqualityKnowledgeBase(ip, true);
378    
379                    String kbStr = kb.toString();
380    
381                    TermEquality query = new TermEquality(new Constant("A"), new Constant(
382                                    "C"));
383    
384                    InferenceResult answer = kb.ask(query);
385    
386                    System.out.println("ABC Equality Axiom Knowledge Base:");
387                    System.out.println(kbStr);
388                    System.out.println("Query: " + query);
389                    for (Proof p : answer.getProofs()) {
390                            System.out.print(ProofPrinter.printProof(p));
391                            System.out.println("");
392                    }
393            }
394    
395            private static void abcEqualityNoAxiomDemo(InferenceProcedure ip) {
396                    StandardizeApartIndexicalFactory.flush();
397    
398                    FOLKnowledgeBase kb = FOLKnowledgeBaseFactory
399                                    .createABCEqualityKnowledgeBase(ip, false);
400    
401                    String kbStr = kb.toString();
402    
403                    TermEquality query = new TermEquality(new Constant("A"), new Constant(
404                                    "C"));
405    
406                    InferenceResult answer = kb.ask(query);
407                    
408                    System.out.println("ABC Equality No Axiom Knowledge Base:");
409                    System.out.println(kbStr);
410                    System.out.println("Query: " + query);
411                    for (Proof p : answer.getProofs()) {
412                            System.out.print(ProofPrinter.printProof(p));
413                            System.out.println("");
414                    }
415            }
416    }