This commit is contained in:
Vitor Santos Costa 2019-04-07 23:09:01 +01:00
parent 31743139b0
commit 1cde9d0eed
7 changed files with 59 additions and 31 deletions

View File

@ -28,6 +28,9 @@
:- python_import(json).
%:- python_import(gc).
:- create_prolog_flag(yap4py_query_output, true, [access(read_write)]).
:- create_prolog_flag(yap4py_query_json, true, [access(read_write)]).
:- meta_predicate yapi_query(:,+), python_query(+,:), python_query(+,:,-) .
%:- start_low_level_trace.
@ -78,28 +81,35 @@ python_query( Caller, String, Bindings ) :-
atomic_to_term( String, Goal, VarNames ),
query_to_answer( user:Goal, VarNames, Status, Bindings),
Caller.q.port := Status,
output(Caller, Bindings).
rational_term_to_tree(Caller+Bindings,_NGoal+NBindings,ExtraBindings,[]),
lists:append(NBindings, ExtraBindings, TotalBindings),
copy_term_nat(TotalBindings,L),
term_variables(L,Vs),
numbervars(Vs,0,_),
output(Caller, L).
%% output( _, Bindings ) :-
%% write_query_answer( Bindings ),
%% fail.
output( _, Bindings ) :-
yap_flag(yap4py_query_output,true),
once( write_query_answer( Bindings ) ),
nl(user_error),
nl(user_error),
fail.
output( Caller, Bindings) :-
copy_term( Bindings, Bs),
simplify(Bs, 1, Bss),
yap_flag(yap4py_query_json,true),
!,
simplify(Bindings, 1, Bss),
numbervars(Bss, 0, _),
maplist(into_dict(Caller),Bss).
output( _Caller, _Bindings).
simplify([],_,[]).
simplify([X=V|Xs], [X=V|NXs]) :-
var(V),
!,
X=V,
simplify(Xs,NXs).
simplify([X=V|Xs], I, NXs) :-
var(V),
!,
X=V,
simplify(Xs,I,NXs).
simplify(Xs,I, NXs).
simplify([X=V|Xs], I, [X=V|NXs]) :-
!,
simplify(Xs,I,NXs).
@ -117,6 +127,9 @@ into_dict(D,V0=T) :-
listify(T,L),
D.q.answer[V0] := L.
listify(X,X) :-
atomic(X),
!.
listify('$VAR'(Bnd), V) :-
!,
listify_var(Bnd, V).
@ -127,6 +140,9 @@ listify(A:As, A:Vs) :-
(atom(A);string(A)),
!,
maplist(listify,As, Vs).
listify({Xs}, I, NXs) :-
!,
simplify(Xs,I,NXs).
listify(WellKnown, V) :-
WellKnown=..[N|As],
length(As,Sz),
@ -134,16 +150,9 @@ listify(WellKnown, V) :-
!,
maplist(listify,As, Vs),
V =.. [N|Vs].
listify('$VAR'(Bnd), V) :-
!,
listify_var(Bnd, V).
listify(T, t(S,V)) :-
T =.. [S,A|As],
!,
maplist(listify, [A|As], Vs),
V =.. [t|Vs].
listify(S, S).
listify(T, [N,LAs]) :-
T=..[N|As],
listify(As, LAs).
listify_var(I, S) :-
I >= 0,

View File

