This commit is contained in:
Vitor Santos Costa
2019-03-19 18:42:17 +00:00
parent 70a43ece1d
commit 96a40f1d50
7 changed files with 90 additions and 116 deletions

View File

@@ -42,6 +42,8 @@ yapi_query( VarNames, Self ) :-
Self.bindings := Dict.
%:- initialization set_preds.
set_preds :-
@@ -70,51 +72,31 @@ argi(N,I,I1) :-
I1 is I+1.
python_query( Caller, String ) :-
python_query( Caller, String, _Bindings).
python_query( Caller, String, _Bindings).
user:user_python_query( Caller, String, Bindings ) :-
python_query( Caller, String, _Bindings).
python_query( Caller, String, Bindings ) :-
atomic_to_term( String, Goal, VarNames ),
query_to_answer( Goal, VarNames, Status, Bindings),
Caller.port := Status,
output(Caller, Bindings).
Caller.q.port := Status,
output(Caller, Bindings).
output( Caller, Bindings ) :-
Caller.answer := {},
/* % start_low_level_trace,
foldl(ground_dict(answer), Bindings, [], Ts),
term_variables( Ts, Hidden),
foldl(bv, Hidden , 0, _),
*/ maplist(into_dict(answer),Bindings),
:= print(answer)},
Caller.answer := answer.
output( _, Bindings ) :-
write_query_answer( Bindings ),
fail.
output(_Caller, _Bindings).
output( Caller, Bindings) :-
maplist(into_dict(Caller),Bindings).
bv(V,I,I1) :-
atomic_concat(['__',I],V),
I1 is I+1.
into_dict(D,V0=T) :-
atom(T),
!,
D[V0] := T.
into_dict(D,V0=T) :-
integer(T),
writeln((D[V0]:=T)),
!,
D[V0] := T,
:= print(D).
into_dict(D,V0=T) :-
string(T),
!,
D[V0] := T.
into_dict(D,V0=T) :-
python_represents(T1,T),
D[V0] := T1.
D.q.answer[V0] := T.
/**
*
*/

View File

@@ -1,7 +1,7 @@
import readline
import copy
from yap4py.yap import *
from yap4py.systuples import *
from yap4py.systuples import python_query, show_answer, library, prolog_library, v0, compile, namedtuple
from os.path import join, dirname
import sys
@@ -52,7 +52,7 @@ class JupyterEngine( Engine ):
pass
class EngineArgs( YAPEngineArgs ):
""" Interface to Engine Options class"""
""" Interface to EngneOptions class"""
def __init__(self, args=None,**kwargs):
super().__init__()
@@ -69,6 +69,7 @@ class Query (YAPQuery):
super().__init__(g)
self.engine = engine
self.port = "call"
self.answer = {}
def __iter__(self):
return self
@@ -80,7 +81,7 @@ class Query (YAPQuery):
if self.port == "fail" or self.port == "exit":
raise StopIteration()
if self.next():
return copy.deepcopy(self.answer)
return True
raise StopIteration()
def name( name, arity):
@@ -125,7 +126,7 @@ class YAPShell:
def query_prolog(self, query):
g = None
#import pdb; pdb.set_trace()
import pdb; pdb.set_trace()
#
# construct a query from a one-line string
# q is opaque to Python
@@ -147,9 +148,11 @@ class YAPShell:
engine = self.engine
bindings = []
loop = False
q = Query( engine, python_query( engine, query) )
self.q = Query( engine, python_query( self, query) )
q = self.q
for answer in q:
bindings += [answer]
bindings += [q.answer]
print(q.answer)
if q.done():
return bindings
if loop:
@@ -170,7 +173,7 @@ class YAPShell:
if self.q:
self.q.close()
self.q = None
print("No (more) answers")
print("No (more) answers, found", bindings)
return bindings
except Exception as e:
if not self.q:
@@ -182,34 +185,41 @@ class YAPShell:
raise
def live(self, engine, **kwargs):
loop = True
self.q = None
while loop:
try:
s = input("?- ")
if not s:
try:
loop = True
self.q = None
while loop:
try:
s = input("?- ")
if not s:
continue
else:
self.query_prolog(s)
except SyntaxError as err:
print("Syntax Error error: {0}".format(err))
continue
else:
self.query_prolog(s)
except SyntaxError as err:
print("Syntax Error error: {0}".format(err))
continue
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
engine.close()
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
engine.close()
except Exception as e:
print("Exception",e)
e.errorNo = 0
raise
#
# initialize engine
# engine = yap.YAPEngine();
# engine = yap.YAPEngine(yap.YAPParams());
#
def __init__(self, engine, **kwargs):
#import pdb; pdb.set_trace()
self.engine = engine
self.live(engine)