2
votes


I'm making an AI project which uses prolog, but I want it to be publish online. I've found pengines (http://pengines.swi-prolog.org/docs/documentation.html, http://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/pengines.html%27)) which is supposed to be a javascript implementation of Prolog, but I can't seem to understand how to use it.

I've tried using the the pengines npm package (https://www.npmjs.com/package/pengines) and running the code from the pengines docs with the default express-generator app:

<html lang="en">
<head>
    <script src="/vendor/jquery/jquery-2.0.3.min.js"></script>
    <script src="/pengine/pengines.js"></script>
    <script type="text/x-prolog">

        main :-
            repeat,
            pengine_input(X),
            pengine_output(X),
            X == stop.

    </script>
    <script>
        var pengine = new Pengine({
            oncreate: handleCreate,
            onprompt: handlePrompt,
            onoutput: handleOutput
        });
        function handleCreate() {
            pengine.ask('main');
        }
        function handlePrompt() {
            pengine.input(prompt(this.data));
        }
        function handleOutput() {
            $('#out').html(this.data);
        }
    </script>
</head>
<body>
    <div id="out"></div>
</body>

But it only returns an error:

http://localhost:3000/pengine/create Failed to load resource: the server responded with a status of 404 (Not Found)

I would be very thankful if someone could explain how to work with pengines or another prolog implementation in javascript.

Thanks!

2

2 Answers

3
votes

I know this post is quite old but for other people stopping by:

I'm making an AI project which uses prolog, but I want it to be publish online. I've found pengines (http://pengines.swi-prolog.org/docs/documentation.html, http://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/pengines.html%27)) which is supposed to be a javascript implementation of Prolog

It is not really a implementation of Prolog, it is just a client written in JavaScript that can talk to a Prolog-server hosting a pengine process. The JavaScript client talks to the pengine-server over HTTP and a particular protocol referred to as Prolog Transport Protocol (PLTP).

With the JavaScript client you are able to send simple queries like pengine.ask("member(X, [1,2,3])") etc provided that the pengine server has exposed the member/2 predicate as safe to use from remote. But you can also, as you demosntrate in your code snippet, write prolog code inside <script type="text/x-prolog"> and have the JavaScript-Client send that prolog-source code to the server who will add it to its knowledge base.

http://localhost:3000/pengine/create Failed to load resource: the server responded with a status of 404 (Not Found)

Before your JavaScript client can create a Pengine there must be a pengine server running somewhere. In your code-snippet no URL is provided so the client will default to the host-url. You can also explicitly specify the URL when you create the pengine.

Here is an example of a simple pengines-server taken from the source-repo examples:

:- module(pengine_server,
          [ server/1                    % +Port
          ]).
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_server_files)).
:- use_module(library(http/http_files)).
:- use_module(library(pengines)).
:- use_module(pengine_sandbox:library(pengines)).

:- http_handler(/, http_reply_from_files(web, []), [prefix]).

server(Port) :-
    http_server(http_dispatch, [port(Port)]).

It sandboxes the pengines API, if you want to make other predicates available you can for example add:

:- use_module(pengine_sandbox:library(semweb/rdf_db)).
sandbox:safe_primitive(rdf_db:rdf(_,_,_)).
1
votes

You can have a look at SWI prolog's manual page about pengines. It includes 3 examples illustrating how to create and interact with a pengine from JavaScript. Over here you can find the docs. Hopefully this will help you solve your problem. Good luck!