public class Query extends Object implements Enumeration
The Query class implements the Enumeration interface, and it is through this interface that one obtains solutions. The Enumeration hasMoreElements() method returns true if the goal succeeded (false, o/w), and if the goal did succeed, the nextElement() method returns a Hashtable representing variable bindings; the elements in the Hashtable are Terms, indexed by the Variables to which they are bound. For example, if p(a) and p(b) are facts in the Prolog database, then the following is equivalent to printing all the solutions to the Prolog query p(X):
Variable X = new Variable();
Term arg[] = { X };
Query q = new Query( "p", arg );
while ( q.hasMoreElements() ){
Term bound_to_x = ((Hashtable)q.nextElement()).get( X );
System.out.println( bound_to_x );
}
Make sure to rewind the Query if you have not asked for all
its solutions. To obtain just one solution from a Query,
use the oneSolution() method.
To obtain all solutions, use the allSolutions() method. Use the
query() method if the Query is a ground query.
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library 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 Library Public License for more details.
| Modifier and Type | Field and Description |
|---|---|
protected Term[] |
args_
the arguments to this Query
|
protected Atom |
atom_
the Atom corresponding to the predicate name in this Query
|
| Constructor and Description |
|---|
Query(Atom atom)
This constructor creates a Prolog query with no arguments
(a "proposition" or "sentence").
|
Query(Atom atom,
Term[] args)
This constructor creates a Query object corresponding to a
Prolog query.
|
Query(String name)
This constructor is shorthand for
|
Query(String name,
Term t0)
This constructor is shorthand for
|
Query(String name,
Term[] args)
This constructor is shorthand for
|
Query(String name,
Term t0,
Term t1)
This constructor is shorthand for
|
Query(String name,
Term t0,
Term t1,
Term t2)
This constructor is shorthand for
|
Query(String name,
Term t0,
Term t1,
Term t2,
Term t3)
This constructor is shorthand for
|
Query(String name,
Term t0,
Term t1,
Term t2,
Term t3,
Term t4)
This constructor is shorthand for
|
Query(String name,
Term t0,
Term t1,
Term t2,
Term t3,
Term t4,
Term t5)
This constructor is shorthand for
|
Query(String name,
Term t0,
Term t1,
Term t2,
Term t3,
Term t4,
Term t5,
Term t6)
This constructor is shorthand for
|
Query(String name,
Term t0,
Term t1,
Term t2,
Term t3,
Term t4,
Term t5,
Term t6,
Term t7)
This constructor is shorthand for
|
Query(String name,
Term t0,
Term t1,
Term t2,
Term t3,
Term t4,
Term t5,
Term t6,
Term t7,
Term t8)
This constructor is shorthand for
|
Query(String name,
Term t0,
Term t1,
Term t2,
Term t3,
Term t4,
Term t5,
Term t6,
Term t7,
Term t8,
Term t9)
This constructor is shorthand for
|
| Modifier and Type | Method and Description |
|---|---|
Hashtable[] |
allSolutions() |
Term[] |
args() |
Atom |
atom() |
String |
debugString() |
boolean |
hasMoreElements()
This method is completes the java.util.Enumeration
interface.
|
boolean |
hasMoreSolutions()
This method returns true if making a Prolog Query using this
Object's Atom and Terms succeeds.
|
boolean |
hasMoreSubstitutions()
Deprecated.
Substitution too hard to spell; use *Solution* instead.
|
static Object |
lock()
Use this method to obtain the lock that is used to synchronize
all calls to the Low-Level Interface.
|
Object |
nextElement()
This method is completes the java.util.Enumeration
interface.
|
Hashtable |
nextSolution()
This method returns a java.util.Hashtable, which represents
a substitution of Terms for Variables in the Term list in this
Query.
|
Hashtable |
nextSubstitution()
Deprecated.
Substitution too hard to spell; use *Solution* instead.
|
Hashtable |
oneSolution() |
static void |
printSolution(Hashtable substitution)
Deprecated.
use Util.toString instead.
|
static void |
printSubstitution(Hashtable substitution)
Deprecated.
use Util.toString instead.
|
boolean |
query() |
void |
rewind()
This method is used to rewind the query so that the query
may be re-run, even if the Query qua Enumeration has more
elements.
|
String |
toString()
Returns the String representation of a Query.
|
protected Atom atom_
protected Term[] args_
public Query(Atom atom, Term[] args)
NB. Creating an instance of the Query class does not result in a call to the Prolog Abstract Machine.
atom - an Atom that names the predicate in this Queryargs - the arguments to this Querypublic Query(String name, Term[] args)
new Query( new Atom( name ), arg )
name - the name of the predicate in this Queryargs - the arguments to this Querypublic Query(String name, Term t0)
new Query(
new Atom( name ),
Util.toTermArray( t0 ) )
name - the name of the predicate in this Queryt0 - a jpl.Termpublic Query(String name, Term t0, Term t1)
new Query(
new Atom( name ),
Util.toTermArray( t0, t1 ) )
name - the name of the predicate in this Queryt0 - a jpl.Termt1 - a jpl.Termpublic Query(String name, Term t0, Term t1, Term t2)
new Query(
new Atom( name ),
Util.toTermArray( t0, t1, t2 ) )
name - the name of the predicate in this Queryt0 - a jpl.Termt1 - a jpl.Termt2 - a jpl.Termpublic Query(String name, Term t0, Term t1, Term t2, Term t3)
new Query(
new Atom( name ),
Util.toTermArray( t0, t1, t2, t3 ) )
name - the name of the predicate in this Queryt0 - a jpl.Termt1 - a jpl.Termt2 - a jpl.Termt3 - a jpl.Termpublic Query(String name, Term t0, Term t1, Term t2, Term t3, Term t4)
new Query(
new Atom( name ),
Util.toTermArray( t0, t1, t2, t3, t4 ) )
name - the name of the predicate in this Queryt0 - a jpl.Termt1 - a jpl.Termt2 - a jpl.Termt3 - a jpl.Termt4 - a jpl.Termpublic Query(String name, Term t0, Term t1, Term t2, Term t3, Term t4, Term t5)
new Query(
new Atom( name ),
Util.toTermArray( t0, t1, t2, t3, t4,
t5 ) )
name - the name of the predicate in this Queryt0 - a jpl.Termt1 - a jpl.Termt2 - a jpl.Termt3 - a jpl.Termt4 - a jpl.Termt5 - a jpl.Termpublic Query(String name, Term t0, Term t1, Term t2, Term t3, Term t4, Term t5, Term t6)
new Query(
new Atom( name ),
Util.toTermArray( t0, t1, t2, t3, t4,
t5, t6 ) )
name - the name of the predicate in this Queryt0 - a jpl.Termt1 - a jpl.Termt2 - a jpl.Termt3 - a jpl.Termt4 - a jpl.Termt5 - a jpl.Termt6 - a jpl.Termpublic Query(String name, Term t0, Term t1, Term t2, Term t3, Term t4, Term t5, Term t6, Term t7)
new Query(
new Atom( name ),
Util.toTermArray( t0, t1, t2, t3, t4,
t5, t6, t7 ) )
name - the name of the predicate in this Queryt0 - a jpl.Termt1 - a jpl.Termt2 - a jpl.Termt3 - a jpl.Termt4 - a jpl.Termt5 - a jpl.Termt6 - a jpl.Termt7 - a jpl.Termpublic Query(String name, Term t0, Term t1, Term t2, Term t3, Term t4, Term t5, Term t6, Term t7, Term t8)
new Query(
new Atom( name ),
Util.toTermArray( t0, t1, t2, t3, t4,
t5, t6, t7, t8 ) )
name - the name of the predicate in this Queryt0 - a jpl.Termt1 - a jpl.Termt2 - a jpl.Termt3 - a jpl.Termt4 - a jpl.Termt5 - a jpl.Termt6 - a jpl.Termt7 - a jpl.Termt8 - a jpl.Termpublic Query(String name, Term t0, Term t1, Term t2, Term t3, Term t4, Term t5, Term t6, Term t7, Term t8, Term t9)
new Query(
new Atom( name ),
Util.toTermArray( t0, t1, t2, t3, t4,
t5, t6, t7, t8, t9 ) )
name - the name of the predicate in this Queryt0 - a jpl.Termt1 - a jpl.Termt2 - a jpl.Termt3 - a jpl.Termt4 - a jpl.Termt5 - a jpl.Termt6 - a jpl.Termt7 - a jpl.Termt8 - a jpl.Termt9 - a jpl.Termpublic Query(Atom atom)
atom - an Atom that names the predicate in this Querypublic Query(String name)
new Query( new Atom( name ) )
name - the name of the predicate in this Querypublic static Object lock()
Query query = // get a query somehow
synchronized ( jpl.Query.lock() ){
while ( query.hasMoreElements() ){
Hashtable solution = query.nextSolution();
// process solution...
}
}
The lock so acquired is the same lock used internally by
hasMoreSolutions and nextSolution methods; indeed, it is
the Class Object for the jpl.fli.Prolog class, so any calls
to the Low-Level Interface will be blocked, as well. However,
you should not intermix calls to the Low-Level Interface when using
the High-Level Interface.public final Atom atom()
public final Term[] args()
public final boolean hasMoreSolutions()
Query q = // obtain Query reference
while ( q.hasMoreSolutions() ){
Hashtable solution = q.nextSolution();
// process solution...
}
To ensure thread-safety, you should wrap sequential calls to
this method in a synchronized block, using the static
lock method to obtain the monitor.
Query q = // obtain Query reference
synchronized ( jpl.Query.lock() ){
while ( q.hasMoreElements() ){
Hashtable solution = q.nextSolution();
// process solution...
}
}
If this method is called while another is in progress, a QueryInProgressException will be thrown with the currently executing Query.
lock(),
hasMoreSolutions(),
nextSolution(),
hasMoreElements(),
nextElement(),
rewind(),
oneSolution(),
allSolutions(),
query()public final Hashtable nextSolution()
For example, if a Query has an occurrence of a jpl.Variable, say, named X, one can obtain the Term bound to X in the solution by looking up X in the Hashtable.
Variable X = new Variable();
Query q = // obtain Query reference (with X in the Term array)
while ( q.hasMoreSolutions() ){
Hashtable solution = q.nextSolution();
// make t the Term bound to X in the solution
Term t = (Term)solution.get( X );
// ...
}
Programmers should obey the following rules when using this method.
This method will throw a JPLException if no query is in progress.
It will throw a QueryInProgressException if another Query
(besides this one) is in progress while this method is called.hasMoreSolutions(),
nextSolution(),
hasMoreElements(),
nextElement(),
rewind(),
oneSolution(),
allSolutions(),
query()public final boolean hasMoreElements()
hasMoreElements in interface EnumerationhasMoreSolutions(),
nextSolution(),
hasMoreElements(),
nextElement(),
rewind(),
oneSolution(),
allSolutions(),
query()public final boolean hasMoreSubstitutions()
public final Object nextElement()
This method will throw a QueryInProgressException if another Query (besides this one) is in progress while this method is called.
nextElement in interface EnumerationhasMoreSolutions(),
nextSolution(),
hasMoreElements(),
nextElement(),
rewind(),
oneSolution(),
allSolutions(),
query()public final Hashtable nextSubstitution()
public final void rewind()
Here is a way to get the first 3 solutions to a Query, while subsequently being able to use the same Query object to obtain new solutions:
Query q = new Query( predicate, args );
int i = 0;
for ( int i = 0; i < 3 && q.hasMoreSolutions(); ++i ){
Hasthable sub = (Hashtable) q.nextSolution();
...
}
q.rewind();
This method will throw a QueryInProgressException if another Query (besides this one) is in progress while this method is called. It is safe to call this method if no query is in progress.
public final Hashtable[] allSolutions()
This method will throw a QueryInProgressException if another Query (besides this one) is in progress while this method is called.
hasMoreElements(),
nextElement(),
hasMoreSolutions(),
nextSolution(),
rewind(),
oneSolution(),
allSolutions(),
query()public final Hashtable oneSolution()
This method will throw a QueryInProgressException if another Query (besides this one) is in progress while this method is called.
hasMoreElements(),
nextElement(),
hasMoreSolutions(),
nextSolution(),
rewind(),
oneSolution(),
allSolutions(),
query()public final boolean query()
This method should only be called for ground queries, since the results of any bindings are discarded.
This method will throw a QueryInProgressException if another Query (besides this one) is in progress while this method is called.
hasMoreElements(),
nextElement(),
hasMoreSolutions(),
nextSolution(),
rewind(),
oneSolution(),
allSolutions(),
query()public static void printSolution(Hashtable substitution)
substitution - The substitution to print.public static void printSubstitution(Hashtable substitution)
public String toString()
public String debugString()
Copyright © 2015. All rights reserved.