#include #undef Realloc #undef Malloc #undef Free #include #include #include #include "real.h" using namespace Rcpp; class yap4r { YAPEngine *yap; YAPQuery *q; std::vector args; bool failed; public: yap4r(); bool query(std::string p_name,std::string p_module,Rcpp::GenericVector sexps); bool more(); bool done(); SEXP peek(int i); }; yap4r::yap4r() { YAPEngineArgs *yargs = new YAPEngineArgs(); yap = new YAPEngine(yargs); }; bool yap4r::query(std::string p_name,std::string p_module,Rcpp::GenericVector sexps) { if (q) { q->close(); q = NULL; } std::vector args = std::vector(); yhandle_t sls = Yap_NewHandles(sexps.length()); for (int i=0; inext(); if (!rc) { failed = true; } return rc; } bool yap4r::done() { if (failed) return false; if (q) q->cut(); q = NULL; return true; } SEXP yap4r::peek(int i) { if (failed || q==nullptr) return R_MissingArg; return term_to_sexp(Yap_InitSlot(Yap_XREGS[i]), false); } RCPP_MODULE(mod_yap4r) { class_( "yap4r" ) .constructor("create an object encapsulating a Prolog 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( "peek", &yap4r::peek, "load arg[i] into R") ; }