Re: Preserving query data in foreign interface

Abramo Bagnara <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Organization BUGSENG srl
Message-ID <[email protected]>
Il 11/08/2013 13:41, Paul Singleton ha scritto:
> On 11/08/2013 11:39, Abramo Bagnara wrote:
> 
>> I'm calling from Java a Prolog query and I need to avoid that returned
>> data is lost.
> 
> The "high level" interface does that for you
> 
>> I'm using low-level interface to avoid term_t <-> Term copying and the
>> very deep stack recursion that this implies when handling long lists.
> 
> Then you'll have to implement your own non-recursive term_t <-> Term
> copying - please donate this to JPL :-)

We have decided (successfully) to use only low level interface so to
avoid this problema and to avoid creation of java objects (we produce
very big prolog compound in java and we elaborate them in Prolog).

>> If I use Prolog.close_query the binding is lost and we have no
>> Prolog.cut_query available.
> 
> Hmm, if your hypothetical Prolog.cut_query retains the binding, then we
> can't reuse the engine until you explicitly close it; I don't think this
> is the way to go...

It is not hypothetical: C interface has it.

>> That apart, I also have noted that in both java interface and C
>> interface it is missing an helper to do deep copy of answers so to
>> preserve multiple results of nondet queries.
> 
> JPL has this; Query#allSolutions returns an array of maps from variable
> names to copied bindings
> 
>> I've missing something? There are other ways to do what above?
> 
> Until term_t <-> Term copying is reimplemented non-recursively (or
> otherwise ingeniously), the best workaround for copying long lists is to
> use a Prolog-side wrapper for long-list-returning predicates which
> converts them to trees (or atoms?), then convert them again Java-side.

Rather ugly, isn't it?

Currently, as fortunately the data obtained from the Prolog query is
rather small, I've written a workaround that extract the data from
results when query is still open in Java objects, then close the query
and rebuild the Prolog result from Java objects.

It works, but nevertheless it seems to me rather uncomfortable. I wonder
if SWI Prolog C interface has the same troubles when trying to save
returned data from queries that should be closed.

As a stupid example (and forgetting for a moment JPL high level
interface that gives support for that copying every Prolog term in java
objects), suppose we want to build from C (or Java) a list of Prolog
terms. One of them should be A as obtained from Prolog query `A is 1
rdiv 2 * 3 rdiv 4'.

Are there simpler (and cheaper when returned data is *big*) way than the
one described above?

-- 
Abramo Bagnara

BUGSENG srl - http://bugseng.com
mailto:[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.