2015-02-09 01:52:10 +00:00
|
|
|
#ifndef YAPQ_HH
|
|
|
|
#define YAPQ_HH 1
|
|
|
|
|
|
|
|
/**
|
|
|
|
Queries and engines
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Queries
|
|
|
|
*
|
|
|
|
* interface to a YAP Query;
|
|
|
|
* uses an SWI-like status info internally.
|
|
|
|
*/
|
2016-03-29 02:02:43 +01:00
|
|
|
class YAPQuery: public YAPPredicate, public open_query_struct {
|
2016-01-07 16:02:08 +00:00
|
|
|
YAPListTerm vnames;
|
2015-04-13 13:28:17 +01:00
|
|
|
YAPTerm goal;
|
2016-01-07 16:02:08 +00:00
|
|
|
Term names;
|
2015-04-13 13:28:17 +01:00
|
|
|
Term t;
|
|
|
|
|
|
|
|
void initOpenQ();
|
|
|
|
void initQuery( Term t );
|
|
|
|
void initQuery( YAPTerm ts[], arity_t arity );
|
2015-02-09 01:52:10 +00:00
|
|
|
public:
|
|
|
|
/// main constructor, uses a predicate and an array of terms
|
|
|
|
///
|
|
|
|
/// It is given a YAPPredicate _p_ , and an array of terms that must have at least
|
|
|
|
/// the same arity as the functor.
|
|
|
|
YAPQuery(YAPPredicate p, YAPTerm t[]);
|
|
|
|
/// full constructor,
|
|
|
|
///
|
|
|
|
/// It is given a functor, module, and an array of terms that must have at least
|
|
|
|
/// the same arity as the functor.
|
|
|
|
YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm t[]);
|
|
|
|
/// functor/term constructor,
|
|
|
|
///
|
|
|
|
/// It is given a functor, and an array of terms that must have at least
|
|
|
|
/// the same arity as the functor. Works within the current module.
|
|
|
|
YAPQuery(YAPFunctor f, YAPTerm t[]);
|
|
|
|
/// string constructor without varnames
|
|
|
|
///
|
|
|
|
/// It is given a string, calls the parser and obtains a Prolog term that should be a callable
|
2015-04-13 13:28:17 +01:00
|
|
|
/// goal.
|
2016-01-07 16:02:08 +00:00
|
|
|
inline YAPQuery(const char *s): YAPPredicate(s, t, names)
|
2015-02-09 01:52:10 +00:00
|
|
|
{
|
2016-01-07 16:02:08 +00:00
|
|
|
vnames = YAPListTerm( names );
|
2016-04-12 16:22:53 +01:00
|
|
|
|
2015-04-13 13:28:17 +01:00
|
|
|
initQuery( t );
|
|
|
|
};
|
|
|
|
|
2015-02-09 01:52:10 +00:00
|
|
|
/// set flags for query execution, currently only for exception handling
|
|
|
|
void setFlag(int flag) {q_flags |= flag; }
|
|
|
|
/// reset flags for query execution, currently only for exception handling
|
|
|
|
void resetFlag(int flag) {q_flags &= ~flag; }
|
2016-04-22 18:25:59 +01:00
|
|
|
/// first query
|
2015-02-09 01:52:10 +00:00
|
|
|
///
|
|
|
|
/// actually implemented by calling the next();
|
|
|
|
inline bool first() { return next(); }
|
|
|
|
/// ask for the next solution of the current query
|
|
|
|
/// same call for every solution
|
|
|
|
bool next();
|
2016-04-12 16:22:53 +01:00
|
|
|
/// does this query have open choice-points?
|
|
|
|
/// or is it deterministic?
|
|
|
|
bool deterministic();
|
2015-02-09 01:52:10 +00:00
|
|
|
/// represent the top-goal
|
2015-04-13 13:28:17 +01:00
|
|
|
const char *text();
|
2015-02-09 01:52:10 +00:00
|
|
|
/// remove alternatives in the current search space, and finish the current query
|
|
|
|
void cut();
|
|
|
|
/// finish the current query: undo all bindings.
|
|
|
|
void close();
|
|
|
|
/// query variables.
|
|
|
|
YAPListTerm namedVars();
|
2016-04-22 18:25:59 +01:00
|
|
|
///simple YAP Query;
|
|
|
|
/// just calls YAP and reports success or failure, Useful when we just
|
|
|
|
/// want things done, eg YAPCommand("load_files(library(lists), )")
|
|
|
|
inline bool command()
|
|
|
|
{
|
|
|
|
bool rc = next();
|
|
|
|
close();
|
|
|
|
return rc;
|
|
|
|
};
|
2015-02-09 01:52:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Java support
|
|
|
|
|
|
|
|
/// This class implements a callback Prolog-side. It will be inherited by the Java or Python
|
|
|
|
/// class that actually implements the callback.
|
|
|
|
class YAPCallback {
|
|
|
|
public:
|
2015-04-13 13:28:17 +01:00
|
|
|
virtual ~YAPCallback() { }
|
|
|
|
virtual void run() { LOG("callback"); }
|
2015-02-09 01:52:10 +00:00
|
|
|
virtual void run(char *s) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief YAP Engine: takes care of the execution environment
|
|
|
|
where we can go executing goals.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class YAPEngine {
|
|
|
|
private:
|
|
|
|
YAPCallback *_callback;
|
|
|
|
YAP_init_args init_args;
|
|
|
|
YAPError yerror;
|
|
|
|
public:
|
|
|
|
YAPEngine(char *savedState = (char *)NULL,
|
|
|
|
size_t stackSize = 0,
|
|
|
|
size_t trailSize = 0,
|
|
|
|
size_t maxStackSize = 0,
|
|
|
|
size_t maxTrailSize = 0,
|
|
|
|
char *libDir = (char *)NULL,
|
|
|
|
char *bootFile = (char *)NULL,
|
|
|
|
char *goal = (char *)NULL,
|
|
|
|
char *topLevel = (char *)NULL,
|
|
|
|
bool script = FALSE,
|
|
|
|
bool fastBoot = FALSE,
|
|
|
|
YAPCallback *callback=(YAPCallback *)NULL); /// construct a new engine, including aaccess to callbacks
|
2015-07-06 12:01:55 +01:00
|
|
|
/// kill engine
|
2015-02-09 01:52:10 +00:00
|
|
|
~YAPEngine() { delYAPCallback(); }
|
|
|
|
/// remove current callback
|
|
|
|
void delYAPCallback() { _callback = 0; }
|
|
|
|
/// set a new callback
|
|
|
|
void setYAPCallback(YAPCallback *cb) { delYAPCallback(); _callback = cb; }
|
|
|
|
/// execute the callback.
|
2015-03-23 07:26:03 +00:00
|
|
|
////void run() { if (_callback) _callback->run(); }
|
2015-02-09 01:52:10 +00:00
|
|
|
/// execute the callback with a text argument.
|
|
|
|
void run( char *s) { if (_callback) _callback->run(s); }
|
|
|
|
/// execute the callback with a text argument.
|
2015-07-06 12:01:55 +01:00
|
|
|
YAPError hasError( ) { return yerror.get(); }
|
2015-02-09 01:52:10 +00:00
|
|
|
/// build a query on the engine
|
2015-04-13 13:28:17 +01:00
|
|
|
YAPQuery *query( const char *s ) {
|
2015-04-15 11:21:15 +01:00
|
|
|
return new YAPQuery( s );
|
2015-04-13 13:28:17 +01:00
|
|
|
};
|
2015-02-09 01:52:10 +00:00
|
|
|
/// current module for the engine
|
|
|
|
YAPModule currentModule( ) { return YAPModule( ) ; }
|
2015-07-23 01:33:30 +01:00
|
|
|
/// current directory for the engine
|
|
|
|
const char *currentDir( ) {
|
|
|
|
char dir[1024];
|
|
|
|
std::string s = Yap_getcwd(dir, 1024-1);
|
|
|
|
return s.c_str();
|
|
|
|
}
|
2016-04-22 18:25:59 +01:00
|
|
|
/// report YAP version as a string
|
|
|
|
const char *version( ) {
|
|
|
|
std::string s = Yap_version();
|
|
|
|
return s.c_str();
|
|
|
|
}
|
2015-02-09 01:52:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* YAPQ_HH */
|