12
13
14:- module(tracesearch,
15 [
16 start_tracing/0,
17 currently_tracing/0,
18 stop_tracing/0,
19 clear_tracing/0,
20
21 add_node/1,
22
23 current_search_tree_depth/1,
24
25 search_tree_term/1,
26
27 search_tree_latex/1,
28 search_tree_latex/0,
29 search_tree_tikz/1,
30 search_tree_tikz/0,
31
32 search_tree_text/0,
33 search_tree_text/1
34 ]
35 ).
99:- use_module(library(drawtree)). 100:- reexport(library(drawtree)). 102
103:- nb_setval(trace_search_flag, 0). 104:- nb_setval(search_tree_depth, 0). 105:- nb_setval(search_tree_prefix, []).
113start_tracing :-
114 b_setval(trace_search_flag, 1),
115 b_setval(search_tree_depth, 0),
116 b_setval(search_tree_prefix, []).
122currently_tracing :-
123 catch(b_getval(trace_search_flag, 1), _, fail).
129stop_tracing :-
130 b_setval(trace_search_flag, 0).
136clear_tracing :-
137 nb_setval(trace_search_flag, 0),
138 nb_setval(search_tree_depth, 0),
139 nb_setval(search_tree_prefix, []).
148add_node(Term) :-
149 (b_getval(trace_search_flag, 1)
150 ->
151 152 with_output_to(atom(Atom), write(Term)),
153 b_getval(search_tree_depth, Depth),
154 nb_getval(search_tree_prefix, List),
155 L = [Depth-Atom | List],
156 nb_setval(search_tree_prefix, L), 157 D1 is Depth+1,
158 b_setval(search_tree_depth, D1) 159 ;
160 true).
167search_tree_term(Term):-
168 nb_getval(search_tree_prefix, List),
169 reverse(List, Prefix),
170 prefix_term(Prefix, Term, _Prefix).
171
173prefix_term([], true, []).
174prefix_term([Depth-Label | Prefix], Term, PrefixTail) :-
175 ((Prefix=[] ; (Prefix=[D1-_ |_], D1=<Depth))
176 ->
177 PrefixTail=Prefix,
178 Term=Label
179 ;
180 DepthSubterms is Depth+1,
181 prefix_subterms(Prefix, DepthSubterms, Subterms, PrefixTail),
182 Term =.. [Label | Subterms]).
183
185prefix_subterms([], _DepthTerm, [], []) :- !.
186prefix_subterms(Prefix, DepthTerm, Terms, PrefixTail) :-
187 Prefix = [Depth-_Label | _Tail],
188 (Depth < DepthTerm
189 ->
190 PrefixTail=Prefix,
191 Terms=[]
192 ;
193 Depth = DepthTerm
194 ->
195 prefix_term(Prefix, Term1, Prefix1),
196 prefix_subterms(Prefix1, DepthTerm, Terms2, PrefixTail),
197 Terms=[Term1 | Terms2]
198 ;
199 fail).
207search_tree_text(Output) :-
208 search_tree_term(Term),
209 term_to_text(Output, Term).
216search_tree_text :-
217 search_tree_text(current_output).
224search_tree_tikz(Output) :-
225 search_tree_term(Term),
226 term_to_tikz(Output, Term).
233search_tree_tikz :-
234 search_tree_tikz(current_output).
241search_tree_latex(Output) :-
242 search_tree_term(Term),
243 term_to_latex(Output, Term).
250search_tree_latex :-
251 search_tree_latex(current_output).
260current_search_tree_depth(Depth) :-
261 nb_getval(search_tree_depth, Depth)
Tracing of search trees and representation of the search tree by a term or a string for drawing it in LaTeX tikz.
General purpose tracing predicates for Prolog and representation of the search tree by a term.
Used in
library(clp)
of pack modeling for implementing a new option trace for labeling/2 predicate.This library uses global variables trace_search_flag, search_tree_depth and search_tree_prefix, which should not be manipulated.
Below is an example from
library(clp)
of pack modeling with new option trace added to labeling/2 predicate.*/