Re: Pengine ask and success template

Jan Wielemaker <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
On 02/09/2014 12:44 PM, Torbjörn Lager wrote:
> Hi Anne,
>
> The template is modelled after the template (i.e. the first arg) in
> the all-solutions predicatex. So, the answer to your question is,
> e.g.:
>
>             function handleCreate () {
>                 pengine.ask("q(X, Y)", {
>                     template:'X-Y'
>                 });
>             }
>
> Then, you need to know the structure of the JSON being returned. There
> is a little bit about that in the manual (mapping terms into JSON),
> and another way to find out is
>
>             function handleSuccess() {
>                 $('#out').html(JSON.stringify(this.data));
>             }
>
> Note that in this particular case, nothing is won by using a template.
> You can instead do
>
>             function handleCreate () {
>                 pengine.ask("q(X, Y)");
>             }
>
> Hope this helps!

Yes :-) that still means you must access the data using the somewhat
hard to interpret data[0].args[0] and data[0].args[1] if my memory suits
me well. Right? 2Alternatively, V7 should allow you to write this (not
tested)

	pengine.ask("q(X,Y)"), {template: "_{'X':X, 'Y':Y}"}),

after which you have the much more natural

	data[0].X and data[0].Y

Should that not be the default? Thus, if there is no template and the
result format is json, the server gets the named variables (not starting
with a '_') and generates a JSON object of the form {VariableName:Value,
...} as template. It is an incompatible change, but I think we are still
at the stage where we can make these and simply announce them in the
changelog.  If you agree, I'll modify that.

	Cheers --- Jan

P.s.	Should we also allow for a new Pengine() that already involves
	the create/ask (and thus is discarded after the query).  So, we
	can do this for the simple cases, which will iterate over all
	solutions of q(X,Y)?  Here, onsolution calls the output function
	and next() and finally destroy?  It may automatically chunk and
	call the onsolution function for each returned value?

	new Pengine({ goal:"q(X,Y)",
		      onsolution: function(data) { ... }
		    });

P.s.	Should ask() now be allowed to specify new handlers?  I'd assume
	that if a page create a handler to ask multiple queries, it wants
	different handlers for the various queries?
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.