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 constant 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 ConstantTermImpl extends LObject implements ConstantTerm {
034:
035: private Object object = null;
036: private Class type = null;
037:
038: /**
039: * Create a new constant term.
040: */
041: public ConstantTermImpl() {
042: super ();
043: }
044:
045: /**
046: * Create a new constant term on a object.
047: * [Prova] Made public to be callable from ws.prova.reference.ResolutionInferenceEngine5
048: * @param o an object
049: */
050: public ConstantTermImpl(Object o) {
051: super ();
052: setObject(o);
053: }
054:
055: /**
056: * Create a new constant term on a object.
057: * @param o an object
058: * @param t the type of the object
059: * @throws an illegal argument exception is thrown if o is not an instance of t
060: */
061: ConstantTermImpl(Object o, Class t) {
062: super ();
063: setObject(o);
064: setType(t);
065: }
066:
067: /**
068: * Get a (rather trivial) term iterator for the subterms.
069: * @return org.mandarax.util.TermIterator
070: */
071: public TermIterator allSubterms() {
072: Term[] t = new Term[1];
073: t[0] = this ;
074: return new TermIterator(t);
075: }
076:
077: /**
078: * Apply a replacement to a term.
079: * Note that constants cannot be replaced!
080: * @return org.mandarax.kernel.Term
081: * @param r org.mandarax.kernel.Replacement
082: */
083: public Term apply(Replacement r) {
084: return this ;
085: }
086:
087: public void getAllSubtermsA(List result) {
088: result.add(this );
089: }
090:
091: // public void getAllSubtermsA( List result, List vars ) {
092: // result.add( this );
093: // vars.add( null );
094: // }
095:
096: public void getAllSubtermsA(List result, boolean left) {
097: result.add(this );
098: }
099:
100: public boolean getAllSubtermsB(List result, Iterator aux) {
101: Object t = nextNotNull(aux);
102: if (t != null) {
103: if (t instanceof ConstantTermImpl) {
104: if (getObject().equals(((ConstantTerm) t).getObject())) {
105: aux.remove();
106: return true;
107: }
108: }
109: result.add(this );
110: }
111: return false;
112: }
113:
114: public boolean getAllSubtermsB(List result, Iterator aux,
115: boolean left) {
116: Object t = nextNotNull(aux);
117: if (t != null) {
118: if (t instanceof ConstantTermImpl) {
119: if (getObject().equals(((ConstantTerm) t).getObject())) {
120: aux.remove();
121: return true;
122: }
123: }
124: result.add(this );
125: }
126: return false;
127: }
128:
129: static public Object nextNotNull(Iterator it) {
130: Object t = null;
131: if (it.hasNext()) {
132: t = it.next();
133: }
134: return t;
135: }
136:
137: /**
138: * Indicates whether the term containes variables.
139: * @return boolean
140: */
141: public boolean containsVariables() {
142: return false;
143: }
144:
145: /**
146: * Indicates whether the term contains the provided variable term.
147: * @return true if the term contains the variable term provided, false otherwise
148: * @param var a variable term
149: */
150: public boolean containsVariable(VariableTerm var) {
151: return false;
152: }
153:
154: /**
155: * Indicates whether the object equals the parameter.
156: * @return boolean
157: * @param obj java.lang.Object
158: */
159: public boolean equals(Object obj) {
160: if (!(obj instanceof org.mandarax.kernel.ConstantTerm)) {
161: return false;
162: }
163:
164: return object.equals(((ConstantTerm) obj).getObject());
165: }
166:
167: /**
168: * Get all subterms as array.
169: * @return an array of terms only containing this object
170: */
171: public Term[] getAllSubterms() {
172: Term[] t = { this };
173:
174: return t;
175: }
176:
177: /**
178: * Get the object.
179: * @return the wrapped object
180: */
181: public Object getObject() {
182: return object;
183: }
184:
185: /**
186: * Get the type of the term.
187: * @return the term type
188: */
189: public Class getType() {
190: // bugfix in 3.3.1 by Jens
191: return type == null ? object.getClass() : type;
192: }
193:
194: /**
195: * Get the hash code of the object.
196: * @return the hash value
197: */
198: public int hashCode() {
199: return (object == null) ? 0 : object.hashCode();
200: }
201:
202: /**
203: * Indicates whether the term is compound.
204: * @return false
205: */
206: public boolean isCompound() {
207: return false;
208: }
209:
210: /**
211: * Indicates whether the term is a constant.
212: * @return true
213: */
214: public boolean isConstant() {
215: return true;
216: }
217:
218: /**
219: * Indicates whether the object (usually a term or a clause set) can be performed
220: * using the java semantics. Constant terms do support the java semantics!
221: * @return true
222: */
223: public boolean isExecutable() {
224: return true;
225: }
226:
227: /**
228: * Indicates whether the term is a variable.
229: * @return false
230: */
231: public boolean isVariable() {
232: return false;
233: }
234:
235: /**
236: * Resolve a constant term. This is trivial: just
237: * return the object.
238: * @param session a session object
239: * @return the result of resolving the term
240: * @throws java.lang.UnsupportedOperationException
241: * @throws java.lang.IllegalArgumentException
242: */
243: public Object resolve(Session session)
244: throws UnsupportedOperationException,
245: IllegalArgumentException {
246: return getObject();
247: }
248:
249: // /**
250: // * Resolve a constant term. This is trivial: just
251: // * return the object.
252: // * @return the result of resolving the term
253: // * @throws java.lang.UnsupportedOperationException
254: // * @throws java.lang.IllegalArgumentException
255: // */
256: // public Object resolve()
257: // throws UnsupportedOperationException, IllegalArgumentException {
258: // return getObject ();
259: // }
260:
261: /**
262: * Indicates whether the object is the same as the parameter.
263: * @return true if the both objects are the same
264: * @param t another term
265: */
266: public boolean sameAs(Term t) {
267: if (!(t instanceof org.mandarax.kernel.ConstantTerm)) {
268: return false;
269: }
270:
271: return object.equals(((ConstantTerm) t).getObject());
272: }
273:
274: /**
275: * Set the object.
276: * @param obj the object that is to be wrapped
277: * @throws an illegal argument exception is thrown if the object is not an instance of
278: * the type previously set using setType()
279: */
280: public void setObject(Object obj) {
281: if ((type != null) && !(type.isAssignableFrom(obj.getClass()))) {
282: throw new IllegalArgumentException("Object " + obj
283: + " and type " + type + " are inconsistent");
284: }
285:
286: object = obj;
287: }
288:
289: /**
290: * Set the type.
291: * @param t the type
292: * @throws an illegal argument exception is thrown if the object previously set using setObject() is not an instance of
293: * the type
294: */
295: public void setType(Class t) {
296: if ((object != null)
297: && !(t.isAssignableFrom(object.getClass()))) {
298: throw new IllegalArgumentException("Object " + object
299: + " and type " + t + " are inconsistent");
300: }
301:
302: type = t;
303: }
304:
305: /**
306: * Convert the object to a string.
307: * @return the string representation of this object
308: */
309: public String toString() {
310: StringBuffer buf = new StringBuffer();
311:
312: buf.append("[");
313: buf.append(object.toString());
314: buf.append("]");
315:
316: return new String(buf);
317: }
318: }
|