001: package org.mandarax.reference;
002:
003: /*
004: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: import org.mandarax.kernel.*;
021: import org.mandarax.util.TermIterator;
022: import java.util.*;
023:
024: /**
025: * Reference implementation for variable terms.
026: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
027: * @version 3.4 <7 March 05>
028: * @since 1.1
029: * Prova re-integration modifications
030: * @author <A HREF="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</A>
031: * @version 3.4 <7 March 05>
032: */
033: public class VariableTermImpl extends LObject implements VariableTerm {
034:
035: private String name = null;
036: private Class type = null;
037:
038: /** Cache the string representation for optimized debugging. */
039: private transient String cachedToString = null;
040:
041: /**
042: * Create a new instance with the name and the type passed as parameter.
043: */
044: public VariableTermImpl() {
045: super ();
046: }
047:
048: /**
049: * Create a new instance with the name and the type passed as parameter.
050: * @param n the name of the variable
051: * @param t the type of the variable
052: */
053: VariableTermImpl(String n, Class t) {
054: super ();
055: setName(n);
056: setType(t);
057: }
058:
059: /**
060: * Get a (trivial) term iterator for the subterms.
061: * @return an iterator for all sub terms
062: */
063: public TermIterator allSubterms() {
064: Term[] t = { this };
065:
066: return new TermIterator(t);
067: }
068:
069: /**
070: * Apply a replacement to a term.
071: * @return the term resulting from applying the replacement
072: * @param r the replacement
073: */
074: public Term apply(Replacement r) {
075: if (r.original.sameAs(this )) {
076: return r.replacement;
077: } else {
078: return this ;
079: }
080: }
081:
082: public void getAllSubtermsA(List result) {
083: result.add(this );
084: }
085:
086: public void getAllSubtermsA(List result, boolean left) {
087: result.add(this );
088: }
089:
090: public boolean getAllSubtermsB(List result, Iterator aux) {
091: Object t = ConstantTermImpl.nextNotNull(aux);
092: if (t != null) {
093: result.add(this );
094: return (t instanceof VariableTermImpl) && (this == t);
095: }
096: return false;
097: }
098:
099: public boolean getAllSubtermsB(List result, Iterator aux,
100: boolean left) {
101: Object t = ConstantTermImpl.nextNotNull(aux);
102: if (t != null) {
103: result.add(this );
104: return (t instanceof VariableTermImpl) && (this == t);
105: }
106: return false;
107: }
108:
109: /**
110: * Indicates whether the term containes variables.
111: * @return true
112: */
113: public boolean containsVariables() {
114: return true;
115: }
116:
117: /**
118: * Indicates whether the term contains the provided variable term.
119: * @return true if the term contains the variable term provided, false otherwise
120: * @param var a variable term
121: */
122: public boolean containsVariable(VariableTerm var) {
123: return this == var;
124: }
125:
126: /**
127: * Indicates whether the objects are equal.
128: * @return true if the objects are equal, false otherwise
129: * @param t the object to compare this object with
130: */
131: public boolean equals(Object t) {
132: if (!(t instanceof org.mandarax.kernel.VariableTerm)) {
133: return false;
134: }
135:
136: return type.equals(((VariableTerm) t).getType())
137: && name.equals(((VariableTerm) t).getName());
138: }
139:
140: /**
141: * Get all subterms as array.
142: * @return an array of terms only containing this object
143: */
144: public Term[] getAllSubterms() {
145: Term[] t = { this };
146:
147: return t;
148: }
149:
150: /**
151: * Get the name of the variable.
152: * @return the name of the variable
153: */
154: public String getName() {
155: return name;
156: }
157:
158: /**
159: * Get the type of the variable.
160: * @return the term type
161: */
162: public Class getType() {
163: return type;
164: }
165:
166: /**
167: * Returns the hashcode of the object.
168: * @return the hash value
169: */
170: public int hashCode() {
171: return type.hashCode() ^ name.hashCode() * 31;
172: }
173:
174: /**
175: * Indicates whether the term is compound.
176: * @return false
177: */
178: public boolean isCompound() {
179: return false;
180: }
181:
182: /**
183: * Indicates whether the term is constant.
184: * @return false
185: */
186: public boolean isConstant() {
187: return false;
188: }
189:
190: /**
191: * Indicates whether the object (usually a term or a clause set) can be performed
192: * using the java semantics. Variable terms do not support the java semantics!
193: * @return false
194: */
195: public boolean isExecutable() {
196: return false;
197: }
198:
199: /**
200: * Indicates whether the term is a variable.
201: * @return true
202: */
203: public boolean isVariable() {
204: return true;
205: }
206:
207: /**
208: * Resolve a constant term. This is trivial: just
209: * throw an excpetion (variables cannot be resolved).
210: * @param session a session object
211: * @return the result of resolving the term
212: * @throws java.lang.UnsupportedOperationException
213: * @throws java.lang.IllegalArgumentException
214: */
215: public Object resolve(Session session)
216: throws UnsupportedOperationException,
217: IllegalArgumentException {
218: throw new UnsupportedOperationException();
219: }
220:
221: // /**
222: // * Resolve a constant term. This is trivial: just
223: // * throw an excpetion (variables cannot be resolved).
224: // * @return the result of resolving the term
225: // * @throws java.lang.UnsupportedOperationException
226: // * @throws java.lang.IllegalArgumentException
227: // */
228: // public Object resolve()
229: // throws UnsupportedOperationException, IllegalArgumentException {
230: // throw new UnsupportedOperationException ();
231: // }
232:
233: /**
234: * Indicates whether the object is the same as the parameter.
235: * @return true if the both objects are the same
236: * @param t another term
237: */
238: public boolean sameAs(Term t) {
239: if (!(t instanceof org.mandarax.kernel.VariableTerm)) {
240: return false;
241: }
242:
243: return type.equals(((VariableTerm) t).getType())
244: && name.equals(((VariableTerm) t).getName());
245: }
246:
247: /**
248: * Set a name.
249: * @param n the new name
250: */
251: public void setName(String n) {
252: name = n;
253: }
254:
255: /**
256: * Set a type.
257: * @param t the type
258: */
259: public void setType(Class t) {
260: type = t;
261: }
262:
263: /**
264: * Convert the receiver to a string.
265: * @return the string representation of this object
266: */
267: public String toString() {
268: if (cachedToString == null) {
269: StringBuffer buf = new StringBuffer();
270:
271: buf.append("<");
272: buf.append(name);
273: buf.append(">");
274:
275: cachedToString = new String(buf);
276: }
277: return cachedToString;
278: }
279: }
|