CiaoJava
Class PLConnection

java.lang.Object
  |
  +--CiaoJava.PLConnection

public class PLConnection
extends java.lang.Object

Starts and handles a connection to a Prolog process via sockets. The PLConnection can be used in two ways, so the CiaoJava interface can work as a Java object server (using the constructor with no arguments), or as a connection to a Prolog query server. Working with a Prolog server using the Java side as a client, the Prolog goals can be launched using the launchGoal method with a PLTerm object representing a goal (those terms where the isRunnable() method returns true), or creating and using PLGoal objects.


Field Summary
private  boolean allowThreads
           
private static PLTerm DATA_SYNC
           
private static PLTerm EVENT_SYNC
           
private  java.io.BufferedReader evIn
           
private  java.io.PrintWriter evOut
           
private  java.io.BufferedReader plIn
           
private  java.io.PrintWriter plOut
           
private  java.lang.Process plProc
          Private fields.
 
Constructor Summary
PLConnection()
          Creates a PLConnection for the Prolog-to-Java interface: waits for a Prolog process that wants to connect to it.
PLConnection(java.lang.String where)
          Creates a PLConnection to use the Java-to-Prolog interface.
 
Method Summary
 boolean allowThreads()
          Asks the Prolog server if it can work with threads.
private  void createSockets(java.io.PrintStream out)
          This private method creates and synchronizes the sockets for communication with the Prolog process.
protected  PLTerm fromProlog()
          Prolog-to-Java communication.
private  PLTerm fromProlog(java.io.BufferedReader in)
          Low level Prolog-to-Java communication.
 PLGoal query(PLTerm term)
          Goal launching.
protected  void toProlog(PLTerm term)
          Low level Java-to-Prolog communication.
private  void toProlog(java.io.PrintWriter out, PLTerm term)
          Low level Java-to-Prolog communication.
protected  void toPrologEvent(PLTerm term)
          Low level Java-to-Prolog event communication.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

plProc

private java.lang.Process plProc
Private fields.

allowThreads

private boolean allowThreads

plIn

private java.io.BufferedReader plIn

plOut

private java.io.PrintWriter plOut

evIn

private java.io.BufferedReader evIn

evOut

private java.io.PrintWriter evOut

DATA_SYNC

private static final PLTerm DATA_SYNC

EVENT_SYNC

private static final PLTerm EVENT_SYNC
Constructor Detail

PLConnection

public PLConnection(java.lang.String where)
             throws java.io.IOException,
                    PLException
Creates a PLConnection to use the Java-to-Prolog interface. Starts the Prolog server process and connects to it creating the sockets.
Parameters:
where - command used to start the Prolog server process.
Throws:
java.io.IOException - if there are I/O problems.
PLException - if there are problems regarding the Prolog process.

PLConnection

public PLConnection()
             throws java.io.IOException,
                    PLException
Creates a PLConnection for the Prolog-to-Java interface: waits for a Prolog process that wants to connect to it.
Throws:
java.io.IOException - if there are I/O problems.
PLException - if there are problems regarding the Prolog process.
Method Detail

allowThreads

public boolean allowThreads()
Asks the Prolog server if it can work with threads.
Returns:
true if the Prolog server can work with threads; false otherwise.

createSockets

private void createSockets(java.io.PrintStream out)
                    throws java.io.IOException,
                           PLException
This private method creates and synchronizes the sockets for communication with the Prolog process.

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 created to manage the goal.
Throws:
java.io.IOException - if there are I/O problems.
PLException - if there are problems regarding the Prolog process.

toProlog

protected void toProlog(PLTerm term)
Low level Java-to-Prolog communication. This method sends Prolog terms from Java to Prolog. Transforms the term in a serialized form (using the Prolog format) and sends the result to Prolog through the data socket.
Parameters:
term - is an object representing a Prolog term.

toPrologEvent

protected void toPrologEvent(PLTerm term)
Low level Java-to-Prolog event communication. This method sends Prolog terms to Prolog through the event socket.
Parameters:
term - is an object representing a Prolog term.

toProlog

private void toProlog(java.io.PrintWriter out,
                      PLTerm term)
Low level Java-to-Prolog communication. Private method that sends Prolog terms to Prolog through a given socket.
Parameters:
out - is the socket output stream to send the term through.
term - is an object representing a Prolog term.

fromProlog

protected PLTerm fromProlog()
                     throws java.io.IOException,
                            PLException
Prolog-to-Java communication. This method listens at the Prolog socket to receive results from the Prolog process as terms.
Returns:
Prolog term received from the socket.
Throws:
java.io.IOException - if the socket stream has been broken.
PLException - if there are problems regarding the Prolog process.

fromProlog

private PLTerm fromProlog(java.io.BufferedReader in)
                   throws java.io.IOException,
                          PLException
Low level Prolog-to-Java communication. Private method that listens at a given Prolog socket to receive results from the Prolog process as terms.
Parameters:
in - socket input stream to receive the Prolog data.
Returns:
Prolog term received from the socket.
Throws:
java.io.IOException - if the socket stream has been broken.
PLException - if there are problems regarding the Prolog process.