This commit is contained in:
Vitor Santos Costa 2017-09-06 01:16:36 +01:00
parent b5bfb638a3
commit 02a2bb0b0b
5 changed files with 743 additions and 762 deletions

View File

@ -121,4 +121,3 @@ public:
#endif /* YAPA_HH */ #endif /* YAPA_HH */
/// @} /// @}

View File

@ -27,14 +27,12 @@ X_API void YAP_UserBackCPredicate(const char *, YAP_UserCPred, YAP_UserCPred,
} }
static YAPEngine *curren;
YAPAtomTerm::YAPAtomTerm(char *s) YAPAtomTerm::YAPAtomTerm(char *s)
{ // build string { // build string
BACKUP_H(); BACKUP_H();
CACHE_REGS CACHE_REGS
seq_tv_t inp, out; seq_tv_t inp, out;
inp.val.c = s; inp.val.c = s;
inp.type = YAP_STRING_CHARS; inp.type = YAP_STRING_CHARS;
out.type = YAP_STRING_ATOM; out.type = YAP_STRING_ATOM;
@ -189,7 +187,6 @@ YAPApplTerm::YAPApplTerm(std::string f, std::vector<YAPTerm> ts)
mk(o); mk(o);
} }
YAPApplTerm::YAPApplTerm(YAPFunctor f) : YAPTerm() YAPApplTerm::YAPApplTerm(YAPFunctor f) : YAPTerm()
{ {
BACKUP_H(); BACKUP_H();
@ -652,20 +649,20 @@ YAPQuery::YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm ts[])
/* ignore flags for now */ /* ignore flags for now */
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
Term *nts; Term *nts;
Term tgoal; Term goal;
if ( ts) { if ( ts) {
goal = new YAPApplTerm(f, ts); goal = YAPApplTerm(f, ts).term();
nts = RepAppl(goal->term())+1; nts = RepAppl(goal)+1;
} else { } else {
goal = new YAPVarTerm(); goal = MkVarTerm();
nts = nullptr; nts = nullptr;
} }
openQuery(goal->term(), nts); openQuery( nts);
names = new YAPPairTerm(); names = YAPPairTerm( TermNil );
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
} }
#if 0 #if 0
@ -674,14 +671,13 @@ YAPQuery::YAPQuery(YAPFunctor f, YAPTerm ts[]) : YAPPredicate(f) {
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
CELL *nts; CELL *nts;
if (ts) { if (ts) {
tgoal = YAPApplTerm(f, nts); goal = YAPApplTerm(f, nts);
} else { } else {
tgoal = YAPVarTerm(); goal = YAPVarTerm();
nts = nullptr; nts = nullptr;
} }
*names = new YAPPairTerm(); names = YAPPairTerm( TermNil );
*goal = new YAPTerm(tgoal); openQuery(term(), nts);
openQuery(goal.term(), nts);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
} }
#endif #endif
@ -695,15 +691,15 @@ YAPQuery::YAPQuery(YAPTerm t) : YAPPredicate(t)
if (IsApplTerm(tt)) { if (IsApplTerm(tt)) {
Functor f = FunctorOfTerm(tt); Functor f = FunctorOfTerm(tt);
if (IsExtensionFunctor(f)) if (IsExtensionFunctor(f))
nts = nullptr; nts = nullptr;
nts = RepAppl(tt)+1; nts = RepAppl(goal.term())+1;
} else if (IsPairTerm(tt)) { } else if (IsPairTerm(tt)) {
nts = RepPair(tt); nts = RepPair(tt);
} else { } else {
nts = nullptr; nts = nullptr;
} }
names = new YAPPairTerm(); openQuery( nts);
goal = new YAPTerm(t); names = YAPPairTerm( TermNil );
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
} }
@ -712,14 +708,15 @@ YAPQuery::YAPQuery(YAPPredicate p, YAPTerm ts[]) : YAPPredicate(p.ap) {
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
arity_t arity = p.ap->ArityOfPE; arity_t arity = p.ap->ArityOfPE;
if (arity) { if (arity) {
goal = new YAPApplTerm(YAPFunctor(p.ap->FunctorOfPred), ts); goal = YAPApplTerm(YAPFunctor(p.ap->FunctorOfPred), ts).term();
for (arity_t i =0; i < arity; i++) { for (arity_t i =0; i < arity; i++)
XREGS[i + 1] = ts[i].term(); XREGS[i+1]=ts[i].term();
} openQuery( nullptr);
} else { } else {
goal = new YAPAtomTerm((Atom)(p.ap->FunctorOfPred)); } goal = YAPAtomTerm((Atom)(p.ap->FunctorOfPred));
openQuery(goal->term(), nullptr); openQuery(nullptr);
names = new YAPPairTerm(); }
names = TermNil;
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
} }
@ -729,21 +726,13 @@ bool YAPQuery::next()
bool result = false; bool result = false;
sigjmp_buf buf, *oldp = LOCAL_RestartEnv; sigjmp_buf buf, *oldp = LOCAL_RestartEnv;
Term terr; Term terr;
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "next %d %ld", try
q_state, LOCAL_CurSlot);
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "next %d %s %ld",
q_state, names->text(), LOCAL_CurSlot);
if (ap == NULL || ap->OpcodeOfPred == UNDEF_OPCODE) {
ap = rewriteUndefQuery();
}
LOCAL_RestartEnv = &q_env;
try
{ {
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
if (!q_open) if (!q_open)
return false; return false;
if (false && sigsetjmp(q_env, false)) LOCAL_RestartEnv = &buf;
if (sigsetjmp(*LOCAL_RestartEnv, false))
{ {
//throw YAPError(); //throw YAPError();
return false; return false;
@ -762,58 +751,60 @@ bool YAPQuery::next()
} }
if (result) if (result)
{ {
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "names %d %s %ld", __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "vnames %d %s %ld",
q_state, names->text(), LOCAL_CurSlot); q_state, vnames.text(), LOCAL_CurSlot);
} }
else else
{ {
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "fail"); __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "fail");
} }
q_state = 1; q_state = 1;
if ((terr = Yap_GetException())) if ((terr = Yap_GetException()))
{
if ((terr = Yap_GetException()))
{ {
if ((terr = Yap_GetException())) LOCAL_RestartEnv = &buf;
{ throw YAPError();
throw YAPError();
}
} }
}
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "out %d", result); __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "out %d", result);
if (!result) if (!result)
{ {
YAP_LeaveGoal(false, &q_h); YAP_LeaveGoal(false, &q_h);
Yap_CloseHandles(q_handles); Yap_CloseHandles(q_handles);
q_open = false; q_open = false;
} }
else else
{ {
q_handles = Yap_StartSlots(); q_handles = Yap_StartSlots();
} }
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
LOCAL_RestartEnv = oldp; LOCAL_RestartEnv = oldp;
return result; return result;
} }
catch (YAPError e) catch (YAPError e)
{ {
q_open = false; q_open = false;
Yap_PopTermFromDB(LOCAL_ActiveError->errorTerm); Yap_PopTermFromDB(LOCAL_ActiveError->errorTerm);
memset(LOCAL_ActiveError, 0, sizeof(*LOCAL_ActiveError)); memset(LOCAL_ActiveError, 0, sizeof(*LOCAL_ActiveError));
YAP_LeaveGoal(false, &q_h); YAP_LeaveGoal(false, &q_h);
Yap_CloseHandles(q_handles); Yap_CloseHandles(q_handles);
q_open = false; q_open = false;
std::cerr << "Exception received by " << __func__ << "( " << YAPTerm(terr).text() << ").\n Forwarded...\n\n"; std::cerr << "Exception received by " << __func__ << "( " << YAPTerm(terr).text() << ").\n Forwarded...\n\n";
throw e; LOCAL_RestartEnv = oldp;
} throw e;
}
} }
PredEntry * PredEntry *
YAPQuery::rewriteUndefQuery() YAPQuery::rewriteUndefQuery()
{ {
Term ts[3]; Term ts[3];
ARG1 = ts[0] = goal->term(); ARG1 = ts[0] = goal.term();
ARG2 = ts[1] = ap->ModuleOfPred; ARG2 = ts[1] = ap->ModuleOfPred;
ARG3 = ts[2] = Yap_cp_as_integer(B PASS_REGS); ARG3 = ts[2] = Yap_cp_as_integer(B PASS_REGS);
goal = new YAPApplTerm(FunctorUndefinedQuery, ts); goal = YAPApplTerm(FunctorUndefinedQuery, ts);
return ap = PredUndefinedQuery; return ap = PredUndefinedQuery;
} }
@ -895,6 +886,39 @@ JNIEXPORT jint JNICALL JNI_MySQLOnLoad(JavaVM *vm, void *reserved)
return JNI_VERSION_1_6; return JNI_VERSION_1_6;
} }
char *Yap_AndroidBufp;
static size_t Yap_AndroidMax, Yap_AndroidSz;
extern void (*Yap_DisplayWithJava)(int c);
void Yap_displayWithJava(int c)
{
char *ptr = Yap_AndroidBufp;
if (!ptr)
ptr = Yap_AndroidBufp = (char *)malloc(Yap_AndroidSz);
ptr[Yap_AndroidSz++] = c;
if (Yap_AndroidMax - 1 == Yap_AndroidSz)
{
if (Yap_AndroidMax < 32 * 1024)
{
Yap_AndroidMax *= 2;
}
else
{
Yap_AndroidMax += 32 * 1024;
}
Yap_AndroidBufp = (char *)realloc(ptr, Yap_AndroidMax);
}
Yap_AndroidBufp[Yap_AndroidSz] = '\0';
if (c == '\n')
{
Yap_AndroidBufp[Yap_AndroidSz] = '\0';
curren->run(Yap_AndroidBufp);
Yap_AndroidSz = 0;
}
}
#endif #endif
@ -908,18 +932,22 @@ void YAPEngine::doInit(YAP_file_type_t BootMode)
/* Begin preprocessor code */ /* Begin preprocessor code */
/* live */ /* live */
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "initialize_prolog"); __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "initialize_prolog");
curren = this; #if __ANDROID__
Yap_AndroidBufp = (char *)malloc(Yap_AndroidMax = 4096);
Yap_AndroidBufp[0] = '\0';
Yap_AndroidSz = 0;
#endif
//yerror = YAPError(); //yerror = YAPError();
#if YAP_PYTHON #if YAP_PYTHON
do_init_python(); do_init_python();
#endif #endif
YAP_PredEntryPtr p = YAP_AtomToPred( YAP_LookupAtom("initialize_prolog") ); YAP_PredEntryPtr p = YAP_AtomToPred( YAP_LookupAtom("initialize_prolog") );
YAPQuery initq = YAPQuery(YAPPredicate(p), nullptr); YAPQuery initq = YAPQuery(YAPPredicate(p), nullptr);
if (initq.next()) if (initq.next())
{ {
initq.cut(); initq.cut();
} }
CurrentModule = TermUser; CurrentModule = TermUser;
} }

