CiaoJava
Class PLConnection

java.lang.Object
  extended byCiaoJava.PLConnection

public class PLConnection
extends java.lang.Object

Class for managing communication to Prolog. Starts and handles a connection to a Prolog process via sockets. The PLConnection can be used in two ways, using the CiaoJava interface as a Java object server (using PLConnection()), or as a connection to a Prolog query server (using PLConnection(java.lang.String)). Working with a Prolog server using the Java side as a client, the Prolog goals can be launched using query(CiaoJava.PLTerm) method with a PLTerm object representing a goal (PLAtoms and PLStructures) or creating and using PLGoal objects.


Constructor Summary
PLConnection()
          Creates a new PLConnection object, establishing a new socket server and listening Prolog connections on a free port.
PLConnection(int port, java.lang.String[] where)
          Creates a new PLConnection object that executes the Prolog server, and starts it.
PLConnection(java.net.ServerSocket ss)
          Creates a new PLConnection object, given an existing socket server.
PLConnection(java.lang.String where)
          Creates a new PLConnection object that executes the Prolog server, and starts it.
PLConnection(java.lang.String[] where)
          Creates a new PLConnection object that executes the Prolog server, and starts it.
PLConnection(java.lang.String host, int port)
          Creates a new PLConnection object that connects to a Prolog server at host and port given as argument.
 
Method Summary
 void close()
          Deprecated. This method is deprecated. Use stop method instead.
protected  void closeSocketStreams()
          Closes interface sockets and related streams.
 CiaoJava.PLInterpreter getInterpreter()
          Gets the Prolog Interpreter object used to interpret Prolog terms received from the Prolog side of the interface.
static PLConnection getPreviousConnection()
          Returns the last started connection to a Prolog process.
 java.lang.Process getPrologProcess()
           
 void join()
          Waits until all the internal threads terminate.
protected  void joinSocketHandlers()
          Waits until socket handling threads terminate.
 PLGoal query(PLTerm term)
          Goal launching.
 void start()
          Starts the PLConnection for the Prolog-to-Java interface: waits for a Prolog connection.
 void start(java.lang.String host, int port)
          Starts the PLConnection for the Prolog-to-Java interface, connecting to an already executing Prolog server, listening at port given as argument.
 void stop()
          Stops the interface.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PLConnection

public PLConnection()
             throws PLException,
                    java.io.IOException
Creates a new PLConnection object, establishing a new socket server and listening Prolog connections on a free port. The port number on which it listens is printed out on the standard output stream. Once a Prolog process connects to it, starts the internal threads to manage the communication.


PLConnection

public PLConnection(java.lang.String[] where)
             throws PLException,
                    java.io.IOException
Creates a new PLConnection object that executes the Prolog server, and starts it.

Parameters:
where - The command-line to start the Prolog process. This constructor forces that the connection be established to the newly created Prolog process, instead of waiting for any Prolog client that wants to connect to it. This constructor allows an array of strings for command-line arguments.

PLConnection

public PLConnection(java.lang.String where)
             throws PLException,
                    java.io.IOException
Creates a new PLConnection object that executes the Prolog server, and starts it.

Parameters:
where - The command-line to start the Prolog process. This constructor forces that the connection be established to the newly created Prolog process, instead of waiting for any Prolog client that wants to connect to it.

PLConnection

public PLConnection(java.net.ServerSocket ss)
             throws PLException,
                    java.io.IOException
Creates a new PLConnection object, given an existing socket server. This constructor is only useful when creating a Java server that accept concurrent connections from different Prolog processes. Once a Prolog process connects to it, starts the internal threads to manage the communication.

Parameters:
ss - ServerSocket object representing a new Prolog/Java server socket object.

PLConnection

public PLConnection(int port,
                    java.lang.String[] where)
             throws PLException,
                    java.io.IOException
Creates a new PLConnection object that executes the Prolog server, and starts it. The connection port between Java side and Prolog side is fixed to the corresponding argument.

Parameters:
port - Port number to be used for the internal communication to Prolog side.
where - The command-line to start the Prolog process. This constructor forces that the connection be established to the newly created Prolog process, instead of waiting for any Prolog client that wants to connect to it. This constructor allows an array of strings for command-line arguments.

PLConnection

public PLConnection(java.lang.String host,
                    int port)
             throws PLException,
                    java.io.IOException
Creates a new PLConnection object that connects to a Prolog server at host and port given as argument.

Parameters:
host - host name where the Prolog side is waiting for Java connection.
port - Port number to be used for the internal communication to Prolog side.
Method Detail

start

public void start(java.lang.String host,
                  int port)
           throws java.io.IOException,
                  PLException
Starts the PLConnection for the Prolog-to-Java interface, connecting to an already executing Prolog server, listening at port given as argument.

Parameters:
port - port number to which the Prolog server is waiting for Java connection.
Throws:
java.io.IOException - if there are I/O problems.
PLException - if there are problems regarding the Prolog process.

start

public void start()
           throws java.io.IOException,
                  PLException
Starts the PLConnection for the Prolog-to-Java interface: waits for a Prolog connection.

Throws:
java.io.IOException - if there are I/O problems.
PLException - if there are problems regarding the Prolog process.

getInterpreter

public CiaoJava.PLInterpreter getInterpreter()
Gets the Prolog Interpreter object used to interpret Prolog terms received from the Prolog side of the interface.


query

public PLGoal query(PLTerm term)
             throws PLException,
                    java.io.IOException
Goal launching. Evaluates the term received as a query and sends it to Prolog for evaluation.

Returns:
The PLGoal object created to manage the goal.
Throws:
java.io.IOException - if there are I/O problems.
PLException - if there are problems regarding the Prolog process.

close

public void close()
           throws java.lang.InterruptedException,
                  java.io.IOException,
                  PLException
Deprecated. This method is deprecated. Use stop method instead.

Closes the communication to the Prolog side and terminates the Prolog process.

Throws:
java.io.IOException - if the socket stream has been broken.
PLException - if there are problems regarding the Prolog process.
java.lang.InterruptedException

stop

public void stop()
          throws java.lang.InterruptedException,
                 java.io.IOException,
                 PLException
Stops the interface. Stops the internal threads and closes the streams related to this interface.

Throws:
java.io.IOException - if the socket stream has been broken.
PLException - if there are problems regarding the Prolog process.
java.lang.InterruptedException

closeSocketStreams

protected void closeSocketStreams()
                           throws java.lang.InterruptedException,
                                  java.io.IOException
Closes interface sockets and related streams.

Throws:
java.io.IOException - if the socket stream has been broken.
java.lang.InterruptedException

join

public void join()
          throws java.lang.InterruptedException,
                 java.io.IOException
Waits until all the internal threads terminate.

Throws:
java.lang.InterruptedException
java.io.IOException

joinSocketHandlers

protected void joinSocketHandlers()
                           throws java.lang.InterruptedException
Waits until socket handling threads terminate.

Throws:
java.lang.InterruptedException

getPreviousConnection

public static PLConnection getPreviousConnection()
Returns the last started connection to a Prolog process.


getPrologProcess

public java.lang.Process getPrologProcess()