better supprt for python/ANdroid

This commit is contained in:
Vitor Santos Costa 2016-06-17 17:04:41 +01:00
parent 79d40f57b1
commit 735ccca095
7 changed files with 176 additions and 53 deletions

View File

@ -16,7 +16,7 @@ set (CXX_SOURCES
if (YAP_SINGLE_FILE OR WIN32) if (YAP_SINGLE_FILE OR WIN32)
add_library (Yap++ OBJECT ${CXX_SOURCES} ) add_library (Yap++ OBJECT ${CXX_SOURCES} )
else(YAP_SINGLE_FILE OR WIN32) else(YAP_SINGLE_FILE OR WIN32)
add_library (Yap++ SHARED ${CXX_SOURCES} ) add_library (Yap++ SHARED ${CXX_SOURCES} )

View File

@ -47,12 +47,14 @@ enum PropTag {
* *
*/ */
class YAPAtom { class YAPAtom {
friend class YAPEngine;
friend class YAPModuleProp; friend class YAPModuleProp;
friend class YAPPredicate; friend class YAPPredicate;
friend class YAPFunctor; friend class YAPFunctor;
friend class YAPAtomTerm; friend class YAPAtomTerm;
friend class YAProp; friend class YAProp;
friend class YAPModule; friend class YAPModule;
friend class YAPQuery;
Atom a; Atom a;
/// construct new YAPAtom from Atom /// construct new YAPAtom from Atom
YAPAtom( Atom at ) { a = at; } YAPAtom( Atom at ) { a = at; }
@ -66,8 +68,10 @@ YAPAtom( const char * s, size_t len) { a = Yap_LookupAtomWithLength( s, len ); }
/// construct new YAPAtom from max-length wide string /// construct new YAPAtom from max-length wide string
YAPAtom( const wchar_t * s, size_t len) { a = Yap_LookupMaybeWideAtomWithLength( s, len ); } YAPAtom( const wchar_t * s, size_t len) { a = Yap_LookupMaybeWideAtomWithLength( s, len ); }
/// get name of atom /// get name of atom
char *getName(void); const char *getName(void);
/// get prop of type /// get name of (other way)
inline const char *text(void) { return getName(); } ;
/// get prop of type
Prop getProp( PropTag tag ) { return Yap_GetAProp( a , (PropFlags)tag ); } Prop getProp( PropTag tag ) { return Yap_GetAProp( a , (PropFlags)tag ); }
}; };

View File

@ -122,12 +122,12 @@ protected:
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
Term *outp; Term *outp;
out = Yap_StringToTerm(s, strlen(s)+1, &LOCAL_encoding, 1200, &names ) ; out = Yap_StringToTerm(s, strlen(s)+1, &LOCAL_encoding, 1200, &names ) ;
//extern char *s0; //extern char *s0;
//fprintf(stderr,"ap=%p arity=%d text=%s", ap, ap->ArityOfPE, s); //fprintf(stderr,"ap=%p arity=%d text=%s", ap, ap->ArityOfPE, s);
// Yap_DebugPlWrite(out); // Yap_DebugPlWrite(out);
// delete [] ns; // delete [] ns;
if (out == 0L) if (out == 0L)
throw YAPError(SYNTAX_ERROR); throw YAPError(SYNTAX_ERROR);
ap = getPred( out, outp); ap = getPred( out, outp);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();

View File

@ -5,6 +5,10 @@
extern "C" { extern "C" {
#if __ANDROID__
#include "android/log.h"
#endif
#include "YapInterface.h" #include "YapInterface.h"
#include "blobs.h" #include "blobs.h"
@ -340,19 +344,39 @@ YAPTerm::YAPTerm(intptr_t i) {
YAPTerm YAPListTerm::car() { YAPTerm YAPListTerm::car() {
Term to = gt(); Term to = gt();
{ LOG("to=%lx", to); }
if (IsPairTerm(to)) if (IsPairTerm(to))
return YAPTerm(HeadOfTerm(to)); return YAPTerm(HeadOfTerm(to));
else else
throw YAPError(TYPE_ERROR_LIST); throw YAPError(TYPE_ERROR_LIST);
} }
YAPListTerm::YAPListTerm(YAPTerm ts[], size_t n) {
CACHE_REGS
Term t;
BACKUP_H();
if (n == 0)
t = TermNil;
while (HR+n*2 > ASP-1024) {
RECOVER_H();
if (!Yap_dogc(0, NULL PASS_REGS)) {
t = TermNil;
}
BACKUP_H();
}
t = AbsPair(HR);
for (int i = 0; i < n; i ++ ) {
HR[2*i] = ts[i].gt();
HR[2*i+1] = AbsPair(HR+(2*i+2));
}
}
YAPVarTerm::YAPVarTerm() { YAPVarTerm::YAPVarTerm() {
CACHE_REGS CACHE_REGS
mk(MkVarTerm()); mk(MkVarTerm());
} }
char *YAPAtom::getName(void) { const char *YAPAtom::getName(void) {
if (IsWideAtom(a)) { if (IsWideAtom(a)) {
// return an UTF-8 version // return an UTF-8 version
size_t sz = 512; size_t sz = 512;
@ -379,14 +403,16 @@ char *YAPAtom::getName(void) {
void YAPQuery::initOpenQ() { void YAPQuery::initOpenQ() {
CACHE_REGS CACHE_REGS
oq = LOCAL_execution; //oq = LOCAL_execution;
LOCAL_execution = this; // LOCAL_execution = this;
q_open = 1; 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
q_handles = Yap_StartSlots();
} }
int YAPError::get() { return errNo; } int YAPError::get() { return errNo; }
@ -403,6 +429,18 @@ void YAPQuery::initQuery(Term t) {
} else { } else {
q_g = 0; q_g = 0;
} }
q_pe = ap;
initOpenQ();
RECOVER_MACHINE_REGS();
}
void YAPQuery::initQuery(YAPAtom at) {
CACHE_REGS
BACKUP_MACHINE_REGS();
PredEntry *ap = RepPredProp(PredPropByAtom(at.a, Yap_CurrentModule()));
goal = YAPAtomTerm(at);
q_g = 0;
q_pe = ap;
initOpenQ(); initOpenQ();
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
} }
@ -444,8 +482,8 @@ YAPQuery::YAPQuery(YAPPredicate p, YAPTerm ts[]) : YAPPredicate(p.ap) {
YAPListTerm YAPQuery::namedVars() { YAPListTerm YAPQuery::namedVars() {
CACHE_REGS CACHE_REGS
Term o = vnames.term(); __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "vnames %s %d", vnames.text(), LOCAL_CurSlot);
return o; // should be o return vnames; // should be o
} }
bool YAPQuery::next() { bool YAPQuery::next() {
@ -453,24 +491,35 @@ bool YAPQuery::next() {
int result; int result;
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
if (q_open != 1) if (!q_open )
return false; return false;
if (setjmp(q_env)) if (setjmp(q_env))
return false; return false;
// don't forget, on success these guys must create slots // don't forget, on success these guys must create slots
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "exec ");
if (this->q_state == 0) { if (this->q_state == 0) {
result = (bool)YAP_EnterGoal((YAP_PredEntryPtr)ap, q_g, &q_h); result = (bool)YAP_EnterGoal((YAP_PredEntryPtr)ap, q_g, &q_h);
} else { } else {
LOCAL_AllowRestart = q_open; LOCAL_AllowRestart = q_open;
result = (bool)YAP_RetryGoal(&q_h); result = (bool)YAP_RetryGoal(&q_h);
} }
if (result)
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "vnames %d %s %d", q_state, vnames.text(), LOCAL_CurSlot);
else
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "fail");
q_state = 1; q_state = 1;
if (Yap_GetException()) { if (Yap_GetException()) {
throw(YAPError(SYSTEM_ERROR_INTERNAL)); throw(YAPError(SYSTEM_ERROR_INTERNAL));
} }
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "out %d", result);
if (!result) { if (!result) {
YAP_LeaveGoal(FALSE, &q_h); YAP_LeaveGoal(FALSE, &q_h);
q_open = 0; q_open = 0;
} else {
q_handles = Yap_StartSlots();
} }
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return result; return result;
@ -480,11 +529,11 @@ void YAPQuery::cut() {
CACHE_REGS CACHE_REGS
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
if (q_open != 1 || q_state == 0) if (!q_open || q_state == 0)
return; return;
YAP_LeaveGoal(FALSE, &q_h); YAP_LeaveGoal(FALSE, &q_h);
q_open = 0; q_open = 0;
LOCAL_execution = this; // LOCAL_execution = this;
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
} }
@ -492,7 +541,7 @@ bool YAPQuery::deterministic() {
CACHE_REGS CACHE_REGS
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
if (q_open != 1 || q_state == 0) if (!q_open || q_state == 0)
return false; return false;
choiceptr myB = (choiceptr)(LCL0 - q_h.b); choiceptr myB = (choiceptr)(LCL0 - q_h.b);
return (B >= myB); return (B >= myB);
@ -511,7 +560,7 @@ void YAPQuery::close() {
} }
YAP_LeaveGoal(FALSE, &q_h); YAP_LeaveGoal(FALSE, &q_h);
q_open = 0; q_open = 0;
LOCAL_execution = this; //LOCAL_execution = this;
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
} }
@ -563,33 +612,56 @@ void Yap_displayWithJava(int c) {
} }
} }
extern "C" void Java_pt_up_fc_dcc_yap_JavaYap_load(JNIEnv *env0, jobject obj,
jobject mgr);
extern "C" void Java_pt_up_fc_dcc_yap_JavaYap_load(JNIEnv *env0, jobject obj,
jobject asset_manager) {
AAssetManager *mgr = AAssetManager_fromJava(Yap_jenv, asset_manager);
if (mgr == NULL) {
LOG("we're doomed, mgr = 0; bip bip bip");
} else {
Yap_assetManager = mgr;
}
}
#endif #endif
YAPEngine::YAPEngine(char *savedState, size_t stackSize, size_t trailSize, void YAPEngine::doInit(YAP_file_type_t BootMode)
size_t maxStackSize, size_t maxTrailSize, char *libDir, {
char *bootFile, char *goal, char *topLevel, bool script, if ((BootMode = YAP_Init(&init_args)) == YAP_FOUND_BOOT_ERROR) {
bool fastBoot, throw(YAPError(SYSTEM_ERROR_INTERNAL));
YAPCallback *cb) }
: _callback(0) { // a single engine can be active /* Begin preprocessor code */
/* live */
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "$init_system");
#if __ANDROID__ #if __ANDROID__
Yap_AndroidBufp = (char *)malloc(Yap_AndroidMax = 4096); Yap_AndroidBufp = (char *)malloc(Yap_AndroidMax = 4096);
Yap_AndroidBufp[0] = '\0'; Yap_AndroidBufp[0] = '\0';
Yap_AndroidSz = 0; Yap_AndroidSz = 0;
#endif #endif
memset((void *)&init_args, 0, sizeof(init_args)); yerror = YAPError();
YAPQuery initq = YAPQuery( YAPAtom( "$init_system" ) );
if (initq.next()) {
initq.cut();
} else {
// should throw exception
}
}
YAPEngine::YAPEngine(char *savedState, char *bootFile,
size_t stackSize, size_t trailSize,
size_t maxStackSize, size_t maxTrailSize, char *libDir,
char *goal, char *topLevel, bool script,
bool fastBoot,
YAPCallback *cb)
: _callback(0) { // a single engine can be active
YAP_file_type_t BootMode;
int Argc = 1;
char **Argv;
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "YAP %s ", bootFile);
//delYAPCallback()b
//if (cb)
// setYAPCallback(cb);
curren = this;
{
size_t l1 = 2 * sizeof(char *);
if (!(Argv = (char **)malloc(l1)))
return;
Argv[0] = "yap";
Argv[1] = NULL;
}
BootMode = Yap_InitDefaults( &init_args, NULL, Argc, Argv);
init_args.SavedState = savedState; init_args.SavedState = savedState;
init_args.StackSize = stackSize; init_args.StackSize = stackSize;
init_args.TrailSize = trailSize; init_args.TrailSize = trailSize;
@ -601,13 +673,22 @@ YAPEngine::YAPEngine(char *savedState, size_t stackSize, size_t trailSize,
init_args.YapPrologTopLevelGoal = topLevel; init_args.YapPrologTopLevelGoal = topLevel;
init_args.HaltAfterConsult = script; init_args.HaltAfterConsult = script;
init_args.FastBoot = fastBoot; init_args.FastBoot = fastBoot;
yerror = YAPError(); doInit(BootMode);
delYAPCallback(); }
if (cb)
setYAPCallback(cb);
YAPEngine::YAPEngine(int argc, char *argv[],
YAPCallback *cb)
: _callback(0){ // a single engine can be active
YAP_file_type_t BootMode;
BootMode = YAP_parse_yap_arguments(argc, argv, &init_args);
//delYAPCallback()b
//if (cb)
// setYAPCallback(cb);
curren = this; curren = this;
if (YAP_Init(&init_args) == YAP_BOOT_ERROR) doInit( BootMode );
throw(YAPError(SYSTEM_ERROR_INTERNAL));
} }
YAPPredicate::YAPPredicate(YAPAtom at) { YAPPredicate::YAPPredicate(YAPAtom at) {
@ -642,6 +723,7 @@ PredEntry *YAPPredicate::getPred(Term &t, Term *&outp) {
ts[0] = t; ts[0] = t;
ts[1] = m; ts[1] = m;
t = Yap_MkApplTerm(FunctorCsult, 2, ts); t = Yap_MkApplTerm(FunctorCsult, 2, ts);
outp = RepAppl(t)+1;
} }
Functor f = FunctorOfTerm(t); Functor f = FunctorOfTerm(t);
if (IsExtensionFunctor(f)) { if (IsExtensionFunctor(f)) {

View File

@ -1,10 +1,11 @@
#define YAP_CPP_INTERFACE 1 #define YAP_CPP_INTERFACE 1
#include <gmpxx.h> #include <gmpxx.h>
//! @{
//! @{
/** /**
* *
* @defgroup yap-cplus-interface An object oriented interface for YAP. * @defgroup yap-cplus-interface An object oriented interface for YAP.
@ -65,6 +66,11 @@ extern "C" {
// taken from yap_structs.h // taken from yap_structs.h
#include "iopreds.h" #include "iopreds.h"
#ifdef SWIGPYTHON
extern PyObject *term_to_python(yhandle_t t);
#endif
X_API void YAP_UserCPredicate(const char *, YAP_UserCPred, YAP_Arity arity); X_API void YAP_UserCPredicate(const char *, YAP_UserCPred, YAP_Arity arity);
/* void UserCPredicateWithArgs(const char *name, int *fn(), unsigned int arity) */ /* void UserCPredicateWithArgs(const char *name, int *fn(), unsigned int arity) */
@ -76,6 +82,7 @@ X_API void YAP_UserCPredicateWithArgs(const char *, YAP_UserCPred, YAP_Arity, YA
X_API Term Yap_StringToTerm(const char *s, size_t len, encoding_t *encp, int prio, Term *bindings_p); X_API Term Yap_StringToTerm(const char *s, size_t len, encoding_t *encp, int prio, Term *bindings_p);
} }
class YAPEngine; class YAPEngine;

View File

@ -11,7 +11,16 @@
* 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 YAPQuery: public YAPPredicate, public open_query_struct { class YAPQuery: public YAPPredicate {
bool q_open;
int q_state;
YAP_handle_t q_g, q_handles;
struct pred_entry *q_pe;
struct yami *q_p, *q_cp;
jmp_buf q_env;
int q_flags;
YAP_dogoalinfo q_h;
YAPQuery *oq;
YAPListTerm vnames; YAPListTerm vnames;
YAPTerm goal; YAPTerm goal;
Term names; Term names;
@ -19,6 +28,7 @@ class YAPQuery: public YAPPredicate, public open_query_struct {
void initOpenQ(); void initOpenQ();
void initQuery( Term t ); void initQuery( Term t );
void initQuery( YAPAtom at );
void initQuery( YAPTerm ts[], arity_t arity ); void initQuery( YAPTerm ts[], arity_t arity );
public: public:
/// main constructor, uses a predicate and an array of terms /// main constructor, uses a predicate and an array of terms
@ -42,10 +52,21 @@ public:
/// goal. /// goal.
inline YAPQuery(const char *s): YAPPredicate(s, t, names) inline YAPQuery(const char *s): YAPPredicate(s, t, names)
{ {
vnames = YAPListTerm( names ); __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "got game %d", LOCAL_CurSlot);
vnames = YAPListTerm( names );
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "%s", vnames.text());
initQuery( t ); initQuery( t );
}; };
/// string constructor with just an atom
///
/// It is given an atom, and a Prolog term that should be a callable
/// goal, say `main`, `init`, `live`.
inline YAPQuery(YAPAtom goal): YAPPredicate( goal )
{
vnames = YAPListTerm( TermNil );
initQuery( goal );
};
/// 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; }
@ -102,19 +123,25 @@ private:
YAPCallback *_callback; YAPCallback *_callback;
YAP_init_args init_args; YAP_init_args init_args;
YAPError yerror; YAPError yerror;
void doInit(YAP_file_type_t BootMode);
public: public:
/// construct a new engine; may use a variable number of arguments
YAPEngine(char *savedState = (char *)NULL, YAPEngine(char *savedState = (char *)NULL,
char *bootFile = (char *)NULL,
size_t stackSize = 0, size_t stackSize = 0,
size_t trailSize = 0, size_t trailSize = 0,
size_t maxStackSize = 0, size_t maxStackSize = 0,
size_t maxTrailSize = 0, size_t maxTrailSize = 0,
char *libDir = (char *)NULL, char *libDir = (char *)NULL,
char *bootFile = (char *)NULL,
char *goal = (char *)NULL, char *goal = (char *)NULL,
char *topLevel = (char *)NULL, char *topLevel = (char *)NULL,
bool script = FALSE, bool script = FALSE,
bool fastBoot = FALSE, bool fastBoot = FALSE,
YAPCallback *callback=(YAPCallback *)NULL); /// construct a new engine, including aaccess to callbacks YAPCallback *callback=(YAPCallback *)NULL); /// construct a new engine, including aaccess to callbacks
/// construct a new engine using argc/argv list of arguments
YAPEngine(int argc,
char *argv[],
YAPCallback *callback=(YAPCallback *)NULL);
/// kill engine /// kill engine
~YAPEngine() { delYAPCallback(); } ~YAPEngine() { delYAPCallback(); }
/// remove current callback /// remove current callback
@ -133,7 +160,7 @@ public:
}; };
/// current module for the engine /// current module for the engine
YAPModule currentModule( ) { return YAPModule( ) ; } YAPModule currentModule( ) { return YAPModule( ) ; }
/// current directory for the engine /// current directory for the engine
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);

View File

@ -14,6 +14,7 @@ class YAPTerm {
friend class YAPModule; friend class YAPModule;
friend class YAPModuleProp; friend class YAPModuleProp;
friend class YAPApplTerm; friend class YAPApplTerm;
friend class YAPListTerm;
protected: protected:
yhandle_t t; /// handle to term, equivalent to term_t yhandle_t t; /// handle to term, equivalent to term_t
void mk(Term t0); /// internal method to convert from term to handle void mk(Term t0); /// internal method to convert from term to handle
@ -74,6 +75,9 @@ public:
/// return a string with a textual representation of the term /// return a string with a textual representation of the term
virtual const char *text(); virtual const char *text();
/// return a handle to the term
inline yhandle_t handle() { return t; };
}; };
/** /**
@ -205,12 +209,11 @@ public:
/// ///
/// @param[in] the term /// @param[in] the term
YAPListTerm(Term t0) { mk(t0); /* else type_error */ } YAPListTerm(Term t0) { mk(t0); /* else type_error */ }
/* /// Create a list term out of an array of terms. /// Create a list term out of an array of terms.
/// ///
/// @param[in] the array of terms /// @param[in] the array of terms
/// @param[in] the length of the array /// @param[in] the length of the array
YAPListTerm(YAPTerm ts[], size_t n); YAPListTerm(YAPTerm ts[], size_t n);
*/
// YAPListTerm( vector<YAPTerm> v ); // YAPListTerm( vector<YAPTerm> v );
/// Return the number of elements in a list term. /// Return the number of elements in a list term.
size_t length() { Term *tailp; Term t1 = gt(); return Yap_SkipList(&t1, &tailp); } size_t length() { Term *tailp; Term t1 = gt(); return Yap_SkipList(&t1, &tailp); }