This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/python/yapex.py

96 lines
2.8 KiB
Python
Raw Normal View History

2016-09-21 20:55:09 +01:00
2016-07-31 16:09:21 +01:00
import yap
2016-08-18 08:13:55 +01:00
import sys
2016-12-10 07:01:10 +00:00
import collections
2016-07-31 16:09:21 +01:00
def query_prolog(engine, s):
2016-09-23 03:53:42 +01:00
2016-09-27 18:28:54 +01:00
def answer(q):
2016-09-23 07:21:42 +01:00
try:
return q.next()
except Exception as e:
2016-09-27 18:28:54 +01:00
print(e.args[1])
2016-09-23 07:21:42 +01:00
return False
2016-09-27 18:28:54 +01:00
2016-12-10 07:01:10 +00:00
#construct a query from a one-line string
# q is opaque to Python
2016-07-31 16:09:21 +01:00
q = engine.query(s)
2016-12-10 07:01:10 +00:00
# vs is the list of variables
# you can print it out, the left-side is the variable name,
# the right side wraps a handle to a variable
vs= q.namedVars()
# atom match either symbols, or if no symbol exists, sttrings, In this case
# variable names should match strings
for eq in vs:
if not isinstance(eq[0],str):
print( "Error: Variable Name matches a Python Symbol")
return
2016-07-31 16:09:21 +01:00
ask = True
2016-12-10 07:01:10 +00:00
# launch the query
2016-09-23 03:53:42 +01:00
while answer(q):
2016-12-10 07:01:10 +00:00
# this new vs should contain bindings to vars
vs= q.namedVars()
#numbervars
i=0
# iteratw
for eq in vs:
name = eq[0]
# this is tricky, we're going to bind the variables in the term so thay we can
# output X=Y. The Python way is to use dictionares.
#Instead, we use the T function to tranform the Python term back to Prolog
binding = yap.T(eq[1])
if binding.isVar():
binding.unify(name)
else:
i = binding.numberVars(i, True)
print(name + " = " + binding.text())
#ok, that was Prolog code
2016-07-31 16:09:21 +01:00
print("yes")
2016-12-10 07:01:10 +00:00
# deterministic = one solution
2016-07-31 16:09:21 +01:00
if q.deterministic():
2016-12-10 07:01:10 +00:00
# done
2016-07-31 16:09:21 +01:00
q.close()
return
if ask:
2016-12-10 07:01:10 +00:00
s = input("more(;), all(*), no(\\n), python(#) ?").lstrip()
2016-07-31 16:09:21 +01:00
if s.startswith(';') or s.startswith('y'):
continue
elif s.startswith('#'):
exec(s.lstrip('#'))
2016-12-10 07:01:10 +00:00
elif s.startswith('*') or s.startswith('a'):
2016-07-31 16:09:21 +01:00
ask = False
2016-12-10 07:01:10 +00:00
continue
2016-07-31 16:09:21 +01:00
else:
break
2016-12-10 07:01:10 +00:00
print("No (more) answers")
q.close()
2016-07-31 16:09:21 +01:00
return
def live():
engine = yap.YAPEngine()
loop = True
while loop:
try:
s = input("?- ")
if not s:
loop = False
query_prolog(engine, s)
except SyntaxError as err:
print("Syntax Error error: {0}".format(err))
except EOFError:
return
except RuntimeError as err:
print("YAP Execution Error: {0}".format(err))
except ValueError:
print("Could not convert data to an integer.")
except:
print("Unexpected error:", sys.exc_info()[0])
raise
2016-09-21 20:55:09 +01:00
engine.close()
2016-07-31 16:09:21 +01:00
#
# initialize engine
# engine = yap.YAPEngine();
# engine = yap.YAPEngine(yap.YAPParams());
2016-09-21 20:55:09 +01:00
live()