001 /* 002 * Created on Dec 4, 2004 003 * 004 */ 005 package aima.test.logictest.prop.algorithms; 006 007 import junit.framework.TestCase; 008 import aima.logic.propositional.algorithms.KnowledgeBase; 009 import aima.logic.propositional.algorithms.Model; 010 import aima.logic.propositional.algorithms.TTEntails; 011 import aima.logic.propositional.parsing.ast.Sentence; 012 import aima.logic.propositional.parsing.ast.Symbol; 013 014 /** 015 * @author Ravi Mohan 016 * 017 */ 018 019 public class TTEntailsTest extends TestCase { 020 TTEntails tte; 021 022 KnowledgeBase kb; 023 024 @Override 025 public void setUp() { 026 tte = new TTEntails(); 027 kb = new KnowledgeBase(); 028 } 029 030 public void testSimpleSentence1() { 031 kb.tell("(A AND B)"); 032 assertEquals(true, kb.askWithTTEntails("A")); 033 } 034 035 public void testSimpleSentence2() { 036 kb.tell("(A OR B)"); 037 assertEquals(false, kb.askWithTTEntails("A")); 038 } 039 040 public void testSimpleSentence3() { 041 kb.tell("((A => B) AND A)"); 042 assertEquals(true, kb.askWithTTEntails("B")); 043 } 044 045 public void testSimpleSentence4() { 046 kb.tell("((A => B) AND B)"); 047 assertEquals(false, kb.askWithTTEntails("A")); 048 } 049 050 public void testSimpleSentence5() { 051 kb.tell("A"); 052 assertEquals(false, kb.askWithTTEntails("NOT A")); 053 } 054 055 public void testSUnkownSymbol() { 056 kb.tell("((A => B) AND B)"); 057 assertEquals(false, kb.askWithTTEntails("X")); 058 } 059 060 public void testSimpleSentence6() { 061 kb.tell("NOT A"); 062 assertEquals(false, kb.askWithTTEntails("A")); 063 } 064 065 public void testNewAIMAExample() { 066 kb.tell("(NOT P11)"); 067 kb.tell("(B11 <=> (P12 OR P21))"); 068 kb.tell("(B21 <=> ((P11 OR P22) OR P31))"); 069 kb.tell("(NOT B11)"); 070 kb.tell("(B21)"); 071 072 assertEquals(true, kb.askWithTTEntails("NOT P12")); 073 assertEquals(false, kb.askWithTTEntails("P22")); 074 } 075 076 public void testTTEntailsSucceedsWithChadCarffsBugReport() { 077 KnowledgeBase kb = new KnowledgeBase(); 078 kb.tell("(B12 <=> (P11 OR (P13 OR (P22 OR P02))))"); 079 kb.tell("(B21 <=> (P20 OR (P22 OR (P31 OR P11))))"); 080 kb.tell("(B01 <=> (P00 OR (P02 OR P11)))"); 081 kb.tell("(B10 <=> (P11 OR (P20 OR P00)))"); 082 kb.tell("(NOT B21)"); 083 kb.tell("(NOT B12)"); 084 kb.tell("(B10)"); 085 kb.tell("(B01)"); 086 087 assertTrue(kb.askWithTTEntails("(P00)")); 088 assertFalse(kb.askWithTTEntails("(NOT P00)")); 089 090 } 091 092 // public void testTTEntailsSucceedsWithCStackOverFlowBugReport() { 093 // KnowledgeBase kb = new KnowledgeBase(); 094 // 095 // assertTrue(kb.askWithTTEntails("((A OR (NOT A)) AND (A OR B))")); 096 // } 097 098 public void testModelEvaluation() { 099 kb.tell("(NOT P11)"); 100 kb.tell("(B11 <=> (P12 OR P21))"); 101 kb.tell("(B21 <=> ((P11 OR P22) OR P31))"); 102 kb.tell("(NOT B11)"); 103 kb.tell("(B21)"); 104 105 Model model = new Model(); 106 model = model.extend(new Symbol("B11"), false); 107 model = model.extend(new Symbol("B21"), true); 108 model = model.extend(new Symbol("P11"), false); 109 model = model.extend(new Symbol("P12"), false); 110 model = model.extend(new Symbol("P21"), false); 111 model = model.extend(new Symbol("P22"), false); 112 model = model.extend(new Symbol("P31"), true); 113 114 Sentence kbs = kb.asSentence(); 115 assertEquals(true, model.isTrue(kbs)); 116 } 117 118 }