% SLCOVERAGE.BLOCKSELECTOR Block selector class
%   Selects a block for filtering by its properties.
%   so = slcoverage.BlockSelector(type, name) - creates a selector object
%       type - slcoverage.BlockSelectorType
%       name - property name, block handle or Simulink ID
%
% SLCOVERAGE.BLOCKSELECTOR methods:
%   slcoverage.BlockSelector.allSelectors(blockH) - all BlockSelectors
%                                                   for a block
% See also: SLCOVERAGE.BLOCKSELECTORTYPE, SLCOVERAGE.FILTERRULE,
%           SLCOVERAGE.FILTER

% Copyright 2017 The MathWorks, Inc.

classdef BlockSelector < slcoverage.Selector

properties (SetAccess = private)
end
methods
   function  this = BlockSelector(type, name)
        [status, msgId] = SlCov.CoverageAPI.checkCvLicense;
        if status == 0
            error(message(msgId));
        end
        sid = '';
        if ~isequal(type, slcoverage.BlockSelectorType.Subsystem) && ... % this should be Library
           ~isequal(type,  slcoverage.BlockSelectorType.TemporalEvent) && ...
           ~isequal(type,  slcoverage.BlockSelectorType.BlockType) && ...
           ~isequal(type,  slcoverage.BlockSelectorType.MaskType)
            if ~Simulink.ID.isValid(name)
                try 
                    name = Simulink.ID.getSID(name);
                    sid = name;
                catch
                end
            else
                sid = name;
            end
        end
        p = inputParser;
        typeValidationFcn = @(x)validateattributes(x, {'slcoverage.BlockSelectorType'}, {'scalar'}, 'BlockSelector', 'type', 1);
        sidValidationFcn = @(x)validateattributes(x, {'char'}, {'vector'}, 'BlockSelector', 'name', 2);
        p.addRequired('type', typeValidationFcn);
        p.addRequired('name', sidValidationFcn);
        parse(p, type, convertStringsToChars(name));
        this@slcoverage.Selector(sid, p.Results.type, p.Results.name);
        this.setConstructorCode(sprintf('slcoverage.BlockSelector(%s, ''%s'')', mat2str(p.Results.type), p.Results.name));
   end
end
%===================
methods  (Static = true)   
    function s = allSelectors(blockH)
        s = slcoverage.Selector.allSelectors(blockH, 'Type', 'slcoverage.BlockSelectorType');
    end
end

end
