001 /* 002 * Created on Dec 4, 2004 003 * 004 */ 005 package aima.test.logictest.prop.visitors; 006 007 import java.util.Set; 008 009 import junit.framework.TestCase; 010 import aima.logic.propositional.parsing.PEParser; 011 import aima.logic.propositional.parsing.ast.Sentence; 012 import aima.logic.propositional.visitors.CNFClauseGatherer; 013 import aima.logic.propositional.visitors.CNFTransformer; 014 015 /** 016 * @author Ravi Mohan 017 * 018 */ 019 public class CNFClauseGathererTest extends TestCase { 020 private CNFClauseGatherer gatherer; 021 022 private PEParser parser; 023 024 @Override 025 public void setUp() { 026 parser = new PEParser(); 027 gatherer = new CNFClauseGatherer(); 028 } 029 030 public void testSymbol() { 031 Sentence simple = (Sentence) parser.parse("A"); 032 Sentence a = (Sentence) parser.parse("A"); 033 Set clauses = gatherer.getClausesFrom(simple); 034 assertNotNull(clauses); 035 assertEquals(1, clauses.size()); 036 assertTrue(clauses.contains(a)); 037 038 } 039 040 public void testNotSentence() { 041 Sentence simple = (Sentence) parser.parse("(NOT A)"); 042 Sentence a = (Sentence) parser.parse("(NOT A)"); 043 Set clauses = gatherer.getClausesFrom(simple); 044 assertNotNull(clauses); 045 assertEquals(1, clauses.size()); 046 assertTrue(clauses.contains(a)); 047 048 } 049 050 public void testSimpleAndClause() { 051 Sentence simple = (Sentence) parser.parse("(A AND B)"); 052 Sentence a = (Sentence) parser.parse("A"); 053 Sentence b = (Sentence) parser.parse("B"); 054 Set clauses = gatherer.getClausesFrom(simple); 055 assertEquals(2, clauses.size()); 056 assertTrue(clauses.contains(a)); 057 assertTrue(clauses.contains(b)); 058 059 } 060 061 public void testMultiAndClause() { 062 Sentence simple = (Sentence) parser.parse("((A AND B) AND C)"); 063 Set clauses = gatherer.getClausesFrom(simple); 064 assertEquals(3, clauses.size()); 065 Sentence a = (Sentence) parser.parse("A"); 066 Sentence b = (Sentence) parser.parse("B"); 067 Sentence c = (Sentence) parser.parse("C"); 068 assertTrue(clauses.contains(a)); 069 assertTrue(clauses.contains(b)); 070 assertTrue(clauses.contains(c)); 071 072 } 073 074 public void testMultiAndClause2() { 075 Sentence simple = (Sentence) parser.parse("(A AND (B AND C))"); 076 Set clauses = gatherer.getClausesFrom(simple); 077 assertEquals(3, clauses.size()); 078 Sentence a = (Sentence) parser.parse("A"); 079 Sentence b = (Sentence) parser.parse("B"); 080 Sentence c = (Sentence) parser.parse("C"); 081 assertTrue(clauses.contains(a)); 082 assertTrue(clauses.contains(b)); 083 assertTrue(clauses.contains(c)); 084 085 } 086 087 public void testAimaExample() { 088 Sentence aimaEg = (Sentence) parser.parse("( B11 <=> (P12 OR P21))"); 089 CNFTransformer transformer = new CNFTransformer(); 090 Sentence transformed = transformer.transform(aimaEg); 091 Set clauses = gatherer.getClausesFrom(transformed); 092 Sentence clause1 = (Sentence) parser.parse("( B11 OR ( NOT P12 ) )"); 093 Sentence clause2 = (Sentence) parser.parse("( B11 OR ( NOT P21 ) )"); 094 Sentence clause3 = (Sentence) parser 095 .parse("( ( NOT B11 ) OR ( P12 OR P21 ) )"); 096 assertEquals(3, clauses.size()); 097 assertTrue(clauses.contains(clause1)); 098 assertTrue(clauses.contains(clause2)); 099 assertTrue(clauses.contains(clause3)); 100 101 } 102 }