Skip to content
Snippets Groups Projects
SCDclass_component.m 3.45 KiB
classdef SCDclass_component
  % Superclass for SCD component
  % Can be a wrapper or a node or all SCD
  % Anything that has a model, a data dictionary
  % and optionally an associated algorithm
  
  properties
    loadverbose = 1;
  end
  
  properties
    name    % name
    mdlname % simulink model name
    ddname  % data dictionary name
    algos   % algorithms contained in this wrapper
  end

  methods
   
    function obj = addalgo(obj,algo)
      assert(isa(algo,'SCDclass_algo'));
      obj.algos = [obj.algos;algo];
    end
    
    function obj = linkalgodd(obj)
      % Set up data dictionary to link to all algo data
      % dictionaries as data sources
      
      % wrapper data dictionary
      ddObj = Simulink.data.dictionary.open(obj.ddname);
      % already linked sources
      prevsources = ddObj.DataSources;
      prevsources_algo = prevsources(startsWith(prevsources,'SCDalgo'));
      
      % make algo data dictionary list list
      reqsources = cell(numel(obj.algos),1);
      for ii=1:numel(obj.algos)
        myalgo = obj.algos(ii);
        reqsources{ii} = myalgo.getdatadictionary;
      end
      
      % removing unnecessary ones
      for ii=1:numel(prevsources_algo)
        mysource = prevsources_algo{ii};
        if ~contains(reqsources,mysource)
          obj.printlog('Removing algorithm data source %s from %s',mysource,obj.ddname);
          ddObj.removeDataSource(prevsources_algo{ii})
        end
      end
      
      % add new ones not yet present
      for ii=1:numel(reqsources)
        mysource = reqsources{ii};
        assert(startsWith(mysource,'SCDalgo'),...
          'attempting to add algo dd: %s that does not start with ''SCDalgo''-aborting',mysource);
        if contains(prevsources,mysource)
          obj.printlog('Not adding algorithm data source %s - already exists',mysource);
        else
          obj.printlog('Adding algorithm data source %s to %s',mysource,obj.ddname);
          ddObj.addDataSource(mysource);
        end
      end
    end
    
    function obj = linknodedd(obj,node)
      assert(isa(node,'SCDclass_node'),'node must be an SCDclass_node object')
      ddObj = Simulink.data.dictionary.open(obj.ddname);
      
      prefix = 'SCD_rtc';
      
      % already linked sources
      prevsources = ddObj.DataSources;
      prevsources_nodes = prevsources(startsWith(prevsources,prefix));
      
      % make algo data dictionary list list
      reqsources = node.ddname;
      
      % removing unnecessary ones
      for ii=1:numel(prevsources_nodes)
        mysource = prevsources_nodes{ii};
        if ~contains(reqsources,mysource)
          obj.printlog('Removing algorithm data source %s from %s',mysource,obj.ddname);
          ddObj.removeDataSource(prevsources_nodes{ii})
        end
      end
      
      % add new ones not yet present
      mysource = reqsources;
      assert(startsWith(mysource,prefix),...
        'attempting to add algo dd: %s that does not start with ''%s''-aborting',prefix,mysource);
      if contains(prevsources,mysource)
        obj.printlog('Not adding node data source %s - already exists',mysource);
      else
        obj.printlog('Adding node data source %s to %s',mysource,obj.ddname);
        ddObj.addDataSource(mysource);
      end
    end
    
    function obj=printlog(obj,str,varargin)
      % printlog, allows sprintf()-like expressions
      if obj.loadverbose==1
        fprintf('%s, ',obj.name);
        fprintf(str,varargin{:}); fprintf('\n');
      end
    end
    
  end
  
  
end