Αρχείο κατηγορίας jpl

Retrieving ERROR messages from SWI-Prolog using Java

When executing prolog programs from java (either using the terminal or JPL) it is important to be able to find out if the prolog program has problems. Such programs may be uploaded by users, as in the case of Gorgias Cloud.

To get the output you need to execute your prolog file from the java runtime. Use the snippet below to get a demo:

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();
    }
  }
}

Debugging a Java application querying Prolog using Eclipse

You normally start a new eclispe java project, let’s name it TestPrologInterface (see Figure 1 below).

To use JPL you need to import the jar file in your project. You right-click on the project’s name and click on Properties (or from the Eclipse menu bar you select Project->Properties). Then on the Libraries tab you click on the Add External JARs… button and select the jpl.jar (see Figure 2 below).

Then, you can add a new class and paste in the code from my previous blog entry (see Figure 3 below).

Subsequently, you will need to setup a run configuration for executing/debugging your project. To do that you select on the Eclipse menu bar the Run->Run Configurations… option, then you right-click on Java application and select New. Then you add the names of the project and main class (see Figure 4 below).

The next thing to do is define the right environment variables. To do that you need a command prompt where you will execute the command:

  "c:\Program Files\swipl\bin\swipl.exe" --dump-runtime-variables=cmd

See the resulting environment variables in Figure 5 below.

Now you are ready to configure the variables on eclipse. In the run configuration go to the Environment tab and then add the variables one by one (click Add and insert the information from the previous screen, from each line the left hand side as name and the right hand side as value). You can see the results in Figure 6 below.

Finally, you run the configuration by clicking on Run (bottom right). The result is shown at the bottom of Figure 7, where you see the eclipse screenshot including the source code.

If you want to export the project to a runnable jar file you right click on the project name and select Export. In the dialog that opens you select Java->Runnable jar file. In the next dialog you define the name of the exported jar and don’t forget to check the Package required libraries into generated JAR if you want the jpl.jar file to be included (see a screenshot in Figure 8).

Now you are ready to distribute your application. Distribute it with the batch file that I used in my previous blog entry.

Figure 1: Create a new project dialog
new_project

Figure 2: Add the JPL JAR file to library
add_external_jars

Figure 3: New class dialog
new_class

Figure 4: Create a new run configuration dialog
new_run_conf

Figure 5: Using the command line to find the SWI-Prolog environment variables
find_variables

Figure 6: The environment tab in the new run configuration dialog
new_run_conf_env

Figure 7: The java class and the execution output
code_and_run_results

Figure 8: The runnable JAR export dialog
runnable_jar

Querying Prolog from Java

This blog entry is for those who want to use Prolog queries from their Java program. I use swi-prolog, a free implementation of the Prolog language.

The SWI-prolog installation includes the java jpl7 package (in the lib installation folder):
http://jpl7.org/JavaApiOverview.jsp

To use it from java you need to setup some system variables before executing your app. Moreover, the java and swi-prolog installations must both be 32 bit or 64bit. For example you can edit a run.bat batch file with the following commands (assuming that swi-prolog is installed in c:/program files/swipl and your app is in the executable <your jar name>.jar file):

  set SWI_PROLOG_PATH=c:\program files\swipl
  set path=%path%;%SWI_PROLOG_PATH%\bin\;%SWI_PROLOG_PATH%\lib\
  swipl.exe --dump-runtime-variables=cmd > plvars.bat
  call plvars.bat
  java -jar <your jar name>.jar

This approach assumes that the jpl.jar library file is included in your jar file, otherwise you must add the jpl.jar to the classpath variable. In the next blog entry I explain how to do that using eclipse.

See an example that adds in the fact father(john,nikos) to the prolog database and then runs the query father(X,nikos):

  
import java.util.Map;
import org.jpl7.*;

public class TestPrologInterface {

  public static void main(String[] args) {
    String s = "assert(father(john,nikos)).";
    Query q = new Query(s);
    // run the query
    if (q.hasNext()) {
      // now I can ask for the father of nikos
      s = "father(X,nikos).";
      q = new Query(s);
      // run the query, repeat while there are more results
      while (q.hasNext()){
        //get a mapping of variables to instantiations:
	Map<String, Term> map = q.next();
	//get the instance of variable X
	String varX = map.get("X").toString();
	//print variable instance
	System.out.println(varX);
      }
    }
  }
}