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

310 lines
8.3 KiB
C++
Raw Normal View History

2017-04-07 23:10:59 +01:00
/// @file yapdb.hh
///
/// @brief C++ Interface to generated code.
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
2017-04-07 23:10:59 +01:00
* @{
2015-02-09 01:52:10 +00:00
* @tableofcontents
*
*
2017-04-07 23:10:59 +01:00
* These classes define the main data-structures stored to represent compiled
* programs:
*
* + YAPFunctor represents a name/arity combination.
2017-05-02 07:38:23 +01:00
*
2017-04-07 23:10:59 +01:00
* + YAPModule wraps the YAP module implementation.
*
2017-05-02 07:38:23 +01:00
* + YAPPredicate and subclasses store the actual program, Preliminary
* support covers Prolog and C-defined predicates.
2015-02-09 01:52:10 +00:00
*/
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
*
*/
2017-06-12 18:00:47 +01:00
class X_API YAPModule : protected YAPAtomTerm {
2015-02-09 01:52:10 +00:00
friend class YAPPredicate;
2017-02-20 14:38:00 +00:00
friend class YAPModuleProp;
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-06-05 13:06:12 +01:00
YAPModule(YAP_Term t) : YAPAtomTerm(t){};
2017-03-20 15:52:48 +00:00
YAPModule() : YAPAtomTerm(curModule()){};
YAPModule(YAPAtom t) : YAPAtomTerm(t){};
2017-06-05 13:06:12 +01:00
Term term() { return gt(); };
2015-02-09 01:52:10 +00:00
};
/**
* @brief YAPModuleProp
* A YAPModuleProp controls access to a module property.
*
*/
2017-06-12 18:00:47 +01:00
class X_API 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-03-20 15:52:48 +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()); };
2017-03-20 15:52:48 +00:00
YAPModuleProp() { CACHE_REGS m = Yap_GetModuleEntry(Yap_CurrentModule()); };
virtual YAPModule module() { return YAPModule(m->AtomOfME); };
2015-02-09 01:52:10 +00:00
};
/**
* @brief Predicates
*
* This class interfaces with PredEntry in Yatom.
*/
2017-06-12 18:00:47 +01:00
class X_API 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.
2017-05-02 07:38:23 +01:00
PredEntry *getPred(YAPTerm &t, Term *&outp);
2016-09-21 20:41:23 +01:00
PredEntry *asPred() { return ap; };
2015-02-09 01:52:10 +00:00
2017-05-19 09:56:37 +01:00
/// Empty constructor for predicates
///
/// Just do nothing.
inline YAPPredicate() {
}
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-05-02 07:38:23 +01:00
YAPPredicate(const char *s0, Term &tout, Term &tnames) {
2015-04-13 13:28:17 +01:00
CACHE_REGS
2016-09-27 18:28:54 +01:00
Term *modp = NULL;
2016-12-10 07:01:10 +00:00
const unsigned char *us = (const unsigned char *)s0;
2017-05-02 07:38:23 +01:00
tnames = MkVarTerm();
tout =
Yap_BufferToTermWithPrioBindings(us, strlen(s0), TermNil, 1200, tnames);
2016-09-21 20:41:23 +01:00
// fprintf(stderr,"ap=%p arity=%d text=%s", ap, ap->ArityOfPE, s);
2016-07-31 16:22:24 +01:00
// Yap_DebugPlWrite(out);
2017-05-02 07:38:23 +01:00
if (tout == 0L) {
Yap_ThrowError(TYPE_ERROR_PREDICATE_INDICATOR, MkStringTerm(s0), "YAPPredicate");
}
YAPTerm tt = YAPTerm(tout);
ap = getPred(tt, modp);
2015-04-13 13:28:17 +01:00
}
2015-02-09 01:52:10 +00:00
/// Term constructor for predicates
///
/// It is just a call to getPred
2017-03-20 15:52:48 +00:00
inline YAPPredicate(Term t) {
2016-09-21 20:41:23 +01:00
CELL *v = NULL;
2017-05-02 07:38:23 +01:00
YAPTerm tt = YAPTerm(t);
ap = getPred(tt, 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;
2017-05-02 07:38:23 +01:00
ap = getPred(t, v);
2016-09-27 18:28:54 +01:00
}
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
2017-05-19 09:56:37 +01:00
/// Functor constructor for predicates, is given a specific module.
/// This version avoids manufacturing objects
inline YAPPredicate(Functor f, Term mod) {
ap = RepPredProp(PredPropByFunc(f, mod));
}
2015-02-09 01:52:10 +00:00
public:
/// Functor constructor for predicates
///
/// Asssumes that we use the current module.
2017-03-20 15:52:48 +00:00
YAPPredicate(YAPFunctor f) {
CACHE_REGS
2017-03-20 15:52:48 +00: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-03-20 15:52:48 +00:00
inline YAPPredicate(YAPFunctor f, YAPTerm mod) {
2017-05-02 07:38:23 +01:00
ap = RepPredProp(PredPropByFunc(f.f, mod.term()));
2015-02-09 01:52:10 +00:00
}
/// Name/arity constructor for predicates.
///
2017-03-20 15:52:48 +00:00
inline YAPPredicate(YAPAtom at, YAPTerm mod) {
2017-05-02 07:38:23 +01:00
ap = RepPredProp(PredPropByAtom(at.a, mod.term()));
2015-02-09 01:52:10 +00:00
}
/// Name/0 constructor for predicates.
///
2017-03-20 15:52:48 +00:00
YAPPredicate(YAPAtom at);
2015-02-09 01:52:10 +00:00
/// Mod:Name/Arity constructor for predicates.
///
2017-03-20 15:52:48 +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);
2017-05-02 07:38:23 +01:00
ap = RepPredProp(PredPropByFunc(f, mod.term()));
2015-02-09 01:52:10 +00:00
} else {
2017-05-02 07:38:23 +01:00
ap = RepPredProp(PredPropByAtom(at.a, mod.term()));
2015-02-09 01:52:10 +00:00
}
}
/// Atom/Arity constructor for predicates.
///
2017-03-20 15:52:48 +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-03-20 15:52:48 +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-03-20 15:52:48 +00:00
inline YAPPredicate(const char *at, uintptr_t arity, YAPTerm mod) {
2016-12-10 07:01:10 +00:00
ap = RepPredProp(
2017-05-02 07:38:23 +01:00
PredPropByFunc(Yap_MkFunctor(Yap_LookupAtom(at), arity), mod.term()));
2016-09-30 23:11:13 +01:00
};
/// char */module constructor for predicates.
///
2017-03-20 15:52:48 +00:00
inline YAPPredicate(const char *at, YAPTerm mod) {
2017-05-02 07:38:23 +01:00
ap = RepPredProp(PredPropByAtom(Yap_LookupAtom(at), mod.term()));
2016-09-30 23:11:13 +01:00
}
2015-02-09 01:52:10 +00:00
/// module of a predicate
///
/// notice that modules are currently treated as atoms, this should change.
2017-03-20 15:52:48 +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)
2017-03-20 15:52:48 +00:00
return YAPFunctor(ap->FunctorOfPred);
2017-05-02 07:38:23 +01:00
Yap_ThrowError(DOMAIN_ERROR_OUT_OF_RANGE, MkIntTerm(0), "YAPFunctor::functor");
2017-02-20 14:38:00 +00:00
}
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; }
2017-06-05 13:06:12 +01:00
PredEntry *predEntry() { return ap; }
2015-02-09 01:52:10 +00:00
};
/**
* @brief PrologPredicate
*
* This class interfaces with Predicates Implemented in Prolog.
*/
2017-06-12 18:00:47 +01:00
class X_API YAPPrologPredicate : public YAPPredicate {
2015-02-09 01:52:10 +00:00
public:
2017-03-20 15:52:48 +00:00
YAPPrologPredicate(YAPTerm t) : YAPPredicate(t){};
YAPPrologPredicate(const char *s, arity_t arity) : YAPPredicate(s, arity){};
2017-07-03 21:18:01 +01:00
YAPPrologPredicate(YAPAtom s, arity_t arity) : YAPPredicate(s, arity){};
2016-09-27 18:28:54 +01:00
/// add a new clause
2017-03-20 15:52:48 +00:00
bool assertClause(YAPTerm clause, bool last = true,
YAPTerm source = YAPTerm());
2016-09-30 23:11:13 +01:00
/// add a new tuple
2017-03-20 15:52:48 +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-03-20 15:52:48 +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.
*/
2017-06-12 18:00:47 +01:00
class X_API 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)
2017-03-20 15:52:48 +00:00
: 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-03-20 15:52:48 +00:00
YAPFLIP(const char *name, uintptr_t arity, YAPModule module = YAPModule(),
2016-09-21 20:41:23 +01:00
bool backtrackable = false)
2017-03-20 15:52:48 +00:00
: 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-03-20 15:52:48 +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
2017-03-20 15:52:48 +00:00
/// @}