YAP 7.1.0
yapq.hh
Go to the documentation of this file.
1
19#ifndef YAPQ_HH
20#define YAPQ_HH 1
21
22class X_API YAPPredicate;
23
28#if __ANDROID__
29
30#endif
31
38class X_API YAPQuery : public YAPPredicate {
39 bool q_open;
40 int q_state;
41 // yhandle_t q_handles;
42 int q_flags;
44 YAPPairTerm *names;
45 Term goal;
46 CELL *nts;
47 // temporaries
48 YAPError *e;
49
50 inline void setNext() { // oq = LOCAL_execution;
51 // LOCAL_execution = this;
52 q_open = true;
53 q_state = 0;
54 q_flags = true; // PL_Q_PASS_EXCEPTION;
55
56 q_h.p = P;
57 q_h.cp = CP;
58 // make sure this is safe
59 q_h.CurSlot = LOCAL_CurSlot;
60 };
61
62 void openQuery();
63
64 PredEntry *rewriteUndefQuery();
65
66public:
67 YAPQuery() {
68 goal = TermTrue;
69 openQuery();
70 };
71 inline ~YAPQuery() { close(); }
88 YAPQuery(YAPFunctor f, YAPTerm mod, Term t[]);
93 // YAPQuery(YAPFunctor f, YAPTerm t[]);
99 inline YAPQuery(const char *s) : YAPPredicate(s, goal, names, (nts = &ARG1)) {
100 __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "got game %ld",
101 LOCAL_CurSlot);
102
103 openQuery();
104 };
105 // inline YAPQuery() : YAPPredicate(s, tgoal, tnames)
106 // {
107 // __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "got game %ld",
108 // if (!ap)
109 // return;
110 // __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "%s", vnames.text());
111 // goal = YAPTerm(tgoal);
112 // names = YAPPairTerm(tnames);
113 // openQuery(tgoal);
114 // };
120 YAPQuery(YAPTerm t) : YAPPredicate((goal = t.term()), (nts = &ARG1)) {
121 BACKUP_MACHINE_REGS();
122 openQuery();
123 names = new YAPPairTerm(TermNil);
124 RECOVER_MACHINE_REGS();
125 }
127 void setFlag(int flag) { q_flags |= flag; }
129 void resetFlag(int flag) { q_flags &= ~flag; }
133 inline bool first() { return next(); }
136 bool next();
139 bool deterministic();
141 const char *text();
145 void close();
147 void cut();
148 Term namedVars() { return names->term(); };
149 YAPPairTerm *namedVarTerms() { return names; };
151 std::vector<Term> namedVarsVector() { return names->listToArray(); };
153 YAPTerm getTerm(yhandle_t t);
157 inline bool command() {
158 bool rc = next();
159 close();
160 return rc;
161 };
162};
163
164
165
166
170class X_API YAPCallback {
171public:
172 virtual ~YAPCallback() {}
173 virtual void run() { LOG("callback"); }
174 virtual void run(char *s) {}
175};
176
179
180public:
181 YAPEngineArgs() {
182 memset(this,0,sizeof(YAPEngineArgs));
183 // const std::string *s = new std::string("startup.yss");
184 Embedded = true;
185 install = false;
186 Yap_InitDefaults(&this->start, nullptr, 0, nullptr);
187#if YAP_PYTHON
188 Embedded = true;
189 python_in_python = Py_IsInitialized();
190#endif
191#if __ANDROID__
192#endif
193 };
194
195 inline void setEmbedded(bool fl) { Embedded = fl; };
196
197 inline bool getEmbedded() { return Embedded; };
198
199 inline void setStackSize(bool fl) { StackSize = fl; };
200
201 inline bool getStackSize() { return StackSize; };
202
203 inline void setTrailSize(bool fl) { TrailSize = fl; };
204
205 inline bool getTrailSize() { return TrailSize; };
206
207 inline bool getMStackSize() { return StackSize; };
208
209 inline void setMaxTrailSize(bool fl) { MaxTrailSize = fl; };
210
211 inline bool getMaxTrailSize() { return MaxTrailSize; };
212
213 inline void createSavedState(bool fl) { install = fl; };
214
215 inline bool creatingSavedState() { return install; };
216
217 inline void setPLDIR(const char *fl) {
218 std::string *s = new std::string(fl);
219 LIBDIR = s->c_str();
220 };
221
222 inline const char *getPLDIR() { return PLDIR; };
223
224 inline void setINPUT_STARTUP(const char *fl) {
225 std::string *s = new std::string(fl);
226 INPUT_STARTUP = s->c_str();
227 };
228
229 inline const char *getINPUT_STARTUP() { return INPUT_STARTUP; };
230
231 inline void setOUTPUT_STARTUP(const char *fl) {
232 std::string *s = new std::string(fl);
233 OUTPUT_STARTUP = s->c_str();
234 };
235
236 inline void setOUTPUT_RESTORE(const char *fl) {
237 std::string *s = new std::string(fl);
238 OUTPUT_STARTUP = s->c_str();
239 };
240
241 inline const char *getOUTPUT_STARTUP() { return OUTPUT_STARTUP; };
242
243 inline void setSOURCEBOOT(const char *fl) {
244 std::string *s = new std::string(fl);
245 SOURCEBOOT = s->c_str();
246 };
247
248 inline const char *getSOURCEBOOT() { return SOURCEBOOT; };
249
250 inline void setPrologBOOTSTRAP(const char *fl) {
251 std::string *s = new std::string(fl);
252 BOOTSTRAP = s->c_str();
253 };
254
255 inline const char *getBOOTSTRAP() { return BOOTSTRAP; };
256
257 inline void setPrologGoal(const char *fl) {
258 std::string *s = new std::string(fl);
259 PrologGoal = s->c_str();
260
261 }
262
263 inline const char *getPrologGoal() { return PrologGoal; };
264
265 inline void setPrologTopLevelGoal(const char *fl) {
266 std::string *s = new std::string(fl);
267 PrologTopLevelGoal = s->c_str() ;
268 };
269
270 inline const char *getPrologTopLevelGoal() { return PrologTopLevelGoal; };
271
272 inline void setHaltAfterBoot(bool fl) { HaltAfterBoot = fl; };
273
274 inline bool getHaltAfterBoot() { return HaltAfterBoot; };
275
276 inline void setFastBoot(bool fl) { FastBoot = fl; };
277
278 inline bool getFastBoot() { return FastBoot; };
279
280 inline void setArgc(int fl) { Argc = fl; };
281
282 inline int getArgc() { return Argc; };
283
284 inline void setArgv(char **fl) { Argv = fl; };
285
286 inline char **getArgv() { return Argv; };
287
288 inline void setBOOTDIR(const char *fl) {
289 std::string *s = new std::string(fl);
290 BOOTDIR = s->c_str() ;
291 }
292
293 inline const char *getBOOTDIR() { return BOOTDIR; };
294
295 inline const char *getBOOTFILE() { return BOOTSTRAP; };
296
297 inline void setBOOTFILE(const char *fl) {
298 std::string *s = new std::string(fl);
299 BOOTSTRAP = s->c_str() ;
300
301 }
302
303 inline void setROOTDIR(const char *fl) {
304 std::string *s = new std::string(fl);
305 ROOTDIR = s->c_str() ;
306
307 }
308
309};
310
318private:
319 YAPEngineArgs *engine_args;
320 YAPCallback *_callback;
321 YAPError yerror;
322 void doInit(YAP_file_type_t BootMode, YAPEngineArgs *cargs);
323 YAPError e;
324 PredEntry *rewriteUndefEngineQuery(PredEntry *ap, Term &t, Term tmod);
325
326public:
329 engine_args = cargs;
330 // doInit(cargs->boot_file_type);
331 __android_log_print(
332 ANDROID_LOG_INFO, "YAPDroid", "start engine ");
333#ifdef __ANDROID__
334 doInit(YAP_PL, cargs);
335
336#else
337 doInit(YAP_QLY, cargs);
338#endif
339 };
341 YAPEngine(int argc, char *argv[],
342 YAPCallback *callback = (YAPCallback *)NULL);
346 void delYAPCallback() { _callback = 0; };
350 _callback = cb;
351 };
355 void run(char *s) {
356 if (_callback)
357 _callback->run(s);
358 }
360 void close() { Yap_exit(0); }
361
363 bool hasError() { return LOCAL_Error_TYPE != YAP_NO_ERROR; }
365 YAPQuery *query(const char *s) { return new YAPQuery(s); };
367 YAPQuery *query(YAPTerm t) { return new YAPQuery(t); };
369 YAPQuery *qt(Term t) { return new YAPQuery(YAPTerm(t)); };
371 Term Yap_CurrentModule() { return CurrentModule; }
373 inline YAPTerm getTerm(yhandle_t h) { return YAPTerm(h); }
375 bool call(YAPPredicate ap, YAPTerm ts[]);
377 bool goal(YAPTerm Yt, YAPModule module, bool release = false) {
378 return mgoal(Yt.term(), module.term(), release);
379 };
385 bool mgoal(Term t, Term tmod, bool release = false);
387
388 bool goal(YAPTerm t, bool release = false) { return goal(t.term(), release); }
389 bool goal(Term t, bool release = false) {
390 return mgoal(t, Yap_CurrentModule(), release);
391 }
393 void reSet();
395 // for last execution
396 void release();
397
398 const char *currentDir() {
399 char dir[1024];
400 std::string *s = new std::string(Yap_getcwd(dir, 1024 - 1));
401 return s->c_str();
402 };
404 const char *version() {
405 std::string *s = new std::string(Yap_version());
406 return s->c_str();
407 };
408 //> call a deterninistic predicate: the user will construct aterm of
409 //> arity N-1. YAP adds an extra variable which will have the
410 //> output.
411 YAPTerm funCall(YAPTerm t) { return YAPTerm(fun(t.term())); };
412 Term fun(Term t);
413 //Term fun(YAPTerm t) { return fun(t.term()); };
414 //> set a StringFlag, usually a path
415 //>
416 bool setStringFlag(std::string arg, std::string path) {
417 return Yap_set_flag(MkAtomTerm(Yap_LookupAtom(arg.data())),
418 MkAtomTerm(Yap_LookupAtom(path.data())));
419 };
420
421 Term top_level(std::string s);
422 Term next_answer(YAPQuery *&Q);
423};
424
425#endif /* YAPQ_HH */
426
@ argv
read-only atom, it describes the list with all arguments received by YAP at boot
Definition: YapGFlagInfo.h:89
YAPModule A YAPModule describes a bare module, which in YAP is just a name.
Definition: yapdb.hh:46
Predicates.
Definition: yapdb.hh:83
take information on a Prolog error:
Definition: yapie.hh:31
YAPFunctor represents Prolog functors Name/Arity.
Definition: yapa.hh:125
YAPEngine(YAPEngineArgs *cargs)
construct a new engine; may use a variable number of arguments
Definition: yapq.hh:328
Term top_level(std::string s)
Definition: yapi.cpp:1261
const char * version()
report YAP version as a string
Definition: yapq.hh:404
bool command()
simple YAP Query; just calls YAP and reports success or failure, Useful when we just want things done...
Definition: yapq.hh:157
void reSet()
reset Prolog state
Definition: yapi.cpp:1247
std::vector< Term > namedVarsVector()
query variables, but copied out
Definition: yapq.hh:151
Term Yap_CurrentModule()
current module for the engine
Definition: yapq.hh:371
void setFlag(int flag)
set flags for query execution, currently only for exception handling
Definition: yapq.hh:127
void release()
assune that there are no stack pointers, just release memory
Definition: yapi.cpp:755
~YAPEngine()
kill engine
Definition: yapq.hh:344
bool goal(YAPTerm Yt, YAPModule module, bool release=false)
current directory for the engine
Definition: yapq.hh:377
YAPQuery * query(const char *s)
build a query on the engine
Definition: yapq.hh:365
Term next_answer(YAPQuery *&Q)
Definition: yapi.cpp:1282
void setYAPCallback(YAPCallback *cb)
set a new callback
Definition: yapq.hh:348
YAPQuery * query(YAPTerm t)
build a query from a term
Definition: yapq.hh:367
void close()
stop yap
Definition: yapq.hh:360
void delYAPCallback()
remove current callback
Definition: yapq.hh:346
YAPTerm getTerm(yhandle_t h)
given a handle, fetch a term from the engine
Definition: yapq.hh:373
bool call(YAPPredicate ap, YAPTerm ts[])
current directory for the engine
Definition: yapi.cpp:658
YAPQuery(YAPTerm t)
string constructor with just an atom
Definition: yapq.hh:120
bool goal(YAPTerm t, bool release=false)
current directory for the engine
Definition: yapq.hh:388
void resetFlag(int flag)
reset flags for query execution, currently only for exception handling
Definition: yapq.hh:129
YAPQuery(const char *s)
functor/term constructor,
Definition: yapq.hh:99
YAPQuery * qt(Term t)
build a query from a Prolog term (internal)
Definition: yapq.hh:369
bool mgoal(Term t, Term tmod, bool release=false)
ru1n a goal in a module
Definition: yapi.cpp:687
void run(char *s)
execute the callback
Definition: yapq.hh:355
bool hasError()
execute the callback with a text argument
Definition: yapq.hh:363
bool first()
first query
Definition: yapq.hh:133
This class implements a callback Prolog-side.
Definition: yapq.hh:170
YAP Engine: takes care of the execution environment where we can go executing goals.
Definition: yapq.hh:317
Queries and engines.
Definition: yapq.hh:38
Setup all arguments to a new engine.
Definition: yapq.hh:178
List Constructor Term.
Definition: yapt.hh:448
Generic Prolog Term.
Definition: yapt.hh:42
Definition: Yatom.h:544