Skip to content
Snippets Groups Projects
Commit 581e8d1b authored by Federico Felici's avatar Federico Felici
Browse files

Cleanup and reorder methods

parent f0611bc7
No related branches found
No related tags found
No related merge requests found
...@@ -56,7 +56,6 @@ classdef SCDclass_algo ...@@ -56,7 +56,6 @@ classdef SCDclass_algo
end end
%% Print infos %% Print infos
function printinfo(obj) function printinfo(obj)
fprintf('*****************************************************\n'); fprintf('*****************************************************\n');
fprintf('* SCD algorithm: ''%s''\n',obj.modelname); fprintf('* SCD algorithm: ''%s''\n',obj.modelname);
...@@ -82,21 +81,19 @@ classdef SCDclass_algo ...@@ -82,21 +81,19 @@ classdef SCDclass_algo
obj.printinits; obj.printinits;
fprintf('*****************************************************\n'); fprintf('*****************************************************\n');
end end
%% Setup function printinits(obj)
if(~isempty(obj.fpinits))
function setup(obj) for ii=1:numel(obj.fpinits)
% setup() if isempty(obj.fpinits{ii}{2})
% fprintf('%s : init function with no outputs\n',char(obj.fpinits{ii}{1}));
% calls updatetemplatetp and else
% buildworkspacetpstruct fprintf('%s -> %s in workspace %s \n',char(obj.fpinits{ii}{1}),char(obj.fpinits{ii}{2}{1}),char(obj.fpinits{ii}{3}));
% end
% an algorithm block diagram end
% should pass ctrl-d (instandalone opening) end
% after this call
obj.updatetemplatetp;
obj.buildworkspacetpstruct;
end end
%% General purpose getters %% General purpose getters
function out = getname(obj) function out = getname(obj)
...@@ -119,6 +116,44 @@ classdef SCDclass_algo ...@@ -119,6 +116,44 @@ classdef SCDclass_algo
out=obj.fpinits; out=obj.fpinits;
end end
%% Setup functions setter and getter
function obj = addstdinitfcn(obj,fcnhandle)
if ischar(fcnhandle)
fcnhandle = str2func(fcnhandle);
end
assert(isa(fcnhandle,'function_handle'),'stdinit function must be a function handle');
assert(nargout(fcnhandle)<=0,'stdinit functions may not have output arguments')
obj.stdinits{end+1} = fcnhandle;
end
function obj = addfpinitfcn(obj, fcnname, targetstruct,targetworkspace)
if nargin < 4
targetworkspace = 'datadictionary';
end
if ~iscell(targetstruct) && ~isempty(targetstruct)
targetstruct = {targetstruct};
end
for ii=1:numel(obj.fpinits)
for istruct = 1:numel(targetstruct)
mytarget = targetstruct{istruct};
if ~isempty(mytarget) && contains(obj.fpinits{ii}{2},mytarget)
warning('SCDclass_algo:addfpinitfcn',...
'A function defining the structure %s has already been added, ignoring.\d',mytarget)
return
end
end
end
% FF question to CG: why is this a cell and not a struct?
temp=cell(10,1);
temp{1}=fcnname;
temp{2}=targetstruct;
temp{3}=targetworkspace;
obj.fpinits{end+1}=temp;
end
%% Data dictionary setter and getter %% Data dictionary setter and getter
function obj = setdatadictionary(obj, datadict) function obj = setdatadictionary(obj, datadict)
obj.datadictionary=datadict; obj.datadictionary=datadict;
...@@ -127,7 +162,12 @@ classdef SCDclass_algo ...@@ -127,7 +162,12 @@ classdef SCDclass_algo
function out = getdatadictionary(obj) function out = getdatadictionary(obj)
out = obj.datadictionary; out = obj.datadictionary;
end end
function obj = setreferenceddatadictionaries(obj,refdd)
if ischar(refdd) && ~iscell(refdd); refdd={refdd}; end % to cell
assert(iscell(refdd) && isrow(refdd),'refdd must be a row cell array of data dictionary names')
obj.refdatadictionaries = refdd;
end
function createdatadictionary(obj,varargin) function createdatadictionary(obj,varargin)
% create data dictionary in path obj.folder/obj.getdatadictionary % create data dictionary in path obj.folder/obj.getdatadictionary
...@@ -146,10 +186,58 @@ classdef SCDclass_algo ...@@ -146,10 +186,58 @@ classdef SCDclass_algo
end end
end end
function obj = setreferenceddatadictionaries(obj,refdd) %% Data dictionary manipulation manipulations
if ischar(refdd) && ~iscell(refdd); refdd={refdd}; end % to cell function addbuses(obj)
assert(iscell(refdd) && isrow(refdd),'refdd must be a row cell array of data dictionary names') % Add buses to data dictionary from .m file descriptions, if
obj.refdatadictionaries = refdd; % available. Bus names in obj.inBus, obj.outBus
if ~isempty(which(obj.inBus)) && ~isempty(which(obj.outBus))
dict = Simulink.data.dictionary.open(obj.getdatadictionary);
designDataObj = getSection(dict, 'Design Data');
fprintf('Add buses to data dictionary: %s, %s\n',obj.inBus,obj.outBus);
eval(obj.inBus); eval(obj.outBus); % eval buses in base workspace
dosave = false;
dosave = obj.replaceorcreateddentry(designDataObj,obj.inBus ,evalin('base',obj.inBus )) || dosave;
dosave = obj.replaceorcreateddentry(designDataObj,obj.outBus,evalin('base',obj.outBus )) || dosave;
if dosave, dict.saveChanges; end % Save if necessary
evalin('base',sprintf('clear %s %s',obj.inBus,obj.outBus));
else
fprintf('no buses imported in datadictionary - assuming they are already there\n')
end
end
function addrefdd(obj)
% add configurations.sldd as referenced data dictionary, plus
% other optional data dictionaries (e.g. used by lower-level components of an algorithm)
dosave = false;
for refdd = ['configurations.sldd',obj.refdatadictionaries]
dictionaryObj = Simulink.data.dictionary.open(obj.getdatadictionary);
if ~ismember(refdd{:},dictionaryObj.DataSources)
fprintf('adding referenced data dictionary %s to %s \n',refdd{:},obj.getdatadictionary)
refddpath = which(refdd{:});
assert(~isempty(refddpath),'could not find %s',refdd{:});
dictionaryObj.addDataSource(refdd{:});
dosave = true;
end
if dosave, dictionaryObj.saveChanges; end
end
end
function replaceorcreateddentry(obj,designDataObj,entry,value)
if designDataObj.exist(entry)
oldEntry = designDataObj.getEntry(entry);
assert(numel(oldEntry)==1,'multiple entries found for %s',entry)
if isequal(oldEntry.getValue,value)
fprintf('%s: keep old value of %s since not changed\n',obj.getname,entry);
else
oldEntry.setValue(value); % replace
fprintf('%s: replaced value of %s since it changed\n',obj.getname,entry);
end
else
fprintf(' %s: added new %s\n',obj.getname, entry);
designDataObj.addEntry(entry,value);
changed = true;
end
end end
%% Tunable parameters structures handling functions %% Tunable parameters structures handling functions
...@@ -259,25 +347,6 @@ classdef SCDclass_algo ...@@ -259,25 +347,6 @@ classdef SCDclass_algo
evalin('base','clear temp;'); evalin('base','clear temp;');
end end
function addbuses(obj)
% Add buses to data dictionary from .m file descriptions, if
% available. Bus names in obj.inBus, obj.outBus
if ~isempty(which(obj.inBus)) && ~isempty(which(obj.outBus))
dict = Simulink.data.dictionary.open(obj.getdatadictionary);
designDataObj = getSection(dict, 'Design Data');
fprintf('Add buses to data dictionary: %s, %s\n',obj.inBus,obj.outBus);
eval(obj.inBus); eval(obj.outBus); % eval buses in base workspace
dosave = false;
dosave = obj.replaceorcreateddentry(designDataObj,obj.inBus ,evalin('base',obj.inBus )) || dosave;
dosave = obj.replaceorcreateddentry(designDataObj,obj.outBus,evalin('base',obj.outBus )) || dosave;
if dosave, dict.saveChanges; end % Save if necessary
evalin('base',sprintf('clear %s %s',obj.inBus,obj.outBus));
else
fprintf('no buses imported in datadictionary - assuming they are already there\n')
end
end
%% MDS container methods forwarders %% MDS container methods forwarders
function obj = addparameter(obj, param) function obj = addparameter(obj, param)
...@@ -327,63 +396,7 @@ classdef SCDclass_algo ...@@ -327,63 +396,7 @@ classdef SCDclass_algo
obj.timing.t_stop=t_stop; obj.timing.t_stop=t_stop;
end end
%% Generic and fixed parameter inits %% Initializations
function obj = addstdinitfcn(obj,fcnhandle)
if ischar(fcnhandle)
fcnhandle = str2func(fcnhandle);
end
assert(isa(fcnhandle,'function_handle'),'stdinit function must be a function handle');
assert(nargout(fcnhandle)<=0,'stdinit functions may not have output arguments')
obj.stdinits{end+1} = fcnhandle;
end
function obj = addfpinitfcn(obj, fcnname, targetstruct,targetworkspace)
if nargin < 4
targetworkspace = 'datadictionary';
end
if ~iscell(targetstruct) && ~isempty(targetstruct)
targetstruct = {targetstruct};
end
for ii=1:numel(obj.fpinits)
for istruct = 1:numel(targetstruct)
mytarget = targetstruct{istruct};
if ~isempty(mytarget) && contains(obj.fpinits{ii}{2},mytarget)
warning('SCDclass_algo:addfpinitfcn',...
'A function defining the structure %s has already been added, ignoring.\d',mytarget)
return
end
end
end
% FF question to CG: why is this a cell and not a struct?
temp=cell(10,1);
temp{1}=fcnname;
temp{2}=targetstruct;
temp{3}=targetworkspace;
obj.fpinits{end+1}=temp;
end
function printinits(obj)
if(~isempty(obj.fpinits))
for ii=1:numel(obj.fpinits)
if isempty(obj.fpinits{ii}{2})
fprintf('%s : init function with no outputs\n',char(obj.fpinits{ii}{1}));
else
fprintf('%s -> %s in workspace %s \n',char(obj.fpinits{ii}{1}),char(obj.fpinits{ii}{2}{1}),char(obj.fpinits{ii}{3}));
end
end
end
end
function init(obj)
obj.callinits; % call generic algorithm init functions
obj.setupdd; % setup data dictionary
end
function setupdd(obj) function setupdd(obj)
% setup data dictionary % setup data dictionary
...@@ -448,7 +461,6 @@ classdef SCDclass_algo ...@@ -448,7 +461,6 @@ classdef SCDclass_algo
% assigns in target datadictionary depending % assigns in target datadictionary depending
% on target workspace % on target workspace
dosave = false;
dictionaryObj = Simulink.data.dictionary.open(obj.getdatadictionary); dictionaryObj = Simulink.data.dictionary.open(obj.getdatadictionary);
dd = getSection(dictionaryObj, 'Design Data'); dd = getSection(dictionaryObj, 'Design Data');
for iout = 1:nout for iout = 1:nout
...@@ -457,53 +469,36 @@ classdef SCDclass_algo ...@@ -457,53 +469,36 @@ classdef SCDclass_algo
target = targetnames{iout}; target = targetnames{iout};
if strcmp(workspace,'datadictionary') if strcmp(workspace,'datadictionary')
% assign in associated data dictionary % assign in associated data dictionary
dosave = obj.replaceorcreateddentry(dd,target,val) || dosave; obj.replaceorcreateddentry(dd,target,val) || dosave;
elseif strcmp(workspace,'base') elseif strcmp(workspace,'base')
% assign in base workspace % assign in base workspace
assignin('base',target,val) assignin('base',target,val)
end end
end end
if dosave, dictionaryObj.saveChanges; end if dictionaryObj.HasUnsavedChanges, dictionaryObj.saveChanges; end
end end
end end
end end
function addrefdd(obj) %% generic operation methods
% add configurations.sldd as referenced data dictionary, plus function init(obj)
% other optional data dictionaries (e.g. used by lower-level components of an algorithm) obj.callinits; % call generic algorithm init functions
dosave = false; obj.setupdd; % setup data dictionary
for refdd = ['configurations.sldd',obj.refdatadictionaries]
dictionaryObj = Simulink.data.dictionary.open(obj.getdatadictionary);
if ~ismember(refdd{:},dictionaryObj.DataSources)
fprintf('adding referenced data dictionary %s to %s \n',refdd{:},obj.getdatadictionary)
refddpath = which(refdd{:});
assert(~isempty(refddpath),'could not find %s',refdd{:});
dictionaryObj.addDataSource(refdd{:});
dosave = true;
end
if dosave, dictionaryObj.saveChanges; end
end
end end
function changed = replaceorcreateddentry(obj,designDataObj,entry,value) function setup(obj)
if designDataObj.exist(entry) % setup()
oldEntry = designDataObj.getEntry(entry); %
assert(numel(oldEntry)==1,'multiple entries found for %s',entry) % calls updatetemplatetp and
if isequal(oldEntry.getValue,value) % buildworkspacetpstruct
fprintf('%s: keep old value of %s since not changed\n',obj.getname,entry); %
changed = false; % an algorithm block diagram
else % should pass ctrl-d (instandalone opening)
oldEntry.setValue(value); % replace % after this call
fprintf('%s: replaced value of %s since it changed\n',obj.getname,entry); obj.updatetemplatetp;
changed = true; obj.buildworkspacetpstruct;
end
else
fprintf(' %s: added new %s\n',obj.getname, entry);
designDataObj.addEntry(entry,value);
changed = true;
end
end end
%% generic operation methods
function compile(obj) function compile(obj)
try try
eval(sprintf('%s([],[],[],''compile'')',obj.modelname)); eval(sprintf('%s([],[],[],''compile'')',obj.modelname));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment