Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pengines

Package Overview
Dependencies
Maintainers
2
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pengines - npm Package Compare versions

Comparing version 1.0.2 to 2.0.0

web/js/pengines.js

45

package.json
{
"name": "pengines",
"version": "1.0.2",
"description": "The Node version of pengines.js - communicating with prolog engines",
"main": "lib/pengines",
"scripts": {
"test": "nodeunit --reporter verbose",
"prepublish": "coffee -o lib/ -c src/pengines.coffee"
},
"devDependencies": {
"coffee-script": ">=1.9.1"
},
"keywords": [
"pengines",
"pengine",
"prolog",
"engine",
"rewrite"
],
"author": "Karl Lundfall <kalle.lundfall@gmail.com>",
"license": "ISC",
"dependencies": {
"request": "^2.55.0",
"string": "^3.1.1"
},
"bugs": {
"url": "https://gitlab.com/kll300/Pengines-for-node"
}
"name": "pengines",
"version": "2.0.0",
"dependencies": {
"najax": "^1.0.4"
},
"devDependencies": {
"jquery": "^3.3.1",
"mocha": "^5.2.0",
"puppeteer": "^1.9.0"
},
"main": "web/js/pengines.js",
"license": "BSD-2-Clause",
"bugs": {
"url": "https://github.com/SWI-Prolog/packages-pengines/issues"
},
"files": [
"web/js/pengines.js"
]
}

161

README.md

@@ -1,93 +0,120 @@

