001 /* 002 * Created on Sep 14, 2003 by Ravi Mohan 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.Connectors; 012 import aima.logic.fol.parsing.FOLVisitor; 013 014 /** 015 * @author Ravi Mohan 016 * @author Ciaran O'Reilly 017 */ 018 public class NotSentence implements Sentence { 019 private Sentence negated; 020 private List<Sentence> args = new ArrayList<Sentence>(); 021 private String stringRep = null; 022 private int hashCode = 0; 023 024 public NotSentence(Sentence negated) { 025 this.negated = negated; 026 args.add(negated); 027 } 028 029 public Sentence getNegated() { 030 return negated; 031 } 032 033 // 034 // START-Sentence 035 public String getSymbolicName() { 036 return Connectors.NOT; 037 } 038 039 public boolean isCompound() { 040 return true; 041 } 042 043 public List<Sentence> getArgs() { 044 return Collections.unmodifiableList(args); 045 } 046 047 public Object accept(FOLVisitor v, Object arg) { 048 return v.visitNotSentence(this, arg); 049 } 050 051 public NotSentence copy() { 052 return new NotSentence(negated.copy()); 053 } 054 055 // END-Sentence 056 // 057 058 @Override 059 public boolean equals(Object o) { 060 061 if (this == o) { 062 return true; 063 } 064 if ((o == null) || (this.getClass() != o.getClass())) { 065 return false; 066 } 067 NotSentence ns = (NotSentence) o; 068 return (ns.negated.equals(negated)); 069 } 070 071 @Override 072 public int hashCode() { 073 if (0 == hashCode) { 074 hashCode = 17; 075 hashCode = 37 * hashCode + negated.hashCode(); 076 } 077 return hashCode; 078 } 079 080 @Override 081 public String toString() { 082 if (null == stringRep) { 083 StringBuilder sb = new StringBuilder(); 084 sb.append("NOT("); 085 sb.append(negated.toString()); 086 sb.append(")"); 087 stringRep = sb.toString(); 088 } 089 return stringRep; 090 } 091 }