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

Update and cleanup and avoid useless saves

parent 2e400db4
No related branches found
No related tags found
No related merge requests found
......@@ -127,12 +127,7 @@ classdef SCDclass_algo
function out = getdatadictionary(obj)
out = obj.datadictionary;
end
function designDataObj = opendatadictionarydesigndata(obj)
dict = Simulink.data.dictionary.open(obj.getdatadictionary);
designDataObj = getSection(dict, 'Design Data');
end
function createdatadictionary(obj,varargin)
% create data dictionary in path obj.folder/obj.getdatadictionary
......@@ -197,7 +192,8 @@ classdef SCDclass_algo
% The default value function of a tunparams can be given
% as an optional third argument of addtunparamstruct
% method
DD = obj.opendatadictionarydesigndata;
dict = Simulink.data.dictionary.open(obj.getdatadictionary);
DD = getSection(dict, 'Design Data');
for ii=1:numel(obj.exportedtps)
if ~isempty(obj.exportedtpsdefaults{ii})
% get current value of parameters from the function
......@@ -221,8 +217,8 @@ classdef SCDclass_algo
continue;
else
% replace
DD.deleteEntry(tmplname); fprintf(' %s: deleted previous entry, ', obj.getname);
DD.addEntry(tmplname,P); fprintf('added new %s\n',tmplname);
oldEntry.setValue(P);
fprintf('%s: replaced value of template %s since it changed\n',obj.getname,tmplname)
end
else
fprintf(' %s: added new %s\n',obj.getname, tmplname);
......@@ -266,22 +262,15 @@ classdef SCDclass_algo
% 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))
designDataObj = opendatadictionarydesigndata(obj);
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 buses in base workspace
eval(obj.outBus);
if ~designDataObj.exist(obj.inBus)
addEntry(designDataObj,obj.inBus ,evalin('base',obj.inBus ));
else
entryObj = getEntry(designDataObj,obj.inBus);
setValue(entryObj,evalin('base',obj.inBus ));
end
if ~designDataObj.exist(obj.outBus)
addEntry(designDataObj,obj.outBus,evalin('base',obj.outBus));
else
entryObj = getEntry(designDataObj,obj.outBus);
setValue(entryObj,evalin('base',obj.outBus));
end
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')
......@@ -387,38 +376,34 @@ classdef SCDclass_algo
end
function init(obj)
% generate/initialize data dictionary
obj.createdatadictionary;
obj.callinits; % call generic algorithm init functions
% link data dictionary to model and enable access to base
obj.load;
set_param(obj.getname,'DataDictionary',obj.getdatadictionary,'EnableAccessToBaseWorkspace','on');
% call fixed parameter setup functions
obj.setupfp;
% populate with template tunable parameters
obj.updatetemplatetp;
% add buses
obj.addbuses;
% 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');
dictionaryObj.addDataSource(refdd{:});
dosave = 1;
end
if dosave, dictionaryObj.saveChanges; end
end
obj.setupdd; % setup data dictionary
end
function setupdd(obj)
% setup data dictionary
% generate/initialize data dictionary
obj.createdatadictionary;
% link data dictionary to model and enable access to base
obj.load;
set_param(obj.getname,'DataDictionary',obj.getdatadictionary,'EnableAccessToBaseWorkspace','on');
% call fixed parameter setup functions
obj.callfpinits;
% populate with template tunable parameters
obj.updatetemplatetp;
% add buses
obj.addbuses;
% add referenced data dictionaries
obj.addrefdd
end
function callinits(obj)
......@@ -429,7 +414,7 @@ classdef SCDclass_algo
end
end
function setupfp(obj)
function callfpinits(obj)
% call initialization functions that set fixed parameters
if ~isempty(obj.fpinits)
for ii=1:numel(obj.fpinits)
......@@ -441,7 +426,7 @@ classdef SCDclass_algo
nout = numel(targetnames);
if ~isempty(targetnames)
fprintf('Calling init function ''%s'', assigning its output to ''%s'' in %s...\n',...
fprintf('Calling fixed parameter init function ''%s'', assigning its output to ''%s'' in %s...\n',...
char(initfunction),cell2mat(targetnames),workspace);
end
if ischar(initfunction)
......@@ -462,22 +447,60 @@ 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
% cycle over number of output arguments of the function
val = value{iout};
target = targetnames{iout};
if strcmp(workspace,'datadictionary')
% assign in associated data dictionary
Simulink.data.assigninGlobal(obj.modelname, 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
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');
dictionaryObj.addDataSource(refdd{:});
dosave = true;
end
if dosave, dictionaryObj.saveChanges; end
end
end
function changed = replaceorcreateddentry(obj,dd,entry,value)
if dd.exist(entry)
oldEntry = dd.getEntry(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);
DD.addEntry(entry,value);
changed = true;
end
end
%% generic operation methods
function compile(obj)
try
......
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