Skip to content
Snippets Groups Projects
SCDclass_mdswg.m 8.51 KiB
classdef SCDclass_mdswg < matlab.mixin.Heterogeneous
    % Superclass for MDS+ wavegens
    % the superclass matlab.mixin.Heterogeneous allows
    % building of lists of mixed kind parameters in the
    % expcode configuration (and later in C++ code)
    
    properties (Access = protected)
    %properties    
        mdsserver           % The MDS+ server hosting the parameter
        mdstree             % The MDS+ Tree hosting the parameter
        tdiexpr             % TDI expression to retrieve data
        wavegenbasestruct   % Wavegen base structure hosting the wavegen timeseries struct
        wavegentarget       % Wavegen target to be filled
        value               % commandile output of the wavegen
        datadictionary      % data dictionary hosting the parameter, if empty base workspace
        modelname           % name of the Simulink model using the parameter
        getcommand          % full command for getting the value (callablSCDalgo_f4ealgoe by matlab eval)
        classname           % class name for logging
        marteclassname      % class name for generating MARTe2 cfg file
        cparser             % constructor parameters parser
        
        timebasestart       % timebase start time variable
        timebasedt          % timebase dt variable
        timebasestop        % timebase stop time variable
        
        mdshelpstr          % MDS help string used in tree autopopulation
        mdsvalidationstr    % MDS validation string used in autopopulation
    end
    
    properties 
        verbose             % Verbosity of the class
    end
    
    methods
        
        %function obj=SCDclass_mdswavegen(srcsrv, srctree, srctdi, modelname, destwavegen, timebasestart, timebasedt, timebasestop)
        function obj=SCDclass_mdswg()
            obj.cparser=inputParser;
            addRequired(obj.cparser,'srctdi',@(x) ischar(x));
            addRequired(obj.cparser,'destwavegen',@(x) ischar(x));
            addParameter(obj.cparser,'srcsrv','tcvdata',@(x) ischar(x));
            addParameter(obj.cparser,'srctree','tcv_shot',@(x) ischar(x));
            addParameter(obj.cparser,'modelname','',@(x) ischar(x));
            addParameter(obj.cparser,'mdshelp','',@(x) ischar(x));
            addParameter(obj.cparser,'mdsvalid','1',@(x) ischar(x));

            obj.verbose=1;
            obj.wavegenbasestruct='';
        end
        
        function obj=parseconstructorcommon(obj, srctdi, destwavegen, varargin)
            parse(obj.cparser,srctdi,destwavegen,varargin{:}{:});
           
            obj.mdsserver=obj.cparser.Results.srcsrv;
            obj.mdstree=obj.cparser.Results.srctree;
            obj.tdiexpr=obj.cparser.Results.srctdi;
            obj.wavegentarget=obj.cparser.Results.destwavegen;
            obj.modelname=obj.cparser.Results.modelname;
            obj.mdshelpstr=obj.cparser.Results.mdshelp;
            obj.mdsvalidationstr=obj.cparser.Results.mdsvalid;
        end    
        
    end
    
    % Not abstract methods common to all child classes
    methods
        function mdsconnect(obj, shot)
           mdsconnect(obj.mdsserver);
           s=mdsopen(obj.mdstree, shot);
           str=sprintf('SCDclass_mdswavegen (%s), failed opening MDS+ tree', obj.wavegentarget);
           assert(s==shot, str);
        end
        
        function printinfocommon(obj)
            fprintf('%s (class %s):\n', obj.gettargetwavegen, obj.classname);
            fprintf('  Simulink model: ''%s''\n', obj.modelname);
            fprintf('  MDS+ source server: ''%s'', Tree: ''%s''\n', obj.mdsserver, obj.mdstree);
            fprintf('  MDS+ TDI expression: ''%s''\n',obj.tdiexpr);
        end
        
        function obj = settiminginfo(obj, timeinfo)
            obj.timebasestart=timeinfo.t_start;
            obj.timebasedt=timeinfo.dt;
            obj.timebasestop=timeinfo.t_stop;
        end
        
        function obj = setmodelname(obj, modelname)
            if(isempty(obj.modelname))
                obj.modelname = modelname;
            end
            
        end
        
        function obj = setdatadictionary(obj, ddname)
            if(isempty(obj.datadictionary))
                obj.datadictionary = ddname;
            end
        end

        function obj = setbasestruct(obj, basestruct)
            obj.wavegenbasestruct = basestruct;
        end
        
        %function out = gettargetparam(obj)
        %    out = obj.modelparam;
        %end

        function entrystring = genMARTe2entrycommon(obj, shot)
            %entrystring = ['+' obj.wavegentarget ' = { Class=' obj.classname ' Path=' obj.tdiexpr ];
            entrystring = sprintf('+%-50s = { Class=%-30s Path=%-40s',obj.wavegentarget,obj.marteclassname,obj.genMARTe2MDStdiexpression);
        end
        
        function name=getmodelname(obj)
            name=obj.modelname;
        end
        
        function [mdsserver] = getMDSserver(obj)
          mdsserver = obj.mdsserver;
        end
        
        function [mdstree] = getMDStree(obj)
          mdstree = obj.mdstree;
        end
        
        function str=gettarget(obj)
           str = obj.wavegentarget; 
        end
       
        function str = genMARTe2MDStdiexpression(obj)
            % Duplicate first backslash
