#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Location
    Juan Les Pins, France
    Posts
    36
    Rep Power
    14

    Problem with Java Prolog Interface


    Editor for Java:
    Eclipse Java EE IDE for Web Developers.
    Build id: 20090621-0832

    Platform: Windows XP

    For Prolog: SWI-Prolog

    Hey guys, I finally figured out on how i can call prolog program in java. But now i am having some other problem and i dont even know wats the problem . So, here is the thing:

    -What i want to do: pass a full sentence or may be more than one sentence with a name for xml file generated in Prolog. The prolog code traverses the sentence and gets some information from it and stores it in the XML file with the name provided.

    Things to consider:
    The prolog part is working fine and is giving the desired output. There are many prolog (*.pl) files but the one which starts the execution is main.pl and the predicate which I call to start the processing is go(String sentence,String file_name). But i don't know what's wrong i am always getting false as output .

    Current Situation:
    When i try this in SWI-Prolog its working fine but when i am calling this from the java code its always giving false.

    Following is the java code which i am using:

    import java.util.*;
    import jpl.Atom;
    import jpl.Query;
    import jpl.Term;

    public class J2p{
    public static void main(String[] args) {
    if (!Query.hasSolution("consult('D:/PROLOG/workspace/main.pl').")) {
    System.out.println("Consult failed");
    } else {
    test3a("I am Mike and I am 22 years old.");
    }
    }
    @SuppressWarnings("deprecation")
    static void test3a(String sent) {
    Random generator = new Random();
    int r = generator.nextInt();
    r=r%10;
    String fname="test"+Integer.toString(r);
    System.out.println("random file name: output"+fname);
    Query q3 =
    new Query(
    "go",
    new Term[] {new Atom(sent),new Atom(fname)}
    );

    System.out.println(
    "go("+sent+","+fname+") is " +
    ( q3.query() ? "Fine " : "Not fine!!!!" )
    );
    }
    }


    The output i am getting everytime is :

    random file name: outputtest3
    go(I am Mike and I am 22 years old.,test3) is Not fine!!!!



    thanks in advance.
    Last edited by pranav8494; August 5th, 2009 at 08:53 AM. Reason: Info about platform, Java and prolog editor used and mistake in description
  2. #2
  3. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5053
    q3.query()
    Why are you using a deprecated method?

    Double file_name
    Double?

    ~
    Last edited by Yawmark; August 5th, 2009 at 08:20 AM.
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Location
    Juan Les Pins, France
    Posts
    36
    Rep Power
    14
    Originally Posted by Yawmark
    Why are you using a deprecated method?


    Double?

    ~
    deprecated method: i have replaced it with .hasSolution()

    Double: Sorry i gave that this wrong.
    The predicate in prolog to start is just:

    go(Sentence, Filename).

    both sentence and Filename are string.

    but still its not working.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Location
    Juan Les Pins, France
    Posts
    36
    Rep Power
    14
    I have tried the same java code for different prolog program. Its running fine just i am getting NULL in the output variable X.

    Following is the code:

    import jpl.Atom;
    import jpl.Query;
    import jpl.Term;
    import jpl.Variable;

    public class J2pl1{
    public static void main(String[] args) {
    if (!Query.hasSolution("consult('D:/PROLOG/learn/familytree.pl').")) {
    System.out.println("Consult failed");
    } else {
    test3a("ria");
    }
    }

    static void test3a(String sent) {
    Variable X=new Variable("X");
    Query q3 =
    new Query(
    "parent",
    new Term[] {X,new Atom(sent)}
    );

    System.out.println(
    "go("+sent+") is " +
    ( q3.hasSolution() ? "Fine " : "Not fine!!!!" )
    );
    System.out.println("Parent of "+sent+" is "+q3.oneSolution().get(X));
    }
    }
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Location
    Juan Les Pins, France
    Posts
    36
    Rep Power
    14

    Thumbs up Steps for implementing Java Prolog Interface - JPL


    Hey guys 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.

    What i was trying to do was that i read a string ("an english sentence") from user on an HTML page and when the user submits the input, a java servlet was called which then calls to a prolog code which parses the sentence and returns the output.

    To start with, 1st u hav to install few things:

    1. install SWI-Prolog Version 5.6.64 . It contains the JPL package so, u dnt need to download it separately.

    2. I used Eclipse IDE for Java EE Developers for my implementation as I was trying to implement with a webpage.

    3. Look for "jpl.dll" file in the bin directory of prolog, in my case it was "c:\pl\bin". copy it and paste it tho "c:\windows\system32" or u can also set the path variable to the "c:\pl\bin".

    4. Now look for the "jpl.jar" in the prolog installation folder. most probably u'll find it in lib folder of prolog instalation. for me it was "c:\pl\lib" and it ur 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/familytree.pl').")) {
    System.out.println("Consult failed");
    } else {
    test3a("ria");
    }
    }

    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 i was calling 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).
    parent(catherine,ria).

    So this was a simple implementation for JPL.

    Now what i was trying to do was pass a 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 ur prolog code starts with accepting a list as argument in ur predicate the above will not work and i think there is no facility in JPL to pass a list to prolog.
    But dont worry it can b done by adding few things.

    So what we can do? We'll pass the sentence as an atom. So, no need to change ur java code.

    What u hav to do is to include one mor 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)". If u want more details Check this out

    So, it'll be like if u want to call a predicate "mycode(String)" to start ur prolog execution. Add following:

    start(Atom):-atom_codes(Atom,String),mycode(String).

    And its done.

    I hope this will help u out.

    Enjoy,
    Pranav.

    Comments on this post

    • NovaX agrees : Congrats, and thanks for the update.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0
    Hi, I know this post is already old, but I'm doing a program for my thesis and I came across a problem, and this post helped me solve a portion of the problem.

    I have this query in java :



    public void testQuery(){

    Variable P = new Variable("P");
    Variable CF = new Variable("CF");
    Query q1 =
    new Query(
    "fact", new Term[] { P, CF });

    while (q1.hasMoreSolutions()) {



    if(!((q1.nextSolution().get("P")) != null) ){

    System.out.println("P = " + q1.nextSolution().get("P"));
    }else {



    System.out.println("P is null!"); }
    System.out.println("CF = " + q1.nextSolution().get("CF"));


    }

    }
    And this is the prolog file which I'm working on:

    :- ensure_loaded('C:/Users/Dharmith/workspace/Thesis/src/cf_solve.pl').

    person( ivo) ? 0.1.
    person( gui) ? 0.2.
    person( tiago) ? 0.3.

    irma ? 0.5.

    irmao( gui, ivo) ? 0.6.
    irmao( tiago, ivo) ? 0.8.

    if ( person(A) and person(B) and person(C) and
    irmao(A,B) and irmao(C,B) )
    then irmaos(A,C) ? 0.9. 0.9.

    The output I'm getting is :

    P is null!
    CF = 0.1
    P is null!
    CF = 0.2
    P is null!
    CF = 0.3
    P is null!
    CF = 0.5
    P is null!
    CF = 0.6
    P is null!
    CF = 0.8
    P is null!
    CF = 0.9

    I don't know why the variable is null, because when I'm debugging the predicates seem to be there.

    I hope you can help me with this problem.

    Thanks already for the guide and info you put on this post as it helped me alot!

IMN logo majestic logo threadwatch logo seochat tools logo