001 /* 002 * Created on Sep 2, 2003 by Ravi Mohan 003 * 004 */ 005 package aima.test.logictest.prop.algorithms; 006 007 import junit.framework.TestCase; 008 import aima.logic.propositional.algorithms.Model; 009 import aima.logic.propositional.parsing.PEParser; 010 import aima.logic.propositional.parsing.ast.Sentence; 011 import aima.logic.propositional.parsing.ast.Symbol; 012 013 /** 014 * @author Ravi Mohan 015 * 016 */ 017 018 public class ModelTest extends TestCase { 019 private Model m; 020 021 private PEParser parser; 022 023 Sentence trueSentence, falseSentence, andSentence, orSentence, 024 impliedSentence, biConditionalSentence; 025 026 @Override 027 public void setUp() { 028 parser = new PEParser(); 029 trueSentence = (Sentence) parser.parse("true"); 030 falseSentence = (Sentence) parser.parse("false"); 031 andSentence = (Sentence) parser.parse("(P AND Q)"); 032 orSentence = (Sentence) parser.parse("(P OR Q)"); 033 impliedSentence = (Sentence) parser.parse("(P => Q)"); 034 biConditionalSentence = (Sentence) parser.parse("(P <=> Q)"); 035 m = new Model(); 036 } 037 038 public void testEmptyModel() { 039 assertEquals(null, m.getStatus(new Symbol("P"))); 040 assertEquals(true, m.isUnknown(new Symbol("P"))); 041 } 042 043 public void testExtendModel() { 044 String p = "P"; 045 m = m.extend(new Symbol(p), true); 046 assertEquals(Boolean.TRUE, m.getStatus(new Symbol("P"))); 047 } 048 049 public void testTrueFalseEvaluation() { 050 assertEquals(true, m.isTrue(trueSentence)); 051 assertEquals(false, m.isFalse(trueSentence)); 052 assertEquals(false, m.isTrue(falseSentence)); 053 assertEquals(true, m.isFalse(falseSentence)); 054 } 055 056 public void testSentenceStatusWhenPTrueAndQTrue() { 057 String p = "P"; 058 String q = "Q"; 059 m = m.extend(new Symbol(p), true); 060 m = m.extend(new Symbol(q), true); 061 assertEquals(true, m.isTrue(andSentence)); 062 assertEquals(true, m.isTrue(orSentence)); 063 assertEquals(true, m.isTrue(impliedSentence)); 064 assertEquals(true, m.isTrue(biConditionalSentence)); 065 } 066 067 public void testSentenceStatusWhenPFalseAndQFalse() { 068 String p = "P"; 069 String q = "Q"; 070 m = m.extend(new Symbol(p), false); 071 m = m.extend(new Symbol(q), false); 072 assertEquals(true, m.isFalse(andSentence)); 073 assertEquals(true, m.isFalse(orSentence)); 074 assertEquals(true, m.isTrue(impliedSentence)); 075 assertEquals(true, m.isTrue(biConditionalSentence)); 076 } 077 078 public void testSentenceStatusWhenPTrueAndQFalse() { 079 String p = "P"; 080 String q = "Q"; 081 m = m.extend(new Symbol(p), true); 082 m = m.extend(new Symbol(q), false); 083 assertEquals(true, m.isFalse(andSentence)); 084 assertEquals(true, m.isTrue(orSentence)); 085 assertEquals(true, m.isFalse(impliedSentence)); 086 assertEquals(true, m.isFalse(biConditionalSentence)); 087 } 088 089 public void testSentenceStatusWhenPFalseAndQTrue() { 090 String p = "P"; 091 String q = "Q"; 092 m = m.extend(new Symbol(p), false); 093 m = m.extend(new Symbol(q), true); 094 assertEquals(true, m.isFalse(andSentence)); 095 assertEquals(true, m.isTrue(orSentence)); 096 assertEquals(true, m.isTrue(impliedSentence)); 097 assertEquals(true, m.isFalse(biConditionalSentence)); 098 } 099 100 public void testComplexSentence() { 101 String p = "P"; 102 String q = "Q"; 103 m = m.extend(new Symbol(p), true); 104 m = m.extend(new Symbol(q), false); 105 Sentence sent = (Sentence) parser.parse("((P OR Q) AND (P => Q))"); 106 Sentence sent2 = (Sentence) parser.parse("((P OR Q) AND (Q))"); 107 assertFalse(m.isTrue(sent)); 108 assertTrue(m.isFalse(sent)); 109 } 110 111 }