1 % (c) 2009-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
2 % Heinrich Heine Universitaet Duesseldorf
3 % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html)
4
5 :- module(visualize_graph,[tcltk_print_states_for_dot/1, tcltk_print_states_for_dot/2,
6 tcltk_print_states_for_dot_sfdp/1, tcltk_print_states_for_dot_sfdp/2,
7 tcltk_print_neighbourhood_states_for_dot/2,
8 tcltk_print_current_state_for_dot/1,
9 tcltk_print_shortest_trace_to_current_state_for_dot/1,
10 %tcltk_print_trace_to_current_state_for_dot/1, % removed
11 tcltk_print_current_state_as_graph_for_dot/1, tcltk_print_state_as_graph_for_dot/2,
12 tcltk_print_history_to_current_state_for_dot/1,
13 tcltk_print_history_to_current_state_with_neighbors_for_dot/1,
14 % tcltk_print_transition_diagram_for_variable_or_expr/2, % moved to state_space_reduction
15 tcltk_print_definitions_as_graph_for_dot/1,
16 tcltk_print_predicate_dependency_as_graph_for_dot/2,
17 tcltk_print_csp_process_states_for_dot/2,
18 print_graph_header/1,
19 print_graph_footer/0,
20
21 convert_state_to_string/2]).
22
23
24 :- use_module(probsrc(module_information)).
25 :- module_info(group,dot).
26 :- module_info(description,'This module generates various dot graphs (state space, single state, transition diagram,...).').
27
28 :- use_module(library(lists)).
29 :- use_module(probsrc(preferences)).
30
31 :- use_module(probsrc(debug)).
32 :- use_module(probsrc(translate)).
33 :- use_module(probsrc(bmachine),[b_is_constant/1]).
34
35 :- use_module(probsrc(state_space)).
36 :- use_module(dotsrc(dot_graph_generator),[translate_bvalue_to_colour/2, translate_int_col/2, legend_top_margin/1]).
37 :- use_module(probsrc(state_space_exploration_modes),[compute_heuristic_function_for_state_id/2]).
38
39 /* --------------------------------------------------- */
40 /* MAIN ENTRY POINTS FOR TCL */
41 /* --------------------------------------------------- */
42
43
44 % print full state space for dot:
45
46 :- use_module(probsrc(state_space),[get_state_space_stats/3]).
47 % a version for large graphs; where we automatically turn off some details
48 tcltk_print_states_for_dot_sfdp(F) :-
49 tcltk_print_states_for_dot_sfdp(F,colour_transitions).
50 tcltk_print_states_for_dot_sfdp(F,ColourTransitions) :-
51 temporary_set_preference(dot_print_node_info,false,CHNG1),
52 temporary_set_preference(dot_print_node_properties,false,CHNG2),
53 temporary_set_preference(dot_print_self_loops,false,CHNG3),
54 temporary_set_preference(dot_fill_normal_nodes,true,CHNG4),
55 get_state_space_stats(NrN,NrT,_),
56 (NrN>200 -> NodeOpts=[no_tooltip_and_label] % tooltips slows down export
57 ; NodeOpts=[]),
58 TransOpts = [dot_print_action_arguments/false],
59 (NrT>1000 -> temporary_set_preference(dot_print_edge_labels,false,CHNG6) ; true),
60 call_cleanup(tcltk_print_states_for_dot(F,ColourTransitions,NodeOpts,TransOpts),
61 (preferences:reset_temporary_preference(dot_print_node_info,CHNG1),
62 preferences:reset_temporary_preference(dot_print_node_properties,CHNG2),
63 preferences:reset_temporary_preference(dot_print_self_loops,CHNG3),
64 preferences:reset_temporary_preference(dot_fill_normal_nodes,CHNG4),
65 (NrT>1000 -> preferences:reset_temporary_preference(dot_print_edge_labels,CHNG6) ; true))).
66
67 :- use_module(probsrc(tools),[start_ms_timer/1, stop_ms_timer_with_silent_msg/2]).
68 tcltk_print_states_for_dot(F) :- tcltk_print_states_for_dot(F,no_colour_for_transitions,[],[]).
69 tcltk_print_states_for_dot(F,C) :- tcltk_print_states_for_dot(F,C,[],[]).
70 tcltk_print_states_for_dot(F,ColourTransitions,NodeOpts,TransOpts) :-
71 get_state_space_stats(NrNodes,NrTransitions,_ProcessedNodes),
72 format('Writing ~w states and ~w transitions to dot file ~w~n',[NrNodes,NrTransitions,F]), start_ms_timer(T),
73 tell(F),
74 call_cleanup((tcltk_print_states_for_dot_aux(ColourTransitions,NodeOpts,TransOpts) -> true ; true),told),
75 stop_ms_timer_with_silent_msg(T,'saving dot file').
76
77 tcltk_print_states_for_dot_aux(ColourTransitions,NodeOptions,TransOptions) :-
78 reset_label_nr(ColourTransitions),
79 print_graph_header(visited_states),
80 ? visited_expression_id(NodeID),
81 write_dot_node_information(NodeID,NodeOptions),
82 write_dot_edge_information(NodeID,_,TransOptions),
83 fail.
84 tcltk_print_states_for_dot_aux(_ColourTransitions,_,_) :-
85 print_graph_footer.
86
87
88 tcltk_print_neighbourhood_states_for_dot(Dist,F) :-
89 retractall(unfiltered_state(_,_)),
90 current_state_id(CurID),
91 (dfs(CurID,Dist) -> true ; true), % mark neighbours until distance Dist
92 tell(F),(tcltk_print_unfiltered_states_for_dot(Dist,[dot_print_action_arguments/true]) -> true ; true),told.
93
94 :- dynamic unfiltered_state/2.
95 dfs(ID,DistRemaining) :- unfiltered_state(ID,D1),
96 (D1>=DistRemaining -> ! % we already found a shorter path
97 ; retract(unfiltered_state(ID,D1))),fail.
98 dfs(ID,DistRemaining) :- assertz(unfiltered_state(ID,DistRemaining)),
99 DistRemaining>0,
100 D1 is DistRemaining-1,
101 any_transition(ID,_,NodeID),
102 dfs(NodeID,D1).
103
104 tcltk_print_unfiltered_states_for_dot(MaxDist,TransOpts) :- reset_label_nr,
105 print_graph_header_with_layout(neighbourhood(MaxDist),circo),
106 unfiltered_state(NodeID,Dist),
107 (Dist = 0 % we have reached the limit of the neighbourhood
108 -> (not_all_transitions_added(NodeID)
109 -> write_dot_node_information(NodeID,[]) % open node, will be colored
110 ; write_dot_node_information(NodeID,[color/lightgray])
111 )
112 ; write_dot_node_information(NodeID,[pendwidth/Dist]),
113 write_dot_edge_information(NodeID,_,TransOpts)
114 ),
115 fail.
116 tcltk_print_unfiltered_states_for_dot(_,_TransOpts) :-
117 print_graph_footer.
118
119 % --------------------------
120 tcltk_print_csp_process_states_for_dot(F,ProcID) :-
121 tell(F),(tcltk_print_csp_process_states_for_dot_root(ProcID) -> true ; true),told.
122
123 tcltk_print_csp_process_states_for_dot_root(ProcID) :-
124 reset_label_nr,
125 print_graph_header(csp_process),
126 write_dot_node_information(root,[]),
127 write_dot_edge_information(root,ProcID,[]),
128 tcltk_print_csp_process_states_for_dot.
129
130 :- use_module(extrasrc(refinement_checker),[dvisited/1]).
131 tcltk_print_csp_process_states_for_dot :-
132 dvisited(NodeID),
133 write_dot_node_information(NodeID,[]),
134 write_dot_edge_information(NodeID,_,[]),
135 fail.
136 tcltk_print_csp_process_states_for_dot :-
137 print_graph_footer.
138
139 % --------------------------
140
141 % just print the current state in the state space, not showing internal structure as graph!
142 tcltk_print_current_state_for_dot(F) :-
143 get_preference(dot_state_space_depth_limit,Dist),
144 tcltk_print_neighbourhood_states_for_dot(Dist,F).
145
146 :- use_module(probsrc(tcltk_interface),[find_shortest_trace_to_node/4]).
147 tcltk_print_shortest_trace_to_current_state_for_dot(F) :-
148 tell(F), (tcltk_print_shortest_trace_to_current_state_for_dot -> true ; true),told.
149 tcltk_print_shortest_trace_to_current_state_for_dot :-
150 reset_label_nr,
151 print_graph_header(shortest_trace_to_current_state),
152 current_state_id(CurID),
153 find_shortest_trace_to_node(root,CurID,_,StateIDList),
154 print_trace_to_current_state(StateIDList),
155 print_graph_footer.
156
157
158 tcltk_print_history_to_current_state_for_dot(F) :-
159 tell(F), (tcltk_print_history_to_current_state_for_dot -> true ; true),told.
160 tcltk_print_history_to_current_state_for_dot :-
161 reset_label_nr,
162 print_graph_header(history),
163 current_state_id(CurID),
164 history(H),
165 reverse([CurID|H],IDList),
166 print_trace_to_current_state(IDList),
167 print_graph_footer.
168
169 % called for fairness formulas (currently only in CSP refinement checking mode)
170 tcltk_print_history_to_current_state_with_neighbors_for_dot(F) :-
171 tell(F), (tcltk_print_history_to_current_state_with_neighbors_for_dot -> true ; true),told.
172 tcltk_print_history_to_current_state_with_neighbors_for_dot :-
173 reset_label_nr,
174 print_graph_header(history),
175 current_state_id(CurID),
176 history(H),
177 reverse([CurID|H],IDList),
178 print_trace_with_neighbors_to_current_state(IDList),
179 print_graph_footer.
180
181 /* ---------------------------------------------------------------------- */
182
183
184 :- use_module(dotsrc(dot_graph_generator), [gen_dot_graph/5, dot_no_same_rank/1]).
185
186 % this predicate is currently not used anywhere, print_cstate_graph is used instead
187 tcltk_print_current_state_as_graph_for_dot(File) :-
188 current_expression(_CurID,State),
189 tcltk_print_state_as_graph_for_dot(State,File).
190 tcltk_print_state_as_graph_for_dot(State,File) :-
191 %print(gen_graph(State)),nl,
192 gen_dot_graph(File,dot_state_node,dot_state_trans(State),dot_no_same_rank,dot_subgraph).
193
194 :- use_module(probsrc(b_global_sets),[b_global_set/1,is_b_global_constant/3,all_elements_of_type/2]).
195 dot_state_node(ID,SET,Desc,box,none,Color) :-
196 b_global_set(SET), Desc = ID,
197 if(is_b_global_constant(SET,_Nr,Cst),
198 (Color=red,ID = Cst),
199 (Color=blue,
200 all_elements_of_type(SET,AllVals),
201 member(X,AllVals),
202 translate_bvalue(X,ID)
203 )
204 ).
205 dot_state_node(root,none,'','Mdiamond',none,green).
206
207 dot_subgraph(SubGraph,filled,lightgray) :-
208 b_global_set(SubGraph).
209
210 % old version:
211 %dot_same_rank(TVals) :-
212 % b_global_set(SET),all_elements_of_type(SET,AllVals),translate_values(AllVals,TVals).
213
214 :- use_module(probsrc(store),[get_id_value/3]).
215 dot_state_trans(State,NodeID,Label,SuccNodeID,Color,Style) :- Style=solid,
216 get_id_value(VarOrConstant,State,VarValue),
217 dot_state_trans_var(VarValue,NodeID,SuccNodeID,VarOrConstant,Label),
218 (b_is_constant(VarOrConstant) -> Color=blue ; Color=black).
219
220 :- use_module(probsrc(gensym)).
221
222 dot_state_trans_var(VarValue,NodeID,SuccNodeID,Var,Var) :-
223 dot_state_basic_pair(VarValue,X1,X2), /* we have a relation or a pair */
224 dot_state_basic_type(X1), El1=X1,
225 dot_state_basic_type(X2), El2=X2, /* in case it is a function: we can also accept El2 to be dot_state_basic_type_or_element */
226 translate_bvalue(El1,NodeID),
227 translate_bvalue(El2,SuccNodeID).
228 dot_state_trans_var(VarValue,NodeID,SuccNodeID,Var,Var) :-
229 dot_state_basic_type_or_element(VarValue,V),
230 NodeID = root,
231 translate_bvalue(V,SuccNodeID).
232 dot_state_trans_var(VarValue,NodeID,SuccNodeID,Var,Label) :-
233 ( (member(X,VarValue),Kind=el)
234 ; (VarValue=(X,_),Kind=prj1)
235 ; (VarValue=(_,X),Kind=prj2)
236 ),
237 \+ directly_representable_element(X),
238 gensym(Var,IntNode),
239 ((NodeID = root, SuccNodeID = IntNode, Label=Var)
240 ;
241 dot_state_trans_internal(IntNode,Kind,X,NodeID,SuccNodeID,Label)
242 ).
243
244
245 directly_representable_element(X) :- dot_state_basic_type(X).
246 directly_representable_element((X1,X2)) :-
247 dot_state_basic_type(X1),
248 dot_state_basic_type(X2).
249
250 dot_state_basic_type(fd(_,_)).
251 dot_state_basic_type(int(_)).
252 dot_state_basic_type(pred_false /* bool_false */).
253 dot_state_basic_type(pred_true /* bool_true */).
254 dot_state_basic_type(string(_)).
255
256 dot_state_basic_type_or_element(X,X) :- dot_state_basic_type(X).
257 dot_state_basic_type_or_element(S,X) :- member(X,S), dot_state_basic_type(X).
258 dot_state_basic_type_or_element([],empty_set).
259
260 :- use_module(library(avl),[avl_member/2]).
261 dot_state_basic_pair((X1,X2),X1,X2).
262 dot_state_basic_pair([H|T],X1,X2) :- member((X1,X2),[H|T]).
263 dot_state_basic_pair(avl_set(A),X1,X2) :- avl_member((X1,X2),A).
264
265 dot_state_trans_internal(IntNode,Kind,VarValue,NodeID,SuccNodeID,Label) :-
266 dot_state_basic_type_or_element(VarValue,V),
267 translate_bvalue(V,SuccNodeID),
268 NodeID = IntNode, Label=Kind.
269 dot_state_trans_internal(IntNode,Kind,VarValue,NodeID,SuccNodeID,Label) :-
270 dot_state_basic_pair(VarValue,X1,X2), /* we have a relation or a pair */
271 dot_state_basic_type_or_element(X1,El1),
272 dot_state_basic_type_or_element(X2,El2),
273 NodeID = IntNode,
274 ( (translate_bvalue(El1,SuccNodeID), Label = prj1(Kind))
275 ;(translate_bvalue(El2,SuccNodeID), Label = prj2(Kind))
276 ).
277 /* MISSING: recursion */
278
279 /* ---------------------------------------------------------------------- */
280
281
282 print_graph_header(Type) :-
283 reset_legend,
284 format('digraph ~w {~n',[Type]),
285 % print('graph [orientation=landscape, page="8.5, 11",ratio=fill,size="7.5,10"];'),nl. % old default layout
286 % print('graph [page="8.5, 11",ratio=fill,size="7.5,10"];'),nl.
287 (get_preference(dot_horizontal_layout,true) -> RDir=', rankdir=LR'; RDir=''), % default is TB
288 %format('graph [nodesep=1.5, ranksep=1.5~w];~n',[RDir]).
289 format('graph [fontname="Helvetica", labelloc=t, nodesep=0.35, ranksep=0.45~w];~n',[RDir]),
290 print_graph_title(Type),
291 print_node_and_edge_defaults.
292
293 % Layout = sdfp, fdp, twopi, dot, circo, ...
294 print_graph_header_with_layout(Type,Layout) :-
295 reset_legend,
296 functor(Type,FType,_), format('digraph ~w {~n',[FType]),
297 (get_preference(dot_horizontal_layout,true) -> RDir=', rankdir=LR'; RDir=''), % default is TB
298 format('graph [fontname="Helvetica", labelloc=t, nodesep=1.5, ranksep=1.5, layout=~w~w];~n',[Layout,RDir]),
299 % other possible options for graph: splines=true overlap=false ?
300 print_graph_title(Type),
301 print_node_and_edge_defaults.
302
303 % print a title of the form "MachineName - state space" at the top of the graph
304 % (enabled by preference dot_print_graph_title)
305 :- use_module(probsrc(specfile),[currently_opened_specification_name/1]).
306 print_graph_title(Type) :-
307 preference(dot_print_graph_title,true),
308 catch(currently_opened_specification_name(Name),_,fail), !,
309 graph_type_description(Type,Desc),
310 ajoin([Name,' - ',Desc],Title),
311 write('graph [label="'), print_escaped_for_dot(Title), get_preference(dot_title_font_size,TFS), format('", fontsize=~w];~n',[TFS]).
312 print_graph_title(_).
313
314 graph_type_description(visited_states,'state space') :- !.
315 graph_type_description(neighbourhood(Dist),Title) :- !,
316 ajoin(['neighbourhood (',Dist,') of current state'],Title).
317 graph_type_description(current_state,'current state and successors') :- !.
318 graph_type_description(shortest_trace_to_current_state,'shortest trace to current state') :- !.
319 graph_type_description(history,'history') :- !.
320 graph_type_description(csp_process,'CSP process') :- !.
321 graph_type_description(minimum_dfa,'minimal DFA of state space') :- !.
322 graph_type_description(nfa_to_dfa,'state space as DFA') :- !.
323 graph_type_description(state_merge,'merged state space') :- !.
324 graph_type_description(Type,Desc) :- % fallback: replace underscores by spaces
325 atom_codes(Type,Codes),
326 findall(C2,(member(C,Codes), (C = 0'_ -> C2 = 0' ; C2 = C)),Codes2),
327 atom_codes(Desc,Codes2).
328
329 % the graph fontname is not inherited by nodes and edges; hence we set it here as well
330 print_node_and_edge_defaults :-
331 print('node [fontname="Helvetica"];'),nl,
332 print('edge [fontname="Helvetica", arrowsize=0.7];'),nl.
333
334 print_graph_footer :- print_legend, print('}'),nl.
335
336
337 /* ---------------------------------------------------------------------- */
338 /* Legend: enabled by preference dot_print_legend */
339 /* entries are recorded while nodes/edges are printed, so that the legend */
340 /* only explains what actually occurs in the graph, with the same colours */
341 /* ---------------------------------------------------------------------- */
342
343 :- dynamic legend_entry/2.
344 reset_legend :- retractall(legend_entry(_,_)).
345
346 record_legend(Kind,Attrs) :-
347 preference(dot_print_legend,true),
348 \+ legend_entry(Kind,_), !,
349 assertz(legend_entry(Kind,Attrs)).
350 record_legend(_,_).
351
352 record_node_legend(root,_,_,_,_,_,_) :- !.
353 record_node_legend(NodeID,Kind,Shape,Style,Col,Fill,_PenWidth) :-
354 preference(dot_print_legend,true), !,
355 (Kind = custom -> true
356 ; Style = none -> record_legend(Kind,node(Shape,Col,none,normal))
357 ; record_legend(Kind,node(Shape,Col,Fill,normal))),
358 % use a neutral colour: the current state can be of any kind (open, invariant violation, ...)
359 (current_state_id(NodeID) -> record_legend(current_state,node(Shape,'#424242',none,double)) ; true),
360 (Shape = triangle -> record_legend(undefined_values,node(Shape,Col,none,normal)) ; true).
361 record_node_legend(_,_,_,_,_,_,_).
362
363 % the order in which node kinds are shown, with their description
364 legend_node_kind(normal,'explored').
365 legend_node_kind(open,'not yet explored').
366 legend_node_kind(current_state,'current state').
367 legend_node_kind(invariant_violation,'invariant violated').
368 legend_node_kind(goal,'goal satisfied').
369 legend_node_kind(counterexample,'on counterexample').
370 legend_node_kind(scope_limit,'outside model checking scope').
371 legend_node_kind(permute,'permutation flooding').
372 legend_node_kind(heuristic,'coloured by HEURISTIC_FUNCTION').
373 legend_node_kind(undefined_values,'with undefined values').
374
375 :- use_module(probsrc(tools),[html_escape/2]).
376 print_legend :-
377 preference(dot_print_legend,true),
378 legend_entry(_,_), !,
379 print('prob_legend [shape=plain, fontsize=10, label=<'),nl,
380 legend_top_margin(Margin), % move the legend away from the top and the graph title
381 format('<table border="0" cellborder="0" cellspacing="0" cellpadding="0"><tr><td height="~w"></td></tr><tr><td>~n',[Margin]),
382 print('<table border="1" color="#BDBDBD" cellborder="0" cellspacing="2" cellpadding="3" style="rounded">'),nl,
383 print('<tr><td colspan="2"><b>Legend</b></td></tr>'),nl,
384 (legend_node_kind(Kind,Txt), legend_entry(Kind,Attrs),
385 print_legend_row(Attrs,Txt), fail ; true),
386 (legend_entry(counterexample_op,Edge) -> print_legend_row(Edge,'counterexample transition') ; true),
387 findall(Op-Edge,legend_entry(op(Op),Edge),Ops), sort(Ops,SOps),
388 (member(Op-Edge,SOps), legend_op_name(Op,OpName), print_legend_row(Edge,OpName), fail ; true),
389 print('</table>'),nl, print('</td></tr></table>>];'),nl,
390 print('{rank=min; prob_legend;}'),nl.
391 print_legend.
392
393 print_legend_row(node(Shape,Col,_Fill,double),Txt) :- !, % double border, as for peripheries=2
394 (legend_shape_info(Shape,Info) -> true ; Info = ''),
395 format('<tr><td><table border="1" color="~w" cellborder="0" cellspacing="2" cellpadding="0"><tr><td border="1" color="~w" width="12" height="8"> </td></tr></table></td><td align="left">~w~w</td></tr>~n',
396 [Col,Col,Txt,Info]).
397 print_legend_row(node(Shape,Col,Fill,PenWidth),Txt) :-
398 (Fill = none -> BG = white ; BG = Fill),
399 (PenWidth = normal -> Border = 1 ; Border is max(2,truncate(PenWidth+0.5))),
400 (legend_shape_info(Shape,Info) -> true ; Info = ''),
401 format('<tr><td bgcolor="~w" color="~w" border="~w" width="20"> </td><td align="left">~w~w</td></tr>~n',
402 [BG,Col,Border,Txt,Info]).
403 print_legend_row(edge(Col),Txt) :-
404 html_escape(Txt,ETxt),
405 format('<tr><td><font color="~w"><b>→</b></font></td><td align="left">~w</td></tr>~n',[Col,ETxt]).
406
407 % mention the shape in the legend if it differs from the normal node shape
408 legend_shape_info(Shape,Info) :-
409 \+ get_preference(dot_normal_node_shape,Shape),
410 ajoin([' (',Shape,')'],Info).
411
412 legend_op_name('$initialise_machine','INITIALISATION') :- !.
413 legend_op_name('$setup_constants','SETUP_CONSTANTS') :- !.
414 legend_op_name(Op,Op).
415
416
417 /* ---------------------------------------------------------------------- */
418
419 :- use_module(library(lists),[clumps/2]).
420 :- use_module(library(samsort)).
421 :- use_module(probsrc(tools),[ajoin/2]).
422 print_trace_to_current_state(IDList) :-
423 samsort(IDList,SIDList), % keeps duplicates
424 clumps(SIDList,Clumps),
425 member([NodeID|T],Clumps),
426 (length([NodeID|T],Len),Len>1
427 -> ajoin(['repeated (',Len,')'],Lbl),
428 write_dot_node_information(NodeID,[extralabel/Lbl])
429 ; write_dot_node_information(NodeID,[])),
430 fail.
431 print_trace_to_current_state(IDList) :-
432 temporary_set_preference(dot_print_self_loops,true,CHNG3),
433 print_trace_to_current_state2(IDList),
434 preferences:reset_temporary_preference(dot_print_self_loops,CHNG3).
435
436 print_trace_to_current_state2([]).
437 print_trace_to_current_state2([_]).
438 print_trace_to_current_state2([ID1,ID2|Tail]) :-
439 write_dot_edge_information(ID1,ID2,[]),
440 print_trace_to_current_state2([ID2|Tail]).
441
442 print_trace_with_neighbors_to_current_state(IDList) :- sort(IDList,SIDList),
443 member(NodeID,SIDList),
444 write_dot_node_information(NodeID,[]),
445 fail.
446 print_trace_with_neighbors_to_current_state(IDList) :-
447 current_state_id(CurID),
448 nth1(N,IDList,CurID,_Rest),!,
449 append_length(PathToLoop,LoopIDs,IDList,N),
450 %format(user_output,'~w pos=~w : ~w , path to loop=~w, loop=~w~n',[CurID,N,IDList,PathToLoop,LoopIDs]),
451 temporary_set_preference(dot_print_self_loops,true,CHNG3),
452 print_trace_to_current_state2(PathToLoop), % print path to loop
453 print_trace_with_neighbors_to_current_state2([CurID|LoopIDs],IDList), % print loop (with neighbors, because of the fairness check),
454 preferences:reset_temporary_preference(dot_print_self_loops,CHNG3).
455
456 print_trace_with_neighbors_to_current_state2([],_).
457 print_trace_with_neighbors_to_current_state2([ID|Tail],IDList) :-
458 findall(ID,(transition(ID,_,_,SuccID),
459 \+ append(_,[ID,SuccID|_],IDList), % check if the edge is not already shown
460 % format(user_output,'Neighb: ~w -> ~w : idlist=~w, tail=~w~n',[ID,SuccID,IDList,Tail]),
461 write_dot_edge_information(ID,SuccID,[])),_IDList),
462 print_trace_with_neighbors_to_current_state2(Tail,IDList).
463
464 extract_b_store(csp_and_b(_,T),R) :- !, R=T.
465 extract_b_store(R,R).
466
467 extract_full_store(concrete_constants(C),R) :- !,R=C.
468 extract_full_store(const_and_vars(ConstID,Vars),R) :- !,
469 specfile:expand_const_and_vars(ConstID,Vars,R).
470 extract_full_store(R,R).
471
472 :- use_module(probsrc(tools),[print_escaped_for_dot/1]).
473 :- use_module(probsrc(state_space),[counterexample_node/1, counterexample_op/1]).
474 :- use_module(probsrc(model_checker),[find_invariant_error/4]).
475 :- use_module(probsrc(specfile),[animation_mode/1]).
476
477
478 write_dot_node_information(NodeID,Options) :-
479 animation_mode(AM),
480 visited_expression(NodeID,CurT),
481 extract_b_store(CurT,CurBT),
482 extract_full_store(CurBT,CurTemp),
483 (NodeID=root -> preference(dot_print_root,true) ; true),
484 (member(shape/Shape,Options) -> true ; dot_node_shape(NodeID,CurTemp,Shape)),
485 % TO DO: penwidth=2.0, fillcolor="#F1F2D8"
486 (find_invariant_error(AM,NodeID,CurTemp,_)
487 -> get_preference(dot_invariant_violated_node_colour,NodeColor),
488 Style='rounded,filled', FillColor=NodeColor, Kind=invariant_violation
489 ; counterexample_node(NodeID)
490 -> get_preference(dot_counterexample_node_colour,NodeColor), Kind=counterexample,
491 (is_goal_node(NodeID) -> Style = 'rounded,filled', get_preference(dot_goal_node_colour,FillColor)
492 ; Style=none, FillColor=none)
493 ; is_goal_node(NodeID)
494 -> get_preference(dot_goal_node_colour,NodeColor), FillColor=NodeColor,
495 Style='rounded,filled', Kind=goal
496 ; (preference(dot_fill_normal_nodes,true) -> Style='rounded,filled' ; Style=none),
497 (member(color/NodeColor,Options) -> FillColor=NodeColor, Kind=custom
498 ; dot_node_color(NodeID,NodeColor,FillColor,Kind))
499 ),
500 (member(fontsize/FSize,Options) -> true ; get_preference(dot_node_font_size,FSize)),
501 (Style=none
502 -> format('~w [shape=~w, color=\"~w\", fontsize=~w',[NodeID,Shape,NodeColor,FSize])
503 ; FillColor=none
504 -> format('~w [shape=~w, style=\"~w\", color=\"~w\", fontsize=~w',[NodeID,Shape,Style, NodeColor,FSize])
505 ; format('~w [shape=~w, style=\"~w\", color=\"~w\", fillcolor=\"~w\", fontsize=~w',
506 [NodeID,Shape,Style, NodeColor,FillColor,FSize])
507 ),
508
509 (member(pendwidth/PenWidth,Options) -> true ; dot_node_pen_width(NodeID,PenWidth)),
510 record_node_legend(NodeID,Kind,Shape,Style,NodeColor,FillColor,PenWidth),
511 (PenWidth = 1 -> true ; print(', penwidth='), print(PenWidth)),
512 (current_state_id(NodeID) -> print(', peripheries=2') ; true), % also visible for filled nodes
513 write(', label=\"'),
514 (member(extralabel/ExtraLbl,Options) -> print_escaped_for_dot(ExtraLbl), print('\\n') ; true),
515 (member(no_tooltip_and_label,Options)
516 -> format('\"];~n',[])
517 ; (preference(dot_print_node_ids,true), NodeID \= root
518 -> format('State ~w\\n',[NodeID]), ToolTip0=''
519 ; ajoin(['State ',NodeID],ToolTip0)
520 ),
521 convert_state_to_string(CurT,StateAsString), string_escape(StateAsString,EncS),
522 (preference(dot_print_node_info,true)
523 -> write(EncS),
524 ToolTip = ToolTip0
525 ; ToolTip0='' -> ToolTip = EncS
526 ; ajoin([ToolTip0,'\\n',EncS],ToolTip)
527 ),
528 (preference(dot_print_node_properties,true) % useful for CSP
529 -> print_node_properties(NodeID)
530 ; true
531 ),
532 (ToolTip = []
533 -> format('\"];~n',[])
534 ; format('\", tooltip=\"~w\"];~n',[ToolTip])
535 )
536 ),
537 fail.
538 write_dot_node_information(_NodeID,_) :- nl.
539
540
541 :- use_module(probsrc(model_checker),[node_satisfies_goal/1]).
542 is_goal_node(NodeID) :- preference(dot_colour_goal_nodes,true),
543 node_satisfies_goal(NodeID). % TO DO csp goal
544
545 dot_node_shape(_StateId,State,triangle) :-
546 % state with abort values
547 memberchk(bind(_,term(no_value_for(_))),State),!.
548 dot_node_shape(root,_State,Shape) :-
549 % the root state
550 !,get_preference(dot_root_shape,Shape).
551 dot_node_shape(StateId,_State,Shape) :-
552 % the currently displayed state in the animation
553 current_state_id(StateId),!,
554 get_preference(dot_current_node_shape,Shape).
555 dot_node_shape(StateId,_State,diamond) :-
556 % a state which was subject to permutation flooding
557 transition(StateId,'*permute*',_),!.
558 dot_node_shape(_StateId,_State,Shape) :-
559 % a normal state
560 get_preference(dot_normal_node_shape,Shape).
561
562 % larger pen width for current node; we could add peripheries=2
563 dot_node_pen_width(StateId,Res) :-
564 get_preference(dot_node_penwidth,PenWidth),
565 (current_state_id(StateId) -> Res is PenWidth *1.5 ; Res = PenWidth).
566
567 dot_node_color(StateId,Color,FillColor,open) :-
568 % an open state where not all transitions are yet calculated
569 not_all_transitions_added(StateId),!,
570 get_preference(dot_open_node_colour,Color),
571 pale_fill_colour(Color,FillColor).
572 dot_node_color(StateId,Color,FillColor,scope_limit) :-
573 % a state which is outside the user-defined state space that should be model-checked
574 not_interesting(StateId),!,
575 get_preference(dot_scope_limit_node_colour,Color), FillColor=none.
576 dot_node_color(StateId,Color,FillColor,permute) :-
577 transition(StateId,'*permute*',_),!, Color = lightgray, FillColor=none.
578 dot_node_color(StateId,Color,FillColor,heuristic) :-
579 %get_preference(dot_normal_node_colour,Color),
580 compute_heuristic_function_for_state_id(StateId,HRes),
581 !,
582 translate_bvalue_to_colour(HRes,Color), %tools:print_message(heur(HRes,Color)).
583 FillColor=Color.
584 dot_node_color(_StateId,Color,FillColor,normal) :-
585 get_preference(dot_normal_node_colour,Color),
586 pale_fill_colour(Color,FillColor).
587
588 % compute a pale version of an RGB colour '#RRGGBB' for filling nodes, so that the
589 % fill colour follows the user's outline colour preference (e.g., pref_group dot_colors);
590 % named colours (e.g., olivedrab2) are used as is
591 pale_fill_colour(Col,Fill) :-
592 atom(Col), atom_codes(Col,[0'#,R1,R2,G1,G2,B1,B2]),
593 hex_byte(R1,R2,R), hex_byte(G1,G2,G), hex_byte(B1,B2,B), !,
594 pale_byte(R,PR), pale_byte(G,PG), pale_byte(B,PB),
595 byte_hex(PR,RC), byte_hex(PG,GC), byte_hex(PB,BC),
596 append([[0'#],RC,GC,BC],FCodes),
597 atom_codes(Fill,FCodes).
598 pale_fill_colour(Col,Col).
599
600 pale_byte(X,P) :- P is 255 - truncate((255-X)*0.2). % mix 20% of colour with 80% white
601
602 hex_byte(H,L,Byte) :- hex_digit_val(H,HV), hex_digit_val(L,LV), Byte is HV*16+LV.
603 hex_digit_val(C,V) :- C >= 0'0, C =< 0'9, !, V is C-0'0.
604 hex_digit_val(C,V) :- C >= 0'a, C =< 0'f, !, V is C-0'a+10.
605 hex_digit_val(C,V) :- C >= 0'A, C =< 0'F, V is C-0'A+10.
606
607 byte_hex(Byte,[H,L]) :- HV is Byte >> 4, LV is Byte /\ 15, hex_digit_code(HV,H), hex_digit_code(LV,L).
608 hex_digit_code(V,C) :- V < 10 -> C is 0'0+V ; C is 0'A+V-10.
609
610
611 :- use_module(probsrc(specfile),[property/2,elementary_prop/2,b_or_z_mode/0]).
612
613 print_node_properties(ID) :-
614 print(' Prop={'),
615 visited_expression(ID,State),
616 ? get_state_property(State,PropAsString),
617 print_string_for_dot(PropAsString), print('\\'),print('n'),
618 fail.
619 print_node_properties(_) :- print('}').
620
621 get_state_property(State,PropAsString) :-
622 ? specfile:property(State,Prop),
623 translate:translate_property_with_limit(Prop,320,PropAsString).
624
625 :- use_module(probsrc(bmachine),[b_get_machine_animation_expression/2]).
626 :- use_module(probsrc(specfile),[state_corresponds_to_fully_setup_b_machine/2]).
627 get_animation_expression_value(State,PropAsString) :-
628 b_or_z_mode,
629 b_get_machine_animation_expression('ANIMATION_EXPRESSION',AExpr),
630 state_corresponds_to_fully_setup_b_machine(State,BState),
631 b_interpreter:b_compute_expression_nowf(AExpr,[],BState,AValue,'ANIMATION_EXPRESSION',0),
632 translate:translate_bvalue(AValue,PropAsString).
633
634 % print transitions in state_space to tod
635 write_dot_edge_information(NodeID,SuccID,Options) :-
636 ? transition(NodeID,Action,TransID,SuccID),
637 visited_expression(NodeID,State), set_translation_context(State),
638 (NodeID=root -> preference(dot_print_root,true) ; true),
639 (Action = '-->'(_Func,_)
640 -> (NodeID \= SuccID ; preference(dot_print_functions,true))
641 ; true),
642 (NodeID \= SuccID -> true ; preference(dot_print_self_loops,true)),
643 format('~w -> ~w',[NodeID,SuccID]),
644 (preference(dot_print_arc_colors,true) ->
645 (NodeID==root
646 -> print(' [style = dotted, color = black, label=\"')
647 ; (Action = '*permute*'
648 -> ArcCol=gray
649 ; counterexample_op(TransID)
650 -> get_preference(dot_counterexample_op_colour,ArcCol),
651 record_legend(counterexample_op,edge(ArcCol))
652 ; dot_arc_color(TransID, Action, ArcCol)
653 ),
654 format(' [color = \"~w\", label=\"',[ArcCol])
655 )
656 ; print(' [label=\"')
657 ),
658 dot_print_transition_label(Action,Options),
659 (get_preference(dot_print_transition_ids,true) -> format('\n~w',[TransID]) ; true),
660 get_preference(dot_edge_font_size,FSize),
661 get_preference(dot_edge_penwidth,PenWidth),
662 (PenWidth = 1
663 -> format('\", fontsize=~w];~n',[FSize])
664 ; format('\", fontsize=~w, penwidth=~w];~n',[FSize,PenWidth])
665 ),
666 fail.
667 write_dot_edge_information(_NodeID,_SuccID,_) :- clear_translation_constants,nl.
668
669
670 %dot_arc_color(TransID, blue) :-
671 % b_abstract_mappings:widened_trans(TransID),!.
672 dot_arc_color(_,Action,ArcCol) :-
673 get_label_nr(Action,Nr), Nr>0,
674 translate_int_col(Nr,ArcCol),
675 !,
676 (functor(Action,'-->',2) -> arg(1,Action,A1), functor(A1,OpName,_) ; functor(Action,OpName,_)),
677 record_legend(op(OpName),edge(ArcCol)).
678 dot_arc_color(_TransId,_Action, ArcCol) :-
679 (preference(dot_normal_arc_colour,ArcCol) -> true ; ArcCol=blue).
680 % TO DO: enable colouring; particularly in sfdp mode
681
682 :- dynamic label_nr/2, next_label_nr/1.
683 next_label_nr(0).
684 reset_label_nr :- retractall(label_nr(_,_)), retractall(next_label_nr(_)).
685 reset_label_nr(Colour) :- reset_label_nr,
686 (Colour=colour_transitions
687 -> assertz(next_label_nr(0)) % asserts next label nr: we will colour transitions
688 ; true).
689
690 get_label_nr(Action,Nr) :- functor(Action,F,_),
691 (label_nr(F,FNr) -> Nr=FNr
692 ; retract(next_label_nr(N)), N1 is N+1, assertz(next_label_nr(N1)), assertz(label_nr(F,N)), Nr=N).
693
694
695 dot_print_transition_label(_,_) :- preference(dot_print_edge_labels,false),!.
696 dot_print_transition_label(Action,Options) :-
697 get_pref_with_default_opts(dot_print_action_arguments,Options,true),
698 translate:translate_event(Action,ActionAsString),
699 print_string_for_dot(ActionAsString),!.
700 dot_print_transition_label('-->'(FAction,_R),_Options) :- nonvar(FAction),
701 functor(FAction,OperationName,_),!,
702 print_string_for_dot(OperationName).
703 dot_print_transition_label(Action,_Options) :- nonvar(Action), functor(Action,OperationName,_),!,
704 print_string_for_dot(OperationName).
705 dot_print_transition_label(Action,_Options) :- print('err:'),print_string_for_dot(Action).
706
707 :- use_module(probsrc(tools),[wrap_and_truncate_atom/4,string_escape/2]).
708 print_string_for_dot(String) :- string_escape(String,EString),
709 wrap_and_truncate_atom(EString,50,350,NS), print(NS).
710
711 % get_preference, if value is default look in options for value, otherwise set it to true
712 get_pref_with_default_opts(Pref,Opts,Res) :- get_preference(Pref,Value),
713 (Value=default -> (member(Pref/Val,Opts) -> Res=Val ; Res=true)
714 ; Res=Value).
715
716 /* ------------------------------------------------------------ */
717 /* converting a state into a string for the graph visualization */
718 /* ------------------------------------------------------------ */
719
720
721 :- use_module(probsrc(tools),[string_concatenate/3]).
722 convert_state_to_string(root,'root') :- !.
723 convert_state_to_string(State,String) :-
724 get_animation_expression_value(State,V),!,
725 String=V.
726 convert_state_to_string(StateTempl,StateAsString) :- animation_mode(xtl),!,
727 convert_state_to_string_default(StateTempl,StateAsString).
728 convert_state_to_string(concrete_constants(CurTemp),StateAsString) :- !,
729 findall(FProp,elementary_prop(CurTemp,FProp),State), /* was fd_findall */
730 % TODO: if this is the only constants node, just print summary info; or maybe just those values that are non-det
731 get_preference(dot_max_constants_node_size,MaxSize),
732 limit_props_for_dot(State,MaxSize,Shown,NrHidden),
733 translate_params_for_dot(Shown,StateAsString0),
734 (NrHidden=0 -> StateAsString=StateAsString0
735 ; ajoin([StateAsString0,',\n\x2026\ +',NrHidden,' more'],StateAsString)).
736 convert_state_to_string(const_and_vars(ConstID,StateTempl),StateAsString) :- !,
737 set_translation_constants(ConstID),
738 convert_state_to_string(StateTempl,StateAsString),
739 clear_translation_constants.
740 convert_state_to_string(csp_and_b(CSPState,BState),StateAsString) :- !,
741 translate_cspm_state(CSPState,CSPText),
742 convert_state_to_string(BState,BString),
743 string_concatenate(CSPText,'\n',C2),
744 string_concatenate(C2,BString,StateAsString).
745 convert_state_to_string([H|T],StateAsString) :- !,
746 CurTemp=[H|T],
747 findall(FProp,elementary_prop(CurTemp,FProp),State), /* was fd_findall */
748 translate_params_for_dot(State,StateAsString).
749 convert_state_to_string(StateTempl,StateAsString) :-
750 convert_state_to_string_default(StateTempl,StateAsString).
751
752 % keep the first properties whose translation fits into MaxSize characters (0: no limit)
753 % the first property is always shown
754 limit_props_for_dot(Props,MaxSize,Shown,NrHidden) :-
755 (MaxSize =< 0 -> Shown=Props, NrHidden=0
756 ; limit_props_for_dot(Props,0,MaxSize,Shown,NrHidden)).
757 limit_props_for_dot([],_,_,[],0).
758 limit_props_for_dot([P|T],Cur,MaxSize,Shown,NrHidden) :-
759 translate_property_with_limit(P,100,TP), atom_length(TP,Len),
760 C1 is Cur+Len+1,
761 (C1 > MaxSize, Cur>0
762 -> Shown=[], length([P|T],NrHidden)
763 ; Shown=[P|ST], limit_props_for_dot(T,C1,MaxSize,ST,NrHidden)).
764
765 convert_state_to_string_default(StateTempl,StateAsString) :-
766 findall(FProp,
767 (property(StateTempl,Prop),filter_elementary_prop(Prop,FProp)),
768 State), /* was fd_findall */
769 translate_params_for_dot_nl(State,StateAsString).
770
771 filter_elementary_prop(X,R) :- \+ b_or_z_mode,!,R=X.
772 filter_elementary_prop('='(Cst,_V),_) :- nonvar(Cst),
773 b_is_constant(Cst),!,fail.
774 filter_elementary_prop(Relation,_) :-
775 functor(Relation,Func,_),
776 b_is_constant(Func),!,fail.
777 filter_elementary_prop('='(_X,pred_false /* bool_false */),_) :- !,fail. /* do not show boolean variables that are false */
778 filter_elementary_prop('='( X,pred_true /* bool_true */), X) :- !. /* just show the boolean variable, not that it is = true */
779 filter_elementary_prop(X,X).
780
781
782 /* ---------------------- */
783 /* DEFINITION dependency */
784 /* ---------------------- */
785
786 % show definition call graph
787
788 :- use_module(dotsrc(dot_graph_generator), [gen_dot_graph/6, dot_no_same_rank/1, dot_no_subgraph/3,
789 use_new_dot_attr_pred/7]).
790 :- use_module(probsrc(bmachine),[b_machine_name/1]).
791 :- use_module(probsrc(tools_strings),[ajoin_with_sep/3]).
792 % one node per DEFINITION (coloured by kind), an edge D1 -> D2 if D1 uses D2
793 tcltk_print_definitions_as_graph_for_dot(File) :-
794 findall(Called, (b_get_definition(_,_,_,_,Deps), member(Called,Deps)),CD),
795 sort(CD,CalledDefs),
796 findall(Def,shown_definition(CalledDefs,Def),Shown),
797 (get_preference(dot_print_graph_title,true), b_machine_name(Main)
798 -> ajoin([Main,' - DEFINITIONS'],Title), get_preference(dot_title_font_size,TFS), TitleAttrs = [label/Title, labelloc/t, fontsize/TFS]
799 ; TitleAttrs = []),
800 gen_dot_graph(File,[graph_name/definitions, no_page_size, rankdir/'LR', fontname/'Helvetica', nodesep/0.2, ranksep/0.6,
801 node_defaults/[fontname/'Helvetica', fontsize/11, shape/box, style/'rounded,filled',
802 margin/'0.1,0.04'],
803 edge_defaults/[color/gray50, arrowsize/0.7]
804 | TitleAttrs],
805 use_new_dot_attr_pred(visualize_graph:dot_def_node(Shown)),
806 use_new_dot_attr_pred(visualize_graph:dot_def_trans(Shown)),
807 dot_no_same_rank,visualize_graph:dot_def_subgraph(Shown)).
808
809
810 :- use_module(probsrc(bmachine),[b_get_definition/5]).
811 :- use_module(library(ordsets),[ord_member/2]).
812
813 shown_definition(CalledDefs,DEFID) :-
814 b_get_definition(DEFID,_DefType,_Args,_Body,Deps),
815 (get_preference(dot_definitions_show_all,true) -> true
816 ; Deps = [_|_] -> true % show if it calls another one
817 ; ord_member(DEFID,CalledDefs) % show if called
818 ).
819
820 :- public dot_def_node/4, dot_def_trans/4, dot_def_subgraph/4.
821 dot_def_node([],no_defs,none,[label/Msg, shape/plaintext, style/'', fontcolor/gray40]) :-
822 (b_get_definition(_,_,_,_,_) -> Msg = 'No DEFINITIONS use other DEFINITIONS' ; Msg = 'No DEFINITIONS visible').
823 dot_def_node(Shown,DEFID,SubGraph,[label/Label, tooltip/Tooltip, fillcolor/Fill, color/Col | Legend]) :-
824 member(DEFID,Shown),
825 b_get_definition(DEFID,DefType,Args,_Body,Deps),
826 (get_preference(dot_definitions_use_sub_graph,true) -> SubGraph = DefType ; SubGraph=none),
827 dot_def_kind(DefType,Fill,Col,LText,Order),
828 (get_preference(dot_print_legend,true) -> Legend=[legend/LText,legend_order/Order] ; Legend=[]),
829 maplist(def_arg_name,Args,ArgNames),
830 (ArgNames = [] -> Label = DEFID
831 ; ajoin_with_sep(ArgNames,',',AS), ajoin([DEFID,'(',AS,')'],Label)),
832 length(Deps,NrDeps),
833 ajoin([LText,' ',Label,' uses ',NrDeps,' other DEFINITION(s)'],Tooltip).
834
835 def_arg_name(identifier(_,ID),R) :- !, R=ID.
836 def_arg_name(definition(_,ID,_),R) :- !, R=ID.
837 def_arg_name(_,'_').
838
839 dot_def_kind(predicate,'#E4F2E5','#2E7D32','predicate DEFINITION',10) :- !.
840 dot_def_kind(expression,'#E3EDF7','#1F5F99','expression DEFINITION',20) :- !.
841 dot_def_kind(substitution,'#F3E5F5','#6A1B9A','substitution DEFINITION',30) :- !.
842 dot_def_kind(_,white,gray50,'other DEFINITION',40).
843
844 dot_def_trans(Shown,NodeID,SuccNodeID,[tooltip/Tooltip]) :-
845 member(NodeID,Shown),
846 b_get_definition(NodeID,_DefType,_Args,_Body,Deps),
847 member(SuccNodeID,Deps),
848 ajoin([NodeID,' uses ',SuccNodeID],Tooltip).
849
850 dot_def_subgraph(Shown,sub_graph_with_attributes(SubGraph,[label/Label, fontname/'Helvetica', fontsize/11,
851 fillcolor/'#FAFAFA', pencolor/gray70, fontcolor/gray40]),
852 'rounded,filled',none) :-
853 get_preference(dot_definitions_use_sub_graph,true),
854 member(SubGraph,[predicate,expression,substitution]),
855 (member(D,Shown), b_get_definition(D,SubGraph,_,_,_) -> true),
856 ajoin([SubGraph,' DEFINITIONS'],Label).
857
858
859 /* Prediate dependency */
860
861 % call -dotexpr predicate_dependency Pred File
862 % separates a predicate into conjuncts and shows edges between predicates with common variables
863
864 :- use_module(probsrc(tools),[string_escape/2]).
865 :- use_module(probsrc(bsyntaxtree),[predicate_identifiers/2,conjunction_to_list/2]).
866 %
867 tcltk_print_predicate_dependency_as_graph_for_dot(Pred,File) :-
868 my_parse_predicate(Pred,Typed),
869 print_predicate_dependency_as_graph_for_dot(Typed,File).
870
871 :- use_module(probsrc(bmachine), [type_with_errors/4]).
872 :- use_module(probsrc(error_manager), [add_error/3]).
873 my_parse_predicate(RawAST,Typed) :- compound(RawAST),!,
874 type_with_errors(RawAST,[variables],Type,Typed),
875 (Type=pred -> true
876 ; add_error(visualize_graph,'Expected predicate by got:',Type),fail).
877 my_parse_predicate(Pred,Typed) :-
878 atom_codes(Pred,Codes),
879 eval_strings:repl_parse_predicate(Codes,exists,Typed,_).
880
881 % one node per conjunct; an undirected edge between two conjuncts sharing identifiers, labelled by them
882 print_predicate_dependency_as_graph_for_dot(Typed,File) :-
883 my_conjunction_to_list(Typed,Conj),
884 get_pred_ids(Conj,1,Preds),
885 length(Preds,NrPreds),
886 (get_preference(dot_print_graph_title,true)
887 -> ajoin(['Dependencies between ',NrPreds,' conjuncts'],Title),
888 get_preference(dot_title_font_size,TFS), TitleAttrs = [label/Title, labelloc/t, fontsize/TFS]
889 ; TitleAttrs = []),
890 gen_dot_graph(File,[graph_name/predicate_dependency, no_page_size, fontname/'Helvetica',
891 nodesep/0.3, ranksep/0.5,
892 node_defaults/[fontname/'Helvetica', fontsize/11, shape/box, style/'rounded,filled',
893 margin/'0.1,0.05'],
894 edge_defaults/[fontname/'Helvetica', fontsize/9, dir/none, color/'#1F5F99',
895 fontcolor/'#1F5F99']
896 | TitleAttrs],
897 use_new_dot_attr_pred(visualize_graph:dot_pred_node(Preds)),
898 use_new_dot_attr_pred(visualize_graph:dot_pred_trans(Preds)),
899 dot_no_same_rank,dot_no_subgraph).
900
901 my_conjunction_to_list(b(exists(_,Body),pred,_),L) :- !, my_conjunction_to_list(Body,L).
902 my_conjunction_to_list(B,L) :- conjunction_to_list(B,L).
903
904 get_pred_ids([],_,[]).
905 get_pred_ids([Pred|T],N,[pred(N,Pred,Ids)|PT]) :- predicate_identifiers(Pred,Ids),
906 N1 is N+1, get_pred_ids(T,N1,PT).
907
908 :- public dot_pred_node/4, dot_pred_trans/4.
909 dot_pred_node(Preds,PID,none,[label/htmllabel(Label), tooltip/Tooltip, fillcolor/Fill, color/Col | Legend]) :-
910 member(pred(PID,Pred,Ids),Preds),
911 translate:translate_bexpression_with_limit(Pred,80,Desc),
912 html_escape(Desc,EDesc),
913 ajoin(['<<font point-size="8" color="#707070">conjunct ',PID,'</font><br/>',EDesc,'>'],Label),
914 (shares_ids_with_other_pred(PID,Ids,Preds)
915 -> Fill='#E3EDF7', Col='#1F5F99', LText='conjunct', Order=10
916 ; Fill=white, Col=gray50, LText='conjunct sharing no identifier with others', Order=20),
917 (get_preference(dot_print_legend,true) -> Legend=[legend/LText,legend_order/Order] ; Legend=[]),
918 ajoin_with_sep(Ids,', ',IdsTxt),
919 translate:translate_bexpression_with_limit(Pred,1000,LongDesc),
920 ajoin(['conjunct ',PID,': ',LongDesc,'; identifiers: ',IdsTxt],Tooltip).
921
922 shares_ids_with_other_pred(PID,Ids,Preds) :-
923 member(pred(PID2,_,Ids2),Preds), PID2 \= PID,
924 ord_intersection(Ids,Ids2,Common), Common \= [], !.
925
926 :- use_module(library(ordsets),[ord_intersection/3]).
927 dot_pred_trans(Preds,NodeID,SuccNodeID,[label/Label, tooltip/Tooltip | Attrs]) :-
928 member(pred(NodeID,_,Ids1),Preds),
929 member(pred(SuccNodeID,_,Ids2),Preds), SuccNodeID>NodeID,
930 ord_intersection(Ids1,Ids2,Common), Common \= [],
931 ajoin_with_sep(Common,', ',Label),
932 ajoin(['conjuncts ',NodeID,' and ',SuccNodeID,' share ',Label],Tooltip),
933 length(Common,Nr),
934 (Nr > 1 -> PW is min(4,Nr), Attrs=[penwidth/PW] ; Attrs=[]).