From pnugues@greyc.ismra.fr  Tue Nov 28 16:40:10 2000
Received: from greyc.ismra.fr (l2i.greyc.ismra.fr [192.93.101.84])
	by swi.psy.uva.nl (8.9.3/8.9.3) with ESMTP id QAA25201
	for <prolog@swi.psy.uva.nl>; Tue, 28 Nov 2000 16:40:10 +0100 (MET)
Received: from penelope (penelope.greyc.ismra.fr [192.93.101.198])
          by greyc.ismra.fr (8.9.3/greyc-1) with SMTP id QAA02590
          for <prolog@swi.psy.uva.nl>; Tue, 28 Nov 2000 16:42:34 GMT
Message-ID: <00a101c05951$76c63c60$c6655dc0@greyc.ismra.fr>
Reply-To: "Pierre Nugues" <pnugues@greyc.ismra.fr>
From: "Pierre Nugues" <pnugues@greyc.ismra.fr>
To: <prolog@swi.psy.uva.nl>
Subject: Launching Prolog from Java
Date: Tue, 28 Nov 2000 16:39:57 +0100
MIME-Version: 1.0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 8bit
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 5.50.4133.2400
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400

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 (pnugues@greyc.ismra.fr)
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

