2015-02-09 01:52:10 +00:00
|
|
|
|
2016-07-31 16:22:24 +01:00
|
|
|
#ifndef _YAPDB_H
|
|
|
|
#define _YAPDB_H
|
|
|
|
|
2015-02-09 01:52:10 +00:00
|
|
|
#define YAP_CPP_DB_INTERFACE 1
|
|
|
|
|
|
|
|
//! @{
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @defgroup yap-cplus-db-interface Data-Base Component of YAP interface.
|
|
|
|
*
|
|
|
|
* @ingroup yap-cplus-interface
|
|
|
|
* @tableofcontents
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Data-base component of C++ interface to YAP. It manipulates sets of
|
|
|
|
* atoms, each one containing a number of props.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class YAPTerm;
|
2016-07-31 16:22:24 +01:00
|
|
|
class YAPAtomTerm;
|
2015-02-09 01:52:10 +00:00
|
|
|
|
|
|
|
class YAPError;
|
|
|
|
|
|
|
|
class YAPModule;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief YAPModule
|
|
|
|
* A YAPModule describes a bare module, which in YAP is just a name.
|
|
|
|
*
|
|
|
|
* Info about the module is in YAPModuleProp
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class YAPModule : protected YAPAtomTerm {
|
|
|
|
friend class YAPPredicate;
|
2017-02-20 14:38:00 +00:00
|
|
|
friend class YAPModuleProp;
|
2016-09-21 20:41:23 +01:00
|
|
|
YAPModule(Term t) : YAPAtomTerm(t){};
|
2015-02-09 01:52:10 +00:00
|
|
|
Term t() { return gt(); }
|
2016-09-21 20:41:23 +01:00
|
|
|
Term curModule() { CACHE_REGS return Yap_CurrentModule(); }
|
|
|
|
|
2015-02-09 01:52:10 +00:00
|
|
|
public:
|
2017-02-20 14:38:00 +00:00
|
|
|
YAPModule() : YAPAtomTerm(curModule()){};
|
|
|
|
YAPModule(YAPAtom t) : YAPAtomTerm(t){};
|
2015-02-09 01:52:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief YAPModuleProp
|
|
|
|
* A YAPModuleProp controls access to a module property.
|
|
|
|
*
|
|
|
|
*/
|
2016-09-21 20:41:23 +01:00
|
|
|
class YAPModuleProp : public YAPProp {
|
2015-02-09 01:52:10 +00:00
|
|
|
friend class YAPPredicate;
|
|
|
|
ModEntry *m;
|
2015-04-13 13:28:17 +01:00
|
|
|
|
2016-09-21 20:41:23 +01:00
|
|
|
YAPModuleProp(ModEntry *mod) { m = mod; };
|
2017-02-20 14:38:00 +00:00
|
|
|
YAPModuleProp(Term tmod) { m = Yap_GetModuleEntry(tmod); };
|
2016-09-21 20:41:23 +01:00
|
|
|
|
2015-04-13 13:28:17 +01:00
|
|
|
public:
|
2017-02-20 14:38:00 +00:00
|
|
|
YAPModuleProp(YAPModule tmod) { m = Yap_GetModuleEntry(tmod.gt()); };
|
|
|
|
YAPModuleProp() { CACHE_REGS m = Yap_GetModuleEntry(Yap_CurrentModule()); };
|
|
|
|
virtual YAPModule module() { return YAPModule(m->AtomOfME); };
|
2015-02-09 01:52:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief YAPFunctor represents Prolog functors Name/Arity
|
|
|
|
*/
|
2016-09-21 20:41:23 +01:00
|
|
|
class YAPFunctor : public YAPProp {
|
2016-09-27 18:28:54 +01:00
|
|
|
friend class YAPApplTerm;
|
|
|
|
friend class YAPTerm;
|
2016-09-21 20:41:23 +01:00
|
|
|
friend class YAPPredicate;
|
|
|
|
friend class YAPQuery;
|
|
|
|
Functor f;
|
|
|
|
/// Constructor: receives Prolog functor and casts it to YAPFunctor
|
|
|
|
///
|
|
|
|
/// Notice that this is designed for internal use only.
|
|
|
|
inline YAPFunctor(Functor ff) { f = ff; }
|
2015-02-09 01:52:10 +00:00
|
|
|
|
2016-09-21 20:41:23 +01:00
|
|
|
public:
|
|
|
|
/// Constructor: receives name as an atom, plus arity
|
|
|
|
///
|
|
|
|
/// This is the default method, and the most popular
|
2017-02-20 14:38:00 +00:00
|
|
|
YAPFunctor(YAPAtom at, uintptr_t arity) { f = Yap_MkFunctor(at.a, arity); }
|
2015-04-13 13:28:17 +01:00
|
|
|
|
2016-09-21 20:41:23 +01:00
|
|
|
/// Constructor: receives name as a string plus arity
|
|
|
|
///
|
|
|
|
/// Notice that this is designed for ISO-LATIN-1 right now
|
|
|
|
/// Note: Python confuses the 3 constructors,
|
|
|
|
/// use YAPFunctorFromString
|
2017-02-20 14:38:00 +00:00
|
|
|
inline YAPFunctor(const char *s, uintptr_t arity, bool isutf8 = true) {
|
2016-09-21 20:41:23 +01:00
|
|
|
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
|
|
|
|
///
|
|
|
|
/// Note: Python confuses the 3 constructors,
|
|
|
|
/// use YAPFunctorFromWideString
|
2017-02-20 14:38:00 +00:00
|
|
|
inline YAPFunctor(const wchar_t *s, uintptr_t arity) {
|
2016-11-08 07:37:36 +00:00
|
|
|
CACHE_REGS f = Yap_MkFunctor(UTF32ToAtom(s PASS_REGS), arity);
|
2016-09-21 20:41:23 +01:00
|
|
|
}
|
|
|
|
/// Getter: extract name of functor as an atom
|
|
|
|
///
|
|
|
|
/// this is for external usage.
|
2017-02-20 14:38:00 +00:00
|
|
|
YAPAtom name(void) { return YAPAtom(NameOfFunctor(f)); }
|
2016-09-21 20:41:23 +01:00
|
|
|
|
|
|
|
/// Getter: extract arity of functor as an unsigned integer
|
|
|
|
///
|
|
|
|
/// this is for external usage.
|
2017-02-20 14:38:00 +00:00
|
|
|
uintptr_t arity(void) { return ArityOfFunctor(f); }
|
2016-09-21 20:41:23 +01:00
|
|
|
};
|
2015-02-09 01:52:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Predicates
|
|
|
|
*
|
|
|
|
* This class interfaces with PredEntry in Yatom.
|
|
|
|
*/
|
2016-09-21 20:41:23 +01:00
|
|
|
class YAPPredicate : public YAPModuleProp {
|
2015-02-09 01:52:10 +00:00
|
|
|
friend class YAPQuery;
|
2016-09-21 20:41:23 +01:00
|
|
|
friend class YAPEngine;
|
2015-02-09 01:52:10 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
PredEntry *ap;
|
|
|
|
|
|
|
|
/// auxiliary routine to find a predicate in the current module.
|
2016-09-21 20:41:23 +01:00
|
|
|
PredEntry *getPred(Term &t, Term *&outp);
|
|
|
|
|
|
|
|
PredEntry *asPred() { return ap; };
|
2015-02-09 01:52:10 +00:00
|
|
|
|
|
|
|
/// String constructor for predicates
|
|
|
|
///
|
|
|
|
/// It also communicates the array of arguments t[]
|
|
|
|
/// and the array of variables
|
|
|
|
/// back to yapquery
|
2017-02-20 14:38:00 +00:00
|
|
|
YAPPredicate(const char *s0, Term &out, Term &names) {
|
2015-04-13 13:28:17 +01:00
|
|
|
CACHE_REGS
|
|
|
|
BACKUP_MACHINE_REGS();
|
2016-09-27 18:28:54 +01:00
|
|
|
Term *modp = NULL;
|
2016-12-10 07:01:10 +00:00
|
|
|
names = MkVarTerm ();
|
|
|
|
const unsigned char *us = (const unsigned char *)s0;
|
|
|
|
out =
|
|
|
|
Yap_BufferToTermWithPrioBindings(us, strlen(s0), TermNil, 1200, names);
|
2016-09-21 20:41:23 +01:00
|
|
|
// extern char *s0;
|
|
|
|
// fprintf(stderr,"ap=%p arity=%d text=%s", ap, ap->ArityOfPE, s);
|
2016-07-31 16:22:24 +01:00
|
|
|
// Yap_DebugPlWrite(out);
|
|
|
|
// delete [] ns;
|
|
|
|
if (out == 0L)
|
2016-09-27 18:28:54 +01:00
|
|
|
ap = nullptr;
|
|
|
|
else
|
|
|
|
ap = getPred(out, modp);
|
2015-04-13 13:28:17 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
}
|
|
|
|
|
2015-02-09 01:52:10 +00:00
|
|
|
/// Term constructor for predicates
|
|
|
|
///
|
|
|
|
/// It is just a call to getPred
|
2017-02-20 14:38:00 +00:00
|
|
|
inline YAPPredicate(Term t) {
|
2016-09-21 20:41:23 +01:00
|
|
|
CELL *v = NULL;
|
|
|
|
ap = getPred(t, v);
|
2015-02-09 01:52:10 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 18:28:54 +01:00
|
|
|
/// Term constructor for predicates
|
|
|
|
///
|
|
|
|
/// It is just a call to getPred
|
|
|
|
inline YAPPredicate(YAPTerm t) {
|
|
|
|
Term *v = nullptr;
|
|
|
|
Term tt = t.term();
|
|
|
|
ap = getPred(tt, v);
|
|
|
|
}
|
|
|
|
|
2015-02-09 01:52:10 +00:00
|
|
|
/// Cast constructor for predicates,
|
|
|
|
/// if we have the implementation data.
|
|
|
|
///
|
2016-09-21 20:41:23 +01:00
|
|
|
inline YAPPredicate(PredEntry *pe) { ap = pe; }
|
2015-02-09 01:52:10 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/// Functor constructor for predicates
|
|
|
|
///
|
|
|
|
/// Asssumes that we use the current module.
|
2017-02-20 14:38:00 +00:00
|
|
|
YAPPredicate(YAPFunctor f) {
|
2015-04-15 11:21:15 +01:00
|
|
|
CACHE_REGS
|
2016-09-21 20:41:23 +01:00
|
|
|
ap = RepPredProp(PredPropByFunc(f.f, Yap_CurrentModule()));
|
2016-09-27 18:28:54 +01:00
|
|
|
}
|
2015-02-09 01:52:10 +00:00
|
|
|
|
|
|
|
/// Functor constructor for predicates, is given a specific module.
|
|
|
|
///
|
2017-02-20 14:38:00 +00:00
|
|
|
inline YAPPredicate(YAPFunctor f, YAPTerm mod) {
|
2016-09-21 20:41:23 +01:00
|
|
|
ap = RepPredProp(PredPropByFunc(f.f, mod.t));
|
2015-02-09 01:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Name/arity constructor for predicates.
|
|
|
|
///
|
2017-02-20 14:38:00 +00:00
|
|
|
inline YAPPredicate(YAPAtom at, YAPTerm mod) {
|
2016-09-21 20:41:23 +01:00
|
|
|
ap = RepPredProp(PredPropByAtom(at.a, mod.t));
|
2015-02-09 01:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Name/0 constructor for predicates.
|
|
|
|
///
|
2017-02-20 14:38:00 +00:00
|
|
|
YAPPredicate(YAPAtom at);
|
2015-02-09 01:52:10 +00:00
|
|
|
|
|
|
|
/// Mod:Name/Arity constructor for predicates.
|
|
|
|
///
|
2017-02-20 14:38:00 +00:00
|
|
|
inline YAPPredicate(YAPAtom at, uintptr_t arity, YAPModule mod) {
|
2015-02-09 01:52:10 +00:00
|
|
|
if (arity) {
|
|
|
|
Functor f = Yap_MkFunctor(at.a, arity);
|
2016-09-21 20:41:23 +01:00
|
|
|
ap = RepPredProp(PredPropByFunc(f, mod.t()));
|
2015-02-09 01:52:10 +00:00
|
|
|
} else {
|
2016-09-21 20:41:23 +01:00
|
|
|
ap = RepPredProp(PredPropByAtom(at.a, mod.t()));
|
2015-02-09 01:52:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Atom/Arity constructor for predicates.
|
|
|
|
///
|
2017-02-20 14:38:00 +00:00
|
|
|
YAPPredicate(YAPAtom at, uintptr_t arity);
|
2015-02-09 01:52:10 +00:00
|
|
|
|
2016-09-30 23:11:13 +01:00
|
|
|
/// char */module constructor for predicates.
|
|
|
|
///
|
2017-02-20 14:38:00 +00:00
|
|
|
inline YAPPredicate(const char *at, uintptr_t arity) {
|
2016-12-10 07:01:10 +00:00
|
|
|
ap = RepPredProp(PredPropByFunc(Yap_MkFunctor(Yap_LookupAtom(at), arity),
|
|
|
|
CurrentModule));
|
2016-09-30 23:11:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/// char */module constructor for predicates.
|
|
|
|
///
|
2017-02-20 14:38:00 +00:00
|
|
|
inline YAPPredicate(const char *at, uintptr_t arity, YAPTerm mod) {
|
2016-12-10 07:01:10 +00:00
|
|
|
ap = RepPredProp(
|
|
|
|
PredPropByFunc(Yap_MkFunctor(Yap_LookupAtom(at), arity), mod.t));
|
2016-09-30 23:11:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/// char */module constructor for predicates.
|
|
|
|
///
|
2017-02-20 14:38:00 +00:00
|
|
|
inline YAPPredicate(const char *at, YAPTerm mod) {
|
2016-09-30 23:11:13 +01:00
|
|
|
ap = RepPredProp(PredPropByAtom(Yap_LookupAtom(at), mod.t));
|
|
|
|
}
|
|
|
|
|
2015-02-09 01:52:10 +00:00
|
|
|
/// module of a predicate
|
|
|
|
///
|
|
|
|
/// notice that modules are currently treated as atoms, this should change.
|
2017-02-20 14:38:00 +00:00
|
|
|
YAPModule module() {
|
2015-02-09 01:52:10 +00:00
|
|
|
if (ap->ModuleOfPred == PROLOG_MODULE)
|
2016-09-21 20:41:23 +01:00
|
|
|
return YAPModule(AtomProlog);
|
2015-02-09 01:52:10 +00:00
|
|
|
else
|
2016-09-21 20:41:23 +01:00
|
|
|
return YAPModule(AtomOfTerm(ap->ModuleOfPred));
|
2015-02-09 01:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// name of predicate
|
|
|
|
///
|
|
|
|
/// notice that we return the atom, not a string.
|
2016-09-21 20:41:23 +01:00
|
|
|
YAPAtom name() {
|
|
|
|
if (ap->ArityOfPE)
|
|
|
|
return YAPAtom((Atom)ap->FunctorOfPred);
|
2015-02-09 01:52:10 +00:00
|
|
|
else
|
2016-09-21 20:41:23 +01:00
|
|
|
return YAPAtom(NameOfFunctor(ap->FunctorOfPred));
|
2015-02-09 01:52:10 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 14:38:00 +00:00
|
|
|
/// functor of predicate
|
|
|
|
///
|
|
|
|
/// onlu defined if arity >= 1
|
|
|
|
YAPFunctor functor() {
|
|
|
|
if (ap->ArityOfPE)
|
|
|
|
return YAPFunctor(ap->FunctorOfPred);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-02-09 01:52:10 +00:00
|
|
|
/// arity of predicate
|
|
|
|
///
|
|
|
|
/// we return a positive number.
|
2016-07-31 16:22:24 +01:00
|
|
|
uintptr_t getArity() { return ap->ArityOfPE; }
|
2016-09-27 18:28:54 +01:00
|
|
|
arity_t arity() { return ap->ArityOfPE; }
|
2015-02-09 01:52:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief PrologPredicate
|
|
|
|
*
|
|
|
|
* This class interfaces with Predicates Implemented in Prolog.
|
|
|
|
*/
|
2016-09-21 20:41:23 +01:00
|
|
|
class YAPPrologPredicate : public YAPPredicate {
|
2015-02-09 01:52:10 +00:00
|
|
|
public:
|
2017-02-20 14:38:00 +00:00
|
|
|
YAPPrologPredicate(YAPTerm t) : YAPPredicate(t){};
|
|
|
|
YAPPrologPredicate(const char *s, arity_t arity) : YAPPredicate(s, arity){};
|
2016-09-27 18:28:54 +01:00
|
|
|
/// add a new clause
|
2017-02-20 14:38:00 +00:00
|
|
|
bool assertClause(YAPTerm clause, bool last = true,
|
2016-09-27 18:28:54 +01:00
|
|
|
YAPTerm source = YAPTerm());
|
2016-09-30 23:11:13 +01:00
|
|
|
/// add a new tuple
|
2017-02-20 14:38:00 +00:00
|
|
|
bool assertFact(YAPTerm *tuple, bool last = true);
|
2016-09-27 18:28:54 +01:00
|
|
|
/// retract at least the first clause matching the predicate.
|
2017-02-20 14:38:00 +00:00
|
|
|
void *retractClause(YAPTerm skeleton, bool all = false);
|
2016-09-27 18:28:54 +01:00
|
|
|
/// return the Nth clause (if source is available)
|
|
|
|
// YAPTerm clause(size_t index, YAPPredicate p) { return YAPTerm(); };
|
|
|
|
/// return the Nth clause (if source is available)
|
|
|
|
YAPTerm *nextClause() { return nullptr; };
|
2015-02-09 01:52:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief PrologPredicate
|
|
|
|
*
|
|
|
|
* This class interfaces with Predicates Implemented in Prolog.
|
|
|
|
*/
|
2016-09-21 20:41:23 +01:00
|
|
|
class YAPFLIP : public YAPPredicate {
|
2015-02-09 01:52:10 +00:00
|
|
|
public:
|
2016-09-21 20:41:23 +01:00
|
|
|
YAPFLIP(CPredicate call, YAPAtom name, uintptr_t arity,
|
|
|
|
YAPModule module = YAPModule(), CPredicate retry = 0,
|
|
|
|
CPredicate cut = 0, size_t extra = 0, bool test = false)
|
|
|
|
: YAPPredicate(name, arity, module) {
|
2015-02-09 01:52:10 +00:00
|
|
|
if (retry) {
|
2016-09-21 20:41:23 +01:00
|
|
|
Yap_InitCPredBackCut(name.getName(), arity, extra, call, retry, cut,
|
|
|
|
UserCPredFlag);
|
2015-02-09 01:52:10 +00:00
|
|
|
} else {
|
|
|
|
if (test) {
|
2016-09-21 20:41:23 +01:00
|
|
|
YAP_UserCPredicate(name.getName(), call, arity);
|
2015-02-09 01:52:10 +00:00
|
|
|
} else {
|
2016-09-21 20:41:23 +01:00
|
|
|
YAP_UserCPredicate(name.getName(), call, arity);
|
2015-02-09 01:52:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-02-20 14:38:00 +00:00
|
|
|
YAPFLIP(const char *name, uintptr_t arity, YAPModule module = YAPModule(),
|
2016-09-21 20:41:23 +01:00
|
|
|
bool backtrackable = false)
|
|
|
|
: YAPPredicate(YAPAtom(name), arity, module) {
|
2015-02-09 01:52:10 +00:00
|
|
|
if (backtrackable) {
|
|
|
|
Yap_InitCPredBackCut(name, arity, 0, 0, 0, 0, UserCPredFlag);
|
|
|
|
} else {
|
2016-09-21 20:41:23 +01:00
|
|
|
YAP_UserCPredicate(name, 0, arity);
|
|
|
|
}
|
2015-02-09 01:52:10 +00:00
|
|
|
};
|
2017-02-20 14:38:00 +00:00
|
|
|
bool addCall(CPredicate call) { return Yap_AddCallToFli(ap, call); }
|
|
|
|
bool addRetry(CPredicate call) { return Yap_AddRetryToFli(ap, call); }
|
2016-09-21 20:41:23 +01:00
|
|
|
bool addCut(CPredicate call) { return Yap_AddCutToFli(ap, call); }
|
2015-04-13 13:28:17 +01:00
|
|
|
};
|
2016-07-31 16:22:24 +01:00
|
|
|
|
|
|
|
#endif
|