rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
Info.m
1 classdef Info < handle
2  % a DataTree.INode extension for data nodes that can store information on
3  % their generation process.
4 
5  properties
6  % a dynamically growing structure of information fields
7  fields = [];
8 
9  % a cell array of strings describing why the generation of this node
10  % terminated.
11  stop_flags = {};
12  end
13 
14  methods
15 
16  function append_field(this, fieldname, value)
17  % function append_field(this, fieldname, value)
18  % appends a value to an information field storing a vector
19  %
20  % Parameters:
21  % fieldname: the name of the information field
22  % value: the value to be appended (scalar or column vector)
23 
24  if ~isfield(this.fields, fieldname)
25  this.fields.(fieldname) = [];
26  end
27  this.fields.(fieldname) = [ this.fields.(fieldname), value ];
28  end
29 
30  function set_field(this, fieldname, value)
31  % function set_field(this, fieldname, value)
32  % sets an information field
33  %
34  % Parameters:
35  % fieldname: the name of the information field
36  % value: the new value
37 
38  this.fields.(fieldname) = value;
39  end
40 
41  function value = get_field(this, fieldname, default)
42  % function value = get_field(this, fieldname, default)
43  % returns the value of an information field
44  %
45  % Parameters:
46  % fieldname: the name of the information field
47  % default: optional default value in case the information field does
48  % not exist.
49  %
50  % Return values:
51  % value: the stored value
52  if nargin == 2
53  assert(isfield(this.fields, fieldname))
54  end
55  if isfield(this.fields, fieldname)
56  value = this.fields.(fieldname);
57  else
58  value = default;
59  this.fields.(fieldname) = default;
60  end
61  end
62 
63  function value = get_field_on_active_child(this, fieldname, model, id)
64  % function value = get_field_on_active_child(this, fieldname, model, id)
65  % returns the value of an information field from the active leaf node
66  %
67  % Parameters:
68  % fieldname: the name of the information field
69  % model: a reduced or detailed model of type ::IModel holding
70  % information about the selected parameters and maybe the time
71  % instant.
72  % id: optional parameter defining a special ID that shall be
73  % attached to the leaf element.
74  %
75  % Optional fields of model:
76  % t: the current time step
77  %
78  % Return values:
79  % value: the stored value
80  if nargin <=3
81  id = [];
82  end
83  if isfield(model, 't')
84  nt = model.t;
85  else
86  nt = [];
87  end
88  mu = get_mu(model);
89 
90 
91  leaf_index = get_index(this, id, mu, nt);
92 
93  cur_elem = this;
94  iind = 1;
95  value = [];
96 
97  while isempty(value)
98  value = get_field(cur_elem, fieldname, []);
99  if iind <= length(leaf_index)
100  cur_elem = get(cur_elem, leaf_index(iind));
101  iind = iind + 1;
102  else
103  break;
104  end
105  end
107  end
108 
109 % function set_field_on_all_leaves(this, field, value)
110 
111 % if isa(value, 'DataTree.INode')
112 % end
113 
114  function set_fields(this, other, fns)
115  % function set_fields(this, other, fns)
116  % copies fields from another struct
117  %
118  % Parameters:
119  % other: another struct whose fields shall be copied as information
120  % fields.
121  % fns: a cell array of strings which acts as a filter for the field
122  % names of 'other'.
123 
124  real_fns = intersect(fieldnames(other), fns);
125  for i=1:length(real_fns)
126  this.fields.(real_fns{i}) = other.(real_fns{i});
127  end
128  end
129 
130  function set_stop_flag(this, flag, on_off)
131  % function set_stop_flag(this, flag, on_off)
132  % sets the stop flag in this node
133  %
134  % Parameters:
135  % flag: the flag name,
136  % on_off: boolean flag specifying whether the flag shall set or unset.
137 
138  if nargin <=2 || isempty(on_off) || on_off
139  this.stop_flags = union(this.stop_flags, {flag});
140  else
141  this.stop_flags = setdiff(this.stop_flags, {flag});
142  end
143  end
144 
145  function propagate_stop_flag(this, flag, on_off)
146  % function propagate_stop_flag(this, flag, on_off)
147  % updates the stop flag in all children nodes of the current one.
148  %
149  % Parameters:
150  % flag: the flag name,
151  % on_off: boolean flag specifying whether the flag shall set or unset.
152 
153  if nargin <= 2
154  on_off = [];
155  end
156 
157  set_stop_flag(this, flag, on_off);
158 
159  for i = 1:length(this)
160  child = get(this, i);
161  propagate_stop_flag(child, flag, on_off);
162  end
163 
164  end
165 
166  function stopped_flags = stopped_on_active_child(this, flags, model, id)
167  % function stopped_flags = stopped_on_active_child(this, flags, model, id)
168  % returns stop flags set on the active child
169  %
170  % Parameters:
171  % flags: a cell array of flag strings for filtering
172  % model: a reduced or detailed model of type ::IModel holding
173  % information about the selected parameters and maybe the time
174  % instant.
175  % id: optional parameter defining a special ID that shall be
176  % attached to the leaf element.
177  %
178  % Optional fields of model:
179  % t: the current time step
180  %
181  % Return values:
182  % stopped_flags: a cell array of stop flags found after filtering. If
183  % only one flag is given in the argument 'flags', a
184  % boolean value is returned, indicating whether the flag
185  % is set.
186 
187  if nargin <=3
188  id = [];
189  end
190  if isfield(model, 't')
191  nt = model.t;
192  else
193  nt = [];
194  end
195 
196  if ~iscell(flags)
197  flags = {flags};
198  end
199 
200  mu = get_mu(model);
201 
202  leaf_index = get_index(this, id, mu, nt);
203 
204  cur_elem = this;
205  stopped_flags = intersect(this.stop_flags, flags);
206 
207  for iind = 1:length(leaf_index)
208  cur_elem = get(cur_elem, leaf_index(iind));
209  stopped_flags = union(stopped_flags, intersect(cur_elem.stopped_flags, flags));
210  end
211 
212  if length(flags) == 1
213  stopped_flags = ~isempty(stopped_flags);
214  end
215  end
217  function stopped_flags = stopped_on_any_child(this, flags)
218  % function stopped_flags = stopped_on_any_child(this, flags)
219  % returns stop flags set on any child of the current node
220  %
221  % Parameters:
222  % flags: a cell array of flag strings for filtering
223  %
224  % Return values:
225  % stopped_flags: a cell array of stop flags found after filtering. If
226  % only one flag is given in the argument 'flags', a
227  % boolean value is returned, indicating whether the flag
228  % is set.
229 
230 
231  if ~iscell(flags)
232  flags = {flags};
233  end
234 
235  stopped_flags = intersect(this.stop_flags, flags);
236 
237  for i = 1:length(this);
238  child = get(this, i);
239  stopped_flags = union(stopped_flags, intersect(child.stopped_flags, flags));
240  end
241 
242  if length(flags) == 1
243  stopped_flags = ~isempty(stopped_flags);
244  end
245  end
246 
247  function stopped_flags = stopped_on_all_leafs(this, flags)
248  % function stopped_flags = stopped_on_all_leafs(this, flags)
249  % returns stop flags set on all children nodes of the current node
250  %
251  % Parameters:
252  % flags: a cell array of flag strings for filtering
253  %
254  % Return values:
255  % stopped_flags: a cell array of stop flags found after filtering. If
256  % only one flag is given in the argument 'flags', a
257  % boolean value is returned, indicating whether the flag
258  % is set.
259 
260  if ~iscell(flags)
261  flags = {flags};
262  end
263 
264  if length(this) == 0
265  stopped_flags = intersect(this.stop_flags, flags);
266  else
267  stopped_flags = flags;
268  for i = 1:length(this)
269  child = get(this, i);
270  stopped_flags = intersect(stopped_flags, stopped_on_all_children(child, flags));
271  end
272  end
273  if length(flags) == 1
274  stopped_flags = ~isempty(stopped_flags);
275  end
276 
277  end
278 
279  end
280 end
This is the common interface for all models, detailed and reduced ones.
Definition: IModel.m:17
Interface for a node in a DataTree.
Definition: INode.m:18
Definition: leaf.m:17