001 /* 002 * Created on Sep 18, 2004 003 * 004 */ 005 package aima.logic.fol.parsing.ast; 006 007 import java.util.ArrayList; 008 import java.util.Collections; 009 import java.util.List; 010 011 import aima.logic.fol.parsing.FOLVisitor; 012 013 /** 014 * @author Ravi Mohan 015 * @author Ciaran O'Reilly 016 */ 017 public class ConnectedSentence implements Sentence { 018 private String connector; 019 private Sentence first, second; 020 private List<Sentence> args = new ArrayList<Sentence>(); 021 private String stringRep = null; 022 private int hashCode = 0; 023 024 public ConnectedSentence(String connector, Sentence first, Sentence second) { 025 this.connector = connector; 026 this.first = first; 027 this.second = second; 028 args.add(first); 029 args.add(second); 030 } 031 032 public String getConnector() { 033 return connector; 034 } 035 036 public Sentence getFirst() { 037 return first; 038 } 039 040 public Sentence getSecond() { 041 return second; 042 } 043 044 // 045 // START-Sentence 046 public String getSymbolicName() { 047 return getConnector(); 048 } 049 050 public boolean isCompound() { 051 return true; 052 } 053 054 public List<Sentence> getArgs() { 055 return Collections.unmodifiableList(args); 056 } 057 058 public Object accept(FOLVisitor v, Object arg) { 059 return v.visitConnectedSentence(this, arg); 060 } 061 062 public ConnectedSentence copy() { 063 return new ConnectedSentence(connector, first.copy(), second.copy()); 064 } 065 066 // END-Sentence 067 // 068 069 @Override 070 public boolean equals(Object o) { 071 072 if (this == o) { 073 return true; 074 } 075 if ((o == null) || (this.getClass() != o.getClass())) { 076 return false; 077 } 078 ConnectedSentence cs = (ConnectedSentence) o; 079 return cs.getConnector().equals(getConnector()) 080 && cs.getFirst().equals(getFirst()) 081 && cs.getSecond().equals(getSecond()); 082 } 083 084 @Override 085 public int hashCode() { 086 if (0 == hashCode) { 087 hashCode = 17; 088 hashCode = 37 * hashCode + getConnector().hashCode(); 089 hashCode = 37 * hashCode + getFirst().hashCode(); 090 hashCode = 37 * hashCode + getSecond().hashCode(); 091 } 092 return hashCode; 093 } 094 095 @Override 096 public String toString() { 097 if (null == stringRep) { 098 StringBuilder sb = new StringBuilder(); 099 sb.append("("); 100 sb.append(first.toString()); 101 sb.append(" "); 102 sb.append(connector); 103 sb.append(" "); 104 sb.append(second.toString()); 105 sb.append(")"); 106 stringRep = sb.toString(); 107 } 108 return stringRep; 109 } 110 }