comparison m-toolbox/classes/@plist/plist2cmds.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 % PLIST2CMDS convert a plist to a set of commands.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: PLIST2CMDS convert a plist to a set of commands.
5 %
6 % CALL: cmds = plist2cmds(pl)
7 %
8 % INPUTS:
9 % pl - parameter list (see below)
10 %
11 % OUTPUTS: cell-array of MATLAB commands.
12 %
13 %
14 % VERSION: $Id: plist2cmds.m,v 1.21 2011/03/28 12:45:31 hewitson Exp $
15 %
16 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
17
18 function cmd = plist2cmds(varargin)
19
20 %%% Check if this is a call for parameters
21 if utils.helper.isinfocall(varargin{:})
22 cmd = getInfo(varargin{3});
23 return
24 end
25
26 pl = varargin{1};
27
28 if numel(pl) ~= 1 || nargin ~= 1
29 error('### Please input (only) one plist');
30 end
31
32 % look at the input parameters
33 if isa(pl, 'plist')
34 [ps, before_pl] = writePlist(pl);
35 else
36 ps = '';
37 before_pl = '';
38 end
39 if strcmp(ps, 'plist([])')
40 ps = '';
41 end
42
43 if ~isempty(ps)
44 if ps(end) == ','
45 ps = ps(1:end-1);
46 end
47 end
48 cmd = sprintf('pl = %s;', ps);
49 cmd = {cmd before_pl{:}};
50 end
51
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53 % Local Functions %
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55
56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57 % %
58 % FUNCTION: writePlist %
59 % %
60 % DESCRIPTION: write a plist %
61 % %
62 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63 function [ps, before_pl] = writePlist(pls)
64
65 before_pl = {};
66 ps = '[';
67
68 for pp=1:numel(pls)
69 pl = pls(pp);
70 % if pl.nparams > 0
71 ps = [ps 'plist('];
72 for i=1:pl.nparams
73 p = pl.params(i);
74 pVal = p.getVal;
75
76 [cmd, pre_cmd] = val2cmd(pVal);
77
78 before_pl = [before_pl pre_cmd];
79
80 if isempty(p.desc)
81 ps = sprintf('%s''%s'', %s, ', ps, p.key, cmd);
82 else
83 ps = sprintf('%s{''%s'', ''%s''}, %s, ', ps, p.key, p.desc, cmd);
84 end
85
86 end
87 if strcmp(ps(end-1:end), ', ')
88 ps = ps(1:end-2);
89 end
90 ps = [ps '), '];
91 % end
92 end
93 if strcmp(ps(end-1:end), ', ')
94 ps = [ps(1:end-2) ']'];
95 end
96
97 end
98
99
100 function [cmd, pre_cmd] = val2cmd(pVal)
101 pre_cmd = {};
102 cmd = '';
103 if ischar(pVal)
104 %%% char %%%
105 cmd = sprintf('''%s''', strrep(pVal, '''', ''''''));
106
107 elseif isnumeric(pVal)
108 %%% numeric %%%
109 if isempty(pVal)
110 cmd = '[]';
111 else
112 cmd = sprintf('[%s]', utils.helper.mat2str(pVal));
113 end
114
115 elseif islogical(pVal)
116 %%% logical %%%
117 cmd = sprintf('[%s]', mat2str(pVal));
118
119 elseif isjava(pVal)
120 %%% java %%%
121 if strcmp(class(pVal), 'sun.util.calendar.ZoneInfo')
122 cmd = sprintf('java.util.TimeZone.getTimeZone(''%s'')',char(pVal.getID));
123 else
124 error('### Unknown java object [%s]', class(pVal));
125 end
126
127 elseif isa(pVal, 'history') || isa(pVal, 'ltpda_uoh')
128 %%% history %%%
129 if isa(pVal, 'ltpda_uoh')
130 h = [pVal(:).hist];
131 h = reshape(h, size(pVal));
132 else
133 h = pVal;
134 end
135
136 varnames = '[';
137
138 for vv1 = 1:size(h,1)
139 for hh1 = 1:size(h,2)
140
141 obj = h(vv1,hh1);
142 objpl = hist2m(obj);
143 objpl(1) = []; % drop last 'a_out' line
144 pre_cmd = [pre_cmd objpl];
145 varnames = [varnames strtok(objpl{1})];
146 if size(h,2) > 1 && hh1 ~= size(h,2)
147 varnames = [varnames, ', '];
148 end
149 end
150 if size(h,1) > 1 && vv1 ~= size(h,1)
151 varnames = [varnames, '; '];
152 end
153 end
154
155 cmd = [strtrim(varnames) ']'];
156
157 elseif isa(pVal, 'ltpda_nuo')
158 %%% non-user object %%%
159 cmd = string(pVal);
160
161 elseif isa(pVal, 'plist')
162 %%% plist object %%%
163 [cmd, pre_cmd] = writePlist(pVal);
164
165 elseif isa(pVal, 'sym')
166 %%% symbolic math object %%%
167 cmd = sprintf('sym(''%s'')', char(pVal));
168
169 elseif iscell(pVal)
170 %%% Cell %%%
171 if isempty(pVal)
172 cmd = sprintf('cell(%d,%d)', size(pVal,1), size(pVal,2));
173 else
174 cmd = '{';
175
176 for vv = 1:size(pVal,1)
177 for hh = 1:size(pVal,2)
178 [cell_cmd, cell_pre_cmd] = val2cmd(pVal{vv,hh});
179 cmd = [cmd, cell_cmd];
180 pre_cmd = [pre_cmd cell_pre_cmd];
181 if size(pVal,2) > 1 && hh ~= size(pVal,2)
182 cmd = [cmd, ', '];
183 end
184 end
185 if size(pVal,1) > 1 && vv ~= size(pVal,1)
186 cmd = [cmd, '; '];
187 end
188 end
189
190 cmd = [strtrim(cmd) '}'];
191 end
192
193 elseif isstruct(pVal)
194 %%% Struct %%%
195 ss = pVal;
196 ss_str = '[';
197 fields = fieldnames(ss);
198 for oo = 1:numel(pVal)
199 ss_str = sprintf('%s struct(', ss_str);
200 for ii = 1:numel(fields)
201 if isnumeric(ss(oo).(fields{ii})) || islogical(ss(oo).(fields{ii}))
202 ss_str = sprintf('%s''%s'', [%s], ', ss_str, fields{ii}, utils.helper.mat2str(ss(oo).(fields{ii})));
203 elseif ischar(ss(oo).(fields{ii}))
204 ss_str = sprintf('%s''%s'', ''%s'', ',ss_str, fields{ii}, strrep(ss(oo).(fields{ii}), '''', ''''''));
205 elseif isa(ss(oo).(fields{ii}), 'ltpda_nuo')
206 ss_str = sprintf('%s''%s'', %s, ', ss_str, fields{ii}, string(ss(oo).(fields{ii})));
207 elseif isa(ss(oo).(fields{ii}), 'plist')
208 [struct_cmd, struct_pre_cmd] = writePlist(ss(oo).(fields{ii}));
209 ss_str = sprintf('%s''%s'', %s, ', ss_str, fields{ii}, struct_cmd);
210 pre_cmd = [pre_cmd struct_pre_cmd];
211 elseif isa(ss(oo).(fields{ii}), 'ltpda_uoh')
212 h = ss(oo).(fields{ii}).hist;
213 varnames = '[';
214 for kk=1:numel(h)
215 obj = h(kk);
216 objpl = hist2m(obj);
217 objpl(1) = []; % drop last 'a_out' line
218 pre_cmd = [pre_cmd objpl];
219 varnames = [varnames strtok(objpl{1}) ' '];
220 end
221 ss_str = sprintf('%s''%s'', %s, ', ss_str, fields{ii}, [strtrim(varnames) ']']);
222 elseif isa(ss(oo).(fields{ii}), 'sym')
223 symstr = char(ss(oo).(fields{ii}));
224 ss_str = sprintf('%s''%s'', sym(''%s''), ', ss_str, fields{ii}, symstr);
225 elseif isjava(ss(oo).(fields{ii}))
226 if strcmp(class(ss(oo).(fields{ii})), 'sun.util.calendar.ZoneInfo')
227 ss_str = sprintf('%s''%s'', java.util.TimeZone.getTimeZone(''%s''), ', ss_str, fields{ii}, char(getID(ss(oo).(fields{ii}))));
228 else
229 error('### Unknown java object [%s]', class(ss(oo).(fields{ii})));
230 end
231 else
232 error('### Unknown type [%s] in struct', class(ss(oo).(fields{ii})));
233 end
234 end
235 ss_str = [ss_str(1:end-2), ')'];
236 end
237 cmd = sprintf('%s]', ss_str);
238 else
239 error(['### unknown parameter type: ' p.key ' ' class(pVal)]);
240 end
241 end
242
243
244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245 %
246 % FUNCTION: getInfo
247 %
248 % DESCRIPTION: Get Info Object
249 %
250 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251
252 function ii = getInfo(varargin)
253 if nargin == 1 && strcmpi(varargin{1}, 'None')
254 sets = {};
255 pl = [];
256 else
257 sets = {'Default'};
258 pl = getDefaultPlist;
259 end
260 % Build info object
261 ii = minfo(mfilename, 'plist', 'ltpda', utils.const.categories.internal, '$Id: plist2cmds.m,v 1.21 2011/03/28 12:45:31 hewitson Exp $', sets, pl);
262 end
263
264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265 %
266 % FUNCTION: getDefaultPlist
267 %
268 % DESCRIPTION: Get Default Plist
269 %
270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271
272 function plo = getDefaultPlist()
273 plo = plist();
274 end
275