This commit is contained in:
Vitor Santos Costa
2018-12-21 20:57:53 +00:00
parent f0a6018a00
commit 31423fcf90
18 changed files with 260 additions and 247 deletions

View File

@@ -6,7 +6,9 @@ INCLUDE(UseSWIG)
include(FindPythonModule)
list (APPEND pl_library ${CMAKE_CURRENT_SOURCE_DIR}/prolog/yapi.yap )
set (PYTHON_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/yap4py/yapi.py ${CMAKE_CURRENT_SOURCE_DIR}/yap4py/__main__.py)
set (PYTHON_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/yap4py/yapi.py
${CMAKE_CURRENT_SOURCE_DIR}/yap4py/systuples.py
${CMAKE_CURRENT_SOURCE_DIR}/yap4py/__main__.py)
SET_SOURCE_FILES_PROPERTIES(../../swig/yap.i PROPERTIES CPLUSPLUS ON)

View File

@@ -25,18 +25,19 @@
:- python_import(yap4py.yapi).
:- python_import(json).
%:- python_import(gc).
:- meta_predicate( yapi_query(:,+) ).
%:- start_low_level_trace.
%% @pred yapi_query( + VarList, - Dictionary)
%%
%% dictionary, Examples
%%
%%
yapi_query( VarNames, Self ) :-
%% @pred yapi_query( + VarList, - Dictionary)
%%
%% dictionary, Examples
%%
%%
yapi_query( VarNames, Self ) :-
show_answer(VarNames, Dict),
Self.bindings := Dict.
@@ -47,6 +48,9 @@ set_preds :-
fail,
current_predicate(P, Q),
functor(Q,P,A),
current_predicate(P, Q),
functor(Q,P,A),
atom_string(P,S),
catch(
:= yap4py.yapi.named( S, A),
@@ -72,27 +76,46 @@ python_query( Caller, String ) :-
atomic_to_term( String, Goal, VarNames ),
query_to_answer( Goal, VarNames, Status, Bindings),
Caller.port := Status,
write_query_answer( Bindings ),
nl(user_error),
maplist(in_dict(Caller.answer, Bindings), Bindings).
>>>>>>> 37d5dcedc17b8c63cd9fa213454edb37816a130f
write_query_answer( Bindings ),
answer := {},
foldl(ground_dict(answer), Bindings, [], Ts),
term_variables( Ts, Hidden),
foldl(bv, Hidden , 0, _),
maplist(into_dict(answer),Ts),
Caller.answer := json.dumps(answer),
S := Caller.answer,
format(user_error, '~nor ~s~n~n',S).
bv(V,I,I1) :-
atomic_concat(['__',I],V),
I1 is I+1.
into_dict(D,V0=T) :-
D[V0] := T.
/**
*
*/
in_dict(_Dict, _, var([_V0])) :-
!.
in_dict(Dict, Bindings, var([V0,V|Vs])) :-
!,
atom_to_string(V0,S0),
atom_to_string(V,S),
Dict[S] := S0,
in_dict( Dict, Bindings, var([V0|Vs])).
in_dict(Dict, Bindings, nonvar([V0|Vs], T)) :-
!,
atom_to_string(V0,S0),
term_to_string(T, S, _Bindings),
Dict[S0] := S,
in_dict( Dict, Bindings, var([V0|Vs])).
in_dict(_, _, _).
ground_dict(_Dict, var([V,V]), I, I) :-
!.
ground_dict(Dict, nonvar([V0|Vs], T),I0, [V0=T| I0]) :-
!,
ground_dict( Dict, var([V0|Vs]), I0, I0).
ground_dict(Dict, var([V0,V|Vs]), I, I) :-
!,
Dict[V]=V0,
ground_dict( Dict, var([V0|Vs]), I, I).
ground_dict(_, _, _, _).
bound_dict(Dict, nonvar([V0|Vs], T)) :-
!,
Dict[V0] := T,
bound_dict( Dict, var([V0|Vs])).
bound_dict(Dict, var([V0,V|Vs])) :-
!,
Dict[V] := V0,
bound_dict( Dict, var([V0|Vs])).
bound_dict(_, _).

View File

@@ -1,22 +1,12 @@
import readline
from yap4py.yap import *
from yap4py.systuples import *
from os.path import join, dirname
from collections import namedtuple
import sys
yap_lib_path = dirname(__file__)
bindvars = namedtuple('bindvars', 'list')
compile = namedtuple('compile', 'file')
jupyter_query = namedtuple('jupyter_query', 'vars dict')
library = namedtuple('library', 'listfiles')
prolog_library = namedtuple('prolog_library', 'listfiles')
python_query = namedtuple('python_query', 'vars dict')
set_prolog_flag = namedtuple('set_prolog_flag', 'flag new_value')
show_answer = namedtuple('show_answer', 'vars dict')
v0 = namedtuple('v', 'slot')
yap_query = namedtuple('yap_query', 'query owner')
yapi_query = namedtuple('yapi_query', 'vars dict')
class Engine( YAPEngine ):
@@ -140,7 +130,7 @@ class YAPShell:
# construct a query from a one-line string
# q is opaque to Python
#
#q = engine.query(python_query(self, s))
# q = engine.query(python_query(self, s))
#
# # vs is the list of variables
# you can print it out, the left-side is the variable name,
@@ -160,7 +150,7 @@ class YAPShell:
q = Query( engine, python_query( engine, query) )
for answer in q:
bindings += [answer]
if g.done():
if q.done():
return bindings
if loop:
continue