Friday, August 7, 2009

Implementation of Java Interface to Prolog (JPL)

Following post is the outcome of the time I spent trying to connect a module written in Prolog with another module written in Java.

Steps for implementing Java Prolog Interface - JPL

I was trying to interface Java and Prolog using JPL(Java Interface to Prolog). I faced many problem and finally got it working. So I am posting this reply so that if someone else is trying to do this may be it will help him/her.

The following implementation is for windows XP.

A string ("an english sentence") was read from user on an HTML page (form) as input using a Java servlet which then passesthe input to a Prolog module which parses the sentence and returns the output.

To start with, first we hav to install few things:

1. Install SWI-Prolog Version 5.6.64 . It contains the JPL package so, no need to download it separately.
2. I used Eclipse IDE for Java EE Developers for my implementation.
3. Look for "jpl.dll" file in the bin directory of prolog, in my case it was "c:\pl\bin". copy-paste it to "c:\windows\system32" or set the windows path variable to the "c:\pl\bin".
4. Now look for the "jpl.jar" in the prolog installation folder. Most probably it'll be in "lib" folder of prolog installation. For me it was "c:\pl\lib" and add it to the java library when creating the webproject in eclipse.
So , now we are ready to call a prolog code from java.

I am giving a simple example in which we call a prolog code from java and get the output.

import jpl.Atom;
import jpl.Query;
import jpl.Term;
import jpl.Variable;
public class J2pl1{
  public static void main(String[] args) {
    // creating a query to consult the prolog program
    if (!Query.hasSolution("consult('D:/PROLOG/learn/').")) {
      System.out.println("Consult failed");
    } else {
  static void test3a(String sent) {
    // a variable which will get the output.
    Variable X=new Variable("X");
    //creating query object to make a query to prolog code.
    Query q3 = new Query("parent", new Term[] {X,new Atom(sent)});
    System.out.println("Parent of "+sent+" is "+q3.oneSolution().get("X"));//get the value stored in X

The prolog code which was called was:

% parent(C,P) is true when C has a parent called P
parent(charles1, james1).
parent(elizabeth, james1).
parent(charles2, charles1).
parent(catherine, charles1).
parent(james2, charles1).
parent(sophia, elizabeth).
parent(george1, sophia).

So this was a simple implementation for JPL.
Now what we tried to do here was to pass an English sentence to Prolog and get the output. In my case, the output was a XML file.
Now in the above java code the arguments of the predicate was passed as atom.

Query q3 = new Query("parent", new Term[] {X,new Atom(sent)});

So, in case if your prolog code starts with accepting a list as argument in your predicate the above will not work and I think there is no method in JPL to pass a list to prolog.
But no need to worry as it can be done by a simple tweak.
So what we can do? We'll pass the sentence as an atom. So, no need to change the java code.
What we have to do is to include one more predicate which accepts an atom as input, convertes it into chars or code and then calls ur main fucntion using the builtin predicates "atom_chars(?Atom,?Charlist)" or "atom_codes(?Atom,?String)". To get more details, Check this out.
So, it'll be like as if a predicate is called "mycode(String)" to start ur prolog execution. Add following:


And it's done.
I hope it's clear enough. 



Follow by Email