classdef StateSpec < modelpack.VariableSpec
    %modelpack.StateSpec class
    %   modelpack.StateSpec extends modelpack.VariableSpec.
    %

    %    modelpack.StateSpec properties:
    %       Description - Property is of type 'ustring'
    %       Name - Property is of type 'string'  (read only)
    %       InitialValue - Property is of type 'MATLAB array'
    %       Known - Property is of type 'MATLAB array'
    %       SteadyState - Property is of type 'MATLAB array'
    %       Minimum - Property is of type 'MATLAB array'
    %       Maximum - Property is of type 'MATLAB array'
    %       TypicalValue - Property is of type 'MATLAB array'
    %
    %    modelpack.StateSpec methods:
    %       getID - Returns the handle of the identifier object associated with THIS.
    %       getName - Returns the name of the state.
    %       setID - Sets the identifier object associated with THIS.
    %       setName - Sets the name of the state.

    % Copyright 2000-2023 The MathWorks, Inc.

    properties (Access=protected, SetObservable)
        %ID Property is of type 'handle'
        ID = [];
    end

    properties (SetAccess=protected, SetObservable)
        %NAME Property is of type 'string'  (read only)
        Name = '';
    end

    properties (SetObservable)
        %INITIALVALUE Property is of type 'MATLAB array'
        InitialValue = [];
        %KNOWN Property is of type 'MATLAB array'
        Known = [];
        %STEADYSTATE Property is of type 'MATLAB array'
        SteadyState = [];
        %MINIMUM Property is of type 'MATLAB array'
        Minimum = [];
        %MAXIMUM Property is of type 'MATLAB array'
        Maximum = [];
        %TYPICALVALUE Property is of type 'MATLAB array'
        TypicalValue = [];
    end


    methods  % constructor block
        function this = StateSpec(stateID, subsName)
            % STATESPEC Constructor
            %
            % This constructor takes a state identifier object and an optional subscripted
            % name to create a STATESPEC object.
            %
            % h = modelpack.StateSpec(stateID, [subsName]);
            %
            % STATEID  a StateID object.
            % SUBSNAME is the optional subscripted state name; otherwise set to empty.

            % Create object

            % No argument constructor call.
            ni = nargin;
            if (ni == 0)
                return
            end

            % Set the default value to empty.
            if (ni < 2 || isempty(subsName)), subsName = ''; end

            % Set properties.
            this.Version = 1.0;
            this.setID(stateID);
            this.setName(subsName);

        end  % StateSpec

    end  % constructor block

    methods
        function set.ID(obj,value)
            % DataType = 'handle'
            validateattributes(value,{'handle'}, {'scalar'},'','ID')
            obj.ID = value;
        end

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

        function set.InitialValue(obj,value)
            obj.InitialValue = LocalSetValue(obj,value,'InitialValue');
        end

        function set.Known(obj,value)
            obj.Known = LocalSetLogical(obj,value,'Known');
        end

        function set.SteadyState(obj,value)
            obj.SteadyState = LocalSetLogical(obj,value,'SteadyState');
        end

        function set.Minimum(obj,value)
            obj.Minimum = LocalSetValue(obj,value,'Minimum');
        end

        function set.Maximum(obj,value)
            obj.Maximum = LocalSetValue(obj,value,'Maximum');
        end

        function set.TypicalValue(obj,value)
            obj.TypicalValue = LocalSetValue(obj,value,'TypicalValue');
        end

    end   % set and get functions

    methods  % public methods
        function ID = getID(this)
            % GETID Returns the handle of the identifier object associated with THIS.

            ID = get(this, {'ID'});
            ID = cat(1, ID{:});

        end  % getID

        function name = getName(this)
            % GETNAME Returns the name of the state.

            name = get(this, 'Name');

        end  % getName

        function this = setID(this, ID)
            % SETID Sets the identifier object associated with THIS.

            % Type checking
            if ~isa(ID, 'modelpack.StateID')
                error(message('SLControllib:modelpack:errArgumentType', ...
                    'ID', 'modelpack.StateID' ));
            end

            % Handle vectorized call
            for ct = 1:numel(this)
                h  = this(ct);
                id = copy( ID(ct) );

                h.ID = id;
                h.setName( id.getName );
            end

        end  % setID

        function this = setName(this, name)
            % SETNAME Sets the name of the state.

            % Type checking
            if ~ischar(name)
                error(message('SLControllib:modelpack:InvalidStringArgument', ...
                    'NAME', 'setName' ));
            end

            this.Name         = modelpack.utCheckVariableSubsref(this.getID, name);
            this.InitialValue = 0;
            this.Known        = true;
            this.SteadyState  = true;
            this.Minimum      = -Inf;
            this.Maximum      = +Inf;
            this.TypicalValue = 0;

        end  % setName

    end  % public methods

end  % classdef

function value = LocalSetValue(this, value, pname)
if ~any( isnan(value(:)) )
    value = modelpack.utValidateValue(this, value, '(');
else
    error(message('SLControllib:general:RealDoubleArrayValue', pname ));
end
end  % LocalSetValue

function value = LocalSetLogical(this, value, pname)
try
    value = logical(value);
catch
    error(message('SLControllib:general:LogicalArrayValue', pname ));
end
value = modelpack.utValidateValue(this, value, '(');
end  % LocalSetLogical

function str = LocalMat2Str(value, N)
try
    str = mat2str(value, N);
catch
    s1  = regexprep( num2str(size(value)), ' *', 'x' );
    str = sprintf('[%s %s]', s1, class(value));
end
end  % LocalMat2Str

% LocalWords:  INITIALVALUE STEADYSTATE TYPICALVALUE STATEID SUBSNAME
