Mercurial > hg > ltpda
comparison m-toolbox/classes/@history/hist2dot.m @ 0:f0afece42f48
Import.
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 23 Nov 2011 19:22:13 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f0afece42f48 |
---|---|
1 % HIST2DOT converts a history object to a 'DOT' file suitable for processing with graphviz | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % | |
4 % DESCRIPTION: HIST2DOT converts a history object to a 'DOT' file suitable for | |
5 % processing with graphviz (www.graphviz.org). | |
6 % | |
7 % CALL: hist2dot(h, 'foo.dot'); | |
8 % | |
9 % INPUT: h - history object | |
10 % foo.dot - file name to view the graphic with the programm | |
11 % graphviz | |
12 % | |
13 % VERSION: $Id: hist2dot.m,v 1.16 2011/02/18 16:48:52 ingo Exp $ | |
14 % | |
15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
16 | |
17 function varargout = hist2dot(varargin) | |
18 | |
19 %%% Set inputs | |
20 h = varargin{1}; | |
21 filename = varargin{2}; | |
22 pl = []; | |
23 if nargin > 2 | |
24 if isa(varargin{3}, 'plist') | |
25 pl = varargin{3}; | |
26 end | |
27 end | |
28 pl = combine(pl, getDefaultPlist); | |
29 stop_option = find(pl, 'stop_option'); | |
30 | |
31 %%% Write .dot file | |
32 | |
33 fd = fopen(filename, 'w+'); | |
34 | |
35 %%% write header | |
36 fprintf(fd, 'digraph G \n{\n'); | |
37 | |
38 old = 0; | |
39 | |
40 if old | |
41 | |
42 [n,a, nodes] = getNodes(h, stop_option); | |
43 nodesWritten = {}; | |
44 %%% Write block set | |
45 fprintf(fd, 'end [label="END"];\n'); | |
46 for j=1:length(nodes) | |
47 node = nodes(j); | |
48 | |
49 % tag = | |
50 | |
51 if ~containsNodeArray(nodesWritten, node) | |
52 | |
53 % wrap label | |
54 % Do some command substitution | |
55 fcn = node.names; | |
56 shape = 'rectangle'; | |
57 fsize = 12; | |
58 extras = ''; | |
59 | |
60 wstr = utils.prog.wrapstring([fcn char(node.params)], 25); | |
61 ss = ''; | |
62 for s=wstr | |
63 ss = [ss '\n' char(s)]; | |
64 end | |
65 ss = ss(3:end); | |
66 fprintf(fd, '%s [%s fontsize=%d shape=%s label="%s"];\n', nodeName(node), extras, fsize, shape, ss); | |
67 nodesWritten = [nodesWritten {node.h.UUID}]; | |
68 end | |
69 end | |
70 | |
71 %%% Write node list | |
72 fprintf(fd, '\n'); | |
73 fprintf(fd, '\n'); | |
74 | |
75 nodesConnected = []; | |
76 for j=length(nodes):-1:1 | |
77 % disp('----------------') | |
78 node = nodes(j); | |
79 % node.h | |
80 % if ~arrayContainsElement(nodesConnected, node.pn) | |
81 if node.pn > 0 | |
82 dst = nodes(node.pn); | |
83 fprintf(fd, '%s -> %s; # %d\n', nodeName(node), nodeName(dst), dst.pn); | |
84 % nodesConnected = [nodesConnected {node.h.UUID}]; | |
85 else | |
86 fprintf(fd, '%s -> end;\n', nodeName(node)); | |
87 end | |
88 nodesConnected = [nodesConnected node.pn]; | |
89 % end | |
90 % disp('----------------') | |
91 end | |
92 else | |
93 | |
94 % Get unique history nodes | |
95 [hists, links] = getHists(h, [], []); | |
96 | |
97 % Now write history blocks | |
98 fprintf(fd, 'end [label="END"];\n'); | |
99 for ll=1:numel(hists) | |
100 hist = hists(ll); | |
101 | |
102 % wrap label | |
103 % Do some command substitution | |
104 fcn = hist.methodInfo.mname; | |
105 shape = 'rectangle'; | |
106 fsize = 12; | |
107 extras = ''; | |
108 | |
109 wstr = utils.prog.wrapstring([fcn char(hist.plistUsed)], 25); | |
110 ss = ''; | |
111 for s=wstr | |
112 ss = [ss '\n' char(s)]; | |
113 end | |
114 ss = ss(3:end); | |
115 fprintf(fd, '%s [%s fontsize=%d shape=%s label="%s"];\n', ... | |
116 nodeName(hist), ... | |
117 extras, fsize, shape, ss); | |
118 | |
119 end | |
120 | |
121 fprintf(fd, '\n'); | |
122 fprintf(fd, '\n'); | |
123 | |
124 % Now write links | |
125 for kk=1:numel(links) | |
126 fprintf(fd, '%s', links{kk}); | |
127 end | |
128 | |
129 fprintf(fd, '%s -> end;\n', nodeName(h)); | |
130 end | |
131 | |
132 %%% close | |
133 fprintf(fd, '}\n'); | |
134 | |
135 %%% Close | |
136 fclose(fd); | |
137 | |
138 end | |
139 | |
140 | |
141 function [hists, links] = getHists(obj, links, hists) | |
142 | |
143 if isa(obj, 'history') && ~arrayContainsObjects(hists, obj) | |
144 pl = obj.plistUsed; | |
145 | |
146 for kk=1:pl.nparams | |
147 | |
148 val = pl.params(kk).getVal(); | |
149 if isa(val, 'history') | |
150 | |
151 for jj=1:numel(val) | |
152 if ~arrayContainsObjects(hists, val(jj)) | |
153 [hists, links] = getHists(val(jj), links, hists); | |
154 end | |
155 | |
156 % write link | |
157 l = sprintf('%s -> %s;\n', nodeName(val(jj)), nodeName(obj)); | |
158 links = [links {l}]; | |
159 end | |
160 | |
161 | |
162 | |
163 end | |
164 | |
165 end | |
166 | |
167 hists = [hists obj]; | |
168 end | |
169 | |
170 | |
171 if isprop(obj, 'hist') | |
172 if ~arrayContainsObjects(hists, obj.hist) | |
173 [hists, links] = getHists(obj.hist, links, hists); | |
174 end | |
175 end | |
176 | |
177 if isprop(obj, 'inhists') | |
178 for kk=1:numel(obj.inhists) | |
179 if ~arrayContainsObjects(hists, obj.inhists(kk)) | |
180 [hists, links] = getHists(obj.inhists(kk), links, hists); | |
181 end | |
182 | |
183 % write link | |
184 l = sprintf('%s -> %s;\n', nodeName(obj.inhists(kk)), nodeName(obj)); | |
185 links = [links {l}]; | |
186 end | |
187 end | |
188 | |
189 | |
190 end | |
191 | |
192 function res = arrayContainsObjects(array, obj) | |
193 res = false; | |
194 for kk=1:numel(array) | |
195 if strcmp(array(kk).UUID, obj.UUID) | |
196 res = true; | |
197 break; | |
198 end | |
199 end | |
200 end | |
201 | |
202 function nn = nodeName(node) | |
203 | |
204 nn = ['node_' strrep(node.UUID, '-', '_')]; | |
205 | |
206 | |
207 | |
208 end | |
209 | |
210 function res = arrayContainsElement(array, element) | |
211 | |
212 res = any(array == element); | |
213 | |
214 % res = false; | |
215 % for kk=1:numel(array) | |
216 % if strcmp(array(kk).UUID, element.UUID) | |
217 % res = true; | |
218 % break; | |
219 % end | |
220 % end | |
221 | |
222 end | |
223 | |
224 function res = containsNodeArray(nodes, node) | |
225 | |
226 x = @(c)(strcmp(c, node.h.UUID)); | |
227 res = any(cellfun(x, nodes)); | |
228 | |
229 % res = cellfun('strcmp', nodes, node); | |
230 | |
231 % res = false; | |
232 % for kk=1:numel(nodes) | |
233 % if strcmp(nodes{kk}, node.h.UUID) | |
234 % res = true; | |
235 % break; | |
236 % end | |
237 % end | |
238 end | |
239 | |
240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
241 % Local Functions % | |
242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
243 | |
244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
245 % | |
246 % FUNCTION: getDefaultPlist | |
247 % | |
248 % DESCRIPTION: Get Default Plist | |
249 % | |
250 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
251 | |
252 function plo = getDefaultPlist() | |
253 plo = plist('stop_option', 'full'); | |
254 end | |
255 |