This example demonstrates how to create two simple check types: a pass/fail check with no fix action and an informational check. A basic pass/fail check finds and reports what the check is reviewing and whether the check passes or fails. An informational check finds and displays a description of what the check is reviewing and any references to applicable standards.
sl_customization
FunctionIn your working folder, create the sl_customization.m
file. To
register the custom checks, within the sl_customization.m
file,
create an sl_customization(cm)
function as shown here. This
function accepts one argument, a customization object. This customization manager
object includes the addModelAdvisorCheckFcn
method for
registering the custom checks. The input to this method is a handle to the function
(defineModelAdvisorChecks
) that contains calls to two check
definition functions. These functions contain the definitions of the simple
pass/fail check and the informational check.
function sl_customization(cm) % SL_CUSTOMIZATION - Model Advisor customization demonstration. % Copyright 2019 The MathWorks, Inc. % register custom checks cm.addModelAdvisorCheckFcn(@defineModelAdvisorCheck); % ----------------------------- % defines Model Advisor Checks % ----------------------------- function defineModelAdvisorCheck definePassFailCheck defineInformationCheck
In this section, you create the check definition function that checks whether a Constant block value is a number or a letter. If the value is a number, the check produces a warning. If the value is a letter, the check passes.
This check uses the DetailStyle
type of callback function. This
style allows you to view results by block, subsystem, or recommended action.
Applying this style produces default formatting, so that you do not have to use the
ModelAdvisor.FormatTemplate
class or
the other Model Advisor formatting APIs to format the results that appear in the
Model Advisor. You specify this style as an input to the setCallbackFcn
method.
Create a new file, definePassFailCheck.m
, and enter the
function shown
here:
function definePassFailCheck mdladvRoot = ModelAdvisor.Root; rec = ModelAdvisor.Check('simplePassFailCheck'); rec.Title = 'Check Constant block usage'; rec.TitleTips = ['Warn if Constant block value is a number; Pass if' ... ' Constant block value is a letter']; rec.setCallbackFcn(@simplePassFailCheck,'None','DetailStyle') mdladvRoot.publish(rec, 'Demo'); % --- Callback function that checks Constant blocks function simplePassFailCheck(system,CheckObj) mdladvObj = Simulink.ModelAdvisor.getModelAdvisor(system); all_constant_blk=find_system(system,'LookUnderMasks','all',... 'FollowLinks','on','BlockType','Constant'); violationBlks=find_system(all_constant_blk,'RegExp','On','Value','^[0-9]'); if isempty(violationBlks) ElementResults = ModelAdvisor.ResultDetail; ElementResults.IsInformer = true; ElementResults.Description = 'Identify Constant blocks with a value that is a number.'; ElementResults.Status = 'All Constant blocks have have a value that is a letter.'; mdladvObj.setCheckResultStatus(true); else ElementResults(1,numel(violationBlks))=ModelAdvisor.ResultDetail; for i=1:numel(ElementResults) ElementResults(i).setData(violationBlks{i}); ElementResults(i).Description = 'Identify Constant blocks with a value that is a number.'; ElementResults(i).Status = 'The following Constant blocks have values that are numbers:'; ElementResults(i).RecAction = 'Change the Constant block value to a letter.'; end mdladvObj.setCheckResultStatus(false); mdladvObj.setActionEnable(true); end CheckObj.setResultDetails(ElementResults);
In this section, you create a check definition function for an informational check that finds and displays the model configuration and checksum information.
For an informational check, the Model Advisor displays the overall check status, but the status is not in the result. In addition, an informational check does not include the following items in the results:
A description of the status.
The recommended action to take when the check does not pass.
Subcheck results.
This check definition function uses the StyleOne
type of
callback function, so you must use the Model Advisor formatting APIs to format
results that appear in the Model Advisor. You specify StyleOne
as
an input to the setCallbackFcn
method.
Create a new file, defineInformationCheck.m
, and enter the
function shown here:
function defineInformationCheck % Create ModelAdvisor.Check object and set properties. rec = ModelAdvisor.Check('com.mathworks.sample.infocheck'); rec.Title = 'Identify model configuration and checksum information'; rec.TitleTips = 'Display model configuration and checksum information'; rec.setCallbackFcn(@modelVersionChecksumCallbackUsingFT,'None','StyleOne'); % Publish check into Demo group. mdladvRoot = ModelAdvisor.Root; mdladvRoot.publish(rec, 'Demo'); end % ----------------------------- % This callback function uses the StyleOne CallbackStyle type. % ----------------------------- function resultDescription = modelVersionChecksumCallbackUsingFT(system) resultDescription = []; system = getfullname(system); model = bdroot(system); % Format results in a list using Model Advisor Result Template API. ft = ModelAdvisor.FormatTemplate('ListTemplate'); % Description of check in results setInformation(ft, 'Display model configuration and checksum information'); % Add See Also section for references to standards. docLinkSfunction{1} = {['IEC 61508-3, Table A.8 (5)' ... ' ''Software configuration management'' ']}; setRefLink(ft,docLinkSfunction); % If running the Model Advisor on a subsystem, add note to description. if strcmp(system, model) == false setInformation(ft,{['NOTE: The Model Advisor is reviewing a' ... ' sub-system, but these results are based on root-level settings.']}); end mdladvObj = Simulink.ModelAdvisor.getModelAdvisor(system); % If error is encountered, use these values. mdlver = 'Error - could not retrieve Version'; mdlauthor = 'Error - could not retrieve Author'; mdldate = 'Error - could not retrieve Date'; mdlsum = 'Error - could not retrieve CheckSum'; % Get model configuration and checksum information. try mdlver = get_param(model,'ModelVersion'); mdlauthor = get_param(model,'LastModifiedBy'); mdldate = get_param(model,'LastModifiedDate'); mdlsum = Simulink.BlockDiagram.getChecksum(model); mdlsum = [num2str(mdlsum(1)) ' ' num2str(mdlsum(2)) ' ' ... num2str(mdlsum(3)) ' ' num2str(mdlsum(4))]; mdladvObj.setCheckResultStatus(true); % init to true catch err mdladvObj.setCheckResultStatus(false); setSubResultStatusText(ft,err.message); resultDescription{end+1} = ft; return end lbStr ='<br/>'; resultStr = ['Model Version: ' mdlver lbStr 'Author: ' mdlauthor lbStr ... 'Date: ' mdldate lbStr 'Model Checksum: ' mdlsum]; % Display the results setSubResultStatusText(ft,resultStr); % The check does not have subresults and does not need a bar. setSubBar(ft,0); resultDescription{end+1} = ft; end
In the Command Window, enter:
Advisor.Manager.refresh_customizations
Open the model sldemo_fuelsys
by typing this command in
the MATLAB command
prompt:
sldemo_fuelsys
In the Modeling tab, select Model Advisor. A System Selector ― Model Advisor dialog box opens. Click OK.
In the left pane, select By Product > Demo > Identify model configuration and checksum information.
Click Run This Check.
The check passes and displays the information.
In the left pane, select By Product > Demo > Check Constant block usage.
Click Run This Check.
The check produces a warning because several blocks contain values that are numbers. The results contain links to these blocks. The result displays a Recommended Action.
Follow the Recommended Action to fix the Constant blocks.
ModelAdvisor.Check
| ModelAdvisor.Check.CallbackContext
| ModelAdvisor.FormatTemplate
| ModelAdvisor.FormatTemplate