ian-andrich / PythonPengines

A Python interface to the Prolog pengines library. Makes it exceedingly easy to call out to SWI-Prolog within Python!
MIT License
12 stars 9 forks source link

Consider linking to example using prologterms #9

Open cmungall opened 6 years ago

cmungall commented 6 years ago

I have a new package on pypi https://pypi.org/project/prologterms/

it allows natural construction of prolog programs from within python. It's independent of any prolog engine or execution method, but works nicely with pengines

An example is found at the bottom of:

http://nbviewer.jupyter.org/github/cmungall/prologterms-py/tree/master/PrologTermsExamples.ipynb/

from pengines.Builder import PengineBuilder
from pengines.Pengine import Pengine
from prologterms import TermGenerator, PrologRenderer, Program, Var

P = TermGenerator()
X = Var('X')
Y = Var('Y')
Z = Var('Z')
R = PrologRenderer()

p = Program(
    P.ancestor(X,Y) <= (P.parent(X,Z), P.ancestor(Z,Y)),
    P.ancestor(X,Y) <= P.parent(X,Y),
    P.parent('a','b'),
    P.parent('b','c'),
    P.parent('c','d')
)

q = P.ancestor(X,Y)

factory = PengineBuilder(urlserver="http://localhost:4242",
                         srctext=R.render(p),
                         ask=R.render(q))
pengine = Pengine(builder=factory, debug=True)
while pengine.currentQuery.hasMore:
    pengine.doNext(pengine.currentQuery)
for p in pengine.currentQuery.availProofs:
    print('{} <- {}'.format(p[X.name], p[Y.name]))

consider adding a link from README

also potentially PythonPengine could include a dependency on prologterms, so term objects could be passed in directly, but I think on balance I slightly prefer the decoupling