001 /* 002 * Created on Dec 4, 2004 003 * 004 */ 005 package aima.test.logictest.prop.visitors; 006 007 import junit.framework.TestCase; 008 import aima.logic.propositional.parsing.PEParser; 009 import aima.logic.propositional.parsing.ast.Sentence; 010 import aima.logic.propositional.visitors.CNFTransformer; 011 012 /** 013 * @author Ravi Mohan 014 * 015 */ 016 public class CNFTransformerTest extends TestCase { 017 private PEParser parser = new PEParser(); 018 019 private CNFTransformer transformer; 020 021 @Override 022 public void setUp() { 023 transformer = new CNFTransformer(); 024 } 025 026 public void testSymbolTransform() { 027 Sentence symbol = (Sentence) parser.parse("A"); 028 Sentence transformed = transformer.transform(symbol); 029 assertEquals("A", transformed.toString()); 030 } 031 032 public void testBasicSentenceTransformation() { 033 Sentence and = (Sentence) parser.parse("(A AND B)"); 034 Sentence transformedAnd = transformer.transform(and); 035 assertEquals(and.toString(), transformedAnd.toString()); 036 037 Sentence or = (Sentence) parser.parse("(A OR B)"); 038 Sentence transformedOr = transformer.transform(or); 039 assertEquals(or.toString(), transformedOr.toString()); 040 041 Sentence not = (Sentence) parser.parse("(NOT C)"); 042 Sentence transformedNot = transformer.transform(not); 043 assertEquals(not.toString(), transformedNot.toString()); 044 } 045 046 public void testImplicationTransformation() { 047 Sentence impl = (Sentence) parser.parse("(A => B)"); 048 Sentence expected = (Sentence) parser.parse("((NOT A) OR B)"); 049 Sentence transformedImpl = transformer.transform(impl); 050 assertEquals(expected.toString(), transformedImpl.toString()); 051 } 052 053 public void testBiConditionalTransformation() { 054 Sentence bic = (Sentence) parser.parse("(A <=> B)"); 055 Sentence expected = (Sentence) parser 056 .parse("(((NOT A) OR B) AND ((NOT B) OR A)) "); 057 Sentence transformedBic = transformer.transform(bic); 058 assertEquals(expected.toString(), transformedBic.toString()); 059 } 060 061 public void testTwoSuccessiveNotsTransformation() { 062 Sentence twoNots = (Sentence) parser.parse("(NOT (NOT A))"); 063 Sentence expected = (Sentence) parser.parse("A"); 064 Sentence transformed = transformer.transform(twoNots); 065 assertEquals(expected.toString(), transformed.toString()); 066 } 067 068 public void testThreeSuccessiveNotsTransformation() { 069 Sentence threeNots = (Sentence) parser.parse("(NOT (NOT (NOT A)))"); 070 Sentence expected = (Sentence) parser.parse("(NOT A)"); 071 Sentence transformed = transformer.transform(threeNots); 072 assertEquals(expected.toString(), transformed.toString()); 073 } 074 075 public void testFourSuccessiveNotsTransformation() { 076 Sentence fourNots = (Sentence) parser 077 .parse("(NOT (NOT (NOT (NOT A))))"); 078 Sentence expected = (Sentence) parser.parse("A"); 079 Sentence transformed = transformer.transform(fourNots); 080 assertEquals(expected.toString(), transformed.toString()); 081 } 082 083 public void testDeMorgan1() { 084 Sentence dm = (Sentence) parser.parse("(NOT (A AND B))"); 085 Sentence expected = (Sentence) parser.parse("((NOT A) OR (NOT B))"); 086 Sentence transformed = transformer.transform(dm); 087 assertEquals(expected.toString(), transformed.toString()); 088 } 089 090 public void testDeMorgan2() { 091 Sentence dm = (Sentence) parser.parse("(NOT (A OR B))"); 092 Sentence expected = (Sentence) parser.parse("((NOT A) AND (NOT B))"); 093 Sentence transformed = transformer.transform(dm); 094 assertEquals(expected.toString(), transformed.toString()); 095 } 096 097 public void testOrDistribution1() { 098 Sentence or = (Sentence) parser.parse("((A AND B) OR C)"); 099 Sentence expected = (Sentence) parser.parse("((C OR A) AND (C OR B))"); 100 Sentence transformed = transformer.transform(or); 101 assertEquals(expected.toString(), transformed.toString()); 102 } 103 104 public void testOrDistribution2() { 105 Sentence or = (Sentence) parser.parse("(A OR (B AND C))"); 106 Sentence expected = (Sentence) parser.parse("((A OR B) AND (A OR C))"); 107 Sentence transformed = transformer.transform(or); 108 assertEquals(expected.toString(), transformed.toString()); 109 } 110 111 public void testAimaExample() { 112 Sentence aimaEg = (Sentence) parser.parse("( B11 <=> (P12 OR P21))"); 113 Sentence expected = (Sentence) parser 114 .parse(" ( ( ( NOT B11 ) OR ( P12 OR P21 ) ) AND ( ( B11 OR ( NOT P12 ) ) AND ( B11 OR ( NOT P21 ) ) ) )"); 115 Sentence transformed = transformer.transform(aimaEg); 116 assertEquals(expected.toString(), transformed.toString()); 117 } 118 119 }