fix predicate search
This commit is contained in:
@@ -18,12 +18,15 @@ yap_query = namedtuple( 'yap_query', 'query owner')
|
||||
jupyter_query = namedtuple( 'jupyter_query', 'vars dict')
|
||||
python_query = namedtuple( 'python_query', 'vars dict')
|
||||
yapi_query = namedtuple( 'yapi_query', 'vars dict')
|
||||
show_answer = namedtuple( 'show_answer', 'vars dict')
|
||||
|
||||
def v():
|
||||
return yap.YAPVarTerm()
|
||||
|
||||
def numbervars( q ):
|
||||
Dict = {}
|
||||
if True:
|
||||
engine.goal(yapi_query( q.namedVars(), Dict))
|
||||
engine.goal(show_answer( q.namedVars(), Dict))
|
||||
return Dict
|
||||
rc = q.namedVarsVector()
|
||||
q.r = q.goal().numbervars()
|
||||
@@ -42,7 +45,10 @@ def numbervars( q ):
|
||||
|
||||
def answer(q):
|
||||
try:
|
||||
return q.next()
|
||||
v = q.next()
|
||||
if v:
|
||||
print( bindings )
|
||||
return v
|
||||
except Exception as e:
|
||||
print(e.args[1])
|
||||
return False
|
||||
@@ -52,7 +58,8 @@ def query_prolog(engine, s):
|
||||
#
|
||||
# construct a query from a one-line string
|
||||
# q is opaque to Python
|
||||
q = engine.query(s)
|
||||
bindings = {}
|
||||
q = engine.query(python_query(s, bindings))
|
||||
# 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
|
||||
@@ -64,10 +71,9 @@ def query_prolog(engine, s):
|
||||
# if not isinstance(eq[0],str):
|
||||
# print( "Error: Variable Name matches a Python Symbol")
|
||||
# return
|
||||
# ask = True
|
||||
ask = True
|
||||
# launch the query
|
||||
while answer(q):
|
||||
print( handler( q ))
|
||||
# deterministic = one solution
|
||||
if q.deterministic():
|
||||
# done
|
||||
@@ -98,7 +104,7 @@ def boot_yap(**kwargs):
|
||||
args.setYapLibDir(yap_lib_path)
|
||||
args.setSavedState(os.path.join(yap_lib_path,"startup.yss"))
|
||||
engine = yap.YAPEngine(args)
|
||||
engine.goal( use_module(library('python') ) )
|
||||
engine.goal( use_module(library('yapi') ) )
|
||||
return engine
|
||||
|
||||
def live(**kwargs):
|
||||
|
@@ -12,24 +12,27 @@
|
||||
:- use_module( library(maplist) ).
|
||||
:- use_module( library(rbtrees) ).
|
||||
:- use_module( library(terms) ).
|
||||
:- use_module( library(python) ).
|
||||
|
||||
|
||||
%% @pred yap_query(sGoal, + VarList, +OutStream, - Dictionary)
|
||||
%% @pred yap_query(sGoal, + VarList, +OutStream, - Dictionary)
|
||||
%% @pred yap_query(0:Goal, + VarList, - Dictionary)
|
||||
%%
|
||||
%% dictionary, Examples
|
||||
%%
|
||||
%%
|
||||
python_query( Engine, String, Dict ) :-
|
||||
python_query( String, D ) :-
|
||||
atomic_to_term( String, Goal, VarNames ),
|
||||
yap_query( Goal, VarNames, user_error, Dict).
|
||||
yap_query( Goal, VarNames, user_error, Dict),
|
||||
D := Dict,
|
||||
yap4py.yapi.bindings := Dict.
|
||||
|
||||
%% @pred yapi_query( + VarList, - Dictionary)
|
||||
%%
|
||||
%% dictionary, Examples
|
||||
%%
|
||||
%%
|
||||
prologun:yapi_query( VarNames, Dict ) :-
|
||||
yapi_query( VarNames, Dict ) :-
|
||||
show_answer(VarNames, Dict).
|
||||
|
||||
|
||||
@@ -42,34 +45,48 @@ python_query( Engine, String, Dict ) :-
|
||||
%%
|
||||
yap_query( Goal, VarNames, Stream, Dictionary) :-
|
||||
(
|
||||
call(Goal)
|
||||
call(Goal)
|
||||
*->
|
||||
!,
|
||||
show_answer(VarNames, Stream, Dictionary)
|
||||
).
|
||||
!,
|
||||
show_answer(VarNames, Stream, Dictionary)
|
||||
).
|
||||
|
||||
yapi_query( VarNames, Dictionary) :-
|
||||
yap_query( VarNames, Dictionary) :-
|
||||
yap_query( VarNames, user_output, Dictionary).
|
||||
|
||||
show_answer(QVs0, Dict) :-
|
||||
show_answer(QVs0, user_error, Dict).
|
||||
show_answer(QVs0, user_error, Dict).
|
||||
|
||||
show_answer(QVs0, Stream, {Dict}) :-
|
||||
show_answer(QVs0, Stream, Dict) :-
|
||||
copy_term(QVs0, QVs),
|
||||
writeln(ivs-IVs),
|
||||
term_variables(Goal, IVs),
|
||||
foldl(enumerate, IVs, 0, _Ns),
|
||||
copy_term(QVs0, QVs1),
|
||||
rb_new(RB),
|
||||
foldl2(bind_qv, QVs, QVs1, [], LP, {}-RB, Dict-_),
|
||||
!,
|
||||
out(QVs, Stream, D).
|
||||
Dictt := {D}.
|
||||
term_variables(QVs, IVs),
|
||||
term_variables(QVs1, IVs1),
|
||||
foldl( enumerate, IVs, IVs1, 1, _ ),
|
||||
out(LP, Stream ).
|
||||
show_answer(_, _, {}) :-
|
||||
format(' yes.~n', [] ).
|
||||
format(' yes.~n', [] ).
|
||||
|
||||
bind_qv(V=V0, Vs, Vs) :- var(V0), !, V0='$VAR'(V).
|
||||
bind_qv(V=V, Vs, Vs) :- !.
|
||||
bind_qv(V=S, Vs, [V=S|Vs]).
|
||||
bind_qv(V=V0, V1 = V01, Vs, Vs, Vs1-RB, Vs1-RB) :-
|
||||
var(V0),
|
||||
!,
|
||||
'$VAR'(V) = V0,
|
||||
V1 = V01.
|
||||
% atom_string(V1, V01).
|
||||
bind_qv(V='$VAR'(Vi), V1=S1, Vs, [V='$VAR'(Vi)|Vs], D0-RB, D-RB) :- !,
|
||||
add2dict(D0, V1:S1, D).
|
||||
bind_qv(V=S, V1=S1, Vs, [V=S|Vs], D0-RB0, D-RB0) :-
|
||||
% fix_tree( S, SS, S1, SS1, RB0, RBT),
|
||||
add2dict(D0, V1:S1, D).
|
||||
|
||||
enumerate('$VAR'(A), I, I1) :-
|
||||
|
||||
add2dict({}, B, {B}).
|
||||
add2dict({C}, B, {B,C}).
|
||||
|
||||
enumerate('$VAR'(A), A, I, I1) :-
|
||||
enum(I, Chars),
|
||||
atom_codes(A,[0'_|Chars]),
|
||||
I1 is I + 1.
|
||||
@@ -84,65 +101,18 @@ enum(I, [C|Cs]) :-
|
||||
C is "A" +K,
|
||||
enum(J, Cs).
|
||||
|
||||
out(Bs, S, _Dict) :-
|
||||
out(Bs, S) :-
|
||||
output(Bs, S),
|
||||
fail.
|
||||
out(Bs, _S, Dict) :-
|
||||
bvs(Bs, Dict).
|
||||
|
||||
v2py(v(I0) = _V, I0, I) :-
|
||||
!,
|
||||
I is I0+1.
|
||||
v2py(v(I0) = v(I0), I0, I) :-
|
||||
I is I0+1.
|
||||
!.
|
||||
out([_|Bs], S) :-
|
||||
out(Bs, S).
|
||||
|
||||
output([V=B], S) :-
|
||||
!,
|
||||
format(S, '~a = ~q~n', [V, B]).
|
||||
output([V=B|Ns], S) :-
|
||||
format( S, '~a = ~q.~n', [V, B]),
|
||||
output( Ns, S).
|
||||
|
||||
bvs([V=B], S:B) :-
|
||||
atom_atring(V,S),
|
||||
!.
|
||||
bvs([V=B|Ns], (S:B,N) ) :-
|
||||
atom_string(V,S),
|
||||
output( Ns, N).
|
||||
|
||||
bindvars( L, NL ) :-
|
||||
rb_new(T),
|
||||
foldl2( bind, L, NL, T, _ , 0, _),
|
||||
term_variables(NL, Vs),
|
||||
foldl( bind_new, Vs, 0, _).
|
||||
output([V=B|_Ns], S) :-
|
||||
format( S, '~a = ~q.~n', [V, B]),
|
||||
fail.
|
||||
|
||||
|
||||
bind(X=Y, X=X, T0, T, N, N) :-
|
||||
var(Y),
|
||||
!,
|
||||
rb_update(T0, Y, X, T).
|
||||
bind(X = G, X = G, T, T, N0, N0) :-
|
||||
ground(G),
|
||||
!.
|
||||
bind(X = C, X = NC, T, NT, N0, NF) :-
|
||||
C =.. [N|L],
|
||||
foldl2( bind_new, L, NL, T, NT, N0, NF),
|
||||
NC =.. [N|NL].
|
||||
|
||||
bind_new(Y, X, T, T, N, N) :-
|
||||
var(Y),
|
||||
rb_lookup(Y, X, T),
|
||||
!.
|
||||
bind_new(Y, X, T, TN, N, NF) :-
|
||||
var(Y),
|
||||
!,
|
||||
rb_insert(Y, T, X, TN),
|
||||
NF is N+1,
|
||||
atomic_concat('_',N,X).
|
||||
bind_new(Y, Y, T, T, N, N) :-
|
||||
ground(Y),
|
||||
!.
|
||||
bind_new(Y, X, T, NT, N0, NF) :-
|
||||
Y =.. [N|L],
|
||||
foldl2(v, L, NL, T, NT, N0, NF),
|
||||
X =.. [N|NL].
|
||||
|
Reference in New Issue
Block a user