The Java interface is centered around the class PrologSession
,
which represents a connection (or session) to a Prolog
server. PrologSession
contains static methods for looking up
named PrologSession
instances using JNDI (Java Naming and
Directory Interface) as well as methods for querying the Prolog server.
Other important classes are: QueryAnswer
, which contains the
answer for a query sent to the Prolog server; Term
, which
represents a Prolog term; and Bindings
, which supports stuffing
of variable values used in queries.
General information about Java, Servlets and JNDI is available at the Java Technology site: http://java.sun.com/
A brief description of the methods in the provided Java classes are presented below. More information about the Java APIs is available in the JavaDoc files in the directory http://www.sics.se/sicstus/docs/.
The PrologSession
object is the connection to the Prolog
server. The constructor PrologSession()
creates a PrologSession
with the default settings
(host = localhost, port = 8066
.
PrologSession
contains the following methods:
(String name)
returns the
PrologSession
registered in JNDI with the given name. Use this method in application servers where services are registered using JNDI. Please note: the application server must be configured to register thePrologSession
with the given name for this method to work. See Tomcat configuration in PB Ex Tomcat.
(String name, HTTPSession session)
returns the
PrologSession
registered in JNDI with the given name. ThePrologSession
will make use of sessions and the session id will be the same as in theHTTPSession
. Use this method in web application servers with support for servlets andHTTPSession
(and when support for sessions is desired).
()
returns the host of the Prolog server (exactly as registered in
setHost
).
(String prologServerHost)
sets the host of the Prolog server (default
localhost
). Either IP-address or host name is allowed.
(int port)
sets the port of the Prolog server (default
8066
).
()
connects to the Prolog server. By default the
executeQuery
will automatically connect to the server when called.
(boolean autoConnect)
sets the connection mode of this
PrologSession
. If set totrue
it will ensure that it is connected to the Prolog server as soon as a call toexecuteQuery
or anything else causing a need for communication happens. This is by default set totrue
(String query)
sends a query to the Prolog server and waits for the answer before returning the
QueryAnswer
. Anonymous variables (underscore, `_'), will be ignored, and thus not accessible in theQueryAnswer
.executeQuery
throwsIOException
if communication problems with the server occurs. Please note:executeQuery
will only return one answer.
(String query, Bindings bindings)
sends a query to the Prolog server and waits for the answer before returning the
QueryAnswer
.Bindings
are variable bindings for the given query and will ensure that the values are stuffed correctly. An example:QueryAnswer answer = executeQuery("evaluate(In,Out)", new Bindings().bind("In","4*9."));
The QueryAnswer
contains the answer (new bindings) for a query
(or the error that occurred during the query process).
QueryAnswer
inherits from Bindings
, and extends and
modifies it with the following methods:
(String variableName)
returns the value of the given variable. If there is a value a
Term
(a parsed Prolog term) is returned, otherwisenull
is returned. All bindings from the query are available in theQueryAnswer
.
()
returns
true
if the query failed (e.g. the Prolog returnedno
). In this case, there will be no answers (no new bindings, andisError
will returnfalse
).
()
returns the error message (which is only set if there was an error, otherwise it will be
null
).
The Term
object is for representing parsed Prolog terms, and has
the following methods:
()
returns
true
if and only ifTerm
is a compound term with principal functor./2
.
()
returns
true
if theTerm
is an instance ofPBString
(which can be used for fast string access by a type-cast toPBString
and the use of the methodgetString()
that returns the string).
()
returns the functor name of the
Term
(seefunctor/3
). If the Term represents a variable (isVariable()
returnstrue
), the variable name is returned.
()
returns the number of arguments of this term (e.g.
parent(A1,A2)
would return 2) (seefunctor/3
). If the term is not a compound term,getArity()
will return 0.
(int index)
returns the
Term
representing the argument at the position given by index. If there are no arguments, or if an argument with the specified index does not exist,IndexOutOfBoundsException
will be thrown. The first argument has index one (seearg/3
).
Bindings
is used for binding variables to values
in a query sent to the Prolog. The values will be automatically
stuffed before they are sent to the Prolog server.
(String name, int value)
binds the variable with the given name to the given value. Please note: this method is also available for values of type
long
,float
,double
, andTerm
.
(String name, String value)
binds the variable with the given name to the given value. The value will be seen as a list of
UNICODE
character codes in Prolog.