Robochameleon  v1.0
paramDeepCopy.m
Go to the documentation of this file.
1 
2 function [ param_copied ] = paramDeepCopy( class, param )
3  if(~ischar(class))
4  robolog('Variable class is supposed to be a string/char.','ERR');
5  end
6  if(~isstruct(param))
7  robolog('Variable param is supposed to be a struct.','ERR');
8  end
9  param_copied = struct;
10  % Get all field names of the param struct
11  names=fields(param);
12  % Check whether the class is a module or unit
13  if(isModule(class))
14  tree=getInternalUnitsNested(class);
15  props=unique(getUnitsTreeProperties(tree));
16  else
17  props=properties(class);
18  end
19  props{find(cellfun(@(x) strcmpi(x,'nOutputs'),props))}='NOPROPERTY';
20  props{find(cellfun(@(x) strcmpi(x,'nInputs'),props))}='NOPROPERTY';
21  names=unique(names);
22  props=unique(props);
23  members_names=cellfun(@sum,cellfun(@(name) cellfun(@(prop) myRegex(prop,name), props), names, 'UniformOutput',false))~=0;
24  if sum(members_names)
25  temp=[{names{members_names}}; {names{members_names}}];
26  eval(sprintf('param_copied.%s=param.%s;',temp{:}));
27  else
28 % robolog('No parameters match the properties of this unit/module (%s).','WRN',class);
29  end
30 end
31 
32 function result = myRegex(prop,name)
33  if length(prop)~=1
34  result = ~isempty( regexpi(name,[prop '\>'],'match') );
35  else
36  result = ~isempty( regexp(name,[prop '\>'],'match') );
37  end
38 end
39 
40 function names = getInternalUnitsNested( moduleName )
41  names = getInternalUnits( moduleName );
42  names{length(names)+1}={moduleName};
43  for ii=1:length(names)-1
44  if(isModule(names{ii}{1}))
45  names{ii}=getInternalUnitsNested( names{ii}{1} );
46  end
47  end
48 end
49 
50 function names = getInternalUnits( moduleName )
51  if(~isModule(moduleName))
52  robolog('%s is not a module.','ERR',moduleName);
53  end
54  text = fileread([moduleName '.m']);
55  %% remove comments
56  [startIndex,endIndex]=regexp(text,'\%.*?\n');
57  text_wo='';
58  for n=1:length(endIndex)-1
59  if(endIndex(n)~=startIndex(n+1)-1)
60  add = text(endIndex(n):startIndex(n+1)-1);
61  text_wo = [text_wo add];
62  end
63  end
64  text = [text_wo text(endIndex(n+1):end)];
65  %% extract constructor
66  [~,endIndex] = regexp(text,['function[a-zA-Z0-9\[\]\,\;\=\s]*' moduleName '.*?)']);
67  text=text(endIndex:end);
68  words={'for', 'while', 'switch', 'try', 'if', 'parfor'};
69  % very ugly implementation since counter is not functional
70  % it depends on text which isnt a passed variable (but it works)
71  counter = @(word) length(regexp(text,['<' word '>']));
72  count=sum(cellfun(counter,words));
73  endPos = regexp(text,'<end>');
74  constructor_text=text(1:endPos(count+1));
75  %% look for units/modules
76  [~,names]=regexp(constructor_text,'=\s([a-zA-Z]+_v\d)\(','match', 'tokens');
77 end
78 
79 function props = getUnitsTreeProperties(tree)
80  props={};
81  for ii=1:length(tree)
82  if(length(tree{ii})>1)
83  props=[props; getUnitsTreeProperties(tree{ii})];
84  elseif(length(tree{ii})==1)
85  props=[props; properties(cat(1,tree{ii}{1}))];
86  end
87  end
88 end
Superclass: basic building block to hold functions.
Definition: unit.m:27
function getInternalUnits(in moduleName)
Gather all units/modules within a modules constructor.
function paramDeepCopy(in class, in param)
Copy parameters aligned to the properties of a module/unit.
function robolog(in msg, in varargin)
This function allows the user to print log messages in a standard way.
function getInternalUnitsNested(in moduleName)
Gather all units within a module as tree structure of cell arrays.
function getUnitsTreeProperties(in tree)
Gather all properties from a tree structure of units.
Superclass: collection/sequence of unit.
Definition: module.m:64