javaprologswi-prologjpl

JPL textToTerm only produces anonymous variables


I'm using JPL to do some SWI-Prolog queries in a Java program. When I want to create a new Query, I would like to be able to use jpl.Util.textToTerm to directly instanciate Terms from a user input, without parsing it myself.

The problem is that this method seems to always parse variable identifiers (i.e. something that starts with a capital letter) as anonymous variables (i.e. something that starts with _).

For example, jpl.Util.textToTerm("X") returns a jpl.Variable that has name _1 instead of X, which is obviously a problem since that means I won't be able to access any bindings after querying.

Creating a jpl.Query directly from a string, like new Query("reverse([1,2],X)") has the exact same problem.


Solution

  • _1 it's not an anonymous variable, so the problem is less important than it appears at first glance.

    Variables with the same name (actually, the same variable) will have the same representation once returned from the JPL interface. Otherwise, you should file a bug of the mailing list...

    You should use read_term family of predicates passing as option variable_names(Vars). For instance, on the REPL

    ?- read_term_from_atom('a(X,Y,X)',T,[variable_names(L)]).
    T = a(_G1434, _G1435, _G1434),
    L = ['X'=_G1434, 'Y'=_G1435].
    

    edit a quick test reusing JPL test infrastructure (I've named the file TestQuery.java)

    import java.util.Map;
    
    import org.jpl7.Query;
    import org.jpl7.Term;
    
    public class TestQuery {
        public static void main(String argv[]) {
            Query q = new Query("X = 1");
            Map<String, Term>[] solutions = q.allSolutions();
            System.out.println(solutions[0]);
        }
    }
    

    outputs

    ./run.sh
    Compiling TestQuery
    
    JPL demo: TestQuery
    
    {X=1}
    

    so, maybe I don't understand your problem in first place, sorry... Are you using an up-to-date installation ?