classdef CSDParameterID < modelpack.ParameterID
    %modelpack.CSDParameterID class
    %   modelpack.CSDParameterID extends modelpack.ParameterID.
    %

%   Copyright 2023 The MathWorks, Inc.

    %    modelpack.CSDParameterID properties:
    %       Description - Property is of type 'ustring'
    %
    %    modelpack.CSDParameterID methods:
    %       getClass -  method to return parameter class
    %       getDimensions -  method to return Parameter dimension
    %       getFullName -  method to return Parameter full name. The full name is a
    %       getLocations -  method to return locations where this parameter is used
    %       getName -  method to return Parameter name
    %       getPath -  method to return Parameter path
    %       isSame - Method to determine whether a given CSDParameterID
    %       update -  method to set SISOTOOL model parameter object's


    properties (Access=protected, SetObservable)
        %CLASS Property is of type 'string'
        Class = '';
        %DIMENSION Property is of type 'MATLAB array'
        Dimension = [];
        %LOCATIONS Property is of type 'string vector'
        Locations = cell(0,1);
        %NAME Property is of type 'string'
        Name = '';
        %UNIQUENAME Property is of type 'string'
        UniqueName = '';
        %PATH Property is of type 'ustring'
        Path = '';
    end


    methods  % constructor block
        function this = CSDParameterID(UniqueName,Dimension,Path, Class, Locations, Name)
            % CSDParameterID  constructor for SISOTOOL parameter object
            %
            % h = modelpack.CSDParameterID(UniqueName,Dimension,[Path], [Class], [Locations], [Name])
            %
            % Inputs:
            %   UniqueName - string with the unique name of the parameter
            %   Dimension  - double vector with dimensions of parameter
            %   Path       - string with path to parameter
            %   Class      - string with class of parameter
            %   Locations  - string vector with locations where parameter is used
            %   Name       - string with parameter name
            %

            %Instantiate object

            % No argument constructor call
            if nargin == 0, return, end

            %Check number of arguments
            if nargin < 2 || nargin > 6
                error(message('SLControllib:modelpack:errNumArguments','2 to 5'))
            end

            %Set default arguments
            if nargin < 3 || isempty(Path),       Path       = ''; end
            if nargin < 4 || isempty(Class),      Class      = 'double'; end
            if nargin < 5 || isempty(Locations),  Locations  = {}; end
            if nargin < 6 || isempty(Name),       Name       = UniqueName; end

            %Set properties
            this.Version    = 1.0;
            this.UniqueName = UniqueName;
            this.Name       = Name;
            this.Path       = Path;
            this.Dimension  = Dimension;
            this.Locations  = Locations;
            this.Class      = Class;

        end  % CSDParameterID

    end  % constructor block

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

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

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

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

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

    end   % set and get functions

    methods  % public methods
        function Class = getClass(this)
            % GETCLASS  method to return parameter class
            %

            Class = get(this,'Class');

        end  % getClass

        function Dim = getDimensions(this)
            % GETDIMENSIONS  method to return Parameter dimension
            %

            Dim = get(this,'Dimension');

        end  % getDimensions

        function FullName = getFullName(this)
            % GETFULLNAME  method to return Parameter full name. The full name is a
            % concatenation of the ParameterID path and Parameter 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 parameters
                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 Locations = getLocations(this)
            % GETLOCATIONS  method to return locations where this parameter is used
            %

            Locations = get(this,'Locations');

        end  % getLocations

        function Name = getName(this)
            % GETNAME  method to return Parameter name
            %

            Name = get(this,'Name');

        end  % getName

        function Path = getPath(this)
            % GETPATH  method to return Parameter path
            %

            Path = get(this,'Path');

        end  % getPath

        function flag = isSame(this, ID)
            % ISSAME Method to determine whether a given CSDParameterID
            % object identifies the same object as THIS.  Equivalent to operator
            % overloading for "==".

            flag = false;

            if (length(this(:)) == length(ID(:))) && ...
                    isa(ID, class(this)) && ...
                    isequal(this.UniqueName,ID.UniqueName) && ...
                    all( strcmp(ID.getFullName, this.getFullName) ) && ...
                    isequal(ID.getDimensions, this.getDimensions) && ...
                    all( strcmp(ID.getClass, this.getClass) )
                flag = true;
            end

        end  % isSame

        function update(this,~,varargin)
            % UPDATE  method to set SISOTOOL model parameter 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 ~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 = {'locations','dimension'};
            if nVarIn > 1
                %List of property value pairs
                inProps = varargin(1:2:end);
            else
                %Structure of property value pairs
                inProps = fieldnames(varargin{1});
            end
            if isempty(intersect(ValidProps,lower(inProps)))
                error(message('SLControllib:modelpack:stErrorProperty','{''locations''|''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{1})
            end

        end  % update

    end  % public methods

end  % classdef

% LocalWords:  UNIQUENAME GETCLASS GETDIMENSIONS GETLOCATIONS
