package org.mandarax.kernel;

/**
 *  Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
* Represents the result set of a query. A result set is very similar to an SQL result set:
* it provides a cursor for the results, and values from the current cursor position can be fetched.
* @see java.sql.ResultSet
* @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
 * @version 3.3.1 <9 August 2004>
* @since 1.7   
*/
public interface ResultSet extends Serializable{
	/**
	 * Moves the cursor to the first row in this ResultSet object.
	 * @return true if the cursor is on a valid row; false if there are no rows in the result set
	 * @exception an inference exception 
	 */
	public boolean first() throws InferenceException ;
	/**
	 * Moves the cursor to the last row in this ResultSet object.
	 * @return true if the cursor is on a valid row; false if there are no rows in the result set
	 * @exception an inference exception 
	 */
	public boolean last() throws InferenceException;
	/**
	 * Retrieves the current result number. The first row is number 1, the second number 2, and so on.
	 * @return a number
	 * @exception an inference exception 
	 */
	public int getResultNumber() throws InferenceException;
	/**
	 * Moves the cursor down one row from its current position. A ResultSet cursor is initially positioned 
	 * before the first row; the first call to the method next makes the first row the current row; 
	 * the second call makes the second row the current row, and so on. 
	 * @return true if the new current row is valid; false if there are no more rows
	 * @exception an inference exception 
	 */
	public boolean next() throws InferenceException;
	/**
	 * Moves the cursor to the previous row in this ResultSet object. 
	 * @return true if the cursor is on a valid row; false if it is off the result set
	 * @exception an inference exception 
	 */
	public boolean previous() throws InferenceException;

	/**
	 * Releases this ResultSet resources.
	 * E.g., database resources associated with SQLClauseSets can be released here.
	 * @exception an inference exception 
	 */
	public void close() throws InferenceException;
	/**
	 * Get the derivation of the current result.
	 * Throw an exception if the cursor is not positioned at a result.
	 * @return the proof (derivation)
	 * @exception an inference exception 
	 */
	public Derivation getProof() throws InferenceException;
	/**
	 * Get the object that replaces the variable with the given name and type.
	 * Throw an exception if the cursor is not positioned at a result.
	 * @return the object that replaced the variable
	 * @param type the type of the variable
	 * @param name the name of the variable
	 * @exception an inference exception 
	 */
	public Object getResult(Class type, String name) throws InferenceException;
	/**
	 * Get the object that replaces the variable.
	 * Throw an exception if the cursor is not positioned at a result.
	 * @return the object that replaced the variable
	 * @param term the variable term that is (perhaps) replaced
	 * @exception an inference exception 
	 */
	public Object getResult(VariableTerm term) throws InferenceException;
	/**
	 * Get a list of all variable terms in the query.
	 * @return a list of variable terms
	 * @exception an inference exception 
	 */
	public List getQueryVariables() throws InferenceException;
	/**
	 * Get the current position of the cursor
	 * Should return -1 if next has never been called
	 * @return an integer
	 */
	public int getCursorPosition() throws InferenceException ;
	/**
	 * Indicates whether the cursor is at the first position.
	 * @return a boolean
	 */
	public boolean isFirst() throws InferenceException;
	/**
	 * Indicates whether the cursor is at the last position.
	 * @return a boolean
	 */
	public boolean isLast() throws InferenceException;
	/**
	 * Moves the cursor a relative number of results, either positive or negative.
	 * @param offset the number of results
	 * @return true if the cursor is on the result set; false otherwise.
	 */
	public boolean relative(int offset) throws InferenceException;
	/**
	 * Moves the cursor to the given result number.
	 * @param resultNo the number of results.
	 * @return true if the cursor is on the result set; false otherwise
	 */
	public boolean absolute(int resultNo) throws InferenceException;
	/**
	 * Moves the cursor to the front of this ResultSet object, just before the first row. 
	 * This method has no effect if the result set contains no rows.
	 */
	public void beforeFirst() throws InferenceException ;

	/**
	 * Moves the cursor to the end of this ResultSet object, just after the last row. 
	 * This method has no effect if the result set contains no rows.
	 */
	public void afterLast() throws InferenceException ;
	/**
	 * Retrieves whether the cursor is before the first row in this ResultSet object.
	 * @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
	 */
	public boolean isBeforeFirst() throws InferenceException ;

	/**
	 * Retrieves whether the cursor is after the last result in this ResultSet object.
	 * @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
	 */
	public boolean isAfterLast() throws InferenceException ;
	/**
	 * Get the replacements as map (term -> term associations).
	 * @return a map
	 */
	public Map getResults() throws InferenceException;
}
