Mercurial > hg > ltpda
comparison m-toolbox/classes/@plist/applyDefaults.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 % APPLYDEFAULTS apply the default plist to the input plists | |
2 % | |
3 % CALL: | |
4 % plout = applyDefaults(defaultPlist, pl1, pl2, ..., pl3) | |
5 % [plout, pl_unused] = applyDefaults(defaultPlist, pl1, pl2, ..., pl3) | |
6 % | |
7 % The default plist is assumed to be the first one. | |
8 % | |
9 % The plists are combined and the resulting keys are compared to the keys | |
10 % in the default plist. If any key is missing from the default plist a | |
11 % warning is issued. | |
12 % | |
13 % M Hewitson 04-04-11 | |
14 % | |
15 % VERSION $Id: applyDefaults.m,v 1.14 2011/08/16 06:05:37 hewitson Exp $ | |
16 % | |
17 | |
18 function varargout = applyDefaults(varargin) | |
19 | |
20 inputs = utils.helper.collect_objects(varargin(:), 'plist'); | |
21 | |
22 % trivial case | |
23 if numel(inputs) == 1 | |
24 varargout{1} = copy(inputs, true); | |
25 return; | |
26 end | |
27 | |
28 % Get the default plist and copy it because we modify it by setting the | |
29 % user's values | |
30 default = copy(inputs(1), true); | |
31 | |
32 % Get the rest | |
33 pls = inputs(2:end); | |
34 | |
35 % run the user plists through parse to combine and resolve expressions | |
36 userplist = parse(pls, default); | |
37 | |
38 % check keys against default keys | |
39 dkeys = default.getKeys; | |
40 ukeys = userplist.getKeys; | |
41 res = ismember(ukeys, dkeys); | |
42 if ~all(res) | |
43 warning('The following keys were not found in the default plist and will be ignored: %s', sprintf('[%s] ', ukeys{res==0})); | |
44 end | |
45 | |
46 % combine defaults | |
47 [default, unused] = overrideDefaults(userplist, default); | |
48 | |
49 % output the used and unused plists | |
50 varargout{1} = default; | |
51 if nargout >= 2 | |
52 varargout{2} = unused; | |
53 end | |
54 | |
55 end | |
56 | |
57 | |
58 function [default, unused] = overrideDefaults(userplist, default) | |
59 | |
60 unused = plist(); | |
61 | |
62 for kk = 1:numel(userplist.params) | |
63 | |
64 % which key are we acting on | |
65 key = userplist.params(kk).key; | |
66 | |
67 % get override value from user | |
68 val = userplist.params(kk).val; | |
69 | |
70 % decide what to do based on the presence or not of the parameter in the default plist | |
71 if isparam(default, key) | |
72 % set the parameter value, taking into account it could be itself a plist object | |
73 default.setDefaultForParam(plist('key', key, 'option', val)); | |
74 else | |
75 % append the parameter value, to the unused parameters plist | |
76 unused.append(copy(userplist.params(kk), true)); | |
77 end | |
78 end | |
79 | |
80 end |