support for init, callbacks

This commit is contained in:
Vítor Santos Costa 2014-06-11 19:27:54 +01:00
parent 2455a1d83c
commit 0241151599
2 changed files with 815 additions and 657 deletions

View File

@ -2,7 +2,7 @@
#define YAP_CPP_INTERFACE 1 #define YAP_CPP_INTERFACE 1
#include "yapi.hh" #include "yapi.hh"
#include "SWI-Stream.h"
YAPAtomTerm::YAPAtomTerm(char *s) { // build string YAPAtomTerm::YAPAtomTerm(char *s) { // build string
@ -158,6 +158,10 @@ YAPPairTerm::YAPPairTerm() : YAPTerm() {
t = Yap_MkNewPairTerm( ); t = Yap_MkNewPairTerm( );
} }
void YAPTerm::mk(Term t0) { CACHE_REGS t = Yap_InitSlot( t0 PASS_REGS); }
Term YAPTerm::gt() { CACHE_REGS return Yap_GetFromSlot( t PASS_REGS); }
YAP_tag_t YAPTerm::tag() { YAP_tag_t YAPTerm::tag() {
Term tt = gt( ); Term tt = gt( );
if (IsVarTerm(tt)) { if (IsVarTerm(tt)) {
@ -264,28 +268,33 @@ intptr_t YAPTerm::hash(size_t sz, size_t depth, bool variant) {
} }
char *YAPTerm::text(void) { char *YAPTerm::text(void) {
size_t sze = 4096, sz, length; size_t sze = 4096, length;
char *buf = new char[sze], *b; char *os;
int enc; int enc;
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "I LCL0+%p/t=(%d) %x", LCL0, t, LCL0[-15]) ; }
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
if ((b = Yap_TermToString(gt(), buf, sze, &length, &enc, 0)) != buf) { if (!(os = Yap_HandleToString(t, sze, &length, &enc, 0))) {
if (b) free(b); { CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "IIa LCL0+t=(%p) %x", LCL0, LCL0[-15]) ; }
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return NULL; return NULL;
} }
sz = length+1; { CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "II LCL0+t=(%p) %x", LCL0, LCL0[-15]) ; }
char *os = new char[sz];
if (!os) {
RECOVER_MACHINE_REGS();
return NULL;
}
memcpy(os, buf, sz);
delete buf;
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return os; return os;
} }
bool YAPListTerm::nil() {
CACHE_REGS
__android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "t=%d LCL0+t=(%p)", t, LCL0+t) ;
return gt() == TermNil;
}
YAPIntegerTerm::YAPIntegerTerm(intptr_t i) { CACHE_REGS Term tn = MkIntegerTerm( i ); mk( tn ); }
/* /*
YAPTerm *YAPTerm::vars() YAPTerm *YAPTerm::vars()
{ {
@ -295,7 +304,23 @@ YAPTerm *YAPTerm::vars()
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return lv; return lv;
} }
*/ */
YAPTerm::YAPTerm(void *ptr) { CACHE_REGS mk( MkIntegerTerm( (Int)ptr ) );}
YAPTerm::YAPTerm(intptr_t i) { CACHE_REGS Term tn = MkIntegerTerm( i ); mk( tn ); }
YAPTerm YAPListTerm::car()
{
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "t=%d LCL0+t=(%p)", t, LCL0+t) ; }
Term to = gt();
if (IsPairTerm(to))
return YAPTerm(HeadOfTerm(to));
else
return MkIntTerm(-1);
}
YAPVarTerm::YAPVarTerm() { CACHE_REGS mk( MkVarTerm( ) ); }
char *YAPAtom::name(void) { char *YAPAtom::name(void) {
@ -352,22 +377,64 @@ char *YAPAtom::name(void) {
YAPPredicate::YAPPredicate(const char *s, Term **outp, term_t &vnames) { YAPPredicate::YAPPredicate(const char *s, Term **outp, term_t &vnames) {
CACHE_REGS CACHE_REGS
vnames = Yap_NewSlots(1 PASS_REGS); vnames = Yap_NewSlots(1 PASS_REGS);
Term t = Yap_StringToTerm(s, strlen(s)+1, vnames), m; Term t = Yap_StringToTerm(s, strlen(s)+1, vnames);
t = Yap_StripModule(t, &m); ap = getPred( t, outp );
if (IsVarTerm(t) || IsNumTerm(t)) { CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "OUT vnames=%d ap=%p LCL0=%p", vnames, ap, LCL0) ; }
ap = NULL; }
if (IsAtomTerm(t)) {
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m)); YAPPredicate::YAPPredicate(YAPAtom at) {
*outp = NULL; CACHE_REGS
} else if (IsApplTerm(t)) { ap = RepPredProp(PredPropByAtom(at.a,CurrentModule));
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m)); }
*outp = RepAppl(t)+1;
} else if (IsPairTerm(t)) { YAPPredicate::YAPPredicate(YAPAtom at, arity_t arity) {
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m)); CACHE_REGS
*outp = RepPair(t); if (arity) {
Functor f = Yap_MkFunctor(at.a, arity);
ap = RepPredProp(PredPropByFunc(f,CurrentModule));
} else {
ap = RepPredProp(PredPropByAtom(at.a,CurrentModule));
} }
} }
/// auxiliary routine to find a predicate in the current module.
PredEntry *YAPPredicate::getPred( Term t, Term **outp ) {
CACHE_REGS
Term m = CurrentModule ;
t = Yap_StripModule(t, &m);
if (IsVarTerm(t) || IsNumTerm(t)) {
ap = NULL;
if (outp) *outp = NULL;
}
if (IsAtomTerm(t)) {
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
if (outp) *outp = NULL;
} else if (IsPairTerm(t)) {
ap = RepPredProp(PredPropByFunc(FunctorCsult, PROLOG_MODULE));
if (outp) *outp = HR;
HR[0] = RepPair(t)[0];
HR[1] = m;
HR+=2;
} else {
Functor f = FunctorOfTerm(t);
if (IsExtensionFunctor(f)) {
ap = NULL;
if (outp) *outp = NULL;
}
ap = RepPredProp(PredPropByFunc(f, m));
if (outp) *outp = RepAppl(t)+1;
}
return ap;
}
YAPPredicate::YAPPredicate(YAPFunctor f) {
CACHE_REGS
ap = RepPredProp(PredPropByFunc(f.f,CurrentModule));
}
void void
YAPQuery::initQuery( Term *ts ) YAPQuery::initQuery( Term *ts )
{ {
@ -385,7 +452,7 @@ void
YAPQuery::initQuery( YAPTerm t[], arity_t arity ) YAPQuery::initQuery( YAPTerm t[], arity_t arity )
{ {
Term *ts = new Term[arity]; Term *ts = new Term[arity];
for (int i = 0; i < arity; i++) for (arity_t i = 0; i < arity; i++)
ts[i] = t[i].term(); ts[i] = t[i].term();
return initQuery( ts ); return initQuery( ts );
@ -409,25 +476,37 @@ YAPQuery::YAPQuery(YAPPredicate p, YAPTerm t[]): YAPPredicate(p.ap)
initQuery( t , p.ap->ArityOfPE); initQuery( t , p.ap->ArityOfPE);
} }
int YAPQuery::next() YAPListTerm YAPQuery::namedVars() {
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "vnames=%d LCL0=%p %x", vnames, LCL0, LCL0[vnames]) ; }
CACHE_REGS
Term o = Yap_GetFromSlot( vnames PASS_REGS );
return YAPListTerm( o );
}
bool YAPQuery::next()
{ {
CACHE_REGS CACHE_REGS
int result; int result;
if (this->q_open != 1) return 0; { CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, " CALL vnames=%d ap=%p LCL0=%p %x", vnames, ap, LCL0, LCL0[vnames]) ; }
if (this->q_open != 1) return false;
if (setjmp(((YAPQuery *)LOCAL_execution)->q_env)) if (setjmp(((YAPQuery *)LOCAL_execution)->q_env))
return 0; return false;
// don't forget, on success these guys must create slots // don't forget, on success these guys must create slots
if (this->q_state == 0) { if (this->q_state == 0) {
result = YAP_EnterGoal((YAP_PredEntryPtr)this->ap, this->q_g, &this->q_h); extern void toggle_low_level_trace(void);
//toggle_low_level_trace();
result = (bool)YAP_EnterGoal((YAP_PredEntryPtr)this->ap, this->q_g, &this->q_h);
} else { } else {
LOCAL_AllowRestart = this->q_open; LOCAL_AllowRestart = this->q_open;
result = YAP_RetryGoal(&this->q_h); result = (bool)YAP_RetryGoal(&this->q_h);
} }
this->q_state = 1; this->q_state = 1;
if (result == 0) { { CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "FAIL vnames=%d %d LCL0=(%p) %x", vnames, result, LCL0, LCL0[vnames]) ; }
if (!result) {
YAP_LeaveGoal(FALSE, &this->q_h); YAP_LeaveGoal(FALSE, &this->q_h);
this->q_open = 0; this->q_open = 0;
} }
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "SUCCESS vnames=%d ap=%p LCL0=(%p) %x", vnames, ap, LCL0, LCL0[vnames]) ; }
return result; return result;
} }
@ -466,14 +545,86 @@ int YAPPredicate::call(YAPTerm t[])
return ret; return ret;
} }
YAPEngine::YAPEngine(YAPParams const& params) static YAPEngine *curren;
{ YAP_Init( (YAP_init_args *)&params.init_args ); }
#if __ANDROID__
YAPEngine::YAPEngine() #include <jni.h>
#include <string.h>
extern AAssetManager *assetManager;
extern char *Yap_AndroidBufp;
static size_t Yap_AndroidMax, Yap_AndroidSz;
extern void(*Yap_DisplayWithJava)(int c);
static void
displayWithJava(int c)
{ {
YAPParams *params = new YAPParams(); char *ptr = Yap_AndroidBufp;
YAP_Init( &params->init_args ); 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';
__android_log_print(ANDROID_LOG_INFO, __FUNCTION__, "after char %c:%s %p",c, Yap_AndroidBufp, curren);
curren->display(Yap_AndroidBufp);
Yap_AndroidSz = 0;
}
} }
#endif
YAPEngine::YAPEngine( char *savedState,
size_t stackSize,
size_t trailSize,
size_t maxStackSize,
size_t maxTrailSize,
char *libDir,
char *bootFile,
char *goal,
char *topLevel,
bool script,
bool fastBoot,
YAPCallback *cb): _callback(0)
{ // a single engine can be active
#if __ANDROID__
if (assetManager == NULL)
return;
Yap_DisplayWithJava = displayWithJava;
Yap_AndroidBufp = (char *)malloc(Yap_AndroidMax = 4096);
Yap_AndroidBufp[0] = '\0';
Yap_AndroidSz = 0;
#endif
memset((void *)&init_args, 0, sizeof(init_args));
init_args.SavedState = savedState;
init_args.StackSize = stackSize;
init_args.TrailSize = trailSize;
init_args.MaxStackSize = maxStackSize;
init_args.MaxTrailSize = maxTrailSize;
init_args.YapLibDir = libDir;
init_args.YapPrologBootFile = bootFile;
init_args.YapPrologGoal = goal;
init_args.YapPrologTopLevelGoal = topLevel;
init_args.HaltAfterConsult = script;
init_args.FastBoot = fastBoot;
delYAPCallback();
if (cb) setYAPCallback(cb);
curren = this;
YAP_Init( &init_args );
}
YAPQuery *YAPEngine::query( char *s ) {
YAPQuery *n = new YAPQuery( s );
return n;
}