@ -81,7 +81,7 @@ extensions=[Extension('_yap', native_sources,
('MINOR_VERSION', '0'),
('_YAP_NOT_INSTALLED_', '1'),
('YAP_PYTHON', '1')],
runtime_library_dirs=['yap4py','${RCMAKE_INSTALL_FULL_LIBDIR}','${CMAKE_INSTALL_FULL_BINDIR}'],
runtime_library_dirs=['yap4py','${CMAKE_INSTALL_FULL_LIBDIR}','${CMAKE_INSTALL_FULL_BINDIR}'],
swig_opts=['-modern', '-c++', '-py3','-I${RELATIVE_SOURCE}/CXX'],
library_dirs=['../../..','../../../CXX','../../packages/python',"${YAP_INSTALL_FULL_DLLDIR}","${CMAKE_INSTALL_FULL_BINDIR}", '.'],
extra_link_args=my_extra_link_args,

View File

@ -809,7 +809,7 @@ def cell_structure(s):
if repeats.strip().isdecimal():
reps = int(repeats)
sep = sepator
elif loop == '*':
elif loop == ';':
reps = -1
elif loop == '?':
reps = 10

View File

@ -954,6 +954,11 @@ prolog:message( correspondence ) -->
prolog:message( r_root ) -->
['Real was unable to find the R root directory. \n If you have installed R from sources set $R_HOME to point to $PREFIX/lib/R.\n You should also make sure libR.so is in a directory appearing in $LD_LIBRARY_PATH' - [] ].
eval_text( Text ) :-
atomic_to_term( Text, Goal, VarNames ),
call(user:Goal).
:- at_halt(halt_r).
:- initialization(start_r, now).

View File

@ -6,10 +6,10 @@
using namespace Rcpp;
RcppExport SEXP _rcpp_module_boot_yap4r();
RcppExport SEXP _rcpp_module_boot_mod_yap4r();
static const R_CallMethodDef CallEntries[] = {
{"_rcpp_module_boot_yap4r", (DL_FUNC) &_rcpp_module_boot_yap4r, 0},
{"_rcpp_module_boot_mod_yap4r", (DL_FUNC) &_rcpp_module_boot_mod_yap4r, 0},
{NULL, NULL, 0}
};

View File

@ -26,6 +26,8 @@ public:
std::string p_module = "user");
bool more();
bool done();
bool eval_text(std::string l);
bool run(SEXP s);
SEXP peek(int i);
bool compile(std::string s);
bool library(std::string s);
@ -74,6 +76,17 @@ bool yap4r::query(std::string p_name, GenericVector sexps,
return rc;
}
bool yap4r::run(SEXP l) {
yhandle_t yh = Yap_InitHandle(MkVarTerm());
if (!sexp_to_pl(yh, l))
return false;
return yap->mgoal(Yap_GetFromHandle(yh), USER_MODULE);
}
bool yap4r::eval_text(std::string l) {
YAPAtomTerm t = YAPAtomTerm(l.c_str());
return yap->mgoal(YAPApplTerm("eval_text", t).term(), MkAtomTerm(Yap_LookupAtom("real")));
}
bool yap4r::compile(std::string s) {
YAPTerm t;
@ -116,13 +129,14 @@ SEXP yap4r::peek(int i) {
return term_to_sexp(Yap_InitSlot(Yap_XREGS[i]), false);
}
RCPP_MODULE(yap4r) {
RCPP_MODULE(mod_yap4r) {
class_<yap4r>("yap4r")
.constructor("create an object encapsulating a Prolog engine")
.method("query", &yap4r::query,
"create an active query within the engine")
.method("query", &yap4r::query, "create an active query within the engine")
.method("more", &yap4r::more, "ask for an extra solution")
.method("done", &yap4r::done, "terminate the query")
.method("eval_text", &yap4r::eval_text, "terminate the query")
.method("run", &yap4r::run, "terminate the query")
.method("compile", &yap4r::compile, "compile the file")
.method("library", &yap4r::library, "compile the library")
.method("peek", &yap4r::peek, "load arg[i] into R");

View File

@ -530,8 +530,8 @@ write_query_answer( Bindings ) :-
write_term(user_error,G,Opts) ;
format(user_error,'~w',[G])
).
'$write_goal_output'(G, First, [M:G|NG], next, NG) :-
'$current_module'(M),
'$write_goal_output'(G0, First, [M:G|NG], next, NG) :-
'$yap_strip_module'(G0,M,G),
( First = first -> true ; format(user_error,',~n',[]) ),
( yap_flag(toplevel_print_options, Opts) ->
write_term(user_error,G,Opts) ;