public class PrologSession
extends java.lang.Object
PrologSession
handles the connection with the Prolog Server.
Currently only synchronous connections with the server are supported.Modifier and Type | Field and Description |
---|---|
static java.lang.String |
SESSION_ATTRIBUTE_NAME
The
HttpSession session attribute name. |
Constructor and Description |
---|
PrologSession()
Creates a new
PrologSession instance with default Prolog server
settings. |
Modifier and Type | Method and Description |
---|---|
protected void |
cancelQuery()
Called by the timeout monitor when a query takes too long.
|
void |
connect()
Connects to the Prolog server.
|
static boolean |
debugging() |
void |
disconnect()
Closes the connection with the Prolog server.
|
QueryAnswer |
executeQuery(java.lang.String query)
Sends a query to the Prolog server and waits for the answer before returning
the
QueryAnswer . |
QueryAnswer |
executeQuery(java.lang.String query,
Bindings bindings)
Sends a query to the Prolog server and waits for the answer before returning
the
QueryAnswer . |
QueryAnswer |
executeQuery(java.lang.String query,
Bindings bindings,
java.lang.String sessionID)
Sends a query to the Prolog server and waits for the answer before returning
the
QueryAnswer . |
java.lang.String |
getHost()
Returns the host of the Prolog server (exactly as registered in
setHost() ). |
int |
getPort()
Returns the port of the Prolog server.
|
static PrologSession |
getPrologSession(java.lang.String name)
Returns the
PrologSession registered in JNDI with the given name. |
static PrologSession |
getPrologSession(java.lang.String name,
javax.servlet.http.HttpSession httpSession)
Returns the
PrologSession registered in JNDI with the given name. |
int |
getTimeout()
Returns the timeout in milliseconds before the connection to the Prolog
server is reset (when a query is not answered).
|
boolean |
isAlwaysClosing() |
boolean |
isAutoConnecting()
Returns the state of the auto connect mode.
|
boolean |
isConnected()
Returns
true if a connection with the Prolog server is open and
false otherwise. |
void |
setAlwaysClose(boolean close)
Set whether to close connections after each send.
|
void |
setAutoConnect(boolean autoConnect)
Sets the connection mode of this
PrologSession . |
void |
setHost(java.lang.String prologServerHost)
Sets the host of the Prolog server (default is
localhost ). |
void |
setPort(int prologServerPort)
Sets the port of the Prolog server (default
8066 ). |
void |
setTimeout(int timeout)
Sets the timeout in milliseconds before the connection to the Prolog server
is reset (when a query is not answered).
|
public static final java.lang.String SESSION_ATTRIBUTE_NAME
HttpSession
session attribute name.public PrologSession()
PrologSession
instance with default Prolog server
settings.public static boolean debugging()
public int getTimeout()
public void setTimeout(int timeout)
0
will disable timeouts for this prolog session. Default is 2000 milliseconds.
When a connection times out it will be closed silently.timeout
- int
timeout in milliseconds or 0
to disable
timeoutspublic static PrologSession getPrologSession(java.lang.String name)
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 the
PrologSession
with the given name for this method to work.name
- the name of the prolog sessionnull
if no such session could be
foundpublic static PrologSession getPrologSession(java.lang.String name, javax.servlet.http.HttpSession httpSession)
PrologSession
registered in JNDI with the given name. The
PrologSession
will make use of sessions and the session id will be
the same as in the HTTPSession
. Use this method in web application
servers with support for servlets and HTTPSession
(and when support
for sessions is desired). Note: This will cause the PrologSession
to
include the session id in its queries.name
- the name of the prolog sessionhttpSession
- the http sessionpublic int getPort()
public void setPort(int prologServerPort)
8066
).prologServerPort
- the port of the Prolog serverpublic java.lang.String getHost()
setHost()
).public void setHost(java.lang.String prologServerHost)
localhost
). The host
can be specified as either IP-address or host name.prologServerHost
- the host as an IP-address or host namepublic boolean isAlwaysClosing()
public void setAlwaysClose(boolean close)
false
.close
- whether to close after each sendpublic void setAutoConnect(boolean autoConnect)
PrologSession
. If set to
true
it will ensure that it is connected to the Prolog server as soon
as a call to executeQuery()
or anything
else causing a need for communication happens. This is by default set to
false
.autoConnect
- whether to auto-connectpublic boolean isAutoConnecting()
setAutoConnect(boolean)
public QueryAnswer executeQuery(java.lang.String query) throws java.io.IOException, IllegalCharacterSetException
QueryAnswer
. Anonymous variables (underscore,
_
), will be ignored, and thus not accessible in the
QueryAnswer
. executeQuery
throws
IOException
if communication problems with the
server occurs. Please note: executeQuery
will only return one answer.query
- the query to send to the prolog server. The characters in the
query are restricted to ISO-8859-1.java.io.IOException
- if an error occurs. A possible cause is a timeout.IllegalCharacterSetException
- if the query contains non-ISO-8859-1 characterssetTimeout(int)
public QueryAnswer executeQuery(java.lang.String query, Bindings bindings) throws java.io.IOException, IllegalCharacterSetException
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."));
query
- the query to send to the prolog server The characters in the query
are restricted to ISO-8859-1.bindings
- the variable bindings to use in the queryjava.io.IOException
- if an error occurs. A possible cause is a timeout.IllegalCharacterSetException
- if the query contains non-ISO-8859-1 characterssetTimeout(int)
public QueryAnswer executeQuery(java.lang.String query, Bindings bindings, java.lang.String sessionID) throws java.io.IOException, IllegalCharacterSetException
QueryAnswer
. bindings
are variable bindings
for the given query and will ensure that the values are stuffed correctly.query
- the query to send to the prolog server The characters in the query
are restricted to ISO-8859-1.bindings
- the variable bindings to use in the querysessionID
- the session id to give to the prolog serverjava.io.IOException
- if an error occurs. A possible cause is a timeout.IllegalCharacterSetException
- if the query contains non-ISO-8859-1 characterssetTimeout(int)
public void connect() throws java.io.IOException
executeQuery()
will automatically connect to the server when called.java.io.IOException
- if there is an error connectingpublic boolean isConnected()
true
if a connection with the Prolog server is open and
false
otherwise.public void disconnect()
connect()
.protected void cancelQuery()