| 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|---|
| 2 | % |
|---|
| 3 | % Logtalk - Open source object-oriented logic programming language |
|---|
| 4 | % Release 2.31.1 |
|---|
| 5 | % |
|---|
| 6 | % a useful but fragile hack for SWI Prolog 5.6.x that allows setting the |
|---|
| 7 | % Logtalk top-level context to other object than the pseudo-object "user" |
|---|
| 8 | % |
|---|
| 9 | % this is useful mostly when debugging as all queries will be interpreted |
|---|
| 10 | % as being made within the current context (thus, plain Prolog calls must |
|---|
| 11 | % be made using the {}/1 control construct) |
|---|
| 12 | % |
|---|
| 13 | % usage is simple: call the predicate set_logtalk_context(Obj) to switch |
|---|
| 14 | % the context to "Obj"; call set_logtalk_context(user) to return to the |
|---|
| 15 | % normal Logtak/Prolog top-level |
|---|
| 16 | % |
|---|
| 17 | % last updated: April 16, 2007 |
|---|
| 18 | % |
|---|
| 19 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|---|
| 20 | |
|---|
| 21 | :- system_module. |
|---|
| 22 | |
|---|
| 23 | :- dynamic('$lgt_current_context_'/2). |
|---|
| 24 | |
|---|
| 25 | '$lgt_current_context_'(user, _). |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | set_logtalk_context(Obj) :- |
|---|
| 29 | var(Obj), |
|---|
| 30 | throw(error(instantiation_error, set_logtalk_context(Obj))). |
|---|
| 31 | |
|---|
| 32 | set_logtalk_context(Obj) :- |
|---|
| 33 | \+ callable(Obj), |
|---|
| 34 | throw(error(type_error(object_identifier, Obj), set_logtalk_context(Obj))). |
|---|
| 35 | |
|---|
| 36 | set_logtalk_context(Obj) :- |
|---|
| 37 | \+ '$lgt_current_object_'(Obj, _, _, _, _, _, _, _), |
|---|
| 38 | throw(error(existence_error(object, Obj), set_logtalk_context(Obj))). |
|---|
| 39 | |
|---|
| 40 | set_logtalk_context(Obj) :- |
|---|
| 41 | catch('$lgt_set_logtalk_context'(Obj), Error, (writeq(Error), nl, fail)). |
|---|
| 42 | |
|---|
| 43 | '$lgt_set_logtalk_context'(user) :- |
|---|
| 44 | !, |
|---|
| 45 | retract('$lgt_current_context_'(_, HookRef)), |
|---|
| 46 | ( nonvar(HookRef) -> |
|---|
| 47 | erase(HookRef) |
|---|
| 48 | ; true |
|---|
| 49 | ), |
|---|
| 50 | assertz('$lgt_current_context_'(user, _)), |
|---|
| 51 | '$set_prompt'('?- '). |
|---|
| 52 | |
|---|
| 53 | '$lgt_set_logtalk_context'(Obj) :- |
|---|
| 54 | retract('$lgt_current_context_'(Old, _)), |
|---|
| 55 | ( Old == user -> |
|---|
| 56 | '$lgt_expand_query_hook'(Hook), |
|---|
| 57 | asserta(Hook, HookRef) |
|---|
| 58 | ; true |
|---|
| 59 | ), |
|---|
| 60 | assertz('$lgt_current_context_'(Obj, HookRef)), |
|---|
| 61 | term_to_atom(Obj, ObjAtom), |
|---|
| 62 | concat_atom(['[', ObjAtom, '] ?- '], Prompt), |
|---|
| 63 | '$set_prompt'(Prompt). |
|---|
| 64 | |
|---|
| 65 | :- user:dynamic(expand_query/4). |
|---|
| 66 | :- user:multifile(expand_query/4). |
|---|
| 67 | |
|---|
| 68 | '$lgt_expand_query_hook'((user:expand_query(Query, Expanded, Bindings, Bindings) :- |
|---|
| 69 | '$lgt_current_context_'(Obj, _), |
|---|
| 70 | '$lgt_current_object_'(Obj, Prefix, _, _, _, _, _, _), |
|---|
| 71 | '$lgt_ctx_ctx'(Ctx, _, Obj, Obj, Obj, Prefix, [], _), |
|---|
| 72 | '$lgt_tr_body'(Query, TQuery, DQuery, Ctx), |
|---|
| 73 | ( '$lgt_dbg_debugging_', '$lgt_debugging_'(Obj) -> |
|---|
| 74 | Expanded = catch(DQuery, Error, '$lgt_runtime_error_handler'(Error)) |
|---|
| 75 | ; Expanded = catch(TQuery, Error, '$lgt_runtime_error_handler'(Error)) |
|---|
| 76 | ))). |
|---|