Subject: Launching Prolog from Java

From: "Pierre Nugues" <[email-masked]>
Date: Tue, 28 Nov 2000 16:39:57 +0100

Dear all,

Many thanks to Paul, Jan, Fred, Steffen and Uwe that helped me to write a
program to launch Prolog from Java.

Here is the redirection program. A Launcher class creates a PrologReader
object. PrologReader launches the Prolog program myprog.pl and sends it the
clauses it reads from the user.

Myprog accepts clauses "a., "b." and "halts." and loops on the Java input
until it halts.
The Prolog program must be an executable. The saved state must be compiled
using PLCon. Here is the command to obtain it:
plcon --goal=on_demarre --stand_alone=true -o myprog -c myprog.pl

I would like to put this code in the collaborative page but I have a couple
of problems to solve before. The first one that I can't open a file in the
Prolog program. In myprog.pl there is a quoted init clause that opens and
closes a file. If I unquote it, the java program crashes and I can't
understand why. Is this a bug?
Second, apparently the Prolog program must be compiled before before Java
launches it. It's impossible to launch PLcon from Java and to load the
myprog.pl file using for instance
plcon -g "load_files('\\windows\\bureau\\essai\\myprog.pl',
[silent(true)])" -t on_demarre
from Java.

Pierre
--
The Java Code
/*
 *
 * Created on 22 novembre 2000, 10:19
 */

/**
 *
 * @author  Pierre Nugues
 * @version 1.0
 */
import java.io.*;
import java.util.*;

public class Launcher extends Object {

 /**
    * @param args the command line arguments
    */

     public static void main (String args[]) {
  String command;
  if (args.length == 0) command = "c:/mesdoc~1/essai/myprog";
  else command = args[0];
  PrologReader myPrologReader = new PrologReader(command);
  myPrologReader.loop();
     }

}

class PrologReader extends Object {
 Process prologProcess;

 BufferedReader inFromProlog;
 BufferedReader errFromProlog;
 PrintWriter outToProlog;

 BufferedReader inFromUser;

 String command;
    /** Creates new PrologReader */
     public PrologReader(String command) {
  InputStream inFromPrologAux;
  OutputStream outToPrologAux;
  InputStream errFromPrologAux;

  this.command = command;

  // Lauching Prolog and I/O redirection
  try {
   prologProcess = Runtime.getRuntime().exec(command);
   inFromPrologAux = prologProcess.getInputStream();
   outToPrologAux = prologProcess.getOutputStream();
   errFromPrologAux = prologProcess.getErrorStream();

   inFromProlog = new BufferedReader(new
InputStreamReader(inFromPrologAux));
   errFromProlog = new BufferedReader(new
InputStreamReader(errFromPrologAux));
   outToProlog = new PrintWriter(outToPrologAux);

   inFromUser = new BufferedReader(new InputStreamReader(System.in));
  } catch (IOException e) {
   System.out.println("Launching Prolog failed");
  }
 }

 public Vector readLinesFromProlog() {
  String prologLine;
  Vector prologLines = new Vector();
  int lineIndex = 0;

  while (true) {
   try {
    prologLine = inFromProlog.readLine();
    System.out.println(prologLine);
   } catch (IOException e) {
    break;
   }
   if (prologLine == null) {
    continue;
   }
   prologLines.addElement(prologLine);
   if (prologLine.startsWith("#p")) {
    break;
   }
   lineIndex++;
  }
  return prologLines;
 }
 public void loop() {
  Vector prologLines;

  String command;
  // Clean the input
  prologLines = this.readLinesFromProlog();

  while (true) {
   try {
    command = inFromUser.readLine();
   } catch (IOException e) {
    System.out.println(e);
    prologProcess.destroy();
    break;
   }
   if (command.equals("halt.")) {
    outToProlog.println(command);
    prologProcess.destroy();
    break;
   }
   outToProlog.println(command);
   outToProlog.flush();

   prologLines = this.readLinesFromProlog();
  }
 }
}

------------------------------------------------------------
The Prolog Code myprog.pl
loop :-
 read(X),
 X,
 write('#p'), nl,
 loop.

a  :- write('c''est a'), nl.
b :- write('c''est b'), nl.

on_demarre :-
 write('on démarre'), nl,
 write('#p'), nl,
% init,
 loop.

init :-
 see(param_file),
 % init some parameters
 seen.

--
Pierre Nugues, Professeur d'informatique ([email-masked])
ISMRA, 6, bd du Maréchal Juin, F-14050 Caen, France
Tél. (33) 231-452-705 -- Fax. (33) 231-452-760
http://www.ensicaen.ismra.fr/~nugues