Re: Pengine ask and success template
Torbjörn Lager <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <CA+Y3JZhpQhwOULKQzZzzYquJ91z=XOk2nRFLUEwTj_4osJP4pA@mail.gmail.com> |
On Sun, Feb 9, 2014 at 7:27 PM, Jan Wielemaker <[email protected]> wrote: > 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? No, hangon, that is not the way that it used to work! It used to be done like this, i.e. in the way that you write below: data[0].X and data[0].Y So hopefully your memory does NOT suit you right. :-) > 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) { ... } > }); I agree that something like this should be possible. I have been playing with two ideas here: 1) Introduce an ask(Query) option and a template(Term) option for pengine_create/1. This would allow us to return the first solution already with the create event. It saves us making an extra network round-trip. 2) Introduce an option that means "this pengine will only be used for making ONE query, so let's kill it when we are done with one" (Don't know how to name it.) 1) and 2) appears to be orthogonal to each other, and the combination seems to do what you suggest (unless I misunderstand you). And I don't see the need for a new handler such as 'onsolution'. > 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? I tend to think in terms of statemachines here and that pengine_event_loop/1 can be seen as running a single-state statemachine with transistions triggered by events (in combo with conditions) and executing actions (e.g. executing a command such as pengine_ask/3. If we in order to complement pengine_event_loop/1 also had pengine_statemachine/1, we could easily define a statemachine (expressed in some nice notation and maybe even partly by drawing it) that would in essence be able to change from one system of handlers into another when changing states. I think that would work. Cheers, Torbjörn -- Torbjörn Lager Professor of General and Computational Linguistics Department of Philosophy, Linguistics and Theory of Science University of Gothenburg Box 200, SE-405 30 Gothenburg, Sweden Phone: +46317864962