View File

@ -6,13 +6,12 @@
*/ */
#define YAP_CPP_INTERFACE 1 #define YAP_CPP_INTERFACE 1
#include <gmpxx.h> #include <gmpxx.h>
#include <vector>
#include <string>
#include <iostream> #include <iostream>
#include <string>
#include <vector>
/*! /*!
* *
@ -71,26 +70,31 @@ extern "C" {
#endif #endif
#if _MSC_VER || defined(__MINGW32__) #if _MSC_VER || defined(__MINGW32__)
//#include <windows.h> //#include <windows.h>
#endif #endif
// taken from yap_structs.h // taken from yap_structs.h
#include "iopreds.h" #include "iopreds.h"
X_API extern void YAP_UserCPredicate(const char *, YAP_UserCPred, YAP_Arity arity); X_API extern void YAP_UserCPredicate(const char *, YAP_UserCPred,
YAP_Arity arity);
/* extern void UserCPredicateWithArgs(const char *name, int *fn(), unsigned int arity) /* extern void UserCPredicateWithArgs(const char *name, int *fn(), unsigned int
*/ * arity)
X_API extern void YAP_UserCPredicateWithArgs(const char *, YAP_UserCPred, YAP_Arity, */
YAP_Term); X_API extern void YAP_UserCPredicateWithArgs(const char *, YAP_UserCPred,
YAP_Arity, YAP_Term);
X_API extern void UserBackCPredicate(const char *name, int *init(), int *cont(),
int arity, int extra);
X_API extern YAP_Term YAP_ReadBuffer(const char *s, YAP_Term *tp);
X_API extern void UserBackCPredicate(const char *name, int *init(), int *cont(), int
arity, int extra);
#if YAP_PYTHON #if YAP_PYTHON
#include <Python.h> #include <Python.h>
extern bool python_in_python; extern bool python_in_python;
#endif #endif
} }