Performing remote Prolog against a server can now be done also in Node.
# Pengines
## Example
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.
```
peng = pengines(
server: "http://localhost:3020/pengine",
chunk: 2,
src_text: 'animal(cat).\n' + 'animal(dog).\n'
ask: 'animal(Animal)'
)
.on 'success', (result) ->
for resultData in result.data
console.log resultData.Animal
```
## The JavaScript API
output:
The Pengines JavaScript API allows a web application developer to create a pengine object like so:
```js
var pengine = new Pengine(options);
```
cat
dog
```
## Functionality
options is a JavaScript object contaning zero or more of
<dl>
<dt>server:string</dt>
<dd>A URL pointing to the Pengines server on which to create the pengine. Default is the server from which pengines.js is loaded.</dd>
<dt>application:string</dt>
<dd>The name of the application in which the pengine is to be run. Default is "pengine_sandbox".</dd>
<dt>ask:string</dt>
<dd>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.</dd>
<dt>template:string</dt>
<dd>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.</dd>
<dt>chunk:integer</dt>
<dd>The maximum number of solutions to retrieve in one chunk. 1 means no chunking (default).</dd>
<dt>destroy:boolean</dt>
<dd>Determines if the pengine is to destroy itself after having run a query to completion. Defaults to true.</dd>
<dt>src:string</dt>
<dd>Prolog source code to be injected in the pengine before attempting to solve any queries.</dd>
<dt>format:string</dt>
<dd>Determines the format of event responses, default is json. New formats can be added by defining event_to_json/3. See library(pengines_io).</dd>
<dt>oncreate:function</dt>
<dd>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.</dd>
<dt>onsuccess:function</dt>
<dd>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.</dd>
<dt>onfailure:function</dt>
<dd>Called when the pengine fails to find a solution. The expression this.id points to the id of the pengine reporting the failure.</dd>
<dt>onerror:function</dt>
<dd>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.</dd>
<dt>onprompt:function</dt>
<dd>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.</dd>
<dt>onoutput:function</dt>
<dd>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.</dd>
<dt>onstop:function</dt>
<dd>Called when a running query has been successfully stopped. The expression this.id points to the id of the pengine having been stopped.</dd>
<dt>onabort:function</dt>
<dd>Called when a running query has been successfully aborted. The expression this.id points to the id of the pengine having been aborted.</dd>
<dt>ondestroy:function</dt>
<dd>Called when the pengine has been successfully destroyed. The expression this.id points to the id of the pengine having been destroyed.</dd>
</dl>
### pengines(options)
A pengine object also provides access to the following fields and methods.
Constructs a pengine. Options:
<dl>
<dt>pengine.id</dt>
<dd>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.</dd>
<dt>pengine.ask(query, options)</dt>
<dd>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
<dl>
<dt>template: string</dt>
<dd>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.</dd>
<dt>chunk: integer</dt>
<dd>The maximum number of solutions to retrieve in one chunk. 1 means no chunking (default).</dd>
</dt>
</dd>
<dt>pengine.next()</dt>
<dd>Triggers a search for the next solution.</dd>
<dt>pengine.stop()</dt>
<dd>Stops searching for solutions. Terminates the running query gracefully.</dd>
<dt>pengine.respond(string or object)</dt>
<dd>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.</dd>
<dt>pengine.abort()</dt>
<dd>Terminates the running query by force.</dd>
<dt>pengine.destroy()</dt>
<dd>Destroys the pengine.</dd>
</dl>
- `server` - A URL pointing to the Pengines server on which to create the pengine.
- `application` - The name of the application in which the pengine is to be run. Default is `swish`.
- `ask` - 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`- 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`- The maximum number of solutions to retrieve in one chunk. 1 means no chunking (default).
- `destroy`- Determines if the pengine is to destroy itself after having run a query to completion. Defaults to `true`.
- `sourceText` - Prolog source code to be injected in the pengine before attempting to solve any queries.
- `format` - Determines the format of event responses. Can be one of the following options:
* `json` (default)
* `json-s`
* `json-html`
* New formats can be added by defining `event_to_json/3`. See `library(pengines_io)`.
- `cookieJar`- An instance of a jar containing cookies which will be used in the queries, usually retreived from the [request.jar()](https://github.com/request/request#requestjar).
## Examples
Please refer to documentation at [here][documentation] for working examples.
### on(event,function)
[documentation]:http://www.swi-prolog.org/pldoc/man?section=pengine-overview
Make `function` trigger on `event`. Supported values of `event` are:
## The Prolog API
- `create` - The pengine has been created.
- `success` - The pengine responds with a successful answer to a query. The callback is an object containing the following fields:
* `data` - A list of objects each representing a solution to the query,
* `more` - A boolean indicating whether more solutions may exist
* `id `- The id of the pengine returning the answer.
- `error` - 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.
- `prompt` - 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 an object.
- `failure` - The pengine fails to find a solution.
- `stop` - A running query has been successfully stopped.
- `abort` - A running query has been successfully aborted.
- `destroy` - The pengine has been successfully destroyed.
The Prolog API is documented in the following document: [Prolog API][prolog-api].
### ask(expression)
[prolog-api]:http://www.swi-prolog.org/pldoc/man?section=pengine-libs
Forwards a Prolog expression to the server.
## Usage in Node.js
### next()
Install using NPM: `npm install pengines` and use:
Asks the pengine for the next solution from last asked expression.
```js
var Pengine = require('pengines');
var pengine = new Pengine();
```
and follow the JavaScript API description above.
### abort()
## Testing
Terminates the running query by force.
To test the Pengines JavaScript API, you need to install required
testing dependencies using the `npm install` command in the
project root directory.
### destroy()
Then load `test_js.pl` in SWI-Prolog:
Destroys the pengine.
```
-? [test_js].
```
### respond(item)
and run tests using `run_tests`.
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. Generates an error if string cannot be parsed as a Prolog term or if object cannot be serialised into JSON.
## License
### stop()
Stops the query currently running without destroying the object.
## Set it off!
```npm install pengines```
## Credits
Modified version of [pengines.js](http://swish.swi-prolog.org/pengine/pengines.js), developed by Torbjörn Lager. Most options and events are identical to as documented in [this version](http://pengines.swi-prolog.org/docs/documentation.html). For more information on the application flow, see [this resource](http://www.swi-prolog.org/pldoc/man?section=pengine-overview).
Pengines is licensed under the BSD 2-Clause license. See the header of web/js/pengines.js file.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc