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/yapdb.hh

291 lines
7.9 KiB
C++
Raw Normal View History

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;
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:
2016-09-21 20:41:23 +01:00
~YAPModule(){};
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; };
YAPModuleProp(Term tmod) { m = Yap_GetModuleEntry(tmod); };
2015-04-13 13:28:17 +01:00
public:
2016-09-21 20:41:23 +01:00
YAPModuleProp() { CACHE_REGS m = Yap_GetModuleEntry(Yap_CurrentModule()); };
YAPModuleProp(YAPModule tmod);
2015-02-09 01:52:10 +00:00
virtual YAPModule module() { return YAPModule(m->AtomOfME); };
};
/**
* @brief YAPFunctor represents Prolog functors Name/Arity
*/
2016-09-21 20:41:23 +01:00
class YAPFunctor : public YAPProp {
friend class YAPApplTerm;
friend class YAPTerm;
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
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
inline YAPFunctor(const char *s, uintptr_t arity, bool isutf8 = true) {
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
inline YAPFunctor(const wchar_t *s, uintptr_t arity) {
f = Yap_MkFunctor(Yap_LookupWideAtom(s), arity);
}
~YAPFunctor(){};
/// Getter: extract name of functor as an atom
///
/// 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.
uintptr_t arity(void) { return ArityOfFunctor(f); }
};
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
2016-09-21 20:41:23 +01:00
YAPPredicate(const char *s0, Term &out, Term &names) {
2015-04-13 13:28:17 +01:00
CACHE_REGS
BACKUP_MACHINE_REGS();
2016-09-23 07:21:42 +01:00
Term *modp = NULL;;
2016-07-31 16:22:24 +01:00
2016-09-21 20:41:23 +01:00
out = Yap_StringToTerm(s0, strlen(s0) + 1, &LOCAL_encoding, 1200, &names);
// 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-21 20:41:23 +01:00
throw YAPError();
2016-09-23 07:21:42 +01:00
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
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
}
/// 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:
2016-09-21 20:41:23 +01:00
~YAPPredicate(){};
2015-02-09 01:52:10 +00:00
/// Functor constructor for predicates
///
/// Asssumes that we use the current module.
2016-09-21 20:41:23 +01:00
YAPPredicate(YAPFunctor f) {
CACHE_REGS
2016-09-21 20:41:23 +01:00
ap = RepPredProp(PredPropByFunc(f.f, Yap_CurrentModule()));
2015-02-09 01:52:10 +00:00
};
/// Functor constructor for predicates, is given a specific module.
///
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.
///
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.
///
YAPPredicate(YAPAtom at);
/// Mod:Name/Arity constructor for predicates.
///
2016-07-31 16:22:24 +01: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.
///
2016-07-31 16:22:24 +01:00
YAPPredicate(YAPAtom at, uintptr_t arity);
2015-02-09 01:52:10 +00:00
/// module of a predicate
///
/// notice that modules are currently treated as atoms, this should change.
YAPModule module() {
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
}
/// arity of predicate
///
/// we return a positive number.
2016-07-31 16:22:24 +01:00
uintptr_t getArity() { 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:
2016-09-21 20:41:23 +01:00
YAPPrologPredicate(YAPAtom name, uintptr_t arity,
YAPModule module = YAPModule(), bool tabled = false,
bool logical_updates = false, bool local = false,
bool sourced = true, bool discontiguous = false,
bool multiFile = false, bool hidden = false,
bool untraceable = false, bool unspyable = false,
bool meta = false, bool sync = false,
bool quasi_quotable = false, size_t mega_clause = 0);
void *assertClause(YAPTerm clause, bool last = true,
YAPTerm source = YAPTerm(TermNil));
void *retractClause(YAPTerm skeleton, bool all = false);
void *clause(YAPTerm skeleton, YAPTerm &body);
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
}
}
};
2016-09-21 20:41:23 +01:00
YAPFLIP(const char *name, uintptr_t arity, YAPModule module = YAPModule(),
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
};
2016-09-21 20:41:23 +01:00
bool addCall(CPredicate call) { return Yap_AddCallToFli(ap, call); }
bool addRetry(CPredicate call) { return Yap_AddRetryToFli(ap, call); }
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