SWI-Prolog / packages-jpl

JPL: The Prolog <-> Java interface
BSD 2-Clause "Simplified" License
54 stars 33 forks source link

Make Query1(String text, Term[] args) in Query.java **public**? #11

Closed ssardina closed 5 years ago

ssardina commented 6 years ago

Hi,

Class Query.java has a useful public method to convert a string with args into a Term:

private static Term Query1(String text, Term[] args)

Would be very nice to have this as a public method as it is useful to get the term itself, not just the whole query. And maybe rename it to queryBuilder or something?

Can this be done? It is a small change and would help, otherwise I need to replicate that code in my framework.

Thanks in advance,

Sebastian

anionic commented 5 years ago

org.jpl7.Util has public static Term textParamsToTerm(String text, Term[] params) which does this (or is intended to).

NB Query1 is dubiously overloaded: if text merely denotes an atom, it builds a Compound.

ssardina commented 5 years ago

mmm yes you are right, Util.textParamsToTerm(String text, Term[] params) hsa that already. In fact, Query1 uses it (well its non params version) and then injects the params into the ? placeholders...

Now this is interesting... It seems to me we would like to get rid of Query1 and use the already existing tool in org.jpl7.Util.

As you said Query1 seems to be "careful" and transform a text Atom into a Compound, but textParamsToTerm should handle or we should make it to handle that.

In the end, we would like to write Query without using Query1, that is ,we want:

    public Query(String text, Term[] args) {
        this(Util.textParamsToTerm(text, args));
    }

so the strange case would be that text is not a compound text with placeholders ?, but just an atom and you do pass arguments...

presumably you should have as many arguments as ? in text. There must be an error otherwise...

ssardina commented 5 years ago

OK all tested. and is good:

  1. No need to make Query1 public as there is Util.textParamsToTerm(text, args) available.
  2. In fact Query1 is still needed to allow building queries in two ways:
    1. passing the name of the functor, and the args separately. A Compound is built form the two.
    2. passing a string that is the full query but with placeholders ?, where the args will be "injected" To do that Query1 generates a corresponding Term as needed depending on input of text.
  3. Query1 has been renamed to buildQueryTerm
  4. Have added standalone test org.jpl7.test.standalone.QueryBuilder to test all possible ways of building a Query and all possible exceptions that can be thrown.

All this is in commit 2507f1b73c06a46646ec994d6eab62687b85ea69