001    /*
002     * Created on Sep 20, 2004
003     *
004     */
005    package aima.test.logictest.foltest;
006    
007    import java.util.Set;
008    
009    import junit.framework.TestCase;
010    import aima.logic.fol.VariableCollector;
011    import aima.logic.fol.domain.DomainFactory;
012    import aima.logic.fol.parsing.FOLParser;
013    import aima.logic.fol.parsing.ast.Sentence;
014    import aima.logic.fol.parsing.ast.Variable;
015    
016    /**
017     * @author Ravi Mohan
018     * 
019     */
020    
021    public class VariableCollectorTest extends TestCase {
022            private Sentence sentence;
023    
024            FOLParser parser;
025    
026            VariableCollector vc;
027    
028            @Override
029            public void setUp() {
030                    parser = new FOLParser(DomainFactory.crusadesDomain());
031                    vc = new VariableCollector();
032            }
033    
034            public void testSimplepredicate() {
035                    Set<Variable> variables = vc.collectAllVariables(parser.parse("King(x)"));
036                    assertEquals(1, variables.size());
037                    assertTrue(variables.contains(new Variable("x")));
038            }
039    
040            public void testMultipleVariables() {
041                    Set<Variable> variables = vc.collectAllVariables(parser
042                                    .parse("BrotherOf(x) = EnemyOf(y)"));
043                    assertEquals(2, variables.size());
044                    assertTrue(variables.contains(new Variable("x")));
045                    assertTrue(variables.contains(new Variable("y")));
046            }
047            
048            public void testQuantifiedVariables() {
049                    // Note: Should collect quantified variables
050                    // even if not mentioned in clause.
051                    Set<Variable> variables = vc.collectAllVariables(parser
052                                    .parse("FORALL x,y,z (BrotherOf(x) = EnemyOf(y))"));
053                    assertEquals(3, variables.size());
054                    assertTrue(variables.contains(new Variable("x")));
055                    assertTrue(variables.contains(new Variable("y")));
056                    assertTrue(variables.contains(new Variable("z")));
057            }
058    }