The Pengines JavaScript API allows a web application developer to create a pengine object like so:
var pengine = new Pengine(options);
options is a JavaScript object contaning zero or more of
server:string
- A URL pointing to the Pengines server on which to create the pengine. Default is the server from which pengines.js is loaded.
application:string
- The name of the application in which the pengine is to be run. Default is "pengine_sandbox".
ask:string
- The query passed to the pengine immediately after its creation. By default no query is passed. Using this option will typically save one network roundtrip and thus using it with a deterministic query will result in just one roundtrip being made.
template:string
- A Prolog variable (or a term containing Prolog variables) shared with the query. Meaningful only if the ask option is specified. By default the value of this option is the variable bindings of the query passed in the ask option (a list of Name=Var pairs). Variable names in the query starting with an underscore character will however not appear in the list.
chunk:integer
- The maximum number of solutions to retrieve in one chunk. 1 means no chunking (default).
destroy:boolean
- Determines if the pengine is to destroy itself after having run a query to completion. Defaults to
true
.
src_text:string
- Prolog source code to be injected in the pengine before attempting to solve any queries.
src_url:string
- A URL resolving to a file containing Prolog source code to be injected in the pengine before attempting to solve any queries.
format:string
- Determines the format of event responses. Format is a string, either
json
(default), json-s
or json-html
. In addition, new formats can be added by defining event_to_json/3
. See library(pengines_io)
.
oncreate:function
- JavaScript function to be called when a pengine has been created. The expression
this.id
in the scope of the function points to the id of the pengine.
onsuccess:function
- Called when the pengine responds with a successful answer to a query. In the scope of the function the expression
this.data
points to a list of objects each representing a solution to the query, this.more
evaluates to a boolean indicating whether more solutions may exist, andthis.id
points to the id of the pengine returning the answer.
onfailure:function
- Called when the pengine fails to find a solution. The expression
this.id
points to the id of the pengine reporting the failure.
onerror:function
- Called when the pengine throws an error. The expression
this.data
in the scope of the function evaluates to an error message in the form of a string. The expression this.id
points to the id of the pengine returning the error.
onprompt:function
- Called when the pengine evaluates the
pengine_input/2
predicate. The expression this.data
in the scope of the function evaluates to a prompt in the form of a string or a JavaScript object. The expression this.id
points to the id of the pengine producing the prompt.
onoutput:function
- Called when the pengine has evaluated the built in
pengine_output/1
predicate. The expression this.data
in the scope of the function evaluates to a string or a JavaScript object. The expression this.id
points to the id of the pengine generating the output.
onstop:function
- Called when a running query has been successfully stopped. The expression
this.id
points to the id of the pengine having been stopped.
onabort:function
- Called when a running query has been successfully aborted. The expression
this.id
points to the id of the pengine having been aborted.
ondestroy:function
- Called when the pengine has been successfully destroyed. The expression
this.id
points to the id of the pengine having been destroyed.
A pengine object also provides access to the following fields and methods.
pengine.id
- Evaluates to the id of the pengine (a string). Note that the pengine must have been created before this field will have a non-null value, i.e. the oncreate handler must have been called.
pengine.ask(query, options)
- Runs query in search for the first solution. Throws an error if the query is syntactically or semantically malformed or if running it could compromise the safety of the server. options is a JavaScript object contaning zero or more of
template: string
- A Prolog variable (or a term containing Prolog variables) shared with the query. Meaningful only if the ask option is specified. By default the value of this option is the variable bindings of the query passed in the ask option (a list of Name=Var pairs). Variable names in the query starting with an underscore character will however not appear in the list.
chunk: integer
- The maximum number of solutions to retrieve in one chunk. 1 means no chunking (default).
pengine.next()
- Triggers a search for the next solution.
pengine.stop()
- Stops searching for solutions. Terminates the running query gracefully.
pengine.respond(string or object)
- Inputs a term in response to a prompt from an invocation of
pengine_input/2
that is now waiting to receive data from the outside. Throws an error if string cannot be parsed as a Prolog term or if object cannot be serialised into JSON.
pengine.abort()
- Terminates the running query by force.
pengine.destroy()
- Destroys the pengine.