CiaoJava
Class PLConnection

java.lang.Object
  |
  +--CiaoJava.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(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.
 
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.
 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 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.
Method Detail

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.
Parameters:
goal - Prolog term that will be evaluated as a Prolog goal.
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.

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.

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.

join

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

joinSocketHandlers

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

getPreviousConnection

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