comparison m-toolbox/classes/@smodel/smodel.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children a71a40911c27
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % SMODEL constructor for smodel class.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: SMODEL constructor for smodel class.
5 %
6 % CONSTRUCTOR:
7 %
8 % mdl = smodel() - creates an empty smodel object
9 % mdl = smodel('foo.mu') - construct from MuPAD file
10 % mdl = smodel('expression') - construct from a expression
11 % description
12 %
13 % <a href="matlab:utils.helper.displayMethodInfo('smodel', 'smodel')">Parameters Description</a>
14 %
15 % VERSION: $Id: smodel.m,v 1.42 2011/08/15 13:02:26 hewitson Exp $
16 %
17 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
18
19 classdef smodel < ltpda_uoh
20
21 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
22 % Property definition %
23 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
24
25 %---------- Public (read/write) Properties ----------
26 properties
27 end
28
29 %---------- Protected read-only Properties ----------
30 properties (SetAccess = protected)
31 expr = msym(''); % Expression of the model
32 params = {}; % Parameters which are used in the model
33 values = {}; % Default values for the parameters
34 trans = {}; % Transformation strings mapping xvals in terms of xvar to X in the model
35 aliasNames = {}; % {'v', 'H'};
36 aliasValues = {}; % {'a*b', [1:20]};
37 xvar = {}; % Cell-array with x-variable(s)
38 xvals = {}; % Cell-array of double-values for the different x-variable(s)
39 xunits = unit; % vector of units of the different x-axis
40 yunits = unit; % units of the y-axis
41 end
42
43 %---------- Private Properties ----------
44 properties (GetAccess = protected, SetAccess = protected)
45 end
46
47 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 % Check property setting %
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50
51 methods
52 %--- trans
53 function set.trans(obj, val)
54 if isempty(val)
55 if ~isempty(obj.trans)
56 % Get the default value of the property from the meta data
57 m = metaclass(obj);
58 p = [m.Properties{:}];
59 dv = p(strcmp({p(:).Name}, 'trans')).DefaultValue;
60 obj.trans = dv;
61 end
62 elseif iscell(val)
63 obj.trans = val;
64 elseif ischar(val)
65 obj.trans = {val};
66 elseif isnumeric(val)
67 obj.trans = {num2str(val)};
68 else
69 error('### The value for the property ''trans'' must be a cell-array. But it is from class [%s]', class(val));
70 end
71 end
72 %--- expr
73 function set.expr(obj, val)
74 if ischar(val)
75 obj.expr = msym(val);
76 elseif isa(val, 'msym')
77 obj.expr = val;
78 else
79 error('### The value for the property ''expr'' must be a MSYM object. But it is from class [%s]', class(val));
80 end
81 end
82 %--- params
83 function set.params(obj, val)
84 if isempty(val)
85 if ~isempty(obj.params)
86 % Get the default value of the property from the meta data
87 m = metaclass(obj);
88 p = [m.Properties{:}];
89 dv = p(strcmp({p(:).Name}, 'params')).DefaultValue;
90 obj.params = dv;
91 end
92 elseif iscellstr(val)
93 obj.params = val;
94 elseif ischar(val)
95 obj.params = cellstr(val);
96 else
97 error('### The value for the property ''params'' must be a cell of strings. But it is from class [%s]', class(val));
98 end
99 end
100 %--- values
101 function set.values(obj, val)
102 if iscell(val)
103 obj.values = val;
104 elseif isnumeric(val)
105 obj.values = num2cell(reshape(val, 1, []));
106 else
107 error('### The value for the property ''values'' must be a cell of numbers. But it is from class [%s]', class(val));
108 end
109 end
110 %--- aliasNames
111 function set.aliasNames(obj, val)
112 if isempty(val)
113 if ~isempty(obj.aliasNames)
114 % Get the default value of the property from the meta data
115 m = metaclass(obj);
116 p = [m.Properties{:}];
117 dv = p(strcmp({p(:).Name}, 'aliasNames')).DefaultValue;
118 obj.aliasNames = dv;
119 end
120 elseif ischar(val)
121 obj.aliasNames = cellstr(val);
122 elseif iscell(val)
123 obj.aliasNames = val;
124 else
125 error('### The value for the property ''aliasNames'' must be a cell of strings. But it is from class [%s]', class(val));
126 end
127 end
128 %--- aliasValues
129 function set.aliasValues(obj, val)
130 if isempty(val)
131 if ~isempty(obj.aliasValues)
132 % Get the default value of the property from the meta data
133 m = metaclass(obj);
134 p = [m.Properties{:}];
135 dv = p(strcmp({p(:).Name}, 'aliasValues')).DefaultValue;
136 obj.aliasValues = dv;
137 end
138 elseif isnumeric(val)
139 obj.aliasValues = num2cell(reshape(val, 1, []));
140 elseif ischar(val)
141 obj.aliasValues = cellstr(val);
142 elseif isa(val,'smodel')
143 obj.aliasValues = cell(val);
144 elseif iscell(val)
145 obj.aliasValues = val;
146 else
147 error('### The value for the property ''values'' must be a cell of numbers, strings or smodels. But it is from class [%s]', class(val));
148 end
149 end
150 %--- xvar
151 function set.xvar(obj, val)
152 % Convert a string into a cell-array. This is necessary for backwards compatibility
153 if isempty(val)
154 if ~isempty(obj.xvar)
155 % Get the default value of the property from the meta data
156 m = metaclass(obj);
157 p = [m.Properties{:}];
158 dv = p(strcmp({p(:).Name}, 'xvar')).DefaultValue;
159 obj.xvar = dv;
160 end
161 elseif iscell(val)
162 obj.xvar = val;
163 elseif ischar(val)
164 obj.xvar = cellstr(val);
165 else
166 error('### The value for the property ''xvar'' must be a string or a cell array of strings. But it is from class [%s]', class(val));
167 end
168 end
169 %--- xvals
170 function set.xvals(obj, val)
171 % Convert the value into a cell-array. This is necessary for backwards compatibility
172 if isempty(val)
173 if ~isempty(obj.xvals)
174 % Get the default value of the property from the meta data
175 m = metaclass(obj);
176 p = [m.Properties{:}];
177 dv = p(strcmp({p(:).Name}, 'xvals')).DefaultValue;
178 obj.xvals = dv;
179 end
180 elseif iscell(val)
181 obj.xvals = val;
182 elseif isnumeric(val)
183 obj.xvals = {val};
184 elseif isa(val, 'ao')
185 obj.xvals = {val.data.y};
186 else
187 error('### The value for the property ''xvals'' must be a cell-array. But it is from class [%s]', class(val));
188 end
189 end
190 %--- xunits
191 function set.xunits(obj, val)
192 switch class(val)
193 case 'char'
194 obj.xunits = unit(val);
195 case 'unit'
196 obj.xunits = val;
197 case 'cell'
198 obj.xunits = unit(val{:});
199 otherwise
200 error('### The value for the property ''xunits'' must be a array of unit-object(s)');
201 end
202 end
203 %--- yunits
204 function set.yunits(obj, val)
205 if ischar(val)
206 obj.yunits = unit(val);
207 elseif isa(val, 'unit')
208 obj.yunits = val;
209 else
210 error('### The value for the property ''yunits'' must be a unit-object or a string');
211 end
212 end
213 end
214
215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216 % Constructor %
217 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218
219 methods
220 function obj = smodel(varargin)
221
222 import utils.const.*
223 utils.helper.msg(msg.OMNAME, 'running %s/%s', mfilename('class'), mfilename);
224
225 % Collect all smodel objects
226 [mdls, invars, rest] = utils.helper.collect_objects(varargin(:), 'smodel');
227
228 if isempty(rest) && ~isempty(mdls)
229 % Do copy constructor and return
230 utils.helper.msg(msg.OPROC1, 'copy constructor');
231 obj = copy(mdls, 1);
232 for kk=1:numel(obj)
233 obj(kk).addHistory(smodel.getInfo('smodel', 'None'), [], [], obj(kk).hist);
234 end
235 return
236 end
237
238 switch nargin
239 case 0
240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 %%%%%%%%%%%%%%%%%%%%%%%%%%%% no input %%%%%%%%%%%%%%%%%%%%%%%%%%%*
242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 utils.helper.msg(msg.OPROC1, 'empty constructor');
244 obj.addHistory(smodel.getInfo('smodel', 'None'), plist(), [], []);
245
246 case 1
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248 %%%%%%%%%%%%%%%%%%%%%%%%%%% One input %%%%%%%%%%%%%%%%%%%%%%%%%%%
249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250
251 if ischar(varargin{1})
252 %%%%%%%%%% mdl = smodel('foo.mat') %%%%%%%%%%
253 %%%%%%%%%% mdl = smodel('foo.xml') %%%%%%%%%%
254 %%%%%%%%%% mdl = smodel('foo.mu') %%%%%%%%%%
255
256 % Is this a file?
257 [path, name, ext] = fileparts(varargin{1});
258
259 if ismember(ext, {'.xml', '.mat', '.txt', '.dat', '.fil'})
260 utils.helper.msg(msg.OPROC1, 'constructing from file %s', varargin{1});
261 obj = obj.fromFile(varargin{1});
262 else
263 obj = obj.fromExpression(plist('expression', varargin{1}));
264 end
265
266 elseif isnumeric(varargin{1})
267 %%%%%%%%%% mdl = smodel(123) %%%%%%%%%%
268 obj = obj.fromExpression(plist('expression', varargin{1}));
269
270 elseif isstruct(varargin{1})
271 %%%%%%%%%% mdl = smodel(struct) %%%%%%%%%%
272 utils.helper.msg(msg.OPROC1, 'constructing from struct');
273 obj = obj.fromStruct(varargin{1});
274
275 elseif isa(varargin{1}, 'sym')
276 %%%%%%%%%% mdl = smodel(symbolic-object) %%%%%%%%%%
277 utils.helper.msg(msg.OPROC1, 'constructing from symbol');
278 obj = obj.fromSymbol(plist('symbol', varargin{1}));
279
280 elseif isa(varargin{1}, 'plist')
281 %%%%%%%%%% mdl = smodel(plist-object) %%%%%%%%%%
282
283 pl = varargin{1};
284
285 if pl.isparam('expression')
286 utils.helper.msg(msg.OPROC1, 'constructing from expression');
287 obj = obj.fromExpression(pl);
288
289 elseif pl.isparam('filename')
290 utils.helper.msg(msg.OPROC1, 'constructing from filename [%s]', pl.find('filename'));
291 obj = obj.fromFile(pl);
292
293 elseif pl.isparam('hostname') || pl.isparam('conn')
294 utils.helper.msg(msg.OPROC1, 'constructing from repository %s', pl.find('hostname'));
295 obj = obj.fromRepository(pl);
296
297 elseif pl.isparam('built-in')
298 utils.helper.msg(msg.OPROC1, 'constructing from built-in model');
299 obj = fromModel(obj, pl);
300
301 else
302 obj.setObjectProperties(pl);
303 obj.addHistory(smodel.getInfo('smodel', 'None'), pl, [], []);
304 end
305
306 else
307 error('### Unknown single argument constructor.');
308 end
309
310 case 2
311 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312 %%%%%%%%%%%%%%%%%%%%%%%%%%% two input %%%%%%%%%%%%%%%%%%%%%%%%%%%
313 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314 if (isa(varargin{1}, 'database') || isa(varargin{1}, 'mpipeline.repository.RepositoryConnection')) && isnumeric(varargin{2})
315 %%%%%%%%%% mdl = smodel(<database-object>, [IDs]) %%%%%%%%%%
316 obj = obj.fromRepository(plist('conn', varargin{1}, 'id', varargin{2}));
317
318 elseif isa(varargin{1}, 'smodel') && isa(varargin{2}, 'plist') && isempty(varargin{2}.params)
319 %%%%%%%%%% f = smodel(smodel, <empty-plist>) %%%%%%%%%%
320 obj = smodel(varargin{1});
321
322 elseif isa(varargin{1}, 'org.apache.xerces.dom.DeferredElementImpl') && ...
323 isa(varargin{2}, 'history')
324 %%%%%%%%%% obj = smodel(DOM node, history-objects) %%%%%%%%%%
325 obj = fromDom(obj, varargin{1}, varargin{2});
326
327 elseif isa(varargin{1}, 'ltpda_uoh') && isa(varargin{2}, 'plist')
328 %%%%%%%%%%% smodel(<ltpda_uoh>-object, plist-object) %%%%%%%%%%
329 % always recreate from plist
330
331 % If we are trying to load from file, and the file exists, do
332 % that. Otherwise, copy the input object.
333 if varargin{2}.isparam('filename')
334 if exist(fullfile('.', find(varargin{2}, 'filename')), 'file')==2
335 obj = smodel(varargin{2});
336 else
337 obj = smodel(varargin{1});
338 end
339 else
340 obj = smodel(varargin{2});
341 end
342 else
343 error('### Unknown 2 argument constructor.');
344 end
345
346 otherwise
347 [mdls, invars, rest] = utils.helper.collect_objects(varargin, 'smodel');
348
349 %%% Do we have a list of smodels as input
350 if ~isempty(mdls) && isempty(rest)
351 obj = smodel(mdls);
352 else
353 error('### Unknown number of arguments.');
354 end
355 end
356
357 end % End constructor
358 end % End public methods
359
360 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
361 % Methods (Public, hidden) %
362 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
363
364 methods (Hidden = true)
365 varargout = attachToDom(varargin)
366 end
367
368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
369 % Methods (protected) %
370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
371 methods (Access = protected)
372 varargout = fromDatafile(varargin)
373 varargout = fromStruct(varargin)
374 varargout = fromDom(varargin)
375 end
376
377 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
378 % Methods (private) %
379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
380 methods (Access = private)
381 % Constructors
382 varargout = fromExpression(varargin)
383
384 % Others
385 varargout = sop(varargin)
386 end
387
388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
389 % Methods (static) %
390 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
391 methods (Static)
392
393 function mdls = getBuiltInModels(varargin)
394 mdls = ltpda_uo.getBuiltInModels('smodel');
395 end
396
397 function out = VEROUT()
398 out = '$Id: smodel.m,v 1.42 2011/08/15 13:02:26 hewitson Exp $';
399 end
400
401 function ii = getInfo(varargin)
402 ii = utils.helper.generic_getInfo(varargin{:}, 'smodel');
403 end
404
405 function out = SETS()
406 out = [SETS@ltpda_uoh, ...
407 {'From Expression'}, ...
408 {'From ASCII File'}];
409 end
410
411
412 function plout = getDefaultPlist(set)
413 persistent pl;
414 persistent lastset;
415 if ~exist('pl', 'var') || isempty(pl) || ~strcmp(lastset, set)
416 pl = smodel.buildplist(set);
417 lastset = set;
418 end
419 plout = pl;
420 end
421
422 function out = buildplist(set)
423
424 if ~utils.helper.ismember(lower(smodel.SETS), lower(set))
425 error('### Unknown set [%s]', set);
426 end
427
428 out = plist();
429 out = smodel.addGlobalKeys(out);
430 out = buildplist@ltpda_uoh(out, set);
431
432 switch lower(set)
433 case 'from expression'
434
435 % Expression
436 p = param({'expression','Expression of the model.'}, paramValue.EMPTY_STRING);
437 out.append(p);
438
439 % Params
440 p = param({'params','Parameters which are used in the model.'}, {1, {{}}, paramValue.OPTIONAL});
441 out.append(p);
442
443 % Values
444 p = param({'values','Default values for the parameters.'}, {1, {{}}, paramValue.OPTIONAL});
445 out.append(p);
446
447 % Xvar
448 p = param({'xvar','The X-dependent variable.'}, paramValue.EMPTY_STRING);
449 out.append(p);
450
451 % Xvals
452 p = param({'xvals','Values for the x-variable.'}, paramValue.EMPTY_CELL);
453 out.append(p);
454
455 % Yunits
456 p = param({'yunits','Units of the y output.'}, paramValue.EMPTY_STRING);
457 out.append(p);
458
459 % Xunits
460 p = param({'xunits','Units of the x output.'}, paramValue.EMPTY_STRING);
461 out.append(p);
462
463 case 'from ascii file'
464
465 % Filename
466 p = param({'filename','ASCII filename.'}, paramValue.EMPTY_STRING);
467 out.append(p);
468
469 % Xvals
470 p = param({'xvals','Values for the x-variable.'}, paramValue.EMPTY_CELL);
471 out.append(p);
472
473 % Yunits
474 p = param({'yunits','Units of the y output.'}, paramValue.EMPTY_STRING);
475 out.append(p);
476
477 % Xunits
478 p = param({'xunits','Units of the x output.'}, paramValue.EMPTY_STRING);
479 out.append(p);
480
481 end
482 end % function out = getDefaultPlist(varargin)
483
484 function obj = initObjectWithSize(n,m)
485 obj = smodel.newarray([n m]);
486 end
487
488 end % End static methods
489
490 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
491 % Methods (static, private) %
492 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
493
494 methods (Static, Access = private)
495 varargout = elementOp(varargin)
496 varargout = mergeFields(varargin)
497 end % End static, private methods
498
499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
500 % Methods (static, hidden) %
501 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
502
503 methods (Static = true, Hidden = true)
504 varargout = loadobj(varargin)
505 varargout = update_struct(varargin);
506 end
507
508 end % End classdef
509