This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/CXX/yapq.hh

385 lines
11 KiB
C++
Raw Normal View History

2017-05-02 07:38:23 +01:00
2017-03-20 15:52:48 +00:00
/**
2017-04-07 23:10:59 +01:00
* @file yapq.hh
2017-05-02 07:38:23 +01:00
*
2017-05-19 09:56:37 +01:00
* @defgroup yap-cplus-query-handling Query Handling in the YAP interface.
2017-04-07 23:10:59 +01:00
* @brief Engine and Query Management
2017-03-20 15:52:48 +00:00
*
* @ingroup yap-cplus-interface
*
2017-04-07 23:10:59 +01:00
* @{
2017-05-02 07:38:23 +01:00
*
2017-12-20 00:29:15 +00:00
* These classes wrap engine and query. An engine is an environment where we
* can rum Prolog, that is, where we can run queries.
2017-04-07 23:10:59 +01:00
*
2017-05-02 07:38:23 +01:00
* Also, supports callbacks and engine configuration.
*
2017-03-20 15:52:48 +00:00
*/
2015-02-09 01:52:10 +00:00
#ifndef YAPQ_HH
#define YAPQ_HH 1
2017-06-12 18:00:47 +01:00
class X_API YAPPredicate;
2016-07-31 16:22:24 +01:00
2015-02-09 01:52:10 +00:00
/**
Queries and engines
*/
2017-11-21 15:44:43 +00:00
#if __ANDROID__
#endif
2015-02-09 01:52:10 +00:00
/**
* @brief Queries
*
* interface to a YAP Query;
* uses an SWI-like status info internally.
*/
2017-12-20 00:29:15 +00:00
class X_API YAPQuery : public YAPPredicate {
2016-07-31 16:22:24 +01:00
bool q_open;
int q_state;
2018-02-26 21:38:19 +00:00
yhandle_t q_handles;
2016-07-31 16:22:24 +01:00
struct yami *q_p, *q_cp;
int q_flags;
YAP_dogoalinfo q_h;
2017-09-06 01:16:36 +01:00
YAPPairTerm names;
YAPTerm goal;
2016-09-21 20:41:23 +01:00
// temporaries
2017-12-20 00:29:15 +00:00
Term tnames, tgoal;
2018-03-12 15:11:59 +00:00
YAPError *e;
2017-12-20 00:29:15 +00:00
inline void setNext() { // oq = LOCAL_execution;
// LOCAL_execution = this;
q_open = true;
q_state = 0;
q_flags = true; // PL_Q_PASS_EXCEPTION;
2017-06-05 13:06:12 +01:00
2017-12-20 00:29:15 +00:00
q_p = P;
q_cp = CP;
// make sure this is safe
q_handles = LOCAL_CurSlot;
};
2017-05-19 09:56:37 +01:00
2017-12-20 00:29:15 +00:00
void openQuery();
2016-07-31 16:22:24 +01:00
2017-12-20 00:29:15 +00:00
PredEntry *rewriteUndefQuery();
2017-05-19 09:56:37 +01:00
2015-02-09 01:52:10 +00:00
public:
2017-12-20 00:29:15 +00:00
YAPQuery() {
goal = TermTrue;
openQuery();
};
2015-02-09 01:52:10 +00:00
/// main constructor, uses a predicate and an array of terms
///
2016-07-31 16:22:24 +01:00
/// It is given a YAPPredicate _p_ , and an array of terms that must have at
/// least
2015-02-09 01:52:10 +00:00
/// the same arity as the functor.
YAPQuery(YAPPredicate p, YAPTerm t[]);
/// full constructor,
///
2017-05-02 07:38:23 +01:00
///
2016-07-31 16:22:24 +01:00
/// It is given a functor, module, and an array of terms that must have at
/// least
2015-02-09 01:52:10 +00:00
/// the same arity as the functor.
YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm t[]);
/// functor/term constructor,
///
/// It is given a functor, and an array of terms that must have at least
/// the same arity as the functor. Works within the current module.
2017-12-20 00:29:15 +00:00
// YAPQuery(YAPFunctor f, YAPTerm t[]);
2015-02-09 01:52:10 +00:00
/// string constructor without varnames
///
2016-07-31 16:22:24 +01:00
/// It is given a string, calls the parser and obtains a Prolog term that
/// should be a callable
2015-04-13 13:28:17 +01:00
/// goal.
2017-12-20 00:29:15 +00:00
inline YAPQuery(const char *s) : YAPPredicate(s, tgoal, tnames) {
2017-05-27 22:54:00 +01:00
CELL *qt = nullptr;
2017-12-20 00:29:15 +00:00
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "got game %d",
LOCAL_CurSlot);
if (!ap)
return;
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "%s", names.text());
if (IsPairTerm(tgoal)) {
qt = RepPair(tgoal);
tgoal = Yap_MkApplTerm(FunctorCsult, 1, qt);
ap = RepPredProp(PredPropByFunc(FunctorCsult, TermProlog));
}
goal = YAPTerm(tgoal);
if (IsApplTerm(tgoal)) {
Functor f = FunctorOfTerm(tgoal);
if (!IsExtensionFunctor(f)) {
2018-02-07 11:24:05 +00:00
arity_t arity = ap->ArityOfPE;
if (arity) {
qt = RepAppl(tgoal) + 1;
for (arity_t i = 0; i < arity; i++)
XREGS[i + 1] = qt[i];
}
2017-05-27 22:54:00 +01:00
}
2017-12-20 00:29:15 +00:00
}
names = YAPPairTerm(tnames);
openQuery();
2017-05-19 09:56:37 +01:00
};
// inline YAPQuery() : YAPPredicate(s, tgoal, tnames)
// {
// __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "got game %ld",
// if (!ap)
// return;
2017-09-06 01:16:36 +01:00
// __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "%s", vnames.text());
2017-05-19 09:56:37 +01:00
// goal = YAPTerm(tgoal);
// names = YAPPairTerm(tnames);
// openQuery(tgoal);
// };
2016-07-31 16:22:24 +01:00
/// string constructor with just an atom
///
2017-05-19 09:56:37 +01:00
/// It i;
///};
2017-05-27 22:54:00 +01:00
YAPQuery(YAPTerm t);
2015-02-09 01:52:10 +00:00
/// set flags for query execution, currently only for exception handling
2016-07-31 16:22:24 +01:00
void setFlag(int flag) { q_flags |= flag; }
2015-02-09 01:52:10 +00:00
/// reset flags for query execution, currently only for exception handling
2017-12-20 00:29:15 +00:00
void resetFlag(int flag) { q_flags &= ~flag; }
/// first query
///
/// actually implemented by calling the next();
inline bool first() { return next(); }
/// ask for the next solution of the current query
/// same call for every solution
bool next();
/// does this query have open choice-points?
/// or is it deterministic?
bool deterministic();
/// represent the top-goal
const char *text();
/// remove alternatives in the current search space, and finish the current
/// query
/// finish the current query: undo all bindings.
void close();
/// query variables.
void cut();
Term namedVars() { return names.term(); };
YAPPairTerm namedVarTerms() { return names; };
/// query variables, but copied out
std::vector<Term> namedVarsVector() { return names.listToArray(); };
/// convert a ref to a binding.
YAPTerm getTerm(yhandle_t t);
/// simple YAP Query;
/// just calls YAP and reports success or failure, Useful when we just
/// want things done, eg YAPCommand("load_files(library(lists), )")
inline bool command() {
bool rc = next();
close();
return rc;
};
2015-02-09 01:52:10 +00:00
};
// Java support
2016-07-31 16:22:24 +01:00
/// This class implements a callback Prolog-side. It will be inherited by the
/// Java or Python
2015-02-09 01:52:10 +00:00
/// class that actually implements the callback.
2017-12-20 00:29:15 +00:00
class X_API YAPCallback {
2015-02-09 01:52:10 +00:00
public:
2016-07-31 16:22:24 +01:00
virtual ~YAPCallback() {}
virtual void run() { LOG("callback"); }
virtual void run(char *s) {}
2015-02-09 01:52:10 +00:00
};
2017-05-02 07:38:23 +01:00
/// @brief Setup all arguments to a new engine
2017-12-20 00:29:15 +00:00
struct X_API YAPEngineArgs : YAP_init_args {
2018-02-21 17:41:00 +00:00
2017-05-02 07:38:23 +01:00
public:
2018-02-14 00:13:13 +00:00
YAPEngineArgs() {
2018-02-26 21:38:19 +00:00
// const std::string *s = new std::string("startup.yss");
2018-01-27 10:17:27 +00:00
Embedded = true;
2018-02-25 00:29:08 +00:00
Yap_InitDefaults(this, nullptr, 0, nullptr);
2017-11-29 13:47:57 +00:00
#if YAP_PYTHON
2017-12-20 00:29:15 +00:00
Embedded = true;
2017-11-27 13:36:19 +00:00
python_in_python = Py_IsInitialized();
2018-02-21 17:41:00 +00:00
#endif
#if __ANDROID__
2017-11-27 13:36:19 +00:00
#endif
2017-09-06 01:16:36 +01:00
};
2017-05-02 07:38:23 +01:00
2017-12-20 00:29:15 +00:00
inline void setEmbedded(bool fl) { Embedded = fl; };
2017-05-02 07:38:23 +01:00
2017-12-20 00:29:15 +00:00
inline bool getEmbedded() { return Embedded; };
2017-05-02 07:38:23 +01:00
2017-12-20 00:29:15 +00:00
inline void setStackSize(bool fl) { StackSize = fl; };
2017-05-02 07:38:23 +01:00
2017-12-20 00:29:15 +00:00
inline bool getStackSize() { return StackSize; };
2017-05-02 07:38:23 +01:00
2017-12-20 00:29:15 +00:00
inline void setTrailSize(bool fl) { TrailSize = fl; };
2017-05-02 07:38:23 +01:00
2017-12-20 00:29:15 +00:00
inline bool getTrailSize() { return TrailSize; };
2017-05-02 07:38:23 +01:00
2017-12-20 00:29:15 +00:00
inline bool getMStackSize() { return StackSize; };
2017-05-02 07:38:23 +01:00
2017-12-20 00:29:15 +00:00
inline void setMaxTrailSize(bool fl) { MaxTrailSize = fl; };
inline bool getMaxTrailSize() { return MaxTrailSize; };
2017-05-02 07:38:23 +01:00
2018-03-02 21:18:24 +00:00
inline void setPLDIR(const char *fl) {
LIBDIR = (const char *)malloc(strlen(fl) + 1);
strcpy((char *)LIBDIR, fl);
2017-05-02 07:38:23 +01:00
};
2018-03-02 21:18:24 +00:00
inline const char *getPLDIR() { return PLDIR; };
inline void setINPUT_STARTUP(const char *fl) {
2018-02-26 21:38:19 +00:00
INPUT_STARTUP = (const char *)malloc(strlen(fl) + 1);
strcpy((char *)INPUT_STARTUP, fl);
2017-05-02 07:38:23 +01:00
};
2018-02-26 21:38:19 +00:00
inline const char *getINPUT_STARTUP() { return INPUT_STARTUP; };
2018-02-25 00:29:08 +00:00
2018-02-26 21:38:19 +00:00
inline void setOUTPUT_RESTORE(const char *fl) {
OUTPUT_STARTUP = (const char *)malloc(strlen(fl) + 1);
strcpy((char *)OUTPUT_STARTUP, fl);
};
2017-05-14 11:36:09 +01:00
2018-02-26 21:38:19 +00:00
inline const char *getOUTPUT_STARTUP() { return OUTPUT_STARTUP; };
2017-05-14 11:36:09 +01:00
2018-02-26 21:38:19 +00:00
inline void setBOOTFILE(const char *fl) {
BOOTFILE = (const char *)malloc(strlen(fl) + 1);
strcpy((char *)BOOTFILE, fl);
2017-05-02 07:38:23 +01:00
};
inline const char *getBOOTFILE() { return BOOTFILE; };
2017-05-02 07:38:23 +01:00
2018-02-25 00:29:08 +00:00
inline void setPrologBOOTSTRAP(const char *fl) {
BOOTSTRAP = (const char *)malloc(strlen(fl) + 1);
strcpy((char *)BOOTSTRAP, fl);
2018-02-07 11:24:05 +00:00
};
2018-02-25 00:29:08 +00:00
inline const char *getBOOTSTRAP() { return BOOTSTRAP; };
2018-02-07 11:24:05 +00:00
2018-01-18 14:47:27 +00:00
inline void setPrologGoal(const char *fl) { PrologGoal = fl; };
2017-05-02 07:38:23 +01:00
2018-01-18 14:47:27 +00:00
inline const char *getPrologGoal() { return PrologGoal; };
2017-12-20 00:29:15 +00:00
2018-01-18 14:47:27 +00:00
inline void setPrologTopLevelGoal(const char *fl) {
PrologTopLevelGoal = fl;
2017-05-02 07:38:23 +01:00
};
2018-02-07 11:24:05 +00:00
inline const char *getPrologTopLevelGoal() { return PrologTopLevelGoal; };
2017-05-02 07:38:23 +01:00
2018-03-14 00:41:05 +00:00
inline void setHaltAfterBoot(bool fl) { HaltAfterBoot = fl; };
2017-05-02 07:38:23 +01:00
2018-03-14 00:41:05 +00:00
inline bool getHaltAfterBoot() { return HaltAfterBoot; };
2017-05-02 07:38:23 +01:00
2017-12-20 00:29:15 +00:00
inline void setFastBoot(bool fl) { FastBoot = fl; };
2017-05-02 07:38:23 +01:00
2017-12-20 00:29:15 +00:00
inline bool getFastBoot() { return FastBoot; };
2017-11-21 15:44:43 +00:00
2017-12-20 00:29:15 +00:00
inline void setArgc(int fl) { Argc = fl; };
2017-05-02 07:38:23 +01:00
2017-12-20 00:29:15 +00:00
inline int getArgc() { return Argc; };
2017-05-02 07:38:23 +01:00
2017-12-20 00:29:15 +00:00
inline void setArgv(char **fl) { Argv = fl; };
2017-05-02 07:38:23 +01:00
2017-12-20 00:29:15 +00:00
inline char **getArgv() { return Argv; };
2017-05-02 07:38:23 +01:00
};
2015-02-09 01:52:10 +00:00
/**
* @brief YAP Engine: takes care of the execution environment
where we can go executing goals.
*
*
*/
2017-12-20 00:29:15 +00:00
class YAPEngine {
2015-02-09 01:52:10 +00:00
private:
2017-05-08 18:51:29 +01:00
YAPEngineArgs *engine_args;
2015-02-09 01:52:10 +00:00
YAPCallback *_callback;
YAPError yerror;
2018-02-14 00:13:13 +00:00
void doInit(YAP_file_type_t BootMode, YAPEngineArgs *cargs);
2016-10-16 23:18:51 +01:00
YAP_dogoalinfo q;
2018-03-12 15:11:59 +00:00
YAPError e;
PredEntry *rewriteUndefEngineQuery(PredEntry *ap, Term &t, Term tmod);
2016-07-31 16:22:24 +01:00
2017-12-20 00:29:15 +00:00
public:
/// construct a new engine; may use a variable number of arguments
YAPEngine(YAPEngineArgs *cargs) {
2017-05-08 18:51:29 +01:00
engine_args = cargs;
2017-12-20 00:29:15 +00:00
// doInit(cargs->boot_file_type);
2018-02-14 00:13:13 +00:00
doInit(YAP_QLY, cargs);
2017-05-08 18:51:29 +01:00
}; /// construct a new engine, including aaccess to callbacks
/// construct a new engine using argc/argv list of arguments
2016-07-31 16:22:24 +01:00
YAPEngine(int argc, char *argv[],
2017-12-20 00:29:15 +00:00
YAPCallback *callback = (YAPCallback *)NULL);
2016-07-31 16:22:24 +01:00
/// kill engine
2017-05-08 18:51:29 +01:00
~YAPEngine() { delYAPCallback(); };
2015-02-09 01:52:10 +00:00
/// remove current callback
2017-05-08 18:51:29 +01:00
void delYAPCallback() { _callback = 0; };
2015-02-09 01:52:10 +00:00
/// set a new callback
2017-12-20 00:29:15 +00:00
void setYAPCallback(YAPCallback *cb) {
2016-07-31 16:22:24 +01:00
delYAPCallback();
_callback = cb;
2017-05-08 18:51:29 +01:00
};
2015-02-09 01:52:10 +00:00
/// execute the callback.
2017-05-02 07:38:23 +01:00
////void run() { if (_callback) _callback.run(); }
2015-02-09 01:52:10 +00:00
/// execute the callback with a text argument.
2017-12-20 00:29:15 +00:00
void run(char *s) {
2016-07-31 16:22:24 +01:00
if (_callback)
_callback->run(s);
}
2016-09-21 20:41:23 +01:00
/// stop yap
2016-09-27 18:28:54 +01:00
void close() { Yap_exit(0); }
2016-09-21 20:41:23 +01:00
2015-02-09 01:52:10 +00:00
/// execute the callback with a text argument.
2016-09-21 20:41:23 +01:00
bool hasError() { return LOCAL_Error_TYPE != YAP_NO_ERROR; }
2015-02-09 01:52:10 +00:00
/// build a query on the engine
2016-07-31 16:22:24 +01:00
YAPQuery *query(const char *s) { return new YAPQuery(s); };
2017-05-27 22:54:00 +01:00
/// build a query from a term
YAPQuery *query(YAPTerm t) { return new YAPQuery(t); };
/// build a query from a Prolog term (internal)
YAPQuery *qt(Term t) { return new YAPQuery(YAPTerm(t)); };
2015-02-09 01:52:10 +00:00
/// current module for the engine
2016-07-31 16:22:24 +01:00
YAPModule currentModule() { return YAPModule(); }
/// given a handle, fetch a term from the engine
inline YAPTerm getTerm(yhandle_t h) { return YAPTerm(h); }
2015-07-23 01:33:30 +01:00
/// current directory for the engine
2016-09-21 20:41:23 +01:00
bool call(YAPPredicate ap, YAPTerm ts[]);
2016-09-27 18:28:54 +01:00
/// current directory for the engine
2018-03-19 15:41:06 +00:00
bool goal(YAPTerm Yt, YAPModule module) { return mgoal(Yt.term(),module.term()); };
2017-02-20 14:38:00 +00:00
/// current directory for the engine
bool mgoal(Term t, Term tmod);
2016-10-16 23:18:51 +01:00
/// current directory for the engine
2017-02-20 14:38:00 +00:00
2017-12-20 00:29:15 +00:00
bool goal(Term t) { return mgoal(t, CurrentModule); }
2016-10-16 23:18:51 +01:00
/// reset Prolog state
void reSet();
2017-05-02 07:38:23 +01:00
/// assune that there are no stack pointers, just release memory
2016-10-16 23:18:51 +01:00
// for last execution
void release();
2016-09-21 20:41:23 +01:00
2017-12-20 00:29:15 +00:00
const char *currentDir() {
2016-07-31 16:22:24 +01:00
char dir[1024];
std::string s = Yap_getcwd(dir, 1024 - 1);
return s.c_str();
};
/// report YAP version as a string
2017-12-20 00:29:15 +00:00
const char *version() {
2016-07-31 16:22:24 +01:00
std::string s = Yap_version();
return s.c_str();
};
2017-05-02 07:38:23 +01:00
//> call a deterninistic predicate: the user will construct aterm of
//> arity N-1. YAP adds an extra variable which will have the
//> output.
2017-06-22 10:41:41 +01:00
YAPTerm funCall(YAPTerm t) { return YAPTerm(fun(t.term())); };
2016-10-16 23:18:51 +01:00
Term fun(Term t);
2017-06-22 10:41:41 +01:00
Term fun(YAPTerm t) { return fun(t.term()); };
2017-05-08 18:51:29 +01:00
//> set a StringFlag, usually a path
//>
2017-12-20 00:29:15 +00:00
bool setStringFlag(std::string arg, std::string path) {
return setYapFlag(MkAtomTerm(Yap_LookupAtom(arg.data())),
MkAtomTerm(Yap_LookupAtom(path.data())));
2017-05-08 18:51:29 +01:00
};
2017-05-19 09:56:37 +01:00
2017-12-20 00:29:15 +00:00
Term top_level(std::string s);
Term next_answer(YAPQuery *&Q);
};
2015-02-09 01:52:10 +00:00
#endif /* YAPQ_HH */
2017-03-20 15:52:48 +00:00
2017-12-20 00:29:15 +00:00
/// @}