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

Add dynamic generation of data dictionaries

Allows to entirely remove them from SCDliuqe
parent a10761f8
No related branches found
No related tags found
No related merge requests found
*.sldd
......@@ -5,14 +5,15 @@ classdef SCDclass_algo
% algorithm
properties (Access = private)
modelname % Name of the model
mdscontainer % Container class for MDS+ interface objects
taskcontainer % Container class for init and term tasks
exportedtps % List of tunable parameters variable to be exported
datadictionary % Name of the used data dictionary
stdinits % Standard inits scripts for fixed parameters
timing % Timing info structure
exportedtpsdefaults % default tunable parameters defining functions
modelname % Name of the model
mdscontainer % Container class for MDS+ interface objects
taskcontainer % Container class for init and term tasks
exportedtps % List of tunable parameters variable to be exported
datadictionary % Name of the used data dictionary
refdatadictionaries % Cell array of referenced data dictionaries
stdinits % Standard inits scripts for fixed parameters
timing % Timing info structure
exportedtpsdefaults % default tunable parameters defining functions
end
methods
......@@ -28,6 +29,7 @@ classdef SCDclass_algo
obj.exportedtpsdefaults = {};
obj.stdinits = {};
obj.datadictionary = '';
obj.refdatadictionaries = {};
obj.timing.t_start=0;
obj.timing.t_stop=1;
obj.timing.dt=1e-3;
......@@ -100,9 +102,8 @@ classdef SCDclass_algo
function out = getinits(obj)
out=obj.stdinits;
end
%% Data dictionary setter and getter
function obj = setdatadictionary(obj, datadict)
obj.datadictionary=datadict;
end
......@@ -116,12 +117,37 @@ classdef SCDclass_algo
dict = Simulink.data.dictionary.open(ddname);
out=getSection(dict, 'Design Data');
end
function dictionaryObj = createdatadictionary(obj,varargin)
% create and populate data dictionary with parameter,value pairs
% obj.populatedd('parameter',value);
dd = obj.getdatadictionary;
% close open data dictionary
listopendictionaries = Simulink.data.dictionary.getOpenDictionaryPaths;
if numel(listopendictionaries)>0 && any(contains(listopendictionaries,dd))
Simulink.data.dictionary.closeAll(dd, '-discard');
end
if exist(dd, 'file')
% delete the existing data dictionary
delete(dd)
end
% Create
dictionaryObj = Simulink.data.dictionary.create(dd);
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
%% Tunable parameters structures handling functions
function out = getexportedtps(obj)
out = obj.exportedtps;
end
end
function obj = addtunparamstruct(obj, structname, varargin)
% obj = addtunparamstruct(structname, varargin)
......@@ -276,17 +302,7 @@ classdef SCDclass_algo
function obj = cleanwavegens(obj)
obj.mdscontainer=obj.mdscontainer.cleanwavegens;
end
%function obj = bindparameters(obj)
% obj.mdscontainer=obj.mdscontainer.bindparameters(obj.modelname, obj.datadictionary, obj.exportedtps);
%end
% This method has only sense in the expcode context
%function obj = buildworkspacesimstruct(obj)
% obj.mdscontainer=obj.mdscontainer.buildworkspacesimstruct;
%end
%% Task container methods forwarders
function obj = addtask(obj, task)
obj.taskcontainer=obj.taskcontainer.addtask(task);
......@@ -297,17 +313,6 @@ classdef SCDclass_algo
obj.taskcontainer=obj.taskcontainer.printtasks;
end
%% Wavegen timing structure setters
% function obj = setwavegentimingsources(obj, tstart, dt, tstop)
% assert(ischar(tstart), 'SCDclass_algo.setwavegentimingsources first parameter must be a char array');
% assert(ischar(dt), 'SCDclass_algo.setwavegentimingsources first parameter must be a char array');
% assert(ischar(tstop), 'SCDclass_algo.setwavegentimingsources first parameter must be a char array');
%
% obj.wavegentiming.t_start=tstart;
% obj.wavegentiming.dt=dt;
% obj.wavegentiming.t_stop=tstop;
% end
%% Timing information
function obj = settiming(obj, t_start, dt, t_stop)
obj.timing.t_start=t_start;
......@@ -321,7 +326,7 @@ classdef SCDclass_algo
targetstruct = '';
end
if nargin<4
targetworkspace = 'global'; % default: global workspace (assigninGlobal)
targetworkspace = 'datadictionary'; % default: data dictionary workspace (assigninGlobal)
end
if ~iscell(targetstruct) && ~isempty(targetstruct)
......@@ -353,6 +358,27 @@ classdef SCDclass_algo
end
end
function initdd(obj)
fprintf('generating data dictionary %s\n',obj.getdatadictionary);
% generate/initialize data dictionary
dictionaryObj = obj.createdatadictionary;
% link data dictionary to model and disable access to base
obj.load;
set_param(obj.getname,'DataDictionary',obj.getdatadictionary,'EnableAccessToBaseWorkspace','off');
% call initialization functions to populate dd
obj.callinits;
% always reference configurations.sldd
dictionaryObj.addDataSource('configurations.sldd');
% also reference other optional data dictionaries (e.g. used by
% lower-level components of an algorithm)
for refdd = obj.refdatadictionaries
dictionaryObj.addDataSource(refdd{:});
end
end
function callinits(obj)
% call initialization functions that set fixed parameters
......@@ -360,13 +386,13 @@ classdef SCDclass_algo
for ii=1:numel(obj.stdinits)
initfunction = obj.stdinits{ii}{1};
targetnames = obj.stdinits{ii}{2};
workspace = obj.stdinits{ii}{3};
targetnames = obj.stdinits{ii}{2};
workspace = obj.stdinits{ii}{3};
nout = numel(targetnames);
fprintf('Calling init function ''%s'', assigning its output to ''%s'' ...\n',...
char(initfunction),cell2mat(targetnames));
fprintf('Calling init function ''%s'', assigning its output to ''%s'' in %s...\n',...
char(initfunction),cell2mat(targetnames),workspace);
if ischar(initfunction)
initcmd=sprintf('%s(obj);', initfunction);
......@@ -390,16 +416,19 @@ classdef SCDclass_algo
% cycle over number of output arguments of the function
val = value{iout};
target = targetnames{iout};
if strcmp(workspace,'global')
if strcmp(workspace,'datadictionary')
% assign in associated data dictionary
Simulink.data.assigninGlobal(obj.modelname, target, val);
elseif strcmp(workspace,'base')
% assign in base workspace
assignin('base',target,val)
end
end
end
end
end
%% generic operation methods
function compile(obj)
try
eval(sprintf('%s([],[],[],''compile'')',obj.modelname));
......@@ -433,6 +462,10 @@ classdef SCDclass_algo
function open(obj)
open(obj.modelname)
end
function load(obj)
load_system(obj.modelname);
end
end
end
......@@ -5,26 +5,26 @@ classdef SCDalgo_test < matlab.unittest.TestCase
end
methods (TestClassSetup)
function setup_conf(testCase)
testCase.assertWarningFree(@() SCDconf_setSIMconf);
end
function setup_algo(testCase)
testCase.assertInstanceOf(testCase.algoobj,'SCDclass_algo');
end
end
end
methods (Test)
function test_callinits(testCase)
testCase.algoobj.callinits; % calls inits defining fixed parameters etc
function test_initdd(testCase)
testCase.algoobj.initdd; % init data dictionary
end
function test_setup(testCase)
testCase.algoobj.setup; % calls possible tunable parameters
testCase.algoobj.setup; % setup tunable parameters
end
function test_compile(testCase)
testCase.algoobj.compile;
testCase.algoobj.compile; % update model (Ctrl-D) Simulink
end
function test_test_harness(testCase)
......
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