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]).
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 print('<table border="1" color="#BDBDBD" cellborder="0" cellspacing="2" cellpadding="3" style="rounded">'),nl,
381 print('<tr><td colspan="2"><b>Legend</b></td></tr>'),nl,
382 (legend_node_kind(Kind,Txt), legend_entry(Kind,Attrs),
383 print_legend_row(Attrs,Txt), fail ; true),
384 (legend_entry(counterexample_op,Edge) -> print_legend_row(Edge,'counterexample transition') ; true),
385 findall(Op-Edge,legend_entry(op(Op),Edge),Ops), sort(Ops,SOps),
386 (member(Op-Edge,SOps), legend_op_name(Op,OpName), print_legend_row(Edge,OpName), fail ; true),
387 print('</table>>];'),nl,
388 print('{rank=min; prob_legend;}'),nl.
389 print_legend.
390
391 print_legend_row(node(Shape,Col,_Fill,double),Txt) :- !, % double border, as for peripheries=2
392 (legend_shape_info(Shape,Info) -> true ; Info = ''),
393 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',
394 [Col,Col,Txt,Info]).
395 print_legend_row(node(Shape,Col,Fill,PenWidth),Txt) :-
396 (Fill = none -> BG = white ; BG = Fill),
397 (PenWidth = normal -> Border = 1 ; Border is max(2,truncate(PenWidth+0.5))),
398 (legend_shape_info(Shape,Info) -> true ; Info = ''),
399 format('<tr><td bgcolor="~w" color="~w" border="~w" width="20"> </td><td align="left">~w~w</td></tr>~n',
400 [BG,Col,Border,Txt,Info]).
401 print_legend_row(edge(Col),Txt) :-
402 html_escape(Txt,ETxt),
403 format('<tr><td><font color="~w"><b>→</b></font></td><td align="left">~w</td></tr>~n',[Col,ETxt]).
404
405 % mention the shape in the legend if it differs from the normal node shape
406 legend_shape_info(Shape,Info) :-
407 \+ get_preference(dot_normal_node_shape,Shape),
408 ajoin([' (',Shape,')'],Info).
409
410 legend_op_name('$initialise_machine','INITIALISATION') :- !.
411 legend_op_name('$setup_constants','SETUP_CONSTANTS') :- !.
412 legend_op_name(Op,Op).
413
414
415 /* ---------------------------------------------------------------------- */
416
417 :- use_module(library(lists),[clumps/2]).
418 :- use_module(library(samsort)).
419 :- use_module(probsrc(tools),[ajoin/2]).
420 print_trace_to_current_state(IDList) :-
421 samsort(IDList,SIDList), % keeps duplicates
422 clumps(SIDList,Clumps),
423 member([NodeID|T],Clumps),
424 (length([NodeID|T],Len),Len>1
425 -> ajoin(['repeated (',Len,')'],Lbl),
426 write_dot_node_information(NodeID,[extralabel/Lbl])
427 ; write_dot_node_information(NodeID,[])),
428 fail.
429 print_trace_to_current_state(IDList) :-
430 temporary_set_preference(dot_print_self_loops,true,CHNG3),
431 print_trace_to_current_state2(IDList),
432 preferences:reset_temporary_preference(dot_print_self_loops,CHNG3).
433
434 print_trace_to_current_state2([]).
435 print_trace_to_current_state2([_]).
436 print_trace_to_current_state2([ID1,ID2|Tail]) :-
437 write_dot_edge_information(ID1,ID2,[]),
438 print_trace_to_current_state2([ID2|Tail]).
439
440 print_trace_with_neighbors_to_current_state(IDList) :- sort(IDList,SIDList),
441 member(NodeID,SIDList),
442 write_dot_node_information(NodeID,[]),
443 fail.
444 print_trace_with_neighbors_to_current_state(IDList) :-
445 current_state_id(CurID),
446 nth1(N,IDList,CurID,_Rest),!,
447 append_length(PathToLoop,LoopIDs,IDList,N),
448 %format(user_output,'~w pos=~w : ~w , path to loop=~w, loop=~w~n',[CurID,N,IDList,PathToLoop,LoopIDs]),
449 temporary_set_preference(dot_print_self_loops,true,CHNG3),
450 print_trace_to_current_state2(PathToLoop), % print path to loop
451 print_trace_with_neighbors_to_current_state2([CurID|LoopIDs],IDList), % print loop (with neighbors, because of the fairness check),
452 preferences:reset_temporary_preference(dot_print_self_loops,CHNG3).
453
454 print_trace_with_neighbors_to_current_state2([],_).
455 print_trace_with_neighbors_to_current_state2([ID|Tail],IDList) :-
456 findall(ID,(transition(ID,_,_,SuccID),
457 \+ append(_,[ID,SuccID|_],IDList), % check if the edge is not already shown
458 % format(user_output,'Neighb: ~w -> ~w : idlist=~w, tail=~w~n',[ID,SuccID,IDList,Tail]),
459 write_dot_edge_information(ID,SuccID,[])),_IDList),
460 print_trace_with_neighbors_to_current_state2(Tail,IDList).
461
462 extract_b_store(csp_and_b(_,T),R) :- !, R=T.
463 extract_b_store(R,R).
464
465 extract_full_store(concrete_constants(C),R) :- !,R=C.
466 extract_full_store(const_and_vars(ConstID,Vars),R) :- !,
467 specfile:expand_const_and_vars(ConstID,Vars,R).
468 extract_full_store(R,R).
469
470 :- use_module(probsrc(tools),[print_escaped_for_dot/1]).
471 :- use_module(probsrc(state_space),[counterexample_node/1, counterexample_op/1]).
472 :- use_module(probsrc(model_checker),[find_invariant_error/4]).
473 :- use_module(probsrc(specfile),[animation_mode/1]).
474
475
476 write_dot_node_information(NodeID,Options) :-
477 animation_mode(AM),
478 visited_expression(NodeID,CurT),
479 extract_b_store(CurT,CurBT),
480 extract_full_store(CurBT,CurTemp),
481 (NodeID=root -> preference(dot_print_root,true) ; true),
482 (member(shape/Shape,Options) -> true ; dot_node_shape(NodeID,CurTemp,Shape)),
483 % TO DO: penwidth=2.0, fillcolor="#F1F2D8"
484 (find_invariant_error(AM,NodeID,CurTemp,_)
485 -> get_preference(dot_invariant_violated_node_colour,NodeColor),
486 Style='rounded,filled', FillColor=NodeColor, Kind=invariant_violation
487 ; counterexample_node(NodeID)
488 -> get_preference(dot_counterexample_node_colour,NodeColor), Kind=counterexample,
489 (is_goal_node(NodeID) -> Style = 'rounded,filled', get_preference(dot_goal_node_colour,FillColor)
490 ; Style=none, FillColor=none)
491 ; is_goal_node(NodeID)
492 -> get_preference(dot_goal_node_colour,NodeColor), FillColor=NodeColor,
493 Style='rounded,filled', Kind=goal
494 ; (preference(dot_fill_normal_nodes,true) -> Style='rounded,filled' ; Style=none),
495 (member(color/NodeColor,Options) -> FillColor=NodeColor, Kind=custom
496 ; dot_node_color(NodeID,NodeColor,FillColor,Kind))
497 ),
498 (member(fontsize/FSize,Options) -> true ; get_preference(dot_node_font_size,FSize)),
499 (Style=none
500 -> format('~w [shape=~w, color=\"~w\", fontsize=~w',[NodeID,Shape,NodeColor,FSize])
501 ; FillColor=none
502 -> format('~w [shape=~w, style=\"~w\", color=\"~w\", fontsize=~w',[NodeID,Shape,Style, NodeColor,FSize])
503 ; format('~w [shape=~w, style=\"~w\", color=\"~w\", fillcolor=\"~w\", fontsize=~w',
504 [NodeID,Shape,Style, NodeColor,FillColor,FSize])
505 ),
506
507 (member(pendwidth/PenWidth,Options) -> true ; dot_node_pen_width(NodeID,PenWidth)),
508 record_node_legend(NodeID,Kind,Shape,Style,NodeColor,FillColor,PenWidth),
509 (PenWidth = 1 -> true ; print(', penwidth='), print(PenWidth)),
510 (current_state_id(NodeID) -> print(', peripheries=2') ; true), % also visible for filled nodes
511 write(', label=\"'),
512 (member(extralabel/ExtraLbl,Options) -> print_escaped_for_dot(ExtraLbl), print('\\n') ; true),
513 (member(no_tooltip_and_label,Options)
514 -> format('\"];~n',[])
515 ; (preference(dot_print_node_ids,true), NodeID \= root
516 -> format('State ~w\\n',[NodeID]), ToolTip0=''
517 ; ajoin(['State ',NodeID],ToolTip0)
518 ),
519 convert_state_to_string(CurT,StateAsString), string_escape(StateAsString,EncS),
520 (preference(dot_print_node_info,true)
521 -> write(EncS),
522 ToolTip = ToolTip0
523 ; ToolTip0='' -> ToolTip = EncS
524 ; ajoin([ToolTip0,'\\n',EncS],ToolTip)
525 ),
526 (preference(dot_print_node_properties,true) % useful for CSP
527 -> print_node_properties(NodeID)
528 ; true
529 ),
530 (ToolTip = []
531 -> format('\"];~n',[])
532 ; format('\", tooltip=\"~w\"];~n',[ToolTip])
533 )
534 ),
535 fail.
536 write_dot_node_information(_NodeID,_) :- nl.
537
538
539 :- use_module(probsrc(model_checker),[node_satisfies_goal/1]).
540 is_goal_node(NodeID) :- preference(dot_colour_goal_nodes,true),
541 node_satisfies_goal(NodeID). % TO DO csp goal
542
543 dot_node_shape(_StateId,State,triangle) :-
544 % state with abort values
545 memberchk(bind(_,term(no_value_for(_))),State),!.
546 dot_node_shape(root,_State,Shape) :-
547 % the root state
548 !,get_preference(dot_root_shape,Shape).
549 dot_node_shape(StateId,_State,Shape) :-
550 % the currently displayed state in the animation
551 current_state_id(StateId),!,
552 get_preference(dot_current_node_shape,Shape).
553 dot_node_shape(StateId,_State,diamond) :-
554 % a state which was subject to permutation flooding
555 transition(StateId,'*permute*',_),!.
556 dot_node_shape(_StateId,_State,Shape) :-
557 % a normal state
558 get_preference(dot_normal_node_shape,Shape).
559
560 % larger pen width for current node; we could add peripheries=2
561 dot_node_pen_width(StateId,Res) :-
562 get_preference(dot_node_penwidth,PenWidth),
563 (current_state_id(StateId) -> Res is PenWidth *1.5 ; Res = PenWidth).
564
565 dot_node_color(StateId,Color,FillColor,open) :-
566 % an open state where not all transitions are yet calculated
567 not_all_transitions_added(StateId),!,
568 get_preference(dot_open_node_colour,Color),
569 pale_fill_colour(Color,FillColor).
570 dot_node_color(StateId,Color,FillColor,scope_limit) :-
571 % a state which is outside the user-defined state space that should be model-checked
572 not_interesting(StateId),!,
573 get_preference(dot_scope_limit_node_colour,Color), FillColor=none.
574 dot_node_color(StateId,Color,FillColor,permute) :-
575 transition(StateId,'*permute*',_),!, Color = lightgray, FillColor=none.
576 dot_node_color(StateId,Color,FillColor,heuristic) :-
577 %get_preference(dot_normal_node_colour,Color),
578 compute_heuristic_function_for_state_id(StateId,HRes),
579 !,
580 translate_bvalue_to_colour(HRes,Color), %tools:print_message(heur(HRes,Color)).
581 FillColor=Color.
582 dot_node_color(_StateId,Color,FillColor,normal) :-
583 get_preference(dot_normal_node_colour,Color),
584 pale_fill_colour(Color,FillColor).
585
586 % compute a pale version of an RGB colour '#RRGGBB' for filling nodes, so that the
587 % fill colour follows the user's outline colour preference (e.g., pref_group dot_colors);
588 % named colours (e.g., olivedrab2) are used as is
589 pale_fill_colour(Col,Fill) :-
590 atom(Col), atom_codes(Col,[0'#,R1,R2,G1,G2,B1,B2]),
591 hex_byte(R1,R2,R), hex_byte(G1,G2,G), hex_byte(B1,B2,B), !,
592 pale_byte(R,PR), pale_byte(G,PG), pale_byte(B,PB),
593 byte_hex(PR,RC), byte_hex(PG,GC), byte_hex(PB,BC),
594 append([[0'#],RC,GC,BC],FCodes),
595 atom_codes(Fill,FCodes).
596 pale_fill_colour(Col,Col).
597
598 pale_byte(X,P) :- P is 255 - truncate((255-X)*0.2). % mix 20% of colour with 80% white
599
600 hex_byte(H,L,Byte) :- hex_digit_val(H,HV), hex_digit_val(L,LV), Byte is HV*16+LV.
601 hex_digit_val(C,V) :- C >= 0'0, C =< 0'9, !, V is C-0'0.
602 hex_digit_val(C,V) :- C >= 0'a, C =< 0'f, !, V is C-0'a+10.
603 hex_digit_val(C,V) :- C >= 0'A, C =< 0'F, V is C-0'A+10.
604
605 byte_hex(Byte,[H,L]) :- HV is Byte >> 4, LV is Byte /\ 15, hex_digit_code(HV,H), hex_digit_code(LV,L).
606 hex_digit_code(V,C) :- V < 10 -> C is 0'0+V ; C is 0'A+V-10.
607
608
609 :- use_module(probsrc(specfile),[property/2,elementary_prop/2,b_or_z_mode/0]).
610
611 print_node_properties(ID) :-
612 print(' Prop={'),
613 visited_expression(ID,State),
614 get_state_property(State,PropAsString),
615 print_string_for_dot(PropAsString), print('\\'),print('n'),
616 fail.
617 print_node_properties(_) :- print('}').
618
619 get_state_property(State,PropAsString) :-
620 specfile:property(State,Prop),
621 translate:translate_property_with_limit(Prop,320,PropAsString).
622
623 :- use_module(probsrc(bmachine),[b_get_machine_animation_expression/2]).
624 :- use_module(probsrc(specfile),[state_corresponds_to_fully_setup_b_machine/2]).
625 get_animation_expression_value(State,PropAsString) :-
626 b_or_z_mode,
627 b_get_machine_animation_expression('ANIMATION_EXPRESSION',AExpr),
628 state_corresponds_to_fully_setup_b_machine(State,BState),
629 b_interpreter:b_compute_expression_nowf(AExpr,[],BState,AValue,'ANIMATION_EXPRESSION',0),
630 translate:translate_bvalue(AValue,PropAsString).
631
632 % print transitions in state_space to tod
633 write_dot_edge_information(NodeID,SuccID,Options) :-
634 transition(NodeID,Action,TransID,SuccID),
635 visited_expression(NodeID,State), set_translation_context(State),
636 (NodeID=root -> preference(dot_print_root,true) ; true),
637 (Action = '-->'(_Func,_)
638 -> (NodeID \= SuccID ; preference(dot_print_functions,true))
639 ; true),
640 (NodeID \= SuccID -> true ; preference(dot_print_self_loops,true)),
641 format('~w -> ~w',[NodeID,SuccID]),
642 (preference(dot_print_arc_colors,true) ->
643 (NodeID==root
644 -> print(' [style = dotted, color = black, label=\"')
645 ; (Action = '*permute*'
646 -> ArcCol=gray
647 ; counterexample_op(TransID)
648 -> get_preference(dot_counterexample_op_colour,ArcCol),
649 record_legend(counterexample_op,edge(ArcCol))
650 ; dot_arc_color(TransID, Action, ArcCol)
651 ),
652 format(' [color = \"~w\", label=\"',[ArcCol])
653 )
654 ; print(' [label=\"')
655 ),
656 dot_print_transition_label(Action,Options),
657 (get_preference(dot_print_transition_ids,true) -> format('\n~w',[TransID]) ; true),
658 get_preference(dot_edge_font_size,FSize),
659 get_preference(dot_edge_penwidth,PenWidth),
660 (PenWidth = 1
661 -> format('\", fontsize=~w];~n',[FSize])
662 ; format('\", fontsize=~w, penwidth=~w];~n',[FSize,PenWidth])
663 ),
664 fail.
665 write_dot_edge_information(_NodeID,_SuccID,_) :- clear_translation_constants,nl.
666
667
668 %dot_arc_color(TransID, blue) :-
669 % b_abstract_mappings:widened_trans(TransID),!.
670 dot_arc_color(_,Action,ArcCol) :-
671 get_label_nr(Action,Nr), Nr>0,
672 translate_int_col(Nr,ArcCol),
673 !,
674 (functor(Action,'-->',2) -> arg(1,Action,A1), functor(A1,OpName,_) ; functor(Action,OpName,_)),
675 record_legend(op(OpName),edge(ArcCol)).
676 dot_arc_color(_TransId,_Action, ArcCol) :-
677 (preference(dot_normal_arc_colour,ArcCol) -> true ; ArcCol=blue).
678 % TO DO: enable colouring; particularly in sfdp mode
679
680 :- dynamic label_nr/2, next_label_nr/1.
681 next_label_nr(0).
682 reset_label_nr :- retractall(label_nr(_,_)), retractall(next_label_nr(_)).
683 reset_label_nr(Colour) :- reset_label_nr,
684 (Colour=colour_transitions
685 -> assertz(next_label_nr(0)) % asserts next label nr: we will colour transitions
686 ; true).
687
688 get_label_nr(Action,Nr) :- functor(Action,F,_),
689 (label_nr(F,FNr) -> Nr=FNr
690 ; retract(next_label_nr(N)), N1 is N+1, assertz(next_label_nr(N1)), assertz(label_nr(F,N)), Nr=N).
691
692
693 dot_print_transition_label(_,_) :- preference(dot_print_edge_labels,false),!.
694 dot_print_transition_label(Action,Options) :-
695 get_pref_with_default_opts(dot_print_action_arguments,Options,true),
696 translate:translate_event(Action,ActionAsString),
697 print_string_for_dot(ActionAsString),!.
698 dot_print_transition_label('-->'(FAction,_R),_Options) :- nonvar(FAction),
699 functor(FAction,OperationName,_),!,
700 print_string_for_dot(OperationName).
701 dot_print_transition_label(Action,_Options) :- nonvar(Action), functor(Action,OperationName,_),!,
702 print_string_for_dot(OperationName).
703 dot_print_transition_label(Action,_Options) :- print('err:'),print_string_for_dot(Action).
704
705 :- use_module(probsrc(tools),[wrap_and_truncate_atom/4,string_escape/2]).
706 print_string_for_dot(String) :- string_escape(String,EString),
707 wrap_and_truncate_atom(EString,50,350,NS), print(NS).
708
709 % get_preference, if value is default look in options for value, otherwise set it to true
710 get_pref_with_default_opts(Pref,Opts,Res) :- get_preference(Pref,Value),
711 (Value=default -> (member(Pref/Val,Opts) -> Res=Val ; Res=true)
712 ; Res=Value).
713
714 /* ------------------------------------------------------------ */
715 /* converting a state into a string for the graph visualization */
716 /* ------------------------------------------------------------ */
717
718
719 :- use_module(probsrc(tools),[string_concatenate/3]).
720 convert_state_to_string(root,'root') :- !.
721 convert_state_to_string(State,String) :-
722 get_animation_expression_value(State,V),!,
723 String=V.
724 convert_state_to_string(StateTempl,StateAsString) :- animation_mode(xtl),!,
725 convert_state_to_string_default(StateTempl,StateAsString).
726 convert_state_to_string(concrete_constants(CurTemp),StateAsString) :- !,
727 findall(FProp,elementary_prop(CurTemp,FProp),State), /* was fd_findall */
728 % TODO: if this is the only constants node, just print summary info; or maybe just those values that are non-det
729 get_preference(dot_max_constants_node_size,MaxSize),
730 limit_props_for_dot(State,MaxSize,Shown,NrHidden),
731 translate_params_for_dot(Shown,StateAsString0),
732 (NrHidden=0 -> StateAsString=StateAsString0
733 ; ajoin([StateAsString0,',\n\x2026\ +',NrHidden,' more'],StateAsString)).
734 convert_state_to_string(const_and_vars(ConstID,StateTempl),StateAsString) :- !,
735 set_translation_constants(ConstID),
736 convert_state_to_string(StateTempl,StateAsString),
737 clear_translation_constants.
738 convert_state_to_string(csp_and_b(CSPState,BState),StateAsString) :- !,
739 translate_cspm_state(CSPState,CSPText),
740 convert_state_to_string(BState,BString),
741 string_concatenate(CSPText,'\n',C2),
742 string_concatenate(C2,BString,StateAsString).
743 convert_state_to_string([H|T],StateAsString) :- !,
744 CurTemp=[H|T],
745 findall(FProp,elementary_prop(CurTemp,FProp),State), /* was fd_findall */
746 translate_params_for_dot(State,StateAsString).
747 convert_state_to_string(StateTempl,StateAsString) :-
748 convert_state_to_string_default(StateTempl,StateAsString).
749
750 % keep the first properties whose translation fits into MaxSize characters (0: no limit)
751 % the first property is always shown
752 limit_props_for_dot(Props,MaxSize,Shown,NrHidden) :-
753 (MaxSize =< 0 -> Shown=Props, NrHidden=0
754 ; limit_props_for_dot(Props,0,MaxSize,Shown,NrHidden)).
755 limit_props_for_dot([],_,_,[],0).
756 limit_props_for_dot([P|T],Cur,MaxSize,Shown,NrHidden) :-
757 translate_property_with_limit(P,100,TP), atom_length(TP,Len),
758 C1 is Cur+Len+1,
759 (C1 > MaxSize, Cur>0
760 -> Shown=[], length([P|T],NrHidden)
761 ; Shown=[P|ST], limit_props_for_dot(T,C1,MaxSize,ST,NrHidden)).
762
763 convert_state_to_string_default(StateTempl,StateAsString) :-
764 findall(FProp,
765 (property(StateTempl,Prop),filter_elementary_prop(Prop,FProp)),
766 State), /* was fd_findall */
767 translate_params_for_dot_nl(State,StateAsString).
768
769 filter_elementary_prop(X,R) :- \+ b_or_z_mode,!,R=X.
770 filter_elementary_prop('='(Cst,_V),_) :- nonvar(Cst),
771 b_is_constant(Cst),!,fail.
772 filter_elementary_prop(Relation,_) :-
773 functor(Relation,Func,_),
774 b_is_constant(Func),!,fail.
775 filter_elementary_prop('='(_X,pred_false /* bool_false */),_) :- !,fail. /* do not show boolean variables that are false */
776 filter_elementary_prop('='( X,pred_true /* bool_true */), X) :- !. /* just show the boolean variable, not that it is = true */
777 filter_elementary_prop(X,X).
778
779
780 /* ---------------------- */
781 /* DEFINITION dependency */
782 /* ---------------------- */
783
784 % show definition call graph
785
786 :- use_module(dotsrc(dot_graph_generator), [gen_dot_graph/6, dot_no_same_rank/1, dot_no_subgraph/3,
787 use_new_dot_attr_pred/7]).
788 :- use_module(probsrc(bmachine),[b_machine_name/1]).
789 :- use_module(probsrc(tools_strings),[ajoin_with_sep/3]).
790 % one node per DEFINITION (coloured by kind), an edge D1 -> D2 if D1 uses D2
791 tcltk_print_definitions_as_graph_for_dot(File) :-
792 findall(Called, (b_get_definition(_,_,_,_,Deps), member(Called,Deps)),CD),
793 sort(CD,CalledDefs),
794 findall(Def,shown_definition(CalledDefs,Def),Shown),
795 (get_preference(dot_print_graph_title,true), b_machine_name(Main)
796 -> ajoin([Main,' - DEFINITIONS'],Title), get_preference(dot_title_font_size,TFS), TitleAttrs = [label/Title, labelloc/t, fontsize/TFS]
797 ; TitleAttrs = []),
798 gen_dot_graph(File,[graph_name/definitions, no_page_size, rankdir/'LR', fontname/'Helvetica', nodesep/0.2, ranksep/0.6,
799 node_defaults/[fontname/'Helvetica', fontsize/11, shape/box, style/'rounded,filled',
800 margin/'0.1,0.04'],
801 edge_defaults/[color/gray50, arrowsize/0.7]
802 | TitleAttrs],
803 use_new_dot_attr_pred(visualize_graph:dot_def_node(Shown)),
804 use_new_dot_attr_pred(visualize_graph:dot_def_trans(Shown)),
805 dot_no_same_rank,visualize_graph:dot_def_subgraph(Shown)).
806
807
808 :- use_module(probsrc(bmachine),[b_get_definition/5]).
809 :- use_module(library(ordsets),[ord_member/2]).
810
811 shown_definition(CalledDefs,DEFID) :-
812 b_get_definition(DEFID,_DefType,_Args,_Body,Deps),
813 (get_preference(dot_definitions_show_all,true) -> true
814 ; Deps = [_|_] -> true % show if it calls another one
815 ; ord_member(DEFID,CalledDefs) % show if called
816 ).
817
818 :- public dot_def_node/4, dot_def_trans/4, dot_def_subgraph/4.
819 dot_def_node([],no_defs,none,[label/Msg, shape/plaintext, style/'', fontcolor/gray40]) :-
820 (b_get_definition(_,_,_,_,_) -> Msg = 'No DEFINITIONS use other DEFINITIONS' ; Msg = 'No DEFINITIONS visible').
821 dot_def_node(Shown,DEFID,SubGraph,[label/Label, tooltip/Tooltip, fillcolor/Fill, color/Col | Legend]) :-
822 member(DEFID,Shown),
823 b_get_definition(DEFID,DefType,Args,_Body,Deps),
824 (get_preference(dot_definitions_use_sub_graph,true) -> SubGraph = DefType ; SubGraph=none),
825 dot_def_kind(DefType,Fill,Col,LText,Order),
826 (get_preference(dot_print_legend,true) -> Legend=[legend/LText,legend_order/Order] ; Legend=[]),
827 maplist(def_arg_name,Args,ArgNames),
828 (ArgNames = [] -> Label = DEFID
829 ; ajoin_with_sep(ArgNames,',',AS), ajoin([DEFID,'(',AS,')'],Label)),
830 length(Deps,NrDeps),
831 ajoin([LText,' ',Label,' uses ',NrDeps,' other DEFINITION(s)'],Tooltip).
832
833 def_arg_name(identifier(_,ID),R) :- !, R=ID.
834 def_arg_name(definition(_,ID,_),R) :- !, R=ID.
835 def_arg_name(_,'_').
836
837 dot_def_kind(predicate,'#E4F2E5','#2E7D32','predicate DEFINITION',10) :- !.
838 dot_def_kind(expression,'#E3EDF7','#1F5F99','expression DEFINITION',20) :- !.
839 dot_def_kind(substitution,'#F3E5F5','#6A1B9A','substitution DEFINITION',30) :- !.
840 dot_def_kind(_,white,gray50,'other DEFINITION',40).
841
842 dot_def_trans(Shown,NodeID,SuccNodeID,[tooltip/Tooltip]) :-
843 member(NodeID,Shown),
844 b_get_definition(NodeID,_DefType,_Args,_Body,Deps),
845 member(SuccNodeID,Deps),
846 ajoin([NodeID,' uses ',SuccNodeID],Tooltip).
847
848 dot_def_subgraph(Shown,sub_graph_with_attributes(SubGraph,[label/Label, fontname/'Helvetica', fontsize/11,
849 fillcolor/'#FAFAFA', pencolor/gray70, fontcolor/gray40]),
850 'rounded,filled',none) :-
851 get_preference(dot_definitions_use_sub_graph,true),
852 member(SubGraph,[predicate,expression,substitution]),
853 (member(D,Shown), b_get_definition(D,SubGraph,_,_,_) -> true),
854 ajoin([SubGraph,' DEFINITIONS'],Label).
855
856
857 /* Prediate dependency */
858
859 % call -dotexpr predicate_dependency Pred File
860 % separates a predicate into conjuncts and shows edges between predicates with common variables
861
862 :- use_module(probsrc(tools),[string_escape/2]).
863 :- use_module(probsrc(bsyntaxtree),[predicate_identifiers/2,conjunction_to_list/2]).
864 %
865 tcltk_print_predicate_dependency_as_graph_for_dot(Pred,File) :-
866 my_parse_predicate(Pred,Typed),
867 print_predicate_dependency_as_graph_for_dot(Typed,File).
868
869 :- use_module(probsrc(bmachine), [type_with_errors/4]).
870 :- use_module(probsrc(error_manager), [add_error/3]).
871 my_parse_predicate(RawAST,Typed) :- compound(RawAST),!,
872 type_with_errors(RawAST,[variables],Type,Typed),
873 (Type=pred -> true
874 ; add_error(visualize_graph,'Expected predicate by got:',Type),fail).
875 my_parse_predicate(Pred,Typed) :-
876 atom_codes(Pred,Codes),
877 eval_strings:repl_parse_predicate(Codes,exists,Typed,_).
878
879 % one node per conjunct; an undirected edge between two conjuncts sharing identifiers, labelled by them
880 print_predicate_dependency_as_graph_for_dot(Typed,File) :-
881 my_conjunction_to_list(Typed,Conj),
882 get_pred_ids(Conj,1,Preds),
883 length(Preds,NrPreds),
884 (get_preference(dot_print_graph_title,true)
885 -> ajoin(['Dependencies between ',NrPreds,' conjuncts'],Title),
886 get_preference(dot_title_font_size,TFS), TitleAttrs = [label/Title, labelloc/t, fontsize/TFS]
887 ; TitleAttrs = []),
888 gen_dot_graph(File,[graph_name/predicate_dependency, no_page_size, fontname/'Helvetica',
889 nodesep/0.3, ranksep/0.5,
890 node_defaults/[fontname/'Helvetica', fontsize/11, shape/box, style/'rounded,filled',
891 margin/'0.1,0.05'],
892 edge_defaults/[fontname/'Helvetica', fontsize/9, dir/none, color/'#1F5F99',
893 fontcolor/'#1F5F99']
894 | TitleAttrs],
895 use_new_dot_attr_pred(visualize_graph:dot_pred_node(Preds)),
896 use_new_dot_attr_pred(visualize_graph:dot_pred_trans(Preds)),
897 dot_no_same_rank,dot_no_subgraph).
898
899 my_conjunction_to_list(b(exists(_,Body),pred,_),L) :- !, my_conjunction_to_list(Body,L).
900 my_conjunction_to_list(B,L) :- conjunction_to_list(B,L).
901
902 get_pred_ids([],_,[]).
903 get_pred_ids([Pred|T],N,[pred(N,Pred,Ids)|PT]) :- predicate_identifiers(Pred,Ids),
904 N1 is N+1, get_pred_ids(T,N1,PT).
905
906 :- public dot_pred_node/4, dot_pred_trans/4.
907 dot_pred_node(Preds,PID,none,[label/htmllabel(Label), tooltip/Tooltip, fillcolor/Fill, color/Col | Legend]) :-
908 member(pred(PID,Pred,Ids),Preds),
909 translate:translate_bexpression_with_limit(Pred,80,Desc),
910 html_escape(Desc,EDesc),
911 ajoin(['<<font point-size="8" color="#707070">conjunct ',PID,'</font><br/>',EDesc,'>'],Label),
912 (shares_ids_with_other_pred(PID,Ids,Preds)
913 -> Fill='#E3EDF7', Col='#1F5F99', LText='conjunct', Order=10
914 ; Fill=white, Col=gray50, LText='conjunct sharing no identifier with others', Order=20),
915 (get_preference(dot_print_legend,true) -> Legend=[legend/LText,legend_order/Order] ; Legend=[]),
916 ajoin_with_sep(Ids,', ',IdsTxt),
917 translate:translate_bexpression_with_limit(Pred,1000,LongDesc),
918 ajoin(['conjunct ',PID,': ',LongDesc,'; identifiers: ',IdsTxt],Tooltip).
919
920 shares_ids_with_other_pred(PID,Ids,Preds) :-
921 member(pred(PID2,_,Ids2),Preds), PID2 \= PID,
922 ord_intersection(Ids,Ids2,Common), Common \= [], !.
923
924 :- use_module(library(ordsets),[ord_intersection/3]).
925 dot_pred_trans(Preds,NodeID,SuccNodeID,[label/Label, tooltip/Tooltip | Attrs]) :-
926 member(pred(NodeID,_,Ids1),Preds),
927 member(pred(SuccNodeID,_,Ids2),Preds), SuccNodeID>NodeID,
928 ord_intersection(Ids1,Ids2,Common), Common \= [],
929 ajoin_with_sep(Common,', ',Label),
930 ajoin(['conjuncts ',NodeID,' and ',SuccNodeID,' share ',Label],Tooltip),
931 length(Common,Nr),
932 (Nr > 1 -> PW is min(4,Nr), Attrs=[penwidth/PW] ; Attrs=[]).