3

When I use JPL (from JavaSE 1.8), Prolog (SWI-Prolog version 8.2.2) can return an error message without throwing an exception. E.g. when using consult and the file has errors:

import org.jpl7.Query;

public class Test {
  public static void main(String[] args) {
    try {
      String t1 = "consult('test.pl')";
      Query q1 = new Query(t1);
      q1.hasNext();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

I get the output at the console:

ERROR: test.pl:1:23: Syntax error: Unexpected end of file

But no exception is thrown. Therefore my Java program cannot know that the consulted file has errors. The file test.pl that I used in this simple example only contains a simple predicate with a syntactical error:

brother(mike, stella)..

What can I do so that my Java program can catch this error? A post with a similar title doesn't seem to resolve this issue...

Maybe I can use a syntactical checking method either from JPL, or another source, any concrete ideas?

  • Is the initialization actually correct. According to [this page](https://github.com/ssardina-research/packages-jpl/wiki/Getting-Started---An-Example-Tutorial) you actually have to spell the term out (evidently laborious so it may well have changed): `Query q1 = new Query( "consult", new Term[] {new Atom("test.pl")} );` – David Tonhofer Feb 15 '21 at 23:13
  • Yes it works. But the thing is that `consult/1` _itself_ is happy with a bad file. It succeeds! Interesting. – David Tonhofer Feb 15 '21 at 23:24
  • But I don't know how to solve it. How do you make Prolog fail if the file contains errors (and best transactionally _not_ assert any rules from that file?) – David Tonhofer Feb 15 '21 at 23:39
  • 1
    I managed to solve it by redirecting output to a file when executing the java code and then by opening the file and searching the last instance of the filename (e.g. test.pl). However this can be problematic because a) output files can get very large, thus degrading performance and b) I want to do this from a spring boot restful service... – Nikos Spanoudakis Feb 16 '21 at 13:28
  • Maybe there is a Prolog linter which can be applied to the file-to-be-loaded first. If it coughs, put the file aside. On a different, it sounds like you are trying to implement a REST-based protocol fronting a Prolog Engine? Have you taken a look at [Pengines](https://eu.swi-prolog.org/pldoc/doc_for?object=section(%27packages/pengines.html%27)) which provides a HTTP protocol (stateful, as pure statelessness doesn't do). – David Tonhofer Feb 16 '21 at 14:10
  • Thank you @DavidTonhofer, however I want a Java solution... Do you think there is one? Pengines is a completely different architecture... This is about JPL... – Nikos Spanoudakis Feb 17 '21 at 17:13

1 Answers1

0

It finally occurred to me to try to use the terminal to get the error or warning messages. I used the Java Runtime class to execute swipl.exe with the file in question as parameter. Needed a little processing of the output but it worked fine. The following block of code demonstrates the solution:

import java.io.BufferedReader;
import java.io.IOException;  
import java.io.InputStreamReader;

public class TestCMD {

    public static void main(String[] args) {
        try {
            String userProjectPath = "Path to the folder of your file, e.g. E:\\";
            String userFilename = "Your file name, e.g. test.pl";
            Process p = Runtime.getRuntime().exec("\"Path to swipl.exe, e.g. C:\\Program Files\\swipl\\bin\\swipl.exe\" -o /dev/null -c " + userProjectPath + userFilename);
            p.waitFor();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (InterruptedException e2) {
            e2.printStackTrace();
        }
    }
}

This solution is also given at my blog.

  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/29911452) – Flair Sep 24 '21 at 20:51