comparison m-toolbox/classes/@smodel/addAliases.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 % ADDALIASES Add the key-value pairs to the alias-names and alias-values
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: ADDALIASES Add the key-value pairs to the alias-names and
5 % alias-values. This method will add the key-value pairs to
6 % the properties 'aliasNames' and 'aliasValues'. If the
7 % alias-name exist then replace this method the existing value
8 % with the new value.
9 %
10 % CALL: obj = obj.addAliases('name', 'value');
11 % obj = obj.addAliases(key-value pairs);
12 % obj = obj.addAliases(plist);
13 % obj = obj.addAliases(two cell-arrays);
14 %
15 % INPUTS: obj - a ltpda smodel object.
16 % key-value - A single key-value pair or a list of key-value
17 % pairs. Thereby it is important that the key is
18 % followed by the value.
19 % plist - Parameter list with values for the keys
20 % 'names' and 'values'. The values can be a
21 % single value or a cell array with multiple
22 % values. The number of 'names' and 'values'
23 % must be the same.
24 % cell-arrays - Two cell-arrays with the first of the 'names'
25 % and the second with the 'values'.
26 %
27 % REMARK: This function will replace the alias-value if the
28 % alias-names already exist
29 %
30 % <a href="matlab:utils.helper.displayMethodInfo('smodel', 'addAliases')">Parameters Description</a>
31 %
32 % VERSION: $Id: addAliases.m,v 1.1 2011/04/11 19:51:07 ingo Exp $
33 %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35
36 function varargout = addAliases(varargin)
37
38 % Check if this is a call from a class method
39 callerIsMethod = utils.helper.callerIsMethod;
40
41 if callerIsMethod
42 sm = varargin{1}; % smodel-object(s)
43 names = varargin{2}; % cell-array with alias-names
44 values = varargin{3}; % call-array with alias-values
45
46 else
47 % Check if this is a call for parameters
48 if utils.helper.isinfocall(varargin{:})
49 varargout{1} = getInfo(varargin{3});
50 return
51 end
52
53 import utils.const.*
54 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
55
56 % Collect input variable names
57 in_names = cell(size(varargin));
58 for ii = 1:nargin,in_names{ii} = inputname(ii);end
59
60 % Collect all smodel objects
61 [sm, sm_invars, rest] = utils.helper.collect_objects(varargin(:), 'smodel', in_names);
62 [pls, dummy, rest] = utils.helper.collect_objects(rest(:), 'plist');
63
64 names = {};
65 values = {};
66 %%% If the input PLIST have the keys 'names' and 'values' then use also
67 %%% the
68 if length(pls) == 1 && isa(pls, 'plist') && isparam(pls, 'names') && isparam(pls, 'values')
69 names = find(pls, 'names');
70 values = find(pls, 'values');
71
72 % Make sure that the names and the values are cell-arrays
73 if ~iscell(names)
74 names = cellstr(names);
75 end
76 if ~iscell(values)
77 values = num2cell(values);
78 end
79
80 end
81
82 % Check if we have two cell-array for the 'names' and 'values'
83 if numel(rest) == 2 && iscell(rest{1}) && iscell(rest{2})
84 names = [names rest{1}];
85 values = [values rest{2}];
86 else
87
88 % Check for key-value pairs
89 if mod(numel(rest), 2) ~= 0
90 error('### Please specify for each alias-name a alias-value');
91 end
92 nrest = numel(rest);
93 names = [names rest(1:2:nrest)];
94 values = [values rest(2:2:nrest)];
95 end
96
97 % Combine input plists and default PLIST
98 pls = combine(pls, getDefaultPlist());
99
100 end
101
102 % Check that we have the same number of names as the number of values.
103 if numel(names) ~= numel(values)
104 error('### Please specify for each alias name [%d] one alias value [%d]', numel(names), numel(values));
105 end
106
107 % Decide on a deep copy or a modify
108 sm = copy(sm, nargout);
109
110 % Loop over smodel objects
111 for oo=1:numel(sm)
112
113 for kk=1:numel(names)
114 % Look for the index we have to change
115 idx = strcmp(sm(oo).aliasNames, names{kk});
116
117 if any(idx)
118 % Change at the index the value
119 sm(oo).aliasValues{idx} = values{kk};
120 else
121 % Append the alias key-value pair
122 sm(oo).aliasNames = [sm(oo).aliasNames names(kk)];
123 sm(oo).aliasValues = [sm(oo).aliasValues values(kk)];
124 end
125 end
126
127 if ~callerIsMethod
128 plh = pls.pset('names', names);
129 plh.pset('values', values);
130 sm(oo).addHistory(getInfo('None'), plh, sm_invars(oo), sm(oo).hist);
131 end
132 end
133
134 % Set output
135 varargout = utils.helper.setoutputs(nargout, sm);
136 end
137
138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139 %
140 % FUNCTION: getInfo
141 %
142 % DESCRIPTION: Get Info Object
143 %
144 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145
146 function ii = getInfo(varargin)
147 if nargin == 1 && strcmpi(varargin{1}, 'None')
148 sets = {};
149 pl = [];
150 else
151 sets = {'Default'};
152 pl = getDefaultPlist;
153 end
154 % Build info object
155 ii = minfo(mfilename, mfilename('class'), 'ltpda', utils.const.categories.helper, '$Id: addAliases.m,v 1.1 2011/04/11 19:51:07 ingo Exp $', sets, pl);
156 end
157
158 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159 %
160 % FUNCTION: getDefaultPlist
161 %
162 % DESCRIPTION: Get Default Plist
163 %
164 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165
166 function plout = getDefaultPlist()
167 persistent pl;
168 if exist('pl', 'var')==0 || isempty(pl)
169 pl = buildplist();
170 end
171 plout = pl;
172 end
173
174 function pl = buildplist()
175 pl = plist();
176
177 % alias names
178 p = param({'names', 'A cell-array with the alias names.'}, paramValue.EMPTY_CELL);
179 pl.append(p);
180
181 % alias values
182 p = param({'values', 'A cell-array with the alias values.'}, paramValue.EMPTY_CELL);
183 pl.append(p);
184 end
185