comparison m-toolbox/classes/@ssm/ssm2dot.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 % SSM2DOT converts a statespace model object a DOT file.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: SSM2DOT converts a statespace model object a DOT file.
5 %
6 % CALL: ssm2dot(ssm, options);
7 %
8 % INPUTS: ssm - ssm object
9 % options - plist of options
10 %
11 % <a href="matlab:utils.helper.displayMethodInfo('ssm', 'ssm2dot')">Parameters Description</a>
12 %
13 % VERSION: $Id: ssm2dot.m,v 1.15 2011/04/08 08:56:24 hewitson Exp $
14 %
15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
16
17 function varargout = ssm2dot(varargin)
18
19 % Check if this is a call for parameters
20 if utils.helper.isinfocall(varargin{:})
21 varargout{1} = getInfo(varargin{3});
22 return
23 end
24
25 % starting initial checks
26 utils.helper.msg(utils.const.msg.MNAME, ['running ', mfilename]);
27
28 in_names = cell(size(varargin));
29 for ii = 1:nargin,in_names{ii} = inputname(ii);end
30
31 [inputSSMs, ssm_invars, rest] = utils.helper.collect_objects(varargin(:), 'ssm', in_names);
32 [pl, pl_invars, rest] = utils.helper.collect_objects(rest, 'plist');
33 if ~isempty(rest)
34 pl = combine(pl, plist(rest{:}));
35 end
36 options = combine(pl, getDefaultPlist());
37
38
39 filename = find(options, 'filename');
40 if isempty(filename)
41 error('### Please specify an output filename in the plist');
42 end
43
44 statesOn = find(options, 'States');
45 if strcmpi(statesOn, 'yes')
46 statesOn = true;
47 else
48 statesOn = false;
49 end
50
51 if numel(inputSSMs) ~= 1
52 error('### please input (only) one SSM object.');
53 end
54
55 % Find the last assemble in the history
56 [n,a, nodes] = getNodes(inputSSMs.hist, '');
57 ssmNodes = findAssembleInputs(nodes, 1);
58 % if we only get one node there must not be any assemble blocks so we can
59 % just use the first node.
60 if length(ssmNodes)==1 || isempty(ssmNodes)
61 ssmNodes = 1;
62 end
63
64 colors = {'blueviolet', 'chartreuse4', 'firebrick3', 'darkorange', 'navyblue', 'aquamarine3', 'deepskyblue'};
65
66 % What info do we need?
67 sss(length(ssmNodes),1) = ssm;
68 for j=1:length(ssmNodes)
69 node = nodes(ssmNodes(j));
70 % Collect the nodes to execute
71 % and convert to commands
72 cmds = hist2m(node.h);
73 % execute each command
74 for kk=numel(cmds):-1:1
75 eval(cmds{kk});
76 end
77 % add to outputs
78 sss(j) = a_out;
79 end
80 % Write .dot file
81 fd = fopen(filename, 'w+');
82 % write header
83 fprintf(fd, 'digraph G \n{\n');
84 fprintf(fd, '\trankdir="LR";\n');
85 fprintf(fd, '\tnode [style=filled, fillcolor=white fixedsize=false width=0.5 fontsize=16 shape=rectangle];\n');
86 fprintf(fd, '\tedge [penwidth=5];\n');
87 fprintf(fd, '\n\n');
88 % Write block set
89 for j=1:length(ssmNodes)
90 % get the object
91 ss = sss(j);
92 inputblocks = ss.inputnames;
93 inputvars = ss.inputvarnames;
94 outputblocks = ss.outputnames;
95 outputvars = ss.outputvarnames;
96 ssnames = ss.ssnames;
97 ssvarnames = ss.ssvarnames;
98 % Create sub graphs
99 fprintf(fd, '\tsubgraph cluster%d {\n', j);
100 fprintf(fd, '\t\tlabel="%s";\n', ss.name);
101 fprintf(fd, '\t\tfontcolor=black;\n');
102 fprintf(fd, '\t\tcolor=gray60;\n');
103 fprintf(fd, '\t\tstyle=filled;\n');
104 for k=1:numel(inputblocks)
105 fprintf(fd, '\t\tsubgraph cluster%d%d_in {\n', j, k);
106 fprintf(fd, '\t\t\tlabel="%s";\n', inputblocks{k});
107 fprintf(fd, '\t\t\tstyle=filled;\n');
108 fprintf(fd, '\t\t\tcolor=yellow1;\n');
109 fprintf(fd, '\t\t\tfontcolor=black;\n');
110 for l=1:numel(inputvars{k})
111 fprintf(fd, '\t\t\tssm_in_%d_%d_%d [label="%s"];\n', j,k,l,inputvars{k}{l});
112 end
113 fprintf(fd, '\t\t}\n');
114 end
115 for k=1:numel(outputblocks)
116 fprintf(fd, '\t\tsubgraph cluster%d%d_out {\n', j, k);
117 fprintf(fd, '\t\t\tlabel="%s";\n', outputblocks{k});
118 fprintf(fd, '\t\t\tstyle=filled;\n');
119 fprintf(fd, '\t\t\tcolor=lightblue1;\n');
120 for l=1:numel(outputvars{k})
121 fprintf(fd, '\t\t\tssm_out_%d_%d_%d [label="%s"];\n', j,k,l,outputvars{k}{l});
122 end
123 fprintf(fd, '\t\t}\n');
124 end
125 if statesOn
126 for k=1:numel(ssnames)
127 fprintf(fd, '\t\tsubgraph cluster%d%d_state {\n', j, k);
128 fprintf(fd, '\t\t\tlabel="%s";\n', ssnames{k});
129 fprintf(fd, '\t\t\tstyle=filled;\n');
130 fprintf(fd, '\t\t\tcolor=wheat1;\n');
131 for l=1:numel(ssvarnames{k})
132 fprintf(fd, '\t\t\tssm_state_%d_%d_%d [label="%s"];\n', j,k,l,ssvarnames{k}{l});
133 end
134 fprintf(fd, '\t\t}\n');
135 end
136 end
137 fprintf(fd, '\t}\n');
138 fprintf(fd, '\n\n');
139 end
140 % Write node list
141 fprintf(fd, '\n');
142 fprintf(fd, '\n');
143 nl = 1;
144 for j=1:length(ssmNodes)
145 % get the object
146 ss = sss(j);
147 inputblocks = ss.inputnames;
148 inputvars = ss.inputvarnames;
149 % Now join outputs to inputs
150 for k=1:numel(inputblocks)
151 iblock = inputblocks{k};
152 % look for a matching output block
153 for oj = 1:length(ssmNodes)
154 oss = sss(oj);
155 outputblocks = oss.outputnames;
156 outputvars = oss.outputvarnames;
157 for ok=1:numel(outputblocks)
158 if strcmp(outputblocks{ok}, iblock)
159 % make a connection from each output to each input
160 for ol = 1:numel(outputvars{ok})
161 col = colors{mod(nl, numel(colors))+1};
162 % draw a line
163 fprintf(fd, 'ssm_out_%d_%d_%d -> ssm_in_%d_%d_%d [color="%s"];\n', oj, ok, ol, j, k, ol, col);
164 nl = nl + 1;
165 end
166 end
167 end
168 end
169 end
170 end
171 fprintf(fd, '\n');
172 fprintf(fd, '\n');
173 % close graph
174 fprintf(fd, '}\n');
175 % Close
176 fclose(fd);
177
178 end
179
180 %--------- Get the input SSMs to all assemble blocks
181 function idx = findAssembleInputs(nodes, an)
182 idx = [];
183 % get children
184 children = findChildNodes(nodes, an, '');
185 % find all sub-assembles or ssm end points
186 for j=1:numel(children)
187 ch = children(j);
188 subasmbl = findChildNodes(nodes, ch, 'assemble');
189 if isempty(subasmbl)
190 idx = [idx ch];
191 else
192 idx = [idx findAssembleInputs(nodes, ch)];
193 end
194 end
195 end
196
197 %---- Find particular child nodes
198 function idx = findChildNodes(nodes, pn, name)
199
200 idx = [];
201 if strcmp(nodes(pn).names, name)
202 idx = [idx pn];
203 end
204 Nnodes = numel(nodes);
205 for j=pn:Nnodes
206 if nodes(j).pn == pn
207 if isempty(name)
208 idx = [idx j];
209 else
210 if strcmp(nodes(j).names, name)
211 idx = [idx j]; % we have what we want
212 else
213 if j<Nnodes
214 idx = [idx findChildNodes(nodes, nodes(j).n, name)]; % check below
215 end
216 end
217 end
218 end
219 end
220 end
221
222
223
224 %--------------------------------------------------------------------------
225 % Get Info Object
226 %--------------------------------------------------------------------------
227 function ii = getInfo(varargin)
228
229 if nargin == 1 && strcmpi(varargin{1}, 'None')
230 sets = {};
231 pl = [];
232 else
233 sets = {'Default'};
234 pl = getDefaultPlist;
235 end
236 % Build info object
237 ii = minfo(mfilename, 'ssm', 'ltpda', utils.const.categories.internal, '$Id: ssm2dot.m,v 1.15 2011/04/08 08:56:24 hewitson Exp $', sets, pl);
238 end
239
240 %--------------------------------------------------------------------------
241 % Get Default Plist
242 %--------------------------------------------------------------------------
243 function pl = getDefaultPlist()
244 pl = plist();
245
246 p = param({'filename', 'The output filename to save the graphic to.'}, paramValue.EMPTY_STRING);
247 pl.append(p);
248
249 p = param({'states', 'Draw the states of each model.'}, paramValue.TRUE_FALSE);
250 p.val.setValIndex(2);
251 pl.append(p);
252 end
253