GithubHelp home page GithubHelp logo

erlanger / packages-pengines Goto Github PK

View Code? Open in Web Editor NEW

This project forked from swi-prolog/packages-pengines

0.0 2.0 0.0 1.64 MB

Pengines: Prolog engines

CMake 0.46% Prolog 80.53% Shell 2.17% HTML 0.12% JavaScript 16.33% CSS 0.39%

packages-pengines's Introduction

Pengines

Pengines is short for Prolog Engines. The pengines package greatly simplifies developing JavaScript based web-applications that must talk to a Prolog server and realise distributed programming in Prolog by providing RPC over HTTP.

The JavaScript API

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:string
Prolog source code to be injected in the pengine before attempting to solve any queries.
format:string
Determines the format of event responses, default is json. 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.

Examples

Please refer to documentation at here for working examples.

The Prolog API

The Prolog API is documented in the following document: Prolog API.

Usage in Node.js

Install using NPM: npm install pengines and use:

var Pengine = require('pengines');
var pengine = new Pengine();

and follow the JavaScript API description above.

Testing

To test the Pengines JavaScript API, you need to install required testing dependencies using the npm install command in the project root directory.

Then load test_js.pl in SWI-Prolog:

-? [test_js].

and run tests using run_tests.

License

Pengines is licensed under the BSD 2-Clause license. See the header of web/js/pengines.js file.

packages-pengines's People

Contributors

torbjornlager avatar rla avatar jamesnvc avatar janwielemaker avatar edechter avatar samer-- avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.