Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A node.js interface to the SWI-Prolog library.

License

NotificationsYou must be signed in to change notification settings

rla/node-swipl

 
 

Repository files navigation

A Node.js interface to the SWI-Prolog.

Build Status

Installation:

You need to have SWI-Prolog installed andswipl binary available inPATH and compiler installed. See "Platform support" for operating systemsupport. The library requires Node.js version 7+ and SWI-Prolog 8+.

npm install swipl

Also see "Known issues" for currently unsolved problems.

Usage

Calling a predicate and returning bindings withthe first solution:

constswipl=require('swipl');constret=swipl.call('member(X, [1,2,3,4])');if(ret){console.log(`Variable X value is:${ret.X}`);}else{console.log('Call failed.');}

Outputs:

Variable X value is: 1

Calling a predicate and returning all solutions:

constswipl=require('swipl');constquery=newswipl.Query('member(X, [1,2,3,4])');letret=null;while(ret=query.next()){console.log(`Variable X value is:${ret.X}`);}

Outputs:

Variable X value is: 1Variable X value is: 2Variable X value is: 3Variable X value is: 4

There can be only one query open at a time.

Create modules and specify context

Use module operator: to call code in differentmodules. The default call assumes theuser module.

swipl.call('assert(mymodule:test(1))');console.log(swipl.call('mymodule:test(X)'));

Consult external files

Load code from external files. You might haveto set the working directory if you want to use relative paths.

swipl.call('working_directory(_, prolog)');swipl.call('consult(mycode)');

Constructing safe queries

Queries with data requiring proper escaping can be constructedby using helper functions from swipl.term.

Example:

constswipl=require('./');const{ list, compound, variable, serialize}=swipl.term;constescaped=serialize(compound('member',[variable('X'),list([1,2,3,4])]));console.log(swipl.call(escaped));

Blobs and dicts are not supported.

Output term representation

Prolog terms in variable bindings are converted intoJavaScript objects under the following rules:

  • Integers are converted to numbers.
  • Floats are converted to numbers.
  • Atoms and strings are converted to strings.
  • Empty list is converted to string[].
  • List head tail pair is converted to object{ head, tail } wherehead andtail are converted terms.
  • Compound term is converted to object{ name, args } wherename is the compound functor name andargs is the arrayof converted argument terms.
  • Blobs and dicts are not supported and will throw an error.

Error handling

Syntax errors in queries are thrown. Error messagesare read from the prolog. The current query is automaticallyclosed.

Invalid query example:member(X, [1,2,3,4] (missing closing paren):

swipl.call('member(X, [1,2,3,4]');

Throws error with message:

Error: Error during query execution. Syntax error:Operator expectedmember(X, 1,2,3,4** here **

Known errors are thrown with the error message.

swipl.call('error:must_be(ground, _)');

Throws error with message:

Error: Error during query execution. Arguments arenot sufficiently instantiated.

Custom errors without a message are thrown with JavaScripterror containing the error term:

swipl.call('throw(error(test))');

Throws error with message:

Error: Error during query execution. Unknown message: error(test).

Disable autoinitialization

The embedded SWI-Prolog engine is automatically initializedwhen creating the first query. This behavior can be disabledby callingswipl.autoInitialise(false) before any query isexecuted. The engine can be initialized later manually withtheswipl.initialise() function.

Platform support

The bindings are tested on various Linux distributions, on Windows,and on MacOS. SWI-Prolog commandswipl must be available inPATHon all of these operating systems.

Windows

Microsoft build tools must be installed:

npm install --global --production windows-build-tools

SWI-Prolog commandswipl must be available inPATH.

MacOS

SWI-Prolog must be installed or compiled through Macports. This isdescribed herehttp://www.swi-prolog.org/build/macos.html. The setup wastested on MacOS Sierra by installing dependencies from ports and compilingwith prefix/usr/local (adjustbuild.templ).

Known issues

  • Unicode data cannot be exchanged.
  • Exporting PL_BLOB terms is not handled.
  • Exporting PL_DICT terms is not supported. It is not supported at all by SWI-Prologforeign interface.
  • Installed files cannot be copied around on *nix. The linker haslibswipl locationspecified absolutely in the binding object file. The location ofSWI_HOME_DIR isdetermined install-time and written into the fileplbase.conf.
  • Attempt to use native SWI packages leads to symbol lookup errorslikereadutil.so: undefined symbol: PL_new_atom.
  • Custom initialization parameters are not yet implemented.

Development

A list of helpful resources:

Alternatives

Authors

Please see the AUTHORS file.

License

Licensed under LGPL 3.0. A copy is available inthe LICENSE.txt file.Filelib/serialize_string.js is ported from thepengines project and is licensedunder BSD (see the file header).

About

A node.js interface to the SWI-Prolog library.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++48.8%
  • JavaScript46.7%
  • Python4.4%
  • Raku0.1%

[8]ページ先頭

©2009-2025 Movatter.jp