rbmatlab 0.10.01
rbasis/basisgen/+Greedy/+DataTree/Info.m
00001 classdef Info < handle
00002   % a DataTree.INode extension for data nodes that can store information on
00003   % their generation process.
00004 
00005   properties
00006     % a dynamically growing structure of information fields
00007     fields = [];
00008 
00009     % a cell array of strings describing why the generation of this node
00010     % terminated.
00011     stop_flags = {};
00012   end
00013 
00014   methods
00015 
00016     function append_field(this, fieldname, value)
00017       % function append_field(this, fieldname, value)
00018       % appends a value to an information field storing a vector
00019       %
00020       % Parameters:
00021       %   fieldname: the name of the information field
00022       %   value: the value to be appended (scalar or column vector)
00023 
00024       if ~isfield(this.fields, fieldname)
00025         this.fields.(fieldname) = [];
00026       end
00027       this.fields.(fieldname) = [ this.fields.(fieldname), value ];
00028     end
00029 
00030     function set_field(this, fieldname, value)
00031       % function set_field(this, fieldname, value)
00032       % sets an information field
00033       %
00034       % Parameters:
00035       %   fieldname: the name of the information field
00036       %   value: the new value
00037 
00038       this.fields.(fieldname) = value;
00039     end
00040 
00041     function value = get_field(this, fieldname, default)
00042       % function value = get_field(this, fieldname, default)
00043       % returns the value of an information field
00044       %
00045       % Parameters:
00046       %   fieldname: the name of the information field
00047       %   default: optional default value in case the information field does
00048       %            not exist.
00049       %
00050       % Return values:
00051       %   value:  the stored value
00052       if nargin == 2
00053         assert(isfield(this.fields, fieldname))
00054       end
00055       if isfield(this.fields, fieldname)
00056         value = this.fields.(fieldname);
00057       else
00058         value = default;
00059         this.fields.(fieldname) = default;
00060       end
00061     end
00062 
00063     function value = get_field_on_active_child(this, fieldname, model, id)
00064       % function value = get_field_on_active_child(this, fieldname, model, id)
00065       % returns the value of an information field from the active leaf node
00066       %
00067       % Parameters:
00068       %   fieldname: the name of the information field
00069       %   model: a reduced or detailed model of type ::IModel holding
00070       %           information about the selected parameters and maybe the time
00071       %           instant.
00072       %   id:     optional parameter defining a special ID that shall be
00073       %           attached to the leaf element.
00074       %
00075       % Optional fields of model:
00076       %   t:  the current time step
00077       %
00078       % Return values:
00079       %   value:  the stored value
00080       if nargin <=3
00081         id = [];
00082       end
00083       if isfield(model, 't')
00084         nt = model.t;
00085       else
00086         nt = [];
00087       end
00088       mu = get_mu(model);
00089 
00090 
00091       leaf_index = get_index(this, id, mu, nt);
00092 
00093       cur_elem = this;
00094       iind     = 1;
00095       value    = [];
00096 
00097       while isempty(value)
00098         value = get_field(cur_elem, fieldname, []);
00099         if iind <= length(leaf_index)
00100           cur_elem = get(cur_elem, leaf_index(iind));
00101           iind = iind + 1;
00102         else
00103           break;
00104         end
00105       end
00106 
00107     end
00108 
00109 %    function set_field_on_all_leaves(this, field, value)
00110 
00111 %      if isa(value, 'DataTree.INode')
00112 %    end
00113 
00114     function set_fields(this, other, fns)
00115       % function set_fields(this, other, fns)
00116       % copies fields from another struct
00117       %
00118       % Parameters:
00119       %   other: another struct whose fields shall be copied as information
00120       %          fields.
00121       %   fns:   a cell array of strings which acts as a filter for the field
00122       %          names of 'other'.
00123 
00124       real_fns = intersect(fieldnames(other), fns);
00125       for i=1:length(real_fns)
00126         this.fields.(real_fns{i}) = other.(real_fns{i});
00127       end
00128     end
00129 
00130     function set_stop_flag(this, flag, on_off)
00131       % function set_stop_flag(this, flag, on_off)
00132       % sets the stop flag in this node
00133       %
00134       % Parameters:
00135       %   flag: the flag name,
00136       %   on_off: boolean flag specifying whether the flag shall set or unset.
00137 
00138       if nargin <=2 || isempty(on_off) || on_off
00139         this.stop_flags = union(this.stop_flags, {flag});
00140       else
00141         this.stop_flags = setdiff(this.stop_flags, {flag});
00142       end
00143     end
00144 
00145     function propagate_stop_flag(this, flag, on_off)
00146       % function propagate_stop_flag(this, flag, on_off)
00147       % updates the stop flag in all children nodes of the current one.
00148       %
00149       % Parameters:
00150       %   flag: the flag name,
00151       %   on_off: boolean flag specifying whether the flag shall set or unset.
00152 
00153       if nargin <= 2
00154         on_off = [];
00155       end
00156 
00157       set_stop_flag(this, flag, on_off);
00158 
00159       for i = 1:length(this)
00160         child = get(this, i);
00161         propagate_stop_flag(child, flag, on_off);
00162       end
00163 
00164     end
00165 
00166     function stopped_flags = stopped_on_active_child(this, flags, model, id)
00167       % function stopped_flags = stopped_on_active_child(this, flags, model, id)
00168       % returns stop flags set on the active child
00169       %
00170       % Parameters:
00171       %   flags: a cell array of flag strings for filtering
00172       %   model: a reduced or detailed model of type ::IModel holding
00173       %           information about the selected parameters and maybe the time
00174       %           instant.
00175       %   id:     optional parameter defining a special ID that shall be
00176       %           attached to the leaf element.
00177       %
00178       % Optional fields of model:
00179       %   t:  the current time step
00180       %
00181       % Return values:
00182       %   stopped_flags: a cell array of stop flags found after filtering. If
00183       %                  only one flag is given in the argument 'flags', a
00184       %                  boolean value is returned, indicating whether the flag
00185       %                  is set.
00186 
00187       if nargin <=3
00188         id = [];
00189       end
00190       if isfield(model, 't')
00191         nt = model.t;
00192       else
00193         nt = [];
00194       end
00195 
00196       if ~iscell(flags)
00197         flags = {flags};
00198       end
00199 
00200       mu = get_mu(model);
00201 
00202       leaf_index = get_index(this, id, mu, nt);
00203 
00204       cur_elem      = this;
00205       stopped_flags = intersect(this.stop_flags, flags);
00206 
00207       for iind = 1:length(leaf_index)
00208         cur_elem = get(cur_elem, leaf_index(iind));
00209         stopped_flags = union(stopped_flags, intersect(cur_elem.stopped_flags, flags));
00210       end
00211 
00212       if length(flags) == 1
00213         stopped_flags = ~isempty(stopped_flags);
00214       end
00215     end
00216 
00217     function stopped_flags = stopped_on_any_child(this, flags)
00218       % function stopped_flags = stopped_on_any_child(this, flags)
00219       % returns stop flags set on any child of the current node
00220       %
00221       % Parameters:
00222       %   flags: a cell array of flag strings for filtering
00223       %
00224       % Return values:
00225       %   stopped_flags: a cell array of stop flags found after filtering. If
00226       %                  only one flag is given in the argument 'flags', a
00227       %                  boolean value is returned, indicating whether the flag
00228       %                  is set.
00229 
00230 
00231       if ~iscell(flags)
00232         flags = {flags};
00233       end
00234 
00235       stopped_flags = intersect(this.stop_flags, flags);
00236 
00237       for i = 1:length(this);
00238         child = get(this, i);
00239         stopped_flags = union(stopped_flags, intersect(child.stopped_flags, flags));
00240       end
00241 
00242       if length(flags) == 1
00243         stopped_flags = ~isempty(stopped_flags);
00244       end
00245     end
00246 
00247     function stopped_flags = stopped_on_all_leafs(this, flags)
00248       % function stopped_flags = stopped_on_all_leafs(this, flags)
00249       % returns stop flags set on all children nodes of the current node
00250       %
00251       % Parameters:
00252       %   flags: a cell array of flag strings for filtering
00253       %
00254       % Return values:
00255       %   stopped_flags: a cell array of stop flags found after filtering. If
00256       %                  only one flag is given in the argument 'flags', a
00257       %                  boolean value is returned, indicating whether the flag
00258       %                  is set.
00259 
00260       if ~iscell(flags)
00261         flags = {flags};
00262       end
00263 
00264       if length(this) == 0
00265         stopped_flags = intersect(this.stopped_flags, flags);
00266       else
00267         stopped_flags = flags;
00268         for i = 1:length(this)
00269           child = get(this, i);
00270           stopped_flags = intersect(stopped_flags, stopped_on_all_children(child, flags));
00271         end
00272       end
00273       if length(flags) == 1
00274         stopped_flags = ~isempty(stopped_flags);
00275       end
00276 
00277     end
00278 
00279   end
00280 end
 All Classes Namespaces Files Functions Variables