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(state_as_dot_graph,[
6 print_cstate_graph/2, print_cstate_graph/1]).
7
8 :- use_module(probsrc(module_information)).
9 :- module_info(group,dot).
10 :- module_info(description,'Display canonical form of state graph using dot.').
11
12 :- use_module(extrasrc(state_graph_canon),[state_graph_for_dot/2]).
13
14
15 /* ---------------------------- */
16 /* DOT generation */
17 /* ---------------------------- */
18
19
20 :- use_module(probsrc(error_manager)).
21 :- use_module(probsrc(preferences)).
22 :- use_module(probsrc(state_space),[current_expression/2]).
23 :- use_module(probsrc(tools),[ajoin/2, string_escape/2]).
24 :- use_module(library(lists),[maplist/3]).
25
26 print_cstate_graph(File) :-
27 current_expression(_,State),
28 print_cstate_graph(State,File).
29 print_cstate_graph(State,File) :-
30 (get_preference(dot_state_graph_show_constants,false)
31 -> remove_constants(State,State2) ; State2=State),
32 state_graph_for_dot(State2,GCE),
33 open(File,write,Stream,[encoding(utf8)]),
34 (graph2dot(Stream,GCE) -> close(Stream)
35 ; close(Stream),
36 add_error(graph_canon,'Dot conversion failed: ',graph2dot(File,GCE))
37 ).
38
39
40 % remove the constants from a state, keeping only the variables
41 remove_constants(const_and_vars(_,Vars),R) :- !, R=Vars.
42 remove_constants(concrete_constants(_),R) :- !, R=[].
43 remove_constants(csp_and_b(P,S),R) :- !, R=csp_and_b(P,S2), remove_constants(S,S2).
44 remove_constants(S,S).
45
46 graph2dot(Stream,G0) :- reset_dot,
47 (get_preference(dot_horizontal_layout,true) -> RDir='LR'; RDir='TB'),
48 format(Stream,'digraph state {~n',[]),
49 format(Stream,' graph [rankdir=~w, fontname="Helvetica", fontsize=11, nodesep=0.25, ranksep=0.45];~n',[RDir]),
50 format(Stream,' node [fontname="Helvetica", fontsize=10, color="#6B7785", penwidth=0.8];~n',[]),
51 format(Stream,' edge [fontname="Helvetica", fontsize=9, arrowsize=0.7];~n',[]),
52 % variables whose value is the empty set are encoded as self-loops on the root; show them inside the root node
53 split_empty_set_variables(G0,EmptyVars,G),
54 (EmptyVars=[] -> true ; translate_node_value(sg_root,_)),
55 find_pair_nodes(G),
56 edges2dot(G,Stream),
57 nodes2dot(EmptyVars,Stream),
58 findall(cluster(Ty,El),cluster(Ty,El),T),
59 clusters2dot(T,Stream),
60 format(Stream,'}~n',[]).
61
62 split_empty_set_variables([],[],[]).
63 split_empty_set_variables([(sg_root,V,sg_root)|T],[V|TV],R) :- !, split_empty_set_variables(T,TV,R).
64 split_empty_set_variables([E|T],TV,[E|R]) :- split_empty_set_variables(T,TV,R).
65
66 % abstract nodes representing a pair have an outgoing $from edge (to the first component)
67 :- dynamic pair_node/1.
68 find_pair_nodes(G) :- retractall(pair_node(_)),
69 (member((abs_cn(A,C),'$from',_),G), \+ pair_node(abs_cn(A,C)),
70 assertz(pair_node(abs_cn(A,C))), fail ; true).
71
72 :- use_module(probsrc(b_global_sets)).
73 cluster(Type,Elements) :- b_global_set(Type),
74 % compute all elements of global sets which are used in current state graph
75 findall(Translation, (enum_global_type_limited(El,Type), node_col(El,Translation,_,_)),Elements).
76 % maybe add boolean ??
77 edges2dot([],_).
78 edges2dot([(A,B,C)|T],Stream) :- /* edge from A -> C with label B */
79 translate_node_value(A,AID),
80 (B = abs_cn(BB,ColBB)
81 -> translate_abs_cn_node(BB,ColBB,EdgeLabel)
82 ; EdgeLabel = B),
83 translate_node_value(C,CID),
84 (internal_edge(EdgeLabel,A,Label,Attrs)
85 -> format(Stream,'\"~w\" -> \"~w\" [label = \"~w\", ~w];\n', [AID,CID,Label,Attrs])
86 ; get_col_assoc(edge,EdgeLabel,EdgCol),
87 format(Stream,'\"~w\" -> \"~w\" [label = \"~w\", color = \"~w\", fontcolor = \"~w\"];\n',
88 [AID,CID,EdgeLabel,EdgCol,EdgCol])
89 ),
90 edges2dot(T,Stream).
91
92 % edges used to encode nested values (sets of sets, pairs of sets, ...)
93 internal_edge('$to_el',From,Label,Attrs) :-
94 (pair_node(From) -> Label=prj2 ; Label='\x2208\'), % element of
95 Attrs = 'color = "#9AA3AD", fontcolor = "#6B7785", style = dashed'.
96 internal_edge('$from',_,prj1,'color = "#9AA3AD", fontcolor = "#6B7785", style = dashed').
97
98 reset_dot :- retractall(col_association(_,_,_)),
99 retractall(node_col(_,_,_,_)),
100 retractall(cur_col(_,_)),
101 assertz(cur_col(node,0)), assertz(cur_col(edge,99)).
102
103 :- use_module(probsrc(translate),[translate_bvalue_for_dot/2]).
104 translate_node_value(abs_cn(AA,ColAA), Translation) :- !,
105 translate_abs_cn_node(AA,ColAA,Translation),
106 add_node(abs_cn(AA,ColAA),Translation).
107 translate_node_value(sg_root,Translation) :- !,
108 Translation = 'ROOT-NODE', add_node(sg_root,Translation).
109 translate_node_value(Val,Translation) :-
110 translate_bvalue_for_dot(Val,Translation),
111 add_node(Val,Translation).
112
113 nodes2dot(EmptyVars,Stream) :- node_col(Val,Translation,Col,Shape),
114 (Val=sg_root
115 -> root_label(EmptyVars,Label),
116 format(Stream,'\"~w\" [shape=box, style=\"filled,rounded\", fillcolor=\"#E3EAF0\", color=\"#607D8B\", penwidth=1.2, fontcolor=\"#263238\", label=<~w>]~n',
117 [Translation,Label])
118 ; Val=abs_cn(_,_)
119 -> (pair_node(Val) -> L='( , )' ; L='{ }'),
120 format(Stream,'\"~w\" [shape=circle, style=filled, fillcolor=\"#ECEFF1\", color=\"#9AA3AD\", fontcolor=\"#455A64\", fontsize=9, width=0.35, fixedsize=true, label=\"~w\", tooltip=\"~w\"]~n',
121 [Translation,L,Translation])
122 ; Shape=record(Label)
123 -> format(Stream,'\"~w\" [shape=record, label=\"~w\", fillcolor = \"~w\", style = filled]~n',
124 [Translation,Label,Col])
125 ; (Shape=box -> Style='filled,rounded' ; Style=filled),
126 format(Stream,'\"~w\" [fillcolor = \"~w\", style = \"~w\", shape = \"~w\"]~n',[Translation,Col,Style,Shape])
127 ),
128 fail.
129 nodes2dot(_,_).
130
131 :- use_module(probsrc(tools),[html_escape/2]).
132 :- use_module(probsrc(tools_strings),[ajoin_with_sep/3]).
133 root_label([],'<B>STATE</B>') :- !.
134 root_label(EmptyVars,Label) :-
135 sort(EmptyVars,Sorted),
136 maplist(html_escape,Sorted,Esc),
137 group_names(Esc,4,Lines),
138 ajoin_with_sep(Lines,'<BR/>',Names),
139 ajoin(['<TABLE BORDER="0" CELLBORDER="0" CELLPADDING="2"><TR><TD><B>STATE</B></TD></TR>',
140 '<TR><TD><FONT POINT-SIZE="9" COLOR="#546E7A">= ∅: ',Names,'</FONT></TD></TR></TABLE>'],Label).
141
142 % put at most N names on each line
143 group_names([],_,[]) :- !.
144 group_names(Names,N,[Line|Lines]) :-
145 length(Names,Len),
146 (Len =< N -> First=Names, Rest=[]
147 ; length(First,N), append(First,Rest,Names)),
148 ajoin_with_sep(First,', ',Line),
149 group_names(Rest,N,Lines).
150
151 :- use_module(probsrc(kernel_reals),[is_real/2]).
152 :- dynamic node_col/4.
153 add_node(X,_) :- node_col(X,_,_,_),!. % already processed
154 add_node(sg_root,Translation) :-
155 assertz(node_col(sg_root,Translation,'#E3EAF0',box)).
156 add_node(abs_cn(A,C),Translation) :- !,
157 assertz(node_col(abs_cn(A,C),Translation,'#ECEFF1',circle)).
158 add_node(fd(X,Type),Translation) :- !,
159 get_col_assoc(node,Type,Col),
160 get_preference(dot_normal_node_shape,Shape),
161 assertz(node_col(fd(X,Type),Translation,Col,Shape)).
162 add_node(int(X),Translation) :- !,
163 Col = '#F3E9D2', %get_col_assoc(node,integer,Col),
164 get_preference(dot_normal_node_shape,Shape),
165 assertz(node_col(int(X),Translation,Col,Shape)).
166 add_node(string(X),Translation) :- !,
167 Col = '#FFF4C2', %get_col_assoc(node,integer,Col),
168 get_preference(dot_normal_node_shape,Shape),
169 assertz(node_col(string(X),Translation,Col,Shape)).
170 add_node(pred_true,T) :- !, assertz(node_col(pred_true,T,'#CDE8C0',ellipse)).
171 add_node(pred_false,T) :- !, assertz(node_col(pred_false,T,'#F6D0CB',ellipse)).
172 add_node(RecordOrPair,Translation) :- get_fields(RecordOrPair,Fields),!,
173 translate_fields(Fields,Atoms),
174 ajoin(['|{ ' | Atoms], RecTranslation),
175 assertz(node_col(RecordOrPair,Translation,'#F1E3CB',record(RecTranslation))). % TO DO: maybe use dot record
176 add_node(Val,Translation) :- is_set(Val),!,
177 Col = '#DCE6F2', %get_col_assoc(node,integer,Col),
178 get_preference(dot_normal_node_shape,Shape),
179 assertz(node_col(Val,Translation,Col,Shape)).
180 add_node(Term,Translation) :- is_real(Term,_),!,
181 Col = '#F3E9D2', %get_col_assoc(node,integer,Col),
182 get_preference(dot_normal_node_shape,Shape),
183 assertz(node_col(Term,Translation,Col,Shape)).
184 add_node(_Val,_Translation).
185
186 % copied from state_graph_canon:
187 list_set([]).
188 list_set([_|_]).
189 is_set(avl_set(_)).
190 is_set(X) :- list_set(X).
191
192 get_fields(rec(Fields),Fields).
193 get_fields((A,B),Fields) :- get_pair_fields((A,B),Fields,[]).
194
195 get_pair_fields((A,B)) --> !, get_pair_fields(A), get_pair_fields(B).
196 get_pair_fields(A) --> [field(prj,A)].
197
198 translate_fields([],[' }|']).
199 translate_fields([field(Name,Value)],Res) :- !,
200 trans_field(Name,VS,Res,[' }|']),
201 translate_bvalue_for_dot(Value,VS).
202 translate_fields([field(Name,Value)|T],Res) :-
203 trans_field(Name,VS,Res,['|' | Rest]),
204 translate_bvalue_for_dot(Value,VS),
205 translate_fields(T,Rest).
206
207 trans_field(prj,VS, [' { ', VS, ' } ' | Rest], Rest) :- !.
208 trans_field(Name,VS, [' { ',Name,' | ', VS, ' } ' | Rest], Rest).
209
210 :- dynamic col_association/3. % store colours used for types, edge labels,...
211 get_col_assoc(Domain,T,Col) :- col_association(Domain,T,Col),!.
212 get_col_assoc(Domain,T,Col) :- get_next_col(Domain,Col),
213 assertz(col_association(Domain,T,Col)).
214
215 get_next_col(Type,Col) :- retract(cur_col(Type,N)),!, N1 is N+1,
216 (default_colours(N1,Col) -> NX=N1
217 ; first_colour(Type,NX), default_colours(NX,Col)), % cycle within the palette of the domain
218 assertz(cur_col(Type,NX)).
219 get_next_col(_T,red).
220
221 first_colour(node,1).
222 first_colour(edge,100).
223
224 :- dynamic cur_col/2.
225 cur_col(node,0).
226 cur_col(edge,99).
227 % pastel fill colours for the elements of deferred/enumerated sets (black text remains readable)
228 default_colours(1,'#FCE8B2').
229 default_colours(2,'#D9EAD3').
230 default_colours(3,'#CFE2F3').
231 default_colours(4,'#EAD1DC').
232 default_colours(5,'#FFE0C2').
233 default_colours(6,'#D9D2E9').
234 default_colours(7,'#D0ECE7').
235 default_colours(8,'#E6E6E6').
236
237 % darker colours for edges (one per variable/constant); also used for the edge labels
238 default_colours(100,'#1F77B4').
239 default_colours(101,'#D62728').
240 default_colours(102,'#2CA02C').
241 default_colours(103,'#9467BD').
242 default_colours(104,'#8C564B').
243 default_colours(105,'#E377C2').
244 default_colours(106,'#7F7F7F').
245 default_colours(107,'#B8860B').
246 default_colours(108,'#17BECF').
247 default_colours(109,'#FF7F0E').
248
249 translate_abs_cn_node(AA,ColAA,Translation) :-
250 tools:string_concatenate('_',AA,AID1),
251 tools:string_concatenate(ColAA,AID1,AID2),
252 tools:string_concatenate('abs',AID2,Translation).
253
254 clusters2dot([],_).
255 clusters2dot([cluster(_,[])|T],Stream) :- !, % no element of the set occurs in the state
256 clusters2dot(T,Stream).
257 clusters2dot([cluster(Name,Elements)|T],Stream) :-
258 string_escape(Name,EscName), html_escape(Name,HName),
259 format(Stream,'subgraph \"cluster_~w\" {label=<<B>~w</B>>; style=\"filled,rounded\"; fillcolor=\"#F5F7F9\"; color=\"#C9D1D9\"; fontsize=10; fontcolor=\"#455A64\"; ',[EscName,HName]),
260 cnodes2dot(Elements,Stream),
261 clusters2dot(T,Stream).
262
263 :- use_module(probsrc(tools),[string_escape/2]).
264 cnodes2dot([],Stream) :- format(Stream,'}~n',[]).
265 cnodes2dot([H|T],Stream) :-
266 translate:translate_params_for_dot([H],HP),
267 string_escape(HP,HPE),
268 format(Stream,'~w; ',[HPE]),
269 cnodes2dot(T,Stream).