001 /* 002 * Created on Dec 8, 2004 003 * 004 */ 005 package aima.test.logictest.prop.algorithms; 006 007 import java.util.Set; 008 009 import junit.framework.TestCase; 010 import aima.logic.propositional.algorithms.KnowledgeBase; 011 import aima.logic.propositional.algorithms.PLResolution; 012 import aima.logic.propositional.parsing.PEParser; 013 import aima.logic.propositional.parsing.ast.Sentence; 014 import aima.logic.propositional.parsing.ast.Symbol; 015 016 /** 017 * @author Ravi Mohan 018 * 019 */ 020 021 public class PLResolutionTest extends TestCase { 022 private PLResolution resolution; 023 024 private PEParser parser; 025 026 @Override 027 public void setUp() { 028 resolution = new PLResolution(); 029 parser = new PEParser(); 030 } 031 032 public void testPLResolveWithOneLiteralMatching() { 033 Sentence one = (Sentence) parser.parse("(A OR B)"); 034 Sentence two = (Sentence) parser.parse("((NOT B) OR C)"); 035 Sentence expected = (Sentence) parser.parse("(A OR C)"); 036 Set resolvents = resolution.plResolve(one, two); 037 assertEquals(1, resolvents.size()); 038 assertTrue(resolvents.contains(expected)); 039 } 040 041 public void testPLResolveWithNoLiteralMatching() { 042 Sentence one = (Sentence) parser.parse("(A OR B)"); 043 Sentence two = (Sentence) parser.parse("(C OR D)"); 044 Set resolvents = resolution.plResolve(one, two); 045 assertEquals(0, resolvents.size()); 046 } 047 048 public void testPLResolveWithOneLiteralSentencesMatching() { 049 Sentence one = (Sentence) parser.parse("A"); 050 Sentence two = (Sentence) parser.parse("(NOT A)"); 051 // Sentence expected =(Sentence) parser.parse("(A OR C)"); 052 Set resolvents = resolution.plResolve(one, two); 053 assertEquals(1, resolvents.size()); 054 assertTrue(resolvents.contains(new Symbol("EMPTY_CLAUSE"))); 055 } 056 057 public void testPLResolveWithTwoLiteralsMatching() { 058 Sentence one = (Sentence) parser.parse("((NOT P21) OR B11)"); 059 Sentence two = (Sentence) parser.parse("(((NOT B11) OR P21) OR P12)"); 060 Sentence expected1 = (Sentence) parser 061 .parse("( ( P12 OR P21 ) OR ( NOT P21 ) )"); 062 Sentence expected2 = (Sentence) parser 063 .parse("( ( B11 OR P12 ) OR ( NOT B11 ) )"); 064 Set resolvents = resolution.plResolve(one, two); 065 066 assertEquals(2, resolvents.size()); 067 assertTrue(resolvents.contains(expected1)); 068 assertTrue(resolvents.contains(expected2)); 069 } 070 071 public void testPLResolve1() { 072 boolean b = resolution.plResolution("((B11 => (NOT P11)) AND B11)", 073 "(P11)"); 074 assertEquals(false, b); 075 } 076 077 public void testPLResolve2() { 078 boolean b = resolution.plResolution("(A AND B)", "B"); 079 assertEquals(true, b); 080 } 081 082 public void testPLResolve3() { 083 boolean b = resolution.plResolution("((B11 => (NOT P11)) AND B11)", 084 "(NOT P11)"); 085 assertEquals(true, b); 086 } 087 088 public void testPLResolve4() { 089 boolean b = resolution.plResolution("(A OR B)", "B"); 090 assertEquals(false, b); 091 } 092 093 public void testPLResolve5() { 094 boolean b = resolution.plResolution("((B11 => (NOT P11)) AND B11)", 095 "(NOT B11)"); 096 assertEquals(false, b); 097 } 098 099 public void testMultipleClauseResolution() { 100 // test (and fix) suggested by Huy Dinh. Thanks Huy! 101 PLResolution plr = new PLResolution(); 102 KnowledgeBase kb = new KnowledgeBase(); 103 String fact = "((B11 <=> (P12 OR P21)) AND (NOT B11))"; 104 kb.tell(fact); 105 plr.plResolution(kb, "(B)"); 106 107 } 108 109 // public void testPLResolutionWithChadCarfBugReportData() { 110 // commented out coz this needs a major fix wait for a rewrite 111 // KnowledgeBase kb = new KnowledgeBase(); 112 // kb.tell("(B12 <=> (P11 OR (P13 OR (P22 OR P02))))"); 113 // kb.tell("(B21 <=> (P20 OR (P22 OR (P31 OR P11))))"); 114 // kb.tell("(B01 <=> (P00 OR (P02 OR P11)))"); 115 // kb.tell("(B10 <=> (P11 OR (P20 OR P00)))"); 116 // kb.tell("(NOT B21)"); 117 // kb.tell("(NOT B12)"); 118 // kb.tell("(B10)"); 119 // kb.tell("(B01)"); 120 // assertTrue(resolution.plResolution(kb.asSentence().toString(), "(P00)")); 121 // //assertFalse(kb.askWithDpll("(NOT P00)")); 122 // 123 // 124 // } 125 126 }