javaprologswi-prologjpl

Equivalent of "assert" in JPL7


I am currently creating a Java Swing GUI where the user can select what type of fruits they like. Depending on their choice, only certain fruit products will be shown. For instance, if the user selects "grape", then only grape products like grape jam or grape juice are displayed.

The issue is on how to assert some facts into Prolog. For instance, I am trying to assert that the user has selected "grape". TLDR; I am trying to find the JPL equivalent of the SWI-Prolog command of:

assert(selected_fruit(grape)).

Below are 2 of the attempts I have tried.

Query q2=new Query("assert selected_fruit(grape)");
System.out.println(q2.hasSolution());

And another one I tried is the following:

Query q2 = new Query("selected_fruit", new Term[] {new Atom("grape")});
System.out.println(q2.hasSolution());

The first attempt threw out a syntax_error, while the second one threw out an existence_error upon running. If anyone can shed some light, that would be greatly appreciated.


Solution

  • Never mind, I found the answer after much experimentation. The correct way to assert in my case was the following:

    Query q2 = new Query("assert(selected_fruit(grape))");
    System.out.println(q2.hasSolution());
    

    The console should then print out "true".