View File

@ -13,16 +13,26 @@
* with the swig interface language generator. It uses ideas from the old YAP * with the swig interface language generator. It uses ideas from the old YAP
* interface and from the SWI foreign language interface. * interface and from the SWI foreign language interface.
* *
* @{
*
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <config.h> #include <config.h>
extern "C" {
#ifdef __cplusplus
#define old_cplusplus __cplusplus
#undef __cplusplus
#endif
#if USE_GMP #if USE_GMP
#include <gmp.h> #include <gmp.h>
#endif #endif
#ifdef old_cplusplus
extern "C" { #define __cplusplus old_cplusplus
#undef old_cplusplus
#endif
#include "Yap.h" #include "Yap.h"
@ -60,41 +70,44 @@ extern "C" {
#include <windows.h> #include <windows.h>
#endif #endif
// taken from yap_structs.h // taken from yap_structs.h
#include "iopreds.h" #include "iopreds.h"
extern Term Yap_StringToTerm(const char *s, size_t len, term_t bindings); extern Term Yap_StringToTerm(const char *s, size_t len, term_t bindings);
// we cannot consult YapInterface.h, that conflicts with what we declare, though // we cannot consult YapInterface.h, that conflicts with what we declare, though
// it shouldn't // it shouldn't
} }
//#include <vector> //#include <vector>
class YAPEngine; class YAPEngine;
class YAPTermHandle;
class YAPAtom; class YAPAtom;
class YAPFunctor; class YAPFunctor;
class YAPApplTerm; class YAPApplTerm;
class YAPPairTerm; class YAPPairTerm;
class YAPQuery; class YAPQuery;
class TypeError {};
/** /**
* @brief Generic Prolog Term * @brief Generic Prolog Term
*/ */
class YAPTerm { class YAPTerm {
friend class YAPPredicate; friend class YAPPredicate;
friend class YAPTermHandle;
friend class YAPApplTerm; friend class YAPApplTerm;
friend class YAPPairTerm; friend class YAPPairTerm;
friend class YAPListTerm;
protected: protected:
term_t t; handle_t t;
inline void mk(Term t0) { CACHE_REGS t = Yap_InitSlot( t0 PASS_REGS); } void mk(Term t0);
inline Term gt(void) { CACHE_REGS return Yap_GetFromSlot( t PASS_REGS); } Term gt();
YAPTerm(Term tn) { mk( tn ); }
public: public:
YAPTerm() { mk(TermNil); } // do nothing constructor YAPTerm() { mk(TermNil); } // do nothing constructor
YAPTerm(void *ptr) { CACHE_REGS mk( MkIntegerTerm( (Int)ptr ) );} YAPTerm(intptr_t i);
YAPTerm(Term tn) { mk( tn ); } YAPTerm(void *ptr);
YAPTerm(char *s) { Term tp ; mk( YAP_ReadBuffer(s,&tp) ); } YAPTerm(char *s) { Term tp ; mk( YAP_ReadBuffer(s,&tp) ); }
/*~YAPTerm(void) { /*~YAPTerm(void) {
CACHE_REGS CACHE_REGS
@ -138,10 +151,10 @@ public:
/** /**
* @brief Variable Term * @brief Variable Term
*/ */
class YAPVarTerm: private YAPTerm { class YAPVarTerm: public YAPTerm {
YAPVarTerm(Term t) { if (IsVarTerm(t)) mk( t ); } YAPVarTerm(Term t) { if (IsVarTerm(t)) mk( t ); }
public: public:
YAPVarTerm() { CACHE_REGS mk( MkVarTerm( ) ); } YAPVarTerm();
CELL *getVar() { return VarOfTerm( gt() ); } CELL *getVar() { return VarOfTerm( gt() ); }
bool unbound() { return IsUnboundVar(VarOfTerm( gt() )); } bool unbound() { return IsUnboundVar(VarOfTerm( gt() )); }
}; };
@ -149,7 +162,7 @@ public:
/** /**
* @brief Compound Term * @brief Compound Term
*/ */
class YAPApplTerm: private YAPTerm { class YAPApplTerm: public YAPTerm {
friend class YAPTerm; friend class YAPTerm;
YAPApplTerm(Term t0) { mk(t0); } YAPApplTerm(Term t0) { mk(t0); }
public: public:
@ -163,7 +176,7 @@ public:
/** /**
* @brief List Constructor Term * @brief List Constructor Term
*/ */
class YAPPairTerm: private YAPTerm { class YAPPairTerm: public YAPTerm {
friend class YAPTerm; friend class YAPTerm;
YAPPairTerm(Term t0) { if (IsPairTerm(t0)) mk( t0 ); else mk(0); } YAPPairTerm(Term t0) { if (IsPairTerm(t0)) mk( t0 ); else mk(0); }
public: public:
@ -177,40 +190,48 @@ public:
* @brief Integer Term * @brief Integer Term
*/ */
class YAPIntegerTerm: private YAPTerm { class YAPIntegerTerm: public YAPTerm {
public: public:
YAPIntegerTerm(intptr_t i) { CACHE_REGS Term tn = MkIntegerTerm( i ); mk( tn ); } YAPIntegerTerm(intptr_t i);
intptr_t getInteger() { return IntegerOfTerm( gt() ); } intptr_t getInteger() { return IntegerOfTerm( gt() ); }
bool isTagged() { return IsIntTerm( gt() ); } bool isTagged() { return IsIntTerm( gt() ); }
}; };
class YAPListTerm: private YAPTerm { class YAPListTerm: public YAPTerm {
public: public:
/// Create a list term out of a standard term. Check if a valid operation. /// Create a list term out of a standard term. Check if a valid operation.
/// ///
/// @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);
/// Create a list term out of an array of Prolog terms.
///
/// @param[in] the array of terms
/// @param[in] the length of the array
YAPListTerm(Term 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); }
// vector<YAPTerm> toVector(); /// Extract the first element of a list.
/// Create an array of term out of a list.
/// ///
/// @param[in] the list /// @param[in] the list
YAPTerm car() { if (IsPairTerm(gt())) return YAPTerm(HeadOfTerm(gt())); else return YAPTerm((term_t)0); } YAPTerm car();
YAPListTerm cdr() { if (IsPairTerm(gt())) return YAPListTerm(TailOfTerm(gt())); else return YAPListTerm((term_t)0); } /// Extract the tail elements of a list.
bool nil() { return gt() == TermNil; } ///
/// @param[in] the list
YAPListTerm cdr()
{
Term to = gt();
if (IsPairTerm( to ))
return YAPListTerm(TailOfTerm( to ));
else
return MkIntTerm(-1);
}
/// Check if the list is empty.
///
/// @param[in] the list
bool nil();
}; };
/** /**
@ -233,7 +254,7 @@ public:
/** /**
* @brief String Term * @brief String Term
*/ */
class YAPStringTerm: private YAPTerm { class YAPStringTerm: public YAPTerm {
public: public:
YAPStringTerm(char *s) ; YAPStringTerm(char *s) ;
YAPStringTerm(char *s, size_t len); YAPStringTerm(char *s, size_t len);
@ -245,7 +266,7 @@ public:
/** /**
* @brief Atom Term * @brief Atom Term
*/ */
class YAPAtomTerm: private YAPTerm { class YAPAtomTerm: public YAPTerm {
public: public:
YAPAtomTerm(YAPAtom a): YAPTerm() { mk( MkAtomTerm(a.a) ); } YAPAtomTerm(YAPAtom a): YAPTerm() { mk( MkAtomTerm(a.a) ); }
YAPAtomTerm(Atom a): YAPTerm() { mk( MkAtomTerm(a) ); } YAPAtomTerm(Atom a): YAPTerm() { mk( MkAtomTerm(a) ); }
@ -257,86 +278,105 @@ public:
}; };
/** /**
* @brief Functor * @brief YAPFunctor represents Prolog functors Name/Arity
*/ */
class YAPFunctor { class YAPFunctor {
friend class YAPApplTerm; friend class YAPApplTerm;
friend class YAPPredicate; friend class YAPPredicate;
Functor f; Functor f;
public: /// Constructor: receives Prolog functor and casts it to YAPFunctor
YAPFunctor( char * s, arity_t arity) { f = Yap_MkFunctor( Yap_LookupAtom( s ), arity ); } ///
YAPFunctor( wchar_t * s, arity_t arity) { f = Yap_MkFunctor( Yap_LookupWideAtom( s ), arity ); } /// Notice that this is designed for internal use only.
YAPFunctor( YAPAtom at, arity_t arity) { f = Yap_MkFunctor( at.a, arity ); }
YAPFunctor( Functor ff) { f = ff; } YAPFunctor( Functor ff) { f = ff; }
public:
/// Constructor: receives name as a string plus arity
///
/// Notice that this is designed for ISO-LATIN-1 right now
YAPFunctor( char * s, arity_t arity) { f = Yap_MkFunctor( Yap_LookupAtom( s ), arity ); }
/// Constructor: receives name as a wide string plus arity
///
/// Notice that this is designed for UNICODE right now
YAPFunctor( wchar_t * s, arity_t arity) { f = Yap_MkFunctor( Yap_LookupWideAtom( s ), arity ); }
/// Constructor: receives name as an atom, plus arity
///
/// This is the default method, and the most popi;at
YAPFunctor( YAPAtom at, arity_t arity) { f = Yap_MkFunctor( at.a, arity ); }
Atom name(void) { /// Getter: extract name of functor as an atom
return NameOfFunctor( f ); ///
/// this is for external usage.
YAPAtom name(void) {
return YAPAtom( NameOfFunctor( f ) );
} }
/// Getter: extract arity of functor as an unsigned integer
///
/// this is for external usage.
arity_t arity(void) { arity_t arity(void) {
return ArityOfFunctor( f ); return ArityOfFunctor( f );
} }
}; };
/** /**
* @brief Term Handle * @brief Predicates
*/ *
class YAPTermHandle { * This class interfaces with PredEntry in Yatom.g
long int handle;
public:
YAPTermHandle(Term t) {
CACHE_REGS
handle = Yap_InitSlot(t PASS_REGS);
}
void set(YAPTerm t) {
CACHE_REGS
Yap_PutInSlot(handle, t.t PASS_REGS);
}
};
/**
* @brief Predicate
*/ */
class YAPPredicate { class YAPPredicate {
friend class YAPQuery; friend class YAPQuery;
private:
PredEntry *ap; PredEntry *ap;
// trick to communicate t[] back to yapquery
YAPPredicate(const char *s, Term **outp, term_t& vnames ); /// auxiliary routine to find a predicate in the current module.
PredEntry *getPred( Term t, Term **outp ) ;
/// String constructor for predicates
///
/// It also communicates the array of arguments t[] abd the array of variables
/// back to yapquery
YAPPredicate(const char *s, Term **outp, handle_t& vnames );
/// Term constructor for predicates
///
/// It is just a call to getPred
inline YAPPredicate(Term t) { inline YAPPredicate(Term t) {
CACHE_REGS ap = getPred( t , NULL );
Term m = CurrentModule ;
t = Yap_StripModule(t, &m);
if (IsVarTerm(t) || IsNumTerm(t))
ap = NULL;
if (IsAtomTerm(t)) {
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
} else if (IsApplTerm(t)) {
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m));
} else if (IsPairTerm(t)) {
ap = RepPredProp(PredPropByFunc(FunctorCsult, PROLOG_MODULE));
} else {
ap = NULL;
}
} }
/// Cast constructor for predicates,
/// if we have the implementation data.
///
inline YAPPredicate(PredEntry *pe) { inline YAPPredicate(PredEntry *pe) {
ap = pe; ap = pe;
} }
public: public:
inline YAPPredicate(YAPFunctor f) {
CACHE_REGS /// Functor constructor for predicates
ap = RepPredProp(PredPropByFunc(f.f,CurrentModule)); ///
} /// Asssumes that we use the current module.
YAPPredicate(YAPFunctor f);
/// Functor constructor for predicates, is given a specific module.
///
inline YAPPredicate(YAPFunctor f, YAPTerm mod) { inline YAPPredicate(YAPFunctor f, YAPTerm mod) {
ap = RepPredProp(PredPropByFunc(f.f,mod.t)); ap = RepPredProp(PredPropByFunc(f.f,mod.t));
} }
/// Name/arity constructor for predicates.
///
inline YAPPredicate(YAPAtom at, YAPTerm mod) { inline YAPPredicate(YAPAtom at, YAPTerm mod) {
ap = RepPredProp(PredPropByAtom(at.a,mod.t)); ap = RepPredProp(PredPropByAtom(at.a,mod.t));
} }
inline YAPPredicate(YAPAtom at) {
CACHE_REGS
ap = RepPredProp(PredPropByAtom(at.a,CurrentModule)); /// Name/0 constructor for predicates.
} ///
YAPPredicate(YAPAtom at);
/// Mod:Name/Arity constructor for predicates.
///
inline YAPPredicate(YAPAtom at, arity_t arity, YAPTerm mod) { inline YAPPredicate(YAPAtom at, arity_t arity, YAPTerm mod) {
if (arity) { if (arity) {
Functor f = Yap_MkFunctor(at.a, arity); Functor f = Yap_MkFunctor(at.a, arity);
@ -345,55 +385,45 @@ public:
ap = RepPredProp(PredPropByAtom(at.a,mod.t)); ap = RepPredProp(PredPropByAtom(at.a,mod.t));
} }
} }
inline YAPPredicate(YAPAtom at, arity_t arity) {
CACHE_REGS /// Atom/Arity constructor for predicates.
if (arity) { ///
Functor f = Yap_MkFunctor(at.a, arity); YAPPredicate(YAPAtom at, arity_t arity);
ap = RepPredProp(PredPropByFunc(f,CurrentModule));
} else {
ap = RepPredProp(PredPropByAtom(at.a,CurrentModule)); /// String constructor for predicates.
} ///
} /// String is a Prolog term, we extract the main functor after considering the module qualifiers.
inline YAPPredicate(char *s) { inline YAPPredicate(char *s) {
CACHE_REGS Term t, tp;
Term t, tp, m = CurrentModule ;
t = YAP_ReadBuffer(s,&tp); t = YAP_ReadBuffer(s,&tp);
t = Yap_StripModule(t, &m); ap = getPred( t, NULL );
if (IsVarTerm(t) || IsNumTerm(t))
ap = NULL;
if (IsAtomTerm(t)) {
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
} else {
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m));
}
} }
/// String constructor for predicates, also keeps arguments in tp[]
///
/// String is a Prolog term, we extract the main functor after considering the module qualifiers.
inline YAPPredicate(char *s, Term **outp) { inline YAPPredicate(char *s, Term **outp) {
CACHE_REGS Term t, tp;
Term t, tp, m = CurrentModule ;
t = YAP_ReadBuffer(s,&tp); t = YAP_ReadBuffer(s,&tp);
t = Yap_StripModule(t, &m); ap = getPred( t, NULL );
if (IsVarTerm(t) || IsNumTerm(t))
ap = NULL;
if (IsAtomTerm(t)) {
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
*outp = NULL;
} else if (IsApplTerm(t)) {
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m));
*outp = RepAppl(t)+1;
} else if (IsPairTerm(t)) {
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m));
*outp = RepPair(t);
}
} }
/// meta-call this predicate, with arguments ts[]
///
int call(YAPTerm ts[]); int call(YAPTerm ts[]);
arity_t arity() { return ap->ArityOfPE; }
/// module of predicate /// module of a predicate
/// ///
/// notice that modules are currently treated as atoms, this should change. /// notice that modules are currently treated as atoms, this should change.
YAPAtom module() { if (ap->ModuleOfPred == PROLOG_MODULE) YAPAtom module() {
if (ap->ModuleOfPred == PROLOG_MODULE)
return YAPAtom(AtomProlog); return YAPAtom(AtomProlog);
else else
return YAPAtom(AtomOfTerm(ap->ModuleOfPred)); } return YAPAtom(AtomOfTerm(ap->ModuleOfPred));
}
/// name of predicate /// name of predicate
/// ///
/// notice that we return the atom, not a string. /// notice that we return the atom, not a string.
@ -402,6 +432,12 @@ public:
else else
return YAPAtom(NameOfFunctor(ap->FunctorOfPred)); return YAPAtom(NameOfFunctor(ap->FunctorOfPred));
} }
/// arity of predicate
///
/// we return a positive number.
arity_t getArity() { return ap->ArityOfPE; }
}; };
/** /**
@ -410,7 +446,7 @@ public:
* 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: private YAPPredicate { class YAPQuery: public YAPPredicate {
int q_open; int q_open;
int q_state; int q_state;
Term *q_g; Term *q_g;
@ -419,7 +455,7 @@ class YAPQuery: private YAPPredicate {
int q_flags; int q_flags;
YAP_dogoalinfo q_h; YAP_dogoalinfo q_h;
YAPQuery *oq; YAPQuery *oq;
term_t vnames; handle_t vnames;
void initQuery( Term ts[] ); void initQuery( Term ts[] );
void initQuery( YAPTerm t[], arity_t arity ); void initQuery( YAPTerm t[], arity_t arity );
public: public:
@ -451,88 +487,59 @@ public:
/// first query /// first query
/// ///
/// actually implemented by calling the next(); /// actually implemented by calling the next();
inline int 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
int next(); bool next();
/// remove alternatives in the current search space, and finish the current query /// remove alternatives in the current search space, and finish the current query
void cut(); void cut();
/// finish the current query: undo all bindings. /// finish the current query: undo all bindings.
void close(); void close();
/// query variables. /// query variables.
YAPListTerm namedVars() { CACHE_REGS Term o = Yap_GetFromSlot( vnames PASS_REGS ); return YAPListTerm( o ); } YAPListTerm namedVars();
}; };
// Java support
class YAPParams; // This class implements a callback Prolog-side
class YAPCallback {
public:
virtual ~YAPCallback() { printf("~YAPCallback\n"); }
virtual void run() { __android_log_print(ANDROID_LOG_INFO, __FUNCTION__, "callback"); }
virtual void displayInWindow(char *s) { }
};
/** /**
* @brief YAP Constructor * @brief YAP Engine: takes care of constructing an execution environment where we can go executing goals
* *
*/ */
class YAPEngine { class YAPEngine {
public: private:
YAPEngine(YAPParams const& params); /// construct a new engine YAPCallback *_callback;
YAPEngine(); /// construct a new engine, no arguments char **buf;
YAPQuery *query( char *s ) { return new YAPQuery( s ); } /// build a query on the engine
};
/**
* @brief Parameters for YAP Constructor
*
*/
class YAPParams {
friend class YAPEngine;
YAP_init_args init_args; YAP_init_args init_args;
public: public:
YAPParams(); YAPEngine(char *savedState = NULL,
// sets all the default values for each data member size_t stackSize = 0,
YAPParams& savedState( char * f); size_t trailSize = 0,
YAPParams& stackSize(size_t sz); size_t maxStackSize = 0,
YAPParams& trailSize(size_t sz); size_t maxTrailSize = 0,
YAPParams& maxStackSize(size_t sz); char *libDir = NULL,
YAPParams& maxTrailSize(size_t sz); char *bootFile = NULL,
YAPParams& libDir(char *p); char *goal = NULL,
YAPParams& bootFile(char *f); char *topLevel = NULL,
YAPParams& goal(char *g); bool script = FALSE,
YAPParams& topLevel(char *g); bool fastBoot = FALSE,
YAPParams& script(bool v); YAPCallback *callback=NULL); /// construct a new engine, including aaccess to callbacks
YAPParams& fastBoot(bool v); ~YAPEngine() { delYAPCallback(); } /// kill engine
void delYAPCallback() { _callback = 0; } /// remove current callback
void setYAPCallback(YAPCallback *cb) { delYAPCallback(); _callback = cb; __android_log_print(ANDROID_LOG_INFO, __FILE__, "after loading startup %p",cb); } /// set a new callback
void call() { if (_callback) _callback->run(); } /// execute the callback.
void display( char *s) { __android_log_print(ANDROID_LOG_INFO, __FUNCTION__, "bef calling disp %s %p",s, _callback); if (_callback) _callback->displayInWindow(s); } /// execute the callback.
YAPQuery *query( char *s ); /// build a query on the engine
}; };
inline YAPParams::YAPParams() /*
{ Yap_InitDefaults( &init_args, NULL ); } * @}
*
inline YAPParams& YAPParams::savedState( char * f) */
{ init_args.SavedState = f; return *this; }
inline YAPParams& YAPParams::stackSize(size_t sz)
{ init_args.StackSize = sz; return *this; }
inline YAPParams& YAPParams::trailSize(size_t sz)
{ init_args.TrailSize = sz; return *this; }
inline YAPParams& YAPParams::maxStackSize(size_t sz)
{ init_args.MaxStackSize = sz; return *this; }
inline YAPParams& YAPParams::maxTrailSize(size_t sz)
{ init_args.MaxTrailSize = sz; return *this; }
inline YAPParams& YAPParams::libDir(char *p)
{ init_args.YapLibDir = p; return *this; }
inline YAPParams& YAPParams::bootFile(char *f)
{ init_args.YapPrologBootFile = f; return *this; }
inline YAPParams& YAPParams::goal(char *g)
{ init_args.YapPrologGoal = g; return *this; }
inline YAPParams& YAPParams::topLevel(char *g)
{ init_args.YapPrologTopLevelGoal = g; return *this; }
inline YAPParams& YAPParams::script(bool v)
{ init_args.HaltAfterConsult = v; return *this; }
inline YAPParams& YAPParams::fastBoot(bool v)
{ init_args.FastBoot = v; return *this; }