| 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|---|
| 2 | % |
|---|
| 3 | % Logtalk - Open source object-oriented logic programming language |
|---|
| 4 | % Release 2.33.3 |
|---|
| 5 | % |
|---|
| 6 | % Copyright (c) 1998-2008 Paulo Moura. All Rights Reserved. |
|---|
| 7 | % Logtalk is free software. You can redistribute it and/or modify |
|---|
| 8 | % it under the terms of the "Artistic License 2.0" as published by |
|---|
| 9 | % The Perl Foundation. Consult the "LICENSE.txt" file for details. |
|---|
| 10 | % |
|---|
| 11 | % |
|---|
| 12 | % a useful but fragile hack for SWI Prolog 5.6.x that allows setting the |
|---|
| 13 | % Logtalk top-level context to other object than the pseudo-object "user" |
|---|
| 14 | % |
|---|
| 15 | % this is useful mostly when debugging as all queries will be interpreted |
|---|
| 16 | % as being made within the current context (thus, plain Prolog calls must |
|---|
| 17 | % be made using the {}/1 control construct) |
|---|
| 18 | % |
|---|
| 19 | % usage is simple: call the predicate set_logtalk_context(Obj) to switch |
|---|
| 20 | % the context to "Obj"; call set_logtalk_context(user) to return to the |
|---|
| 21 | % normal Logtak/Prolog top-level |
|---|
| 22 | % |
|---|
| 23 | % as of Logtalk version 2.30.1 you may use in alternative the <</2 control |
|---|
| 24 | % construct, which provides a more robust solution for making queries in |
|---|
| 25 | % the context of objects other than "user" |
|---|
| 26 | % |
|---|
| 27 | % last updated: May 18, 2008 |
|---|
| 28 | % |
|---|
| 29 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|---|
| 30 | |
|---|
| 31 | :- system_module. |
|---|
| 32 | |
|---|
| 33 | :- dynamic('$lgt_current_context_'/2). |
|---|
| 34 | |
|---|
| 35 | '$lgt_current_context_'(user, _). |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | set_logtalk_context(Obj) :- |
|---|
| 39 | var(Obj), |
|---|
| 40 | throw(error(instantiation_error, set_logtalk_context(Obj))). |
|---|
| 41 | |
|---|
| 42 | set_logtalk_context(Obj) :- |
|---|
| 43 | \+ callable(Obj), |
|---|
| 44 | throw(error(type_error(object_identifier, Obj), set_logtalk_context(Obj))). |
|---|
| 45 | |
|---|
| 46 | set_logtalk_context(Obj) :- |
|---|
| 47 | \+ '$lgt_current_object_'(Obj, _, _, _, _, _, _, _), |
|---|
| 48 | throw(error(existence_error(object, Obj), set_logtalk_context(Obj))). |
|---|
| 49 | |
|---|
| 50 | set_logtalk_context(Obj) :- |
|---|
| 51 | catch('$lgt_set_logtalk_context'(Obj), Error, (writeq(Error), nl, fail)). |
|---|
| 52 | |
|---|
| 53 | '$lgt_set_logtalk_context'(user) :- |
|---|
| 54 | !, |
|---|
| 55 | retract('$lgt_current_context_'(_, HookRef)), |
|---|
| 56 | ( nonvar(HookRef) -> |
|---|
| 57 | erase(HookRef) |
|---|
| 58 | ; true |
|---|
| 59 | ), |
|---|
| 60 | assertz('$lgt_current_context_'(user, _)), |
|---|
| 61 | '$set_prompt'('?- '). |
|---|
| 62 | |
|---|
| 63 | '$lgt_set_logtalk_context'(Obj) :- |
|---|
| 64 | retract('$lgt_current_context_'(Old, _)), |
|---|
| 65 | ( Old == user -> |
|---|
| 66 | '$lgt_expand_query_hook'(Hook), |
|---|
| 67 | asserta(Hook, HookRef) |
|---|
| 68 | ; true |
|---|
| 69 | ), |
|---|
| 70 | assertz('$lgt_current_context_'(Obj, HookRef)), |
|---|
| 71 | term_to_atom(Obj, ObjAtom), |
|---|
| 72 | concat_atom(['[', ObjAtom, '] ?- '], Prompt), |
|---|
| 73 | '$set_prompt'(Prompt). |
|---|
| 74 | |
|---|
| 75 | :- user:dynamic(expand_query/4). |
|---|
| 76 | :- user:multifile(expand_query/4). |
|---|
| 77 | |
|---|
| 78 | '$lgt_expand_query_hook'((user:expand_query(Query, Expanded, Bindings, Bindings) :- |
|---|
| 79 | '$lgt_current_context_'(Obj, _), |
|---|
| 80 | '$lgt_current_object_'(Obj, Prefix, _, _, _, _, _, _), |
|---|
| 81 | '$lgt_ctx_ctx'(Ctx, _, Obj, Obj, Obj, Prefix, [], _), |
|---|
| 82 | '$lgt_tr_body'(Query, TQuery, DQuery, Ctx), |
|---|
| 83 | ( '$lgt_dbg_debugging_', '$lgt_debugging_'(Obj) -> |
|---|
| 84 | Expanded = catch(DQuery, Error, '$lgt_runtime_error_handler'(Error)) |
|---|
| 85 | ; Expanded = catch(TQuery, Error, '$lgt_runtime_error_handler'(Error)) |
|---|
| 86 | ))). |
|---|