1

I have a Query generated by my Java Program in Query Variable. I want to send this to SWI-Prolog to get the solution of this. The following shows the subcode I wrote in Java to communicate with SWI-Prolog.

  Process P= new ProcessBuilder("C:\\Program Files\\swipl\\bin\\swipl-win.exe").start();
        BufferedReader processOutput = new BufferedReader(new InputStreamReader(P.getInputStream()));
        BufferedWriter processInput = new BufferedWriter(new OutputStreamWriter(P.getOutputStream()));

        String commandToSend = Query;
         processInput.write(commandToSend);

But the SWI-Prolog Window opens up but the comand I am writing to ProcessInput is not being sent to SWI-Prolog. Can someoe suggest me the best way to do this? I need the output of the Query Execution from SWI-Prolog in a File as well.

  • Using a `ProcessBuilder` seems to be a cumbersome way to interface Java and Prolog. Have you considered using a different API? http://stackoverflow.com/q/4303931/1407656 – toniedzwiedz Apr 21 '14 at 20:40
  • I found JPL which can be used as an interface for Java and SWI-Prolog. But even that is not informative enough... – AnanthaPadmanabhan Apr 21 '14 at 20:43

1 Answers1

0

Save your query to external text file and execute swipl from command line (with appropriate options set) and redirect output to another text file. Then, read that output file.

Example:

  1. Save your query to QUERY.TXT as :- query.
  2. Run swipl with redirection to OUTPUT.TXT: swipl -s QUERY.TXT -g halt --quiet > OUTPUT.TXT
  3. Read contents of OUTPUT.TXT

Also, I suspect you should use combination of Runtime.getRuntime().exec(...) and waitFor(...) to wait for swipl for generating output.

Grzegorz Adam Kowalski
  • 5,243
  • 3
  • 29
  • 40