View File

@ -9,8 +9,8 @@
* *
* @{ * @{
* *
* These classes wrap engine and query. An engine is an environment where we * These classes wrap engine and query. An engine is an environment where we can rum
* can rum Prolog, that is, where we can run queries. * Prolog, that is, where we can run queries.
* *
* Also, supports callbacks and engine configuration. * Also, supports callbacks and engine configuration.
* *
@ -31,7 +31,8 @@ class X_API YAPPredicate;
* interface to a YAP Query; * interface to a YAP Query;
* uses an SWI-like status info internally. * uses an SWI-like status info internally.
*/ */
class X_API YAPQuery : public YAPPredicate { class X_API YAPQuery : public YAPPredicate
{
bool q_open; bool q_open;
int q_state; int q_state;
yhandle_t q_g, q_handles; yhandle_t q_g, q_handles;
@ -40,34 +41,33 @@ class X_API YAPQuery : public YAPPredicate {
int q_flags; int q_flags;
YAP_dogoalinfo q_h; YAP_dogoalinfo q_h;
YAPQuery *oq; YAPQuery *oq;
YAPPairTerm *names; YAPPairTerm names;
YAPTerm *goal; YAPTerm goal;
// temporaries // temporaries
Term tnames, tgoal; Term tnames, tgoal ;
inline void setNext() { // oq = LOCAL_execution; inline void setNext() { // oq = LOCAL_execution;
// LOCAL_execution = this; // LOCAL_execution = this;
q_open = true; q_open = true;
q_state = 0; q_state = 0;
q_flags = true; // PL_Q_PASS_EXCEPTION; q_flags = true; // PL_Q_PASS_EXCEPTION;
q_p = P; q_p = P;
q_cp = CP; q_cp = CP;
// make sure this is safe // make sure this is safe
names = new YAPPairTerm(); q_handles = LOCAL_CurSlot;
goal = new YAPTerm(); };
q_handles = LOCAL_CurSlot;
};
void openQuery(Term *ts);
PredEntry *rewriteUndefQuery(); void openQuery( Term *ts);
PredEntry *rewriteUndefQuery();
public: public:
YAPQuery() { YAPQuery() {
goal = TermTrue; goal = TermTrue;
openQuery(nullptr); openQuery( nullptr);
}; };
/// main constructor, uses a predicate and an array of terms /// main constructor, uses a predicate and an array of terms
/// ///
/// It is given a YAPPredicate _p_ , and an array of terms that must have at /// It is given a YAPPredicate _p_ , and an array of terms that must have at
@ -85,29 +85,32 @@ public:
/// ///
/// It is given a functor, and an array of terms that must have at least /// 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. /// the same arity as the functor. Works within the current module.
// YAPQuery(YAPFunctor f, YAPTerm t[]); //YAPQuery(YAPFunctor f, YAPTerm t[]);
/// string constructor without varnames /// string constructor without varnames
/// ///
/// It is given a string, calls the parser and obtains a Prolog term that /// It is given a string, calls the parser and obtains a Prolog term that
/// should be a callable /// should be a callable
/// goal. /// goal.
inline YAPQuery(const char *s) : YAPPredicate(s, tgoal, tnames) { inline YAPQuery(const char *s) : YAPPredicate(s, tgoal, tnames)
{
CELL *qt = nullptr; CELL *qt = nullptr;
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "got game %ld", __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "got game %ld",
LOCAL_CurSlot); LOCAL_CurSlot);
if (!ap) if (!ap)
return; return;
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "ŸAPQuery"); __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "%s", vnames.text());
if (IsPairTerm(tgoal)) { goal = YAPTerm(tgoal);
qt = RepPair(tgoal); if (IsPairTerm(tgoal)) {
tgoal = qt = RepPair(tgoal);
Yap_MkApplTerm(Yap_MkFunctor(Yap_LookupAtom("consult"), 1), 1, qt); tgoal = Yap_MkApplTerm(Yap_MkFunctor(Yap_LookupAtom("consult"), 1),1,qt);
} else if (IsApplTerm(tgoal)) { } else if (IsApplTerm(tgoal)) {
Functor f = FunctorOfTerm(tgoal); Functor f = FunctorOfTerm(tgoal);
if (!IsExtensionFunctor(f)) { if (!IsExtensionFunctor(f)) {
qt = RepAppl(tgoal) + 1; qt = RepAppl(tgoal)+1;
}
} }
} names = YAPPairTerm(tnames);
openQuery(qt);
}; };
// inline YAPQuery() : YAPPredicate(s, tgoal, tnames) // inline YAPQuery() : YAPPredicate(s, tgoal, tnames)
// { // {
@ -115,7 +118,7 @@ public:
// LOCAL_CurSlot); // LOCAL_CurSlot);
// if (!ap) // if (!ap)
// return; // return;
// __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "%s", names->text()); // __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "%s", vnames.text());
// goal = YAPTerm(tgoal); // goal = YAPTerm(tgoal);
// names = YAPPairTerm(tnames); // names = YAPPairTerm(tnames);
// openQuery(tgoal); // openQuery(tgoal);
@ -128,37 +131,40 @@ public:
/// set flags for query execution, currently only for exception handling /// set flags for query execution, currently only for exception handling
void setFlag(int flag) { q_flags |= flag; } void setFlag(int flag) { q_flags |= flag; }
/// reset flags for query execution, currently only for exception handling /// reset flags for query execution, currently only for exception handling
void resetFlag(int flag) { q_flags &= ~flag; } void resetFlag(int flag) { q_flags &= ~flag; }
/// first query /// first query
/// ///
/// actually implemented by calling the next(); /// actually implemented by calling the next();
inline bool first() { return next(); } inline bool first() { return next(); }
/// ask for the next solution of the current query /// ask for the next solution of the current query
/// same call for every solution /// same call for every solution
bool next(); bool next();
/// does this query have open choice-points? /// does this query have open choice-points?
/// or is it deterministic? /// or is it deterministic?
bool deterministic(); bool deterministic();
/// represent the top-goal /// represent the top-goal
const char *text(); const char *text();
/// remove alternatives in the current search space, and finish tnamedyaphe /// remove alternatives in the current search space, and finish the current
/// current query finish the current query: undo all bindings. /// query
void close(); /// finish the current query: undo all bindings.
/// query variables. void close();
void cut(); /// query variables.
void cut();
Term namedVars() { return names->term(); }; Term namedVars() {return names.term(); };
YAPPairTerm *namedYAPVars() { return names; }; /// query variables, but copied out
/// query variables, but copied out std::vector<Term> namedVarsVector() {
YAPTerm getTerm(yhandle_t t); return names.listToArray(); };
/// simple YAP Query; /// convert a ref to a binding.
/// just calls YAP and reports success or failure, Useful when we just YAPTerm getTerm(yhandle_t t);
/// want things done, eg YAPCommand("load_files(library(lists), )") /// simple YAP Query;
inline bool command() { /// just calls YAP and reports success or failure, Useful when we just
bool rc = next(); /// want things done, eg YAPCommand("load_files(library(lists), )")
close(); inline bool command()
return rc; {
}; bool rc = next();
close();
return rc;
};
}; };
// Java support // Java support
@ -166,96 +172,170 @@ public:
/// This class implements a callback Prolog-side. It will be inherited by the /// This class implements a callback Prolog-side. It will be inherited by the
/// Java or Python /// Java or Python
/// class that actually implements the callback. /// class that actually implements the callback.
class X_API YAPCallback { class X_API YAPCallback
{
public: public:
virtual ~YAPCallback() {} virtual ~YAPCallback() {}
virtual void run() { LOG("callback"); } virtual void run() { LOG("callback"); }
virtual void run(char *s) {} virtual void run(char *s) {}
}; };
/// @brief Setup all arguments to a new engine /// @brief Setup all arguments to a new engine
class X_API YAPEngineArgs { class X_API YAPEngineArgs {
public: public:
YAP_init_args init_args; YAP_init_args init_args;
inline void setEmbedded(bool fl) { init_args.Embedded = fl; }; inline void setEmbedded( bool fl )
{
init_args.Embedded = fl;
};
inline bool getEmbedded() { return init_args.Embedded; }; inline bool getEmbedded( )
{
return init_args.Embedded;
};
inline void setStackSize(bool fl) { init_args.StackSize = fl; }; inline void setStackSize( bool fl )
{
init_args.StackSize = fl;
};
inline bool getStackSize() { return init_args.StackSize; }; inline bool getStackSize( )
{
return init_args.StackSize;
};
inline void setTrailSize(bool fl) { init_args.TrailSize = fl; }; inline void setTrailSize( bool fl )
{
init_args.TrailSize = fl;
};
inline bool getTrailSize() { return init_args.TrailSize; }; inline bool getTrailSize( )
{
return init_args.TrailSize;
};
inline bool getMStackSize() { return init_args.StackSize; }; inline bool getMStackSize( )
{
return init_args.StackSize;
};
inline void setMaxTrailSize(bool fl) { init_args.MaxTrailSize = fl; }; inline void setMaxTrailSize( bool fl )
{
init_args.MaxTrailSize = fl;
};
inline bool getMaxTrailSize() { return init_args.MaxTrailSize; }; inline bool getMaxTrailSize( )
{
return init_args.MaxTrailSize;
};
inline void setYapLibDir(const char *fl) { inline void setYapLibDir( const char * fl )
init_args.YapLibDir = (const char *)malloc(strlen(fl) + 1); {
init_args.YapLibDir = (const char *)malloc(strlen(fl)+1);
strcpy((char *)init_args.YapLibDir, fl); strcpy((char *)init_args.YapLibDir, fl);
}; };
inline const char *getYapLibDir() { return init_args.YapLibDir; }; inline const char * getYapLibDir( )
{
return init_args.YapLibDir;
};
inline void setYapShareDir(const char *fl) { inline void setYapShareDir( const char * fl )
init_args.YapShareDir = (const char *)malloc(strlen(fl) + 1); {
init_args.YapShareDir = (const char *)malloc(strlen(fl)+1);
strcpy((char *)init_args.YapShareDir, fl); strcpy((char *)init_args.YapShareDir, fl);
}; };
inline const char *getYapShareDir() { return init_args.YapShareDir; }; inline const char * getYapShareDir( )
{
return init_args.YapShareDir;
};
inline void setSavedState(const char *fl) { inline void setSavedState( const char * fl )
init_args.SavedState = (const char *)malloc(strlen(fl) + 1); {
init_args.SavedState = (const char *)malloc(strlen(fl)+1);
strcpy((char *)init_args.SavedState, fl); strcpy((char *)init_args.SavedState, fl);
}; };
inline const char *getSavedState() { return init_args.SavedState; }; inline const char * getSavedState( )
{
inline void setYapPrologBootFile(const char *fl) { return init_args.SavedState;
init_args.YapPrologBootFile = (const char *)malloc(strlen(fl) + 1);
strcpy((char *)init_args.YapPrologBootFile, fl);
}; };
inline const char *getYapPrologBootFile() { inline void setYapPrologBootFile( const char * fl )
{
init_args.YapPrologBootFile = (const char *)malloc(strlen(fl)+1);
strcpy((char *)init_args.YapPrologBootFile, fl);
};
inline const char * getYapPrologBootFile( )
{
return init_args.YapPrologBootFile; return init_args.YapPrologBootFile;
}; };
inline void setYapPrologGoal(const char *fl) { inline void setYapPrologGoal( const char * fl )
{
init_args.YapPrologGoal = fl; init_args.YapPrologGoal = fl;
}; };
inline const char *getYapPrologGoal() { return init_args.YapPrologGoal; }; inline const char * getYapPrologGoal( )
{
return init_args.YapPrologGoal;
};
inline void setYapPrologTopLevelGoal(const char *fl) { inline void setYapPrologTopLevelGoal( const char * fl )
{
init_args.YapPrologTopLevelGoal = fl; init_args.YapPrologTopLevelGoal = fl;
}; };
inline const char *getYapPrologTopLevelGoal() { inline const char * getYapPrologTopLevelGoal( )
{
return init_args.YapPrologTopLevelGoal; return init_args.YapPrologTopLevelGoal;
}; };
inline void setHaltAfterConsult(bool fl) { init_args.HaltAfterConsult = fl; }; inline void setHaltAfterConsult( bool fl )
{
init_args.HaltAfterConsult = fl;
};
inline bool getHaltAfterConsult() { return init_args.HaltAfterConsult; }; inline bool getHaltAfterConsult( )
{
return init_args.HaltAfterConsult;
};
inline void setFastBoot(bool fl) { init_args.FastBoot = fl; }; inline void setFastBoot( bool fl )
{
init_args.FastBoot = fl;
};
inline bool getFastBoot() { return init_args.FastBoot; }; inline bool getFastBoot( )
{
return init_args.FastBoot;
};
inline void setArgc(int fl) { init_args.Argc = fl; }; inline void setArgc( int fl )
{
init_args.Argc = fl;
};
inline int getArgc() { return init_args.Argc; }; inline int getArgc( )
{
return init_args.Argc;
};
inline void setArgv(char **fl) { init_args.Argv = fl; }; inline void setArgv( char ** fl )
{
init_args.Argv = fl;
};
inline char **getArgv() { return init_args.Argv; }; inline char ** getArgv( )
{
return init_args.Argv;
};
YAPEngineArgs() { YAPEngineArgs() {
Yap_InitDefaults(&init_args, NULL, 0, NULL); Yap_InitDefaults(&init_args, NULL, 0, NULL);
@ -266,13 +346,15 @@ public:
}; };
}; };
/** /**
* @brief YAP Engine: takes care of the execution environment * @brief YAP Engine: takes care of the execution environment
where we can go executing goals. where we can go executing goals.
* *
* *
*/ */
class YAPEngine { class YAPEngine
{
private: private:
YAPEngineArgs *engine_args; YAPEngineArgs *engine_args;
YAPCallback *_callback; YAPCallback *_callback;
@ -281,29 +363,32 @@ private:
YAP_dogoalinfo q; YAP_dogoalinfo q;
PredEntry *rewriteUndefEngineQuery(PredEntry *ap, Term t, Term tmod); PredEntry *rewriteUndefEngineQuery(PredEntry *ap, Term t, Term tmod);
public: public :
/// construct a new engine; may use a variable number of arguments /// construct a new engine; may use a variable number of arguments
YAPEngine(YAPEngineArgs *cargs) { YAPEngine(YAPEngineArgs *cargs)
{
engine_args = cargs; engine_args = cargs;
// doInit(cargs->init_args.boot_file_type); //doInit(cargs->init_args.boot_file_type);
doInit(YAP_QLY); doInit(YAP_QLY);
}; /// construct a new engine, including aaccess to callbacks }; /// construct a new engine, including aaccess to callbacks
/// construct a new engine using argc/argv list of arguments /// construct a new engine using argc/argv list of arguments
YAPEngine(int argc, char *argv[], YAPEngine(int argc, char *argv[],
YAPCallback *callback = (YAPCallback *)NULL); YAPCallback *callback = (YAPCallback *)NULL);
/// kill engine /// kill engine
~YAPEngine() { delYAPCallback(); }; ~YAPEngine() { delYAPCallback(); };
/// remove current callback /// remove current callback
void delYAPCallback() { _callback = 0; }; void delYAPCallback() { _callback = 0; };
/// set a new callback /// set a new callback
void setYAPCallback(YAPCallback *cb) { void setYAPCallback(YAPCallback *cb)
{
delYAPCallback(); delYAPCallback();
_callback = cb; _callback = cb;
}; };
/// execute the callback. /// execute the callback.
////void run() { if (_callback) _callback.run(); } ////void run() { if (_callback) _callback.run(); }
/// execute the callback with a text argument. /// execute the callback with a text argument.
void run(char *s) { void run(char *s)
{
if (_callback) if (_callback)
_callback->run(s); _callback->run(s);
} }
@ -330,20 +415,25 @@ public:
bool mgoal(Term t, Term tmod); bool mgoal(Term t, Term tmod);
/// current directory for the engine /// current directory for the engine
bool goal(Term t) { return mgoal(t, CurrentModule); } bool goal(Term t)
{
return mgoal(t, CurrentModule);
}
/// reset Prolog state /// reset Prolog state
void reSet(); void reSet();
/// assune that there are no stack pointers, just release memory /// assune that there are no stack pointers, just release memory
// for last execution // for last execution
void release(); void release();
const char *currentDir() { const char *currentDir()
{
char dir[1024]; char dir[1024];
std::string s = Yap_getcwd(dir, 1024 - 1); std::string s = Yap_getcwd(dir, 1024 - 1);
return s.c_str(); return s.c_str();
}; };
/// report YAP version as a string /// report YAP version as a string
const char *version() { const char *version()
{
std::string s = Yap_version(); std::string s = Yap_version();
return s.c_str(); return s.c_str();
}; };
@ -355,15 +445,16 @@ public:
Term fun(YAPTerm t) { return fun(t.term()); }; Term fun(YAPTerm t) { return fun(t.term()); };
//> set a StringFlag, usually a path //> set a StringFlag, usually a path
//> //>
bool setStringFlag(std::string arg, std::string path) { bool setStringFlag(std::string arg, std::string path)
return setYapFlag(MkAtomTerm(Yap_LookupAtom(arg.data())), {
MkAtomTerm(Yap_LookupAtom(path.data()))); return setYapFlag(MkAtomTerm(Yap_LookupAtom(arg.data())), MkAtomTerm(Yap_LookupAtom(path.data())));
}; };
Term top_level(std::string s); Term top_level( std::string s);
Term next_answer(YAPQuery *&Q); Term next_answer(YAPQuery * &Q);
};
};
#endif /* YAPQ_HH */ #endif /* YAPQ_HH */
/// @} /// @}

File diff suppressed because it is too large Load Diff