001    package aima.test.logictest.foltest;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    
006    import junit.framework.TestCase;
007    import aima.logic.fol.kb.data.Chain;
008    import aima.logic.fol.kb.data.Literal;
009    import aima.logic.fol.parsing.ast.Predicate;
010    import aima.logic.fol.parsing.ast.Term;
011    
012    /**
013     * @author Ciaran O'Reilly
014     * 
015     */
016    public class ChainTest extends TestCase {
017            
018            public void testIsEmpty() {
019                    Chain c = new Chain();
020                    
021                    assertTrue(c.isEmpty());
022                    
023                    c.addLiteral(new Literal(new Predicate("P", new ArrayList<Term>())));
024                    
025                    assertFalse(c.isEmpty());
026                    
027                    List<Literal> lits = new ArrayList<Literal>();
028    
029                    lits.add(new Literal(new Predicate("P", new ArrayList<Term>())));
030    
031                    c = new Chain(lits);
032    
033                    assertFalse(c.isEmpty());
034            }
035            
036            public void testContrapositives() {
037                    List<Chain> conts;
038                    Literal p = new Literal(new Predicate("P", new ArrayList<Term>()));
039                    Literal notq = new Literal(new Predicate("Q", new ArrayList<Term>()),
040                                    true);
041                    Literal notr = new Literal(new Predicate("R", new ArrayList<Term>()),
042                                    true);
043    
044                    Chain c = new Chain();
045    
046                    conts = c.getContrapositives();
047                    assertEquals(0, conts.size());
048    
049                    c.addLiteral(p);
050                    conts = c.getContrapositives();
051                    assertEquals(0, conts.size());
052    
053                    c.addLiteral(notq);
054                    conts = c.getContrapositives();
055                    assertEquals(1, conts.size());
056                    assertEquals("<~Q(),P()>", conts.get(0).toString());
057                    
058                    c.addLiteral(notr);
059                    conts = c.getContrapositives();
060                    assertEquals(2, conts.size());
061                    assertEquals("<~Q(),P(),~R()>", conts.get(0).toString());
062                    assertEquals("<~R(),P(),~Q()>", conts.get(1).toString());
063            }
064    }