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
end
%% Print infos
function printinfo(obj)
fprintf('*****************************************************\n');
fprintf('* SCD algorithm: ''%s''\n',obj.modelname);
......@@ -82,21 +81,19 @@ classdef SCDclass_algo
obj.printinits;
fprintf('*****************************************************\n');
end
%% Setup
function setup(obj)
% setup()
%
% calls updatetemplatetp and
% buildworkspacetpstruct
%
% an algorithm block diagram
% should pass ctrl-d (instandalone opening)
% after this call
obj.updatetemplatetp;
obj.buildworkspacetpstruct;
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
%% General purpose getters
function out = getname(obj)
......@@ -119,6 +116,44 @@ classdef SCDclass_algo
out=obj.fpinits;
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
function obj = setdatadictionary(obj, datadict)
obj.datadictionary=datadict;
......@@ -127,7 +162,12 @@ classdef SCDclass_algo
function out = getdatadictionary(obj)
out = obj.datadictionary;
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)
% create data dictionary in path obj.folder/obj.getdatadictionary
......@@ -146,10 +186,58 @@ classdef SCDclass_algo
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;
%% Data dictionary manipulation manipulations
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
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
%% Tunable parameters structures handling functions
......@@ -259,25 +347,6 @@ classdef SCDclass_algo
evalin('base','clear temp;');
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
function obj = addparameter(obj, param)
......@@ -327,63 +396,7 @@ classdef SCDclass_algo
obj.timing.t_stop=t_stop;
end
%% Generic and fixed parameter inits
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
%% Initializations
function setupdd(obj)
% setup data dictionary
......@@ -448,7 +461,6 @@ classdef SCDclass_algo
% assigns in target datadictionary depending
% on target workspace
dosave = false;
dictionaryObj = Simulink.data.dictionary.open(obj.getdatadictionary);
dd = getSection(dictionaryObj, 'Design Data');
for iout = 1:nout
......@@ -457,53 +469,36 @@ classdef SCDclass_algo
target = targetnames{iout};
if strcmp(workspace,'datadictionary')
% assign in associated data dictionary
dosave = obj.replaceorcreateddentry(dd,target,val) || dosave;
obj.replaceorcreateddentry(dd,target,val) || dosave;
elseif strcmp(workspace,'base')
% assign in base workspace
assignin('base',target,val)
end
end
if dosave, dictionaryObj.saveChanges; end
if dictionaryObj.HasUnsavedChanges, dictionaryObj.saveChanges; end
end
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
%% generic operation methods
function init(obj)
obj.callinits; % call generic algorithm init functions
obj.setupdd; % setup data dictionary
end
function changed = 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);
changed = false;
else
oldEntry.setValue(value); % replace
fprintf('%s: replaced value of %s since it changed\n',obj.getname,entry);
changed = true;
end
else
fprintf(' %s: added new %s\n',obj.getname, entry);
designDataObj.addEntry(entry,value);
changed = true;
end
function setup(obj)
% setup()
%
% calls updatetemplatetp and
% buildworkspacetpstruct
%
% an algorithm block diagram
% should pass ctrl-d (instandalone opening)
% after this call
obj.updatetemplatetp;
obj.buildworkspacetpstruct;
end
%% generic operation methods
function compile(obj)
try
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