001 package aima.logic.fol.parsing.ast; 002 003 import java.util.List; 004 005 import aima.logic.fol.parsing.FOLVisitor; 006 007 /** 008 * @author Ravi Mohan 009 * @author Ciaran O'Reilly 010 */ 011 public class Variable implements Term { 012 private String value; 013 private int hashCode = 0; 014 private int indexical = -1; 015 016 public Variable(String s) { 017 value = s.trim(); 018 } 019 020 public Variable(String s, int idx) { 021 value = s.trim(); 022 indexical = idx; 023 } 024 025 public String getValue() { 026 return value; 027 } 028 029 // 030 // START-Term 031 public String getSymbolicName() { 032 return getValue(); 033 } 034 035 public boolean isCompound() { 036 return false; 037 } 038 039 public List<Term> getArgs() { 040 // Is not Compound, therefore should 041 // return null for its arguments 042 return null; 043 } 044 045 public Object accept(FOLVisitor v, Object arg) { 046 return v.visitVariable(this, arg); 047 } 048 049 public Variable copy() { 050 return new Variable(value, indexical); 051 } 052 053 // END-Term 054 // 055 056 public int getIndexical() { 057 return indexical; 058 } 059 060 public void setIndexical(int idx) { 061 indexical = idx; 062 hashCode = 0; 063 } 064 065 public String getIndexedValue() { 066 return value + indexical; 067 } 068 069 @Override 070 public boolean equals(Object o) { 071 072 if (this == o) { 073 return true; 074 } 075 if (!(o instanceof Variable)) { 076 return false; 077 } 078 079 Variable v = (Variable) o; 080 return v.getValue().equals(getValue()) 081 && v.getIndexical() == getIndexical(); 082 } 083 084 @Override 085 public int hashCode() { 086 if (0 == hashCode) { 087 hashCode = 17; 088 hashCode += indexical; 089 hashCode = 37 * hashCode + value.hashCode(); 090 } 091 092 return hashCode; 093 } 094 095 @Override 096 public String toString() { 097 return value; 098 } 099 }