%             if(obj.tdiexprused(1)=='\' && not(obj.tdiexprused(2)=='\'))
%                 martetdi=['\' obj.tdiexprused];
%             else
%                 martetdi=obj.tdiexprused;
%             end
            % Duplicate backslashes
            %martetdi=strrep(obj.tdiexpr, '\', '\\');
            martetdi=obj.tdiexpr;
            %substitute every " with '
            martetdi=strrep(martetdi, '"', '''');
            %put double string quota
            martetdi=['"' martetdi '"'];
            str=martetdi;
        end
        
        function obj = autopopulatemdscommon(obj, shot, basenode, dim, data, units, srcval)
                        
            fprintf('Changing MDS node: ''%s'' <- %s\n',basenode,srcval);

            node = basenode;
            nodeval = [node '.data'];
            nodedim = [node '.dim'];
            nodecom = [node '.comment'];
            nodevalid = obj.mdsvalidationstr;
            signal = ['build_signal(build_with_units(' nodeval ',"' units '"),*,build_with_units(' nodedim ',"s"))'];
            expr = ['build_param(' signal ',' nodecom ',' nodevalid ')'];
              
            obj.mdsconnect(shot);
            retval=mdsput(node, expr, 'x');
            if isnumeric(retval)
                if ~rem(retval,2)
                    warning('SCDclass_mdswg:MDSerror','Error writing node build_param.');
                end
            else
                warning('SCDclass_mdswg:MDSerror','Error writing node build_param.');
            end

            retval=mdsput(nodeval, data);
            if isnumeric(retval)
                if ~rem(retval,2)
                    warning('SCDclass_mdswg:MDSerror','Error writing node data.');
                end
            else
                warning('SCDclass_mdswg:MDSerror','Error writing node data.');
            end

            retval=mdsput(nodedim, dim);
            if isnumeric(retval)
                if ~rem(retval,2)
                    warning('SCDclass_mdswg:MDSerror','Error writing node dim.');
                end
            else
                warning('SCDclass_mdswg:MDSerror','Error writing node dim.');
            end
                        
            retval=mdsput(nodecom, obj.mdshelpstr);
            if isnumeric(retval)
                if ~rem(retval,2)
                    warning('SCDclass_mdswg:MDSerror','Error writing node comment.');
                end
            else
                warning('SCDclass_mdswg:MDSerror','Error writing node comment.');
            end
        end
    end
    
    
    % Abstract method actually implemented by child classes
    methods (Abstract)
        
        % Gets data from MDS+ and actualizes it on the model,
        % MDS+ connection is reopened for every call ...
        actualizedata(obj, shot)
                     
        % Gets data from mds, mds connection is assumed already estabilished
        % and tree opened
        value = getdata(obj)
       
        % Gets a string containing the target wavegen of the model
        out = gettargetwavegen(obj)
        
        % Generate C++ code 
        %gencode(obj)
       
        % Generate MARTe2 configuration entry
        entrystring = genMARTe2entry(obj, shot)
        
        % Prints the parameter info summary
        printinfo(obj)
        
        % Populates the corresponding MDS node with workspace values
        autopopulatemds(obj, shot)

    end
end