classdef CSDStateID < modelpack.StateID
%modelpack.CSDStateID class
%   modelpack.CSDStateID extends modelpack.StateID.
%

%   Copyright 2023 The MathWorks, Inc.

%    modelpack.CSDStateID properties:
%       Description - Property is of type 'ustring'  
%
%    modelpack.CSDStateID methods:
%       getDimensions -  method to return State dimension
%       getFullName -  method to return State full name. The full name is a
%       getName -  method to return State name
%       getPath -  method to return State path
%       getTs -  method to return state sampling period. Zero indicates a continuous
%       update -  method to set SISOTOOL model state object's 


properties (Access=protected, SetObservable)
    %DIMENSION Property is of type 'MATLAB array' 
    Dimension = [];
    %NAME Property is of type 'string' 
    Name = '';
    %PATH Property is of type 'ustring' 
    Path = '';
    %TS Property is of type 'double' 
    Ts = 0;
end


    methods  % constructor block
        function this = CSDStateID(Name,Dimension,Path,Ts) 
        % CSDSTATEID  constructor for SISOTOOL state object
        %
        % h = modelpack.CSDStateID(Name,Dimension,[Path],[Ts]) 
        %
        % Inputs:
        %   Name      - string with state name
        %   Dimension - double vector with dimensions of state
        %   Path      - string with path to state
        %   Ts        - double with state sampling period, zero, implies continuous
        %
        
        %Instantiate object
                
        % No argument constructor call
        if nargin == 0, return, end
        
        %Check number of arguments
        if nargin < 2 || nargin > 4
           error(message('SLControllib:modelpack:errNumArguments','4'))
        end
        
        %Default arguments
        if nargin < 3 || isempty(Path), Path = ''; end
        if nargin < 4 || isempty(Ts),   Ts   = 0; end
        
        %Check input argument types
        if ~isnumeric(Ts) || ~isscalar(Ts) || ~isfinite(Ts) || Ts < 0
           error(message('SLControllib:modelpack:stErrorFinitePositive','Ts'))
        end
        
        %Set properties
        this.Version   = 1.0;
        this.Name      = Name;
        this.Path      = Path;
        this.Dimension = Dimension;
        this.Ts        = Ts;
        
        end  % CSDStateID
        
    end  % constructor block

    methods 
        function set.Name(obj,value)
            % DataType = 'string'
        validateattributes(value,{'char'}, {'row'},'','Name')
        obj.Name = value;
        end

        function set.Path(obj,value)
            % DataType = 'ustring'
        % no cell string checks yet'
        obj.Path = value;
        end

        function set.Ts(obj,value)
            % DataType = 'double'
        validateattributes(value,{'numeric'}, {'scalar'},'','Ts')
        value = double(value); %  convert to double
        obj.Ts = value;
        end

    end   % set and get functions 

    methods  % public methods
       function Dim = getDimensions(this) 
       % GETDIMENSIONS  method to return State dimension
       %
        
       Dim = get(this,'Dimension');
       
       end  % getDimensions
       
       function FullName = getFullName(this) 
       % GETFULLNAME  method to return State full name. The full name is a
       % concatenation of the State path and State name.
       %
        
       name = get(this,'Name');
       path = get(this,'Path');
       
       %Check whether path is empty, if not tag a '/' on end to separate path and
       %name
       if numel(this) > 1
          %Vector of states
          idx = ~cellfun('isempty',path);
          if any(idx)
             %Path is not empty, append path separator
             tmp = strcat(path(idx),'/');
             [path{idx}] = deal(tmp{:});
          end
       else
          %Single object
          if ~isempty(path)
             path = strcat(path,'/');
          end
       end
       
       %Form full name
       FullName = strcat(path,name);
       
       end  % getFullName
       
       function Name = getName(this) 
       % GETNAME  method to return State name
       %
        
       Name = get(this,'Name');
       
       end  % getName
       
       function Path = getPath(this) 
       % GETPATH  method to return State path
       %
        
       Path = get(this,'Path');
       
       end  % getPath
       
       function Ts = getTs(this)
       % GETTS  method to return state sampling period. Zero indicates a continuous
       % state.
       %
        
       Ts = get(this,'Ts');
       
       end  % getTs
       
       function update(this,Model,varargin) 
       % UPDATE  method to set SISOTOOL model state object's 
       % properties.
       %
       % this.update(Model,varargin)
       % 
       % Input:
       %   Model    - a SISOTOOL model object that contains the parameter being
       %              updated
       %   varargin - a variable list of property value pairs or structure with
       %              fields to set
        
       %Check input argument types
       nVarIn = numel(varargin);
       if ~isa(Model,'modelpack.CSDModel')
          error(message('SLControllib:modelpack:errArgumentType','Model','modelpack.CSDModel'));
       end
       if ~iscell(varargin) || ...
             ~(rem(nVarIn,2)==0 || nVarIn==1&&isstruct(varargin{1}))
          error(message('SLControllib:modelpack:errPropertyValuePairs'))
       end
       
       %Now check that we have valid properties to set
       ValidProps = {'ts','dimension'};
       if nVarIn > 1
          %List of property value pairs
          inProps = {varargin{1:2:end}};
       else
          inProps = fieldnames(varargin);
       end
       if isempty(intersect(ValidProps,lower(inProps)))
          error(message('SLControllib:modelpack:stErrorProperty','{''ts''|''dimension''}'));
       end
       
       %Everything's ok set the properties
       if nVarIn > 1
          %List of property value pairs
          set(this,varargin{:});
       else
          %Structure of property value pairs
          set(this,varargin)
       end
       
       end  % update
       
end  % public methods 

end  % classdef

% LocalWords:  GETDIMENSIONS
