Mercurial > hg > ltpda
comparison m-toolbox/classes/@plist/parse.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 % PARSE a plist for strings which can be converted into numbers | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % | |
4 % DESCRIPTION: PARSE a plist for strings which can be converted into numbers | |
5 % depending on the default plist | |
6 % | |
7 % CALL: plo = parse(pl, dpl) | |
8 % | |
9 % EXAMPLE: pl = plist('x', '3', 'y', 'x+5', 'z', 'x-y') | |
10 % dpl = plist('x', [], 'y', [], 'z', []) | |
11 % | |
12 % pl = plist('x', '1:12', 'y', 'randn(length(x),1)') | |
13 % dpl = plist('x', [], 'y', [], 'z', []) | |
14 % | |
15 % VERSION: $Id: parse.m,v 1.17 2011/03/28 12:45:31 hewitson Exp $ | |
16 % | |
17 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
18 | |
19 function ppl = parse(varargin) | |
20 | |
21 %%% Check if this is a call for parameters | |
22 if utils.helper.isinfocall(varargin{:}) | |
23 ppl = getInfo(varargin{3}); | |
24 return | |
25 end | |
26 | |
27 if nargin > 0 | |
28 pl = varargin{1}; | |
29 else | |
30 error('### Input at least one plist.'); | |
31 end | |
32 if nargin > 1 | |
33 dpl = varargin{2}; | |
34 else | |
35 dpl = plist(); | |
36 end | |
37 | |
38 if isempty(pl) | |
39 pl = plist(); | |
40 end | |
41 if isempty(dpl) | |
42 dpl = plist(); | |
43 end | |
44 | |
45 % combine | |
46 pl = combine(pl); % in case we get more than one input plist | |
47 ppl = combine(pl, dpl); | |
48 | |
49 % check each parameter and replace key names with upper-case key names | |
50 matches = zeros(size(ppl.params)); | |
51 for kk=1:numel(ppl.params) | |
52 sval = ppl.params(kk).getVal; | |
53 if ( ischar(sval) || isnumeric(sval) ) | |
54 if ischar(sval) | |
55 if ~isempty(dpl.params) | |
56 dkeys = {dpl.params(:).key}; | |
57 else | |
58 dkeys = {''}; | |
59 end | |
60 if any(strcmp(ppl.params(kk).key, dkeys)) | |
61 if isnumeric(find(dpl, ppl.params(kk).key)) | |
62 % look for matching key strings | |
63 for jj=1:numel(ppl.params) | |
64 if jj ~= kk | |
65 key = ppl.params(jj).key; | |
66 % get words out of sval | |
67 words = regexp(sval, '(\w+)', 'match'); | |
68 wordidxs = regexp(sval, '(\w+)'); | |
69 % do any of these words match the key? | |
70 idx = strcmpi(words, key); | |
71 for ll=1:numel(idx) | |
72 if idx(ll) | |
73 word = words{ll}; | |
74 matches(kk) = 1; | |
75 val = ppl.params(kk).getVal; | |
76 sidx = wordidxs(ll):wordidxs(ll)+length(word)-1; | |
77 val(sidx) = upper(val(sidx)); | |
78 ppl.params(kk).setVal(val); | |
79 % ppl.params(kk).setVal(strrep(ppl.params(kk).getVal, word, upper(word))); | |
80 end % If matched | |
81 end % end loop over matches | |
82 end | |
83 end % End loop over params | |
84 else % not numeric parameter | |
85 matches(kk) = -1; | |
86 end | |
87 else % key is not present in default plist | |
88 matches(kk) = -1; | |
89 end | |
90 else % param value is not char | |
91 matches(kk) = 0; | |
92 end | |
93 else % param value is neither char nor numeric | |
94 matches(kk) = -1; | |
95 end | |
96 end % end loop over params | |
97 | |
98 % Don't eval parameters in default plist if they are not in the input | |
99 % plist | |
100 if ~isempty(pl.params) | |
101 ikeys = {pl.params(:).key}; | |
102 else | |
103 ikeys = {''}; | |
104 end | |
105 for kk=1:numel(ppl.params) | |
106 if ~any(strcmpi(ppl.params(kk).key, ikeys)) | |
107 % if ~ismember(ppl.params(kk).key, ikeys) | |
108 matches(kk) = -1; | |
109 end | |
110 end | |
111 | |
112 % First eval all non-dependent keys | |
113 for kk=1:numel(ppl.params) | |
114 if matches(kk) == 0 | |
115 if ischar(ppl.params(kk).getVal) | |
116 try | |
117 ev = eval(ppl.params(kk).getVal); | |
118 ppl.params(kk).setVal(ev); | |
119 catch me | |
120 utils.helper.warn(sprintf('Unable to evaluate non-dependent parameter [%s]. Leaving it as char.', ppl.params(kk).getVal)); | |
121 end | |
122 else | |
123 ev = ppl.params(kk).getVal; | |
124 end | |
125 % If they are needed later, put them in this workspace | |
126 if any(matches>0) | |
127 cmd = [ppl.params(kk).key '=' mat2str(ev) ';']; | |
128 eval(cmd); | |
129 end | |
130 end | |
131 end | |
132 | |
133 % Now evaluate dependent parameters | |
134 count = 0; | |
135 while any(matches>0) && count < 100 | |
136 for kk=1:numel(ppl.params) | |
137 if matches(kk)==1 | |
138 try | |
139 ppl.params(kk).setVal(eval(ppl.params(kk).getVal)); | |
140 cmd = sprintf('%s = %s;', ppl.params(kk).key, mat2str(ppl.params(kk).getVal)); | |
141 eval(cmd); | |
142 matches(kk) = 0; | |
143 end | |
144 end | |
145 end | |
146 count = count + 1; | |
147 end | |
148 | |
149 % did we reach maximum number of tries? | |
150 if count >= 100 | |
151 for kk=1:numel(matches) | |
152 if matches(kk) == 1 | |
153 warning('!!! Can''t resolve dependency in parameter: %s', ppl.params(kk).key); | |
154 % Chenge the value back to the input values in case of dependency. | |
155 % It might be that we changed this value to upper case. | |
156 ppl.params(kk).setVal(pl.find(ppl.params(kk).key)); | |
157 end | |
158 end | |
159 end | |
160 | |
161 end | |
162 | |
163 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
164 % Local Functions % | |
165 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
166 | |
167 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
168 % | |
169 % FUNCTION: getInfo | |
170 % | |
171 % DESCRIPTION: Get Info Object | |
172 % | |
173 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
174 | |
175 function ii = getInfo(varargin) | |
176 if nargin == 1 && strcmpi(varargin{1}, 'None') | |
177 sets = {}; | |
178 pl = []; | |
179 else | |
180 sets = {'Default'}; | |
181 pl = getDefaultPlist; | |
182 end | |
183 % Build info object | |
184 ii = minfo(mfilename, 'plist', 'ltpda', utils.const.categories.internal, '$Id: parse.m,v 1.17 2011/03/28 12:45:31 hewitson Exp $', sets, pl); | |
185 end | |
186 | |
187 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
188 % | |
189 % FUNCTION: getDefaultPlist | |
190 % | |
191 % DESCRIPTION: Get Default Plist | |
192 % | |
193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
194 | |
195 function plo = getDefaultPlist() | |
196 plo = plist(); | |
197 end | |
198 | |
199 |