001: package org.mandarax.util;
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:
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.mandarax.kernel.Derivation;
025: import org.mandarax.kernel.InferenceException;
026: import org.mandarax.kernel.Result;
027: import org.mandarax.kernel.ResultSet;
028: import org.mandarax.kernel.VariableTerm;
029:
030: /**
031: * Abstract superclass for result set implementations with results being organized in a
032: * list that is fetched at once. Results are represented by a list of instances of
033: * org.mandarax.kernel.Result. Subclasses are responsible for appropriate initialization
034: * of this list.
035: * Moved from org.mandarax.util to org.mandarax.util.resultsetfilters
036: * @see org.mandarax.kernel.ResultSet
037: * @see org.mandarax.kernel.Result
038: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
039: * @version 3.4 <7 March 05>
040: * @since 1.8.1 (in util), 3.0 (in util.resultsetfilters)
041: */
042: public class CachedResultSet implements ResultSet {
043:
044: protected List results = null;
045: protected List vars = null;
046: protected int pos = -1;
047:
048: /**
049: * Constructor.
050: */
051: public CachedResultSet() {
052: super ();
053: }
054:
055: /**
056: * Constructor.
057: */
058: public CachedResultSet(List results, List variables) {
059: super ();
060: this .results = results;
061: this .vars = variables;
062: }
063:
064: /**
065: * Indicates whether the cursor is at the first position.
066: * @return a boolean
067: */
068: public boolean isFirst() throws InferenceException {
069: return pos == 0;
070: }
071:
072: /**
073: * Indicates whether the cursor is at the last position.
074: * @return a boolean
075: */
076: public boolean isLast() throws InferenceException {
077: return pos == results.size() - 1;
078: }
079:
080: /**
081: * Moves the cursor to the first row in this ResultSet object.
082: * @return true if the cursor is on a valid row; false if there are no rows in the result set
083: */
084: public boolean first() throws InferenceException {
085: if (results.size() > 0) {
086: pos = 0;
087: return true;
088: }
089: return false;
090: }
091:
092: /**
093: * Moves the cursor to the last row in this ResultSet object.
094: * @return true if the cursor is on a valid row; false if there are no rows in the results
095: */
096: public boolean last() throws InferenceException {
097: if (results.size() > 0) {
098: pos = results.size() - 1;
099: return true;
100: }
101: return false;
102: }
103:
104: /**
105: * Retrieves the current result number. Numbering starts with 1.
106: * @return a number
107: */
108: public int getResultNumber() throws InferenceException {
109: return pos == -1 ? -1 : pos + 1;
110: }
111:
112: /**
113: * Moves the cursor down one row from its current position. A ResultSet cursor is initially positioned
114: * before the first row; the first call to the method next makes the first row the current row;
115: * the second call makes the second row the current row, and so on.
116: * @return true if the new current row is valid; false if there are no more rows
117: */
118: public boolean next() throws InferenceException {
119: pos = pos + 1;
120: return isValid();
121: }
122:
123: /**
124: * Moves the cursor to the previous row in this ResultSet object.
125: * @return true if the cursor is on a valid row; false if it is off the result set
126: */
127: public boolean previous() throws InferenceException {
128: pos = pos - 1;
129: return isValid();
130: };
131:
132: /**
133: * Releases this ResultSet resources.
134: */
135: public void close() throws InferenceException {
136: }
137:
138: /**
139: * Get a list of all variable terms in the query.
140: * @return a list of variable terms
141: */
142: public List getQueryVariables() throws InferenceException {
143: return vars;
144: }
145:
146: /**
147: * Get the result at the current cursor position.
148: * @return a result
149: */
150: private Result getResult() throws InferenceException {
151: if (!isValid())
152: throw new InferenceException("Illegal cursor position "
153: + pos + " in result set.");
154: return ((Result) results.get(pos));
155: }
156:
157: /**
158: * Get the derivation of the current result.
159: * Throw an exception if the cursor is not positioned at a result.
160: * @return the proof (derivation)
161: */
162: public Derivation getProof() throws InferenceException {
163: return getResult().getProof();
164: }
165:
166: /**
167: * Get the object that replaces the variable with the given name and type.
168: * If there is no such object, return null.
169: * Throw an exception if the cursor is not positioned at a result.
170: * @return the object that replaced the variable
171: * @param type the type of the variable
172: * @param name the name of the variable
173: * @exception an inference exception
174: */
175: public Object getResult(Class type, String name)
176: throws InferenceException {
177: return getResult().getResult(type, name);
178: }
179:
180: /**
181: * Get the object that replaces the variable.
182: * If there is no such object, return null.
183: * Throw an exception if the cursor is not positioned at a result.
184: * @return the object that replaced the variable
185: * @param term the variable term that is (perhaps) replaced
186: * @exception an inference exception
187: */
188: public Object getResult(VariableTerm term)
189: throws InferenceException {
190: return getResult().getResult(term);
191: }
192:
193: /**
194: * Get the current position of the cursor.
195: * @return an integer
196: */
197: public int getCursorPosition() throws InferenceException {
198: return pos;
199: }
200:
201: /**
202: * Moves the cursor a relative number of results, either positive or negative.
203: * @param offset the number of results
204: * @return true if the cursor is on the result set; false otherwise.
205: */
206: public boolean relative(int offset) throws InferenceException {
207: pos = pos + offset;
208: return isValid();
209: }
210:
211: /**
212: * Moves the cursor to the given result number.
213: * @param resultNo the number of results.
214: * @return true if the cursor is on the result set; false otherwise
215: */
216: public boolean absolute(int resultNo) throws InferenceException {
217: pos = resultNo - 1; // like in JDBC - the number of the first result is 1, not 0 !
218: return isValid();
219: }
220:
221: /**
222: * Moves the cursor to the front of this ResultSet object, just before the first row.
223: * This method has no effect if the result set contains no rows.
224: */
225: public void beforeFirst() throws InferenceException {
226: pos = -1;
227: }
228:
229: /**
230: * Moves the cursor to the end of this ResultSet object, just after the last row.
231: * This method has no effect if the result set contains no rows.
232: */
233: public void afterLast() throws InferenceException {
234: pos = results.size();
235: }
236:
237: /**
238: * Retrieves whether the cursor is before the first row in this ResultSet object.
239: * @return true if the cursor is before the first row; false if the cursor is at any other position or the result set contains no rows
240: */
241: public boolean isBeforeFirst() throws InferenceException {
242: return pos == -1;
243: }
244:
245: /**
246: * Retrieves whether the cursor is after the last result in this ResultSet object.
247: * @return true if the cursor is after the last result; false if the cursor is at any other position or the result set contains no results
248: */
249: public boolean isAfterLast() throws InferenceException {
250: return pos == results.size();
251: }
252:
253: /**
254: * Get the replacements as map (term -> term associations).
255: * @return a map
256: */
257: public Map getResults() throws InferenceException {
258: return getResult().getResults();
259: }
260:
261: /**
262: * Indicates whether the cursor is on a valid position.
263: * @return a boolean
264: */
265: private boolean isValid() {
266: return 0 <= pos && pos < results.size();
267: }
268:
269: }
|