3

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.

Fatalize
  • 3,513
  • 15
  • 25

1 Answers1

3

_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 ?

CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • I don't get how this can help. Say I create a Query `query = new Query("X = 1")`. `query.nextSolution()` will return an empty Hashtable, instead of one containing the binding X = 2. What does `read_term_from_atom` have to do with this? – Fatalize Sep 03 '15 at 19:15
  • What you describe seems a different problem than what is requested in the question. The hashtable should contain the binding `_1 = 1` – CapelliC Sep 03 '15 at 19:25
  • Well it does not, I pointed out " I won't be able to access any bindings after querying" in my question. `_1` is not an anonymous variable `_` but it's like a `don't-want-to-know` variable. It seems logical that I get nothing in the hastable since it's that type of variable, I just dont get why it gets renamed to this, when creating a variable `new Variable("X")` works fine. – Fatalize Sep 03 '15 at 19:29
  • Using `allSolutions` I still get an empty binding. So I guess it comes from my JPL jar or linked libraries. I see that your JPL classes are in package `org.jpl7`, whereas mine are just in `jpl`. I got my jar and the linked libraries from the lib and bin directories of my SWI-Prolog install, and I don't see where else I can get them, there's no download link on the JPL webpage. – Fatalize Sep 03 '15 at 20:02
  • Which would also explain why when I ran queries using JPL on version 7 Strings (with quotes `"`), it acted like an old SWI-Prolog version that considers `"` to be character codes. – Fatalize Sep 03 '15 at 20:05
  • Such basic constructs should not have been changed, but since I build SWI-Prolog from sources, I cannot test right now. If you are on Windows, the easier way should be just to download and reinstall... – CapelliC Sep 03 '15 at 20:11
  • I reinstalled it and the jpl.jar seems now to be the same as yours (package org.jpl7 exists in it), but now a simple query doesn't even run and outputs a lot of errors like `Message: error(instantiation_error,context(system: $set_predicate_attribute/3,_G74))` – Fatalize Sep 03 '15 at 20:52
  • I installed SWI-Prolog on another computer and got JPL to work right away like in your example. I'll accept your answer since it helped me identify exactly what's wrong. – Fatalize Sep 04 '15 at 09:09