Minimal pengine example

Hi all,
I’d like to create a gui in js for a program written in prolog, to view exclusively on localhost. I think pengines is the right library to use here. Following the documentation, I downloaded pengines from github and was able to run the genealogist gui, which is a good starting point for the thing I’d like to do.

When I start run.pl though, a lot of unnecessary stuff is added: the swish instance, the scratchpad, the documentation, the admin permissions UI etc. Is there a way to strip all the unnecessary parts? Ideally including as few things as possibile other than my prolog, html, and js files.

In other words, what’s a minimal skeleton, without all the bells and whistles?

1 Like

Please don’t leave us guessing.

My guess: Genealogist Part 1: Your first “real” Pengines application

Right, that was the tutorial I followed. In practice, that page wasn’t very explicit in signalling what repo I should have cloned and modified, so I ended up just using the version in https://github.com/SWI-Prolog/pengines/tree/master/apps/genealogist

If you just want Pengines, library(pengines) as distributed with the system is all you need. You do need to setup the HTTP server yourself though (using library(http/http_server) and possibly a little more depending on what you want). Should be a tutorial :frowning:

Thanks @jan, I think I’ll start removing manually from load.pl, server.pl, etc, all of the parts that I think aren’t needed. In the meantime, could you explain more the “depending on what you want” part? What things are there besides the http/http_server stuff?

I’d start with just Prolog. The minimal example I came up with is this (actually, it adds HTTP CORS support which you probably need, but is not always required).

:- use_module(library(pengines)).
:- use_module(library(http/http_server)).
:- use_module(library(http/http_cors)).
:- use_module(library(settings)).

:- set_setting_default(http:cors, [*]).

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

Now try it out (using the Prolog client):

101 ?- server(5000).
% Started server at http://localhost:5000/
true.

102 ?- pengine_rpc('http://localhost:5000', member(X, [a,b,c])).
X = a ;
X = b ;
X = c.
6 Likes

So it suffices to add

:- use_module(library(pengines)).

to make an otherwise normal http server pengine-enabled? :open_mouth: that’s amazing!
And I’m also amazed that the pengine automatically sees the other predicates I define in that file. That’s a start.

Now a question: I’m creating an html file (it’s not strictly necessary for it to be served by prolog).
But the example one contained the line:

    <script src="/pengine/pengines.js"></script>

and I can’t find a pengines.js file in the example repo! Is there some magic that makes that available in the example?

That will only work if the file is served by the same Prolog server. Otherwise you need the full url (starting with http://).

1 Like

Since the title of the topic is Minimal pengine example and not Minimal pengine example with HTML front using JavaScript others reading this topic might find this post from the days when the forum was in Google Groups useful:

https://groups.google.com/g/swi-prolog/c/FCIrEs8DURA/m/XFlfsTK_CAAJ

1 Like

Thanks @jan, I can indeed use https://pengines.swi-prolog.org/pengine/pengines.js or download it and call it from my local directory. My question was that the genealogist example seems to be using it locally, but there’s no local pengines.js file in the pengines repo, so I wondered how that worked.

It is the same server serving pengines.js, so you can use a relative URL. If you can, that is a better idea as it makes it easier to move the server around. Also in your case it might be a good idea to have your HTML files served through the same (Prolog) server.

Hi,

here https://github.com/hnbeck/ecblackjack is a small Web-Based game I wrote some month before. It is not that simple, but also not complex. Surly not the best code :slight_smile: It uses TauProlog and Pengines, so you may look at it for some ideas what can be done.

Cheers

Hans