Pengines JavaScript: {format: "prolog"}

When querying a pengine from Prolog, the answer is returned a Prolog term. Why is this option not available to the JavaScript interface and where can I look to make it available? It would save a lot of JSON back and forth to integrate with client-side Prolog…

Note sure what you asking for here, but it’s easy to turn the JSON representation into a prolog string version. E.g. See this helper code I wrote in Java.

package com.simularity.os.javapengine;

import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class PengineHelper {
    
    public static String TermToString(Long l) {
        return l.toString();
    }
    
    public static String TermToString(Double d) {
        return d.toString();
    }

    public static String TermToString(Boolean b) {
        return b.toString();
    }

    public static String TermToString(Object o) {
        if (o instanceof JSONArray) {
            return TermToString((JSONArray)o);
        } else if ( o instanceof JSONObject) {
            return TermToString((JSONObject)o);
        }

        return o.toString();
    }

    public static String TermToString(JSONObject value) {
        StringBuilder builder = new StringBuilder();

        if (value.containsKey("functor")) {
            JSONArray args = (JSONArray) value.get("args");
            Iterator it = args.iterator();
            int i = 0;
            while (it.hasNext()) {
                String part = TermToString(it.next());
                if (i > 0) builder.append(", ");
                i++;
                builder.append(part);
            }
            builder.insert(0, value.get("functor") + "(");
            builder.append(")");
        }

        return builder.toString();
    }

    public static String TermToString(JSONArray value) {

        StringBuilder builder = new StringBuilder();
        builder.append("[");

        Iterator it = value.iterator();
        int i = 0;
        while (it.hasNext()) {
            String part = TermToString(it.next());
            if (i > 0) builder.append(", ");
            i++;
            builder.append(part);
        }
        builder.append("]");

        return builder.toString();
    }
}

Have you tried {format: “json-s”} ? That might give you what you want. It returns the variable bindings as JSON, where the values of the variables are just strings rather than JSON.

I need Prolog terms for an interface like this in Tau Prolog:

pengine_ask('member(X, [a, b, c])').

pengine_receive(more, member(X, [a, b, c])) :-
    write(X), pengine_next.
pengine_receive(none, member(X, [a, b, c])) :-
    write(X).

The onsuccess function will query pengine_receive/2 for each result (if chunked).

I think I’ve tracked down the problem, when creating the pengine and receiving responses from it with format: "prolog", it of course returns Prolog terms, but this includes replacing:

{
  "event":"create",
  "id":"aa9a8005-080e-4025-9163-bc5032d3bcbd",
  "slave_limit":3
}

with

create(aa9a8005-080e-4025-9163-bc5032d3bcbd, [slave_limit(3)]).

which I’m guessing pengines.js isn’t expecting and it’s probably not parsing it correctly. So the solution here may well be to skip using pengines.js and write a Tau Prolog interface, in which case it’d probably be better to use the SWI-Prolog WASM version. Looks like full-stack Prolog is going to be quite the adventure!