Re: Loading database from string (C API)

Jan Wielemaker <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
On 12/12/2013 01:08 PM, Edd Barrett wrote:
> Hello again,
>
> On Wed, Dec 11, 2013 at 03:59:09PM +0100, Jan Wielemaker wrote:
>> On 12/11/2013 03:41 PM, Edd Barrett wrote:
>>
>> Search for blob.  Basically, if PL_term_type() gives PL_ATOM, it
>> may be a blob or a real atom.  Use PL_is_atom() to find out.
>>
>
> With this workaround applied I am now at the point where I am calling
> load_files/2, as suggested earlier in this thread. Sadly something
> is amiss; ideally I would have a C program to demonstrate, but this
> would be a fairly large piece of code and I would have to translate it
> from Python CFFI to C. Before I go there let's see if anything obvious
> springs out.
>
> So I have constructed the predicate and arguments needed to call
> load_files(pl_source, [stream(pl_stream)]), where pl_source is an atom to
> identify the code in the stream (as described in the docs) and pl_stream is
> the stream opened using the open_stream C function that we have already
> discussed. For testing purposes I have passed the theory "f(1)." to the
> function.
>
> The call to load_files/2 (in Python/CFFI) looks like this:
> pl_qid = self.libpl.PL_open_query(self.ffi.NULL, self.libpl.PL_CATCH_Q_CATCH_EXCEPTION, pl_pred, pl_args)
>
> pl_pred is a the result of a PL_predicate() call for load_files of arity
> 2. pl_args is an 'array'(?) of term refs as given by PL_new_term_refs().
> To check these are good I can call PL_get_chars with CVT_WRITE_CANONICAL:
>
> Arg 1: swithon_theory -- arbitrary name chosen for the stream
> Arg 2: '.'(stream(<stream>(0x18ebfd824c00)),[]) -- list with one element, the stream
>
> Looks OK. The qid returned looks valid too. The call to open_query is
> followed by: pl_sol = self.libpl.PL_next_solution(pl_qid);
>
> At this point we use PL_exception() to check for an exception etc.
>
> The problem is execution never gets past the call to PL_next_solution.
> The interpreter seems to be stuck in a loop printing:
>
> ---8<---
> ERROR: Syntax error: Unexpected end of clause
> ERROR:
> ERROR: ** here **
> ERROR: .
> ERROR: Syntax error: Unexpected end of clause
> ERROR:
> ERROR: ** here **
> ERROR: .
> ERROR: Syntax error: Unexpected end of clause
> ...
> ---8<---
>
> Stuff I have tried:
>   * Remove dot on the end of the theory string -- same error.
>   * Varying the theory string in other ways -- no change.
>   * Try different PL_Q_* -- no difference.
>   * Change character encoding on the stream -- same.
>
> Is that expected behaviour?

I guess not.  I'd replace the call to load_files/2 by something else and 
then dump the content of the stream.  E.g.

test_files(_Id, [stream(Stream)]) :-
	copy_stream_data(Stream, user_output),
	flush_output(user_output).

That should print f(1). to the user output.  Most likely it will not
do that.  The precise output might give you a hint what is wrong.

I'm quite sure that load_files/2 this way is ok.  You can of course
also test that by loading a real file from a stream by opening the
file yourself.

	Success --- Jan
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.