function [discrules, ruleType, discFunctions]=rules(action)
%  RULES gets discretization rules.
%  For private use only.
% [DISCRULES, RULETYPE] = RULES
% DISCRULES - the rules
% RULETYPE - 0: basic rules + control rules + dsp rules
%        1: basic rules + control rules
%        2: basic rules + dsp rules
%        3: basic rules
%

% Copyright 1990-2010 The MathWorks, Inc.

% Memory block is discrete now. 
%  {{'BlockType', 'strcmpi', 'Memory'},  'DiscretizeMemory'},...

discFunctions = {};
if nargin
  switch action
   case 'get_disc_functions'
    discFunctions = get_disc_functions;
   otherwise
    error(message('Simulink:ModelDiscretizer:InvalidAction'))
  end
end

% The get_disc_functions needs to be updated in case a new entry is added to this table.
basicdiscrules = ...
{{{'BlockType', 'strcmpi', 'DiscreteTransferFcn'},  'DiscretizeTransferFcn'},...
 {{'Mask','strcmpi','on'}, {'MaskType','haselement','Discretized'},{'MaskNames','haselement','SampleTime'},{'MaskNames','haselement','WaveForm'},{'MaskNames','haselement','Units'}, 'DiscretizeParamMask'}...
 {{'BlockType', 'strcmpi', 'Integrator'},  'DiscretizeIntegrator'},...
 {{'MaskType', 'strcmpi',  'DiscretizedDiscreteIntegrator'},  'DiscretizeIntegrator'},...
 {{'BlockType','strcmpi', 'SecondOrderIntegrator'},  'DiscretizeSecondOrderIntegrator'},...
 {{'MaskType', 'strcmpi', 'DiscretizedSecondOrderIntegrator'},  'DiscretizeSecondOrderIntegrator'},...
 {{'BlockType', 'strcmpi', 'Sin'},{'SampleTime','<=','0'}, 'DiscretizeSource'},...
 {{'BlockType', 'strcmpi', 'Step'}, 'DiscretizeSource'},...
 {{'MaskType',  'strcmpi', 'DiscretizedSource'}, 'DiscretizeSource'},...
 {{'BlockType', 'strcmpi', 'RandomNumber'}, 'DiscretizeSource'},...
 {{'BlockType', 'strcmpi', 'UniformRandomNumber'}, 'DiscretizeSource'},...
 {{'BlockType', 'strcmpi', 'FromWorkspace'}, 'DiscretizeSource'},...
 {{'BlockType', 'strcmpi', 'FromFile'}, 'DiscretizeSource'},...
 {{'BlockType', 'strcmpi', 'Clock'} , 'DiscretizeClock'},...
 {{'BlockType', 'strcmpi', 'SignalGenerator'} , 'DiscretizeSignalGenerator'},...
 {{'BlockType', 'strcmpi', 'DiscretePulseGenerator'}, {'PulseType','strcmpi','Time based'}, 'DiscretizePulseGenerator'},...
 {{'MaskType' , 'strcmpi', 'Repeating table'}, 'DiscretizeRepeatingSequence'  },...
 {{'MaskType' , 'strcmpi', 'DiscretizedRepeatingTable'}, 'DiscretizeRepeatingSequence'  },...
 {{'MaskType' , 'strcmpi', 'chirp'}, 'DiscretizeChirp'},...
 {{'MaskType' , 'strcmpi', 'discretizedchirp'}, 'DiscretizeChirp'},...
 {{'MaskType' , 'strcmpi', 'Ramp'}, 'DiscretizeRamp'},...
 {{'MaskType' , 'strcmpi', 'DiscretizedRamp'}, 'DiscretizeRamp'},...
 {{'BlockType', 'strcmpi', 'TransportDelay'},  'DiscretizeTransportDelay'},...
 {{'MaskType', 'strcmpi', 'DiscretizedTransportDelay'},  'DiscretizeTransportDelay'},...
 {{'MaskType', 'strcmpi', 'PID 1dof'},  'DiscretizePID1dof'},...
 {{'MaskType', 'strcmpi', 'PID 2dof'},  'DiscretizePID2dof'}
};

controldiscrules = ...
{{{'BlockType', 'strcmpi', 'Derivative'},  'DiscretizeDerivative'},...
 {{'BlockType', 'strcmpi', 'StateSpace'},  'DiscretizeStateSpace'},... 
 {{'MaskType',  'strcmpi', 'DiscretizedStateSpace'},  'DiscretizeStateSpace'},...
 {{'MaskType',  'strcmpi', 'DiscretizedDiscreteStateSpace'},  'DiscretizeStateSpace'},...
 {{'BlockType', 'strcmpi', 'TransferFcn'},  'DiscretizeTransferFcn'},...
 {{'MaskType',  'strcmpi', 'DiscretizedDiscreteTransferFcn'},  'DiscretizeTransferFcn'},...
 {{'MaskType',  'strcmpi', 'Transfer Function with Initial States'},  'DiscretizedTransferFcnWithIC'},...
 {{'BlockType', 'strcmpi', 'ZeroPole'}, 'DiscretizeZeroPole'},...
 {{'MaskType',  'strcmpi', 'DiscretizedDiscreteZeroPole'}, 'DiscretizeZeroPole'},...
 {{'MaskType',  'strcmpi', 'LTI Block'}, {'MaskWSVariables', 'checkWSVariables','Ts'}, 'DiscretizeLTISystem'},... 
 {{'Mask','strcmpi','on'}, {'MaskType','haselement','Discretized'},{'MaskNames','haselement','method'},{'MaskNames','haselement','SampleTime'},{'MaskNames','haselement','Wc'},'DiscretizeParamMask'}...
};

% dspdiscrules = ...
% {{{'BlockType', 'strcmpi', 'VariableTransportDelay'},  'DiscretizeVarTransportDelay'},...
%  {{'BlockType', 'strcmpi', 'TransportDelay'},  'DiscretizeTransportDelay'}...
% };

dspdiscrules = ...
{{{'BlockType', 'strcmpi', 'VariableTransportDelay'},  'DiscretizeVarTransportDelay'},...
{{'MaskType', 'strcmpi', 'DiscretizedVarTimeDelay'},  'DiscretizeVarTransportDelay'}
};

if(hasControlToolbox)
    if(hasDspBlks)
        discrules = [basicdiscrules controldiscrules dspdiscrules];
        ruleType = 0;
    else
        discrules = [basicdiscrules controldiscrules];
        ruleType = 1;
    end
elseif(hasDspBlks)
    discrules = [basicdiscrules dspdiscrules];
    ruleType = 2;
else    
    discrules = basicdiscrules;
    ruleType = 3;
end
    

%end rules

%===============================================================================
% hasControlToolbox
% Check if Control Toolbox is available
%===============================================================================
%
function ret = hasControlToolbox

if(license('test','Control_Toolbox') == 1)
    ret = 1;
else
    ret = 0;
end

%end function hasControlToolbox

%===============================================================================
% hasDspBlks
% Check if Dsp Blockset is available
%===============================================================================
%
function ret = hasDspBlks

if (license('test','Signal_Blocks') == 1)
    ret = 1;
else
    ret = 0;
end
%end function hasDspBlks

%===============================================================================
function discFunct = get_disc_functions

discFunct = {'DiscretizeIntegrator',...
             'DiscretizedDiscreteIntegrator',...
             'DiscretizeSecondOrderIntegrator',...
             'DiscretizedSecondOrderIntegrator',...
             'DiscretizeSource',...
             'DiscretizeClock',...
             'DiscretizeSignalGenerator',...
             'DiscretePulseGenerator', ...
             'DiscretizePulseGenerator',...
             'DiscretizeRepeatingSequence',...
             'DiscretizeChirp',...
             'discretizedchirp',...
             'DiscretizeRamp',...
             'DiscretizedRamp',...
             'DiscretizeTransportDelay',...
             'DiscretizedTransportDelay',...
             'DiscretizeDerivative',...
             'DiscretizedDerivative',...
             'DiscretizeStateSpace',...
             'DiscretizedStateSpace',...
             'DiscretizeTransferFcn',...
             'DiscretizedSignalGenerator',...
             'DiscretizedDiscreteTransferFcn',...
             'DiscretizedTransferFcn',...
             'DiscretizedTransferFcnWithIC',...
             'DiscretizedSource',...
             'DiscretizedDiscreteStateSpace',...
             'DiscretizedDiscreteZeroPole',...
             'DiscretizeZeroPole',...
             'DiscretizedZeroPole',...
             'DiscretizeLTISystem',... 
             'DiscretizedLTISystem',... 
             'Discretized',...
             'DiscretizeParamMask',...
             'DiscretizeVarTransportDelay',...
             'DiscretizedVarTimeDelay',...
             'DiscretizedRepeatingTable'
            };

%[EOF] rules.m
