001 /* Created on Sep 18, 2004 002 * 003 */ 004 package aima.test.logictest.foltest; 005 006 import junit.framework.TestCase; 007 import aima.logic.common.LogicTokenTypes; 008 import aima.logic.common.Token; 009 import aima.logic.fol.domain.FOLDomain; 010 import aima.logic.fol.parsing.FOLLexer; 011 012 /** 013 * @author Ravi Mohan 014 * 015 */ 016 017 public class FOLLexerTest extends TestCase { 018 FOLLexer lexer; 019 020 @Override 021 public void setUp() { 022 FOLDomain domain = new FOLDomain(); 023 domain.addConstant("P"); 024 domain.addConstant("John"); 025 domain.addConstant("Saladin"); 026 domain.addFunction("LeftLeg"); 027 domain.addFunction("BrotherOf"); 028 domain.addFunction("EnemyOf"); 029 domain.addPredicate("HasColor"); 030 domain.addPredicate("King"); 031 lexer = new FOLLexer(domain); 032 } 033 034 public void testLexBasicExpression() { 035 lexer.setInput("( P )"); 036 assertEquals(new Token(LogicTokenTypes.LPAREN, "("), lexer.nextToken()); 037 assertEquals(new Token(LogicTokenTypes.CONSTANT, "P"), lexer 038 .nextToken()); 039 assertEquals(new Token(LogicTokenTypes.RPAREN, ")"), lexer.nextToken()); 040 assertEquals(new Token(LogicTokenTypes.EOI, "EOI"), lexer.nextToken()); 041 } 042 043 public void testConnectors() { 044 lexer.setInput(" p AND q"); 045 assertEquals(new Token(LogicTokenTypes.VARIABLE, "p"), lexer 046 .nextToken()); 047 assertEquals(new Token(LogicTokenTypes.CONNECTOR, "AND"), lexer 048 .nextToken()); 049 assertEquals(new Token(LogicTokenTypes.VARIABLE, "q"), lexer 050 .nextToken()); 051 assertEquals(new Token(LogicTokenTypes.EOI, "EOI"), lexer.nextToken()); 052 } 053 054 public void testFunctions() { 055 lexer.setInput(" LeftLeg(q)"); 056 assertEquals(new Token(LogicTokenTypes.FUNCTION, "LeftLeg"), lexer 057 .nextToken()); 058 assertEquals(new Token(LogicTokenTypes.LPAREN, "("), lexer.nextToken()); 059 assertEquals(new Token(LogicTokenTypes.VARIABLE, "q"), lexer 060 .nextToken()); 061 assertEquals(new Token(LogicTokenTypes.RPAREN, ")"), lexer.nextToken()); 062 assertEquals(new Token(LogicTokenTypes.EOI, "EOI"), lexer.nextToken()); 063 } 064 065 public void testPredicate() { 066 lexer.setInput(" HasColor(r)"); 067 assertEquals(new Token(LogicTokenTypes.PREDICATE, "HasColor"), lexer 068 .nextToken()); 069 assertEquals(new Token(LogicTokenTypes.LPAREN, "("), lexer.nextToken()); 070 assertEquals(new Token(LogicTokenTypes.VARIABLE, "r"), lexer 071 .nextToken()); 072 assertEquals(new Token(LogicTokenTypes.RPAREN, ")"), lexer.nextToken()); 073 assertEquals(new Token(LogicTokenTypes.EOI, "EOI"), lexer.nextToken()); 074 } 075 076 public void testMultiArgPredicate() { 077 lexer.setInput(" King(x,y)"); 078 assertEquals(new Token(LogicTokenTypes.PREDICATE, "King"), lexer 079 .nextToken()); 080 assertEquals(new Token(LogicTokenTypes.LPAREN, "("), lexer.nextToken()); 081 assertEquals(new Token(LogicTokenTypes.VARIABLE, "x"), lexer 082 .nextToken()); 083 assertEquals(new Token(LogicTokenTypes.COMMA, ","), lexer.nextToken()); 084 assertEquals(new Token(LogicTokenTypes.VARIABLE, "y"), lexer 085 .nextToken()); 086 assertEquals(new Token(LogicTokenTypes.RPAREN, ")"), lexer.nextToken()); 087 // assertEquals(new Token(LogicTokenTypes.EOI, "EOI"), 088 // lexer.nextToken()); 089 } 090 091 public void testQuantifier() { 092 lexer.setInput("FORALL x,y"); 093 assertEquals(new Token(LogicTokenTypes.QUANTIFIER, "FORALL"), lexer 094 .nextToken()); 095 assertEquals(new Token(LogicTokenTypes.VARIABLE, "x"), lexer 096 .nextToken()); 097 assertEquals(new Token(LogicTokenTypes.COMMA, ","), lexer.nextToken()); 098 assertEquals(new Token(LogicTokenTypes.VARIABLE, "y"), lexer 099 .nextToken()); 100 assertEquals(new Token(LogicTokenTypes.EOI, "EOI"), lexer.nextToken()); 101 } 102 103 public void testTermEquality() { 104 lexer.setInput("BrotherOf(John) = EnemyOf(Saladin)"); 105 assertEquals(new Token(LogicTokenTypes.FUNCTION, "BrotherOf"), lexer 106 .nextToken()); 107 assertEquals(new Token(LogicTokenTypes.LPAREN, "("), lexer.nextToken()); 108 assertEquals(new Token(LogicTokenTypes.CONSTANT, "John"), lexer 109 .nextToken()); 110 assertEquals(new Token(LogicTokenTypes.RPAREN, ")"), lexer.nextToken()); 111 assertEquals(new Token(LogicTokenTypes.EQUALS, "="), lexer.nextToken()); 112 assertEquals(new Token(LogicTokenTypes.FUNCTION, "EnemyOf"), lexer 113 .nextToken()); 114 assertEquals(new Token(LogicTokenTypes.LPAREN, "("), lexer.nextToken()); 115 assertEquals(new Token(LogicTokenTypes.CONSTANT, "Saladin"), lexer 116 .nextToken()); 117 assertEquals(new Token(LogicTokenTypes.RPAREN, ")"), lexer.nextToken()); 118 // assertEquals(new Token(LogicTokenTypes.COMMA, ","), 119 // lexer.nextToken()); 120 // assertEquals(new Token(LogicTokenTypes.VARIABLE, "y"), 121 // lexer.nextToken()); 122 // assertEquals(new Token(LogicTokenTypes.EOI, "EOI"), 123 // lexer.nextToken()); 124 } 125 }