Review a Model Against Conditions that You Specify with the Model Advisor
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.
Create an sl_customization
Function
In 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
Create the Check Definition Function for a Pass/Fail Check with No Fix Action
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.Description = 'Identify Constant blocks with a value that is a number.'; ElementResults.Status = 'All Constant blocks have a value that is a letter.'; mdladvObj.setCheckResultStatus(true); else for i=1:numel(violationBlks) ElementResults(1,i) = ModelAdvisor.ResultDetail; end for i=1:numel(ElementResults) ModelAdvisor.ResultDetail.setData(ElementResults(i),'SID',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);
Create the Check Definition Function for an Informational Check
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.
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_Detail,'None','DetailStyle'); % Publish check into Demo group. mdladvRoot = ModelAdvisor.Root; mdladvRoot.publish(rec, 'Demo'); end % ----------------------------- % This callback function uses the DetailStyle CallbackStyle type. % ----------------------------- function modelVersionChecksumCallbackUsingFT_Detail(system,CheckObj) model = bdroot(system); mdladvObj = Simulink.ModelAdvisor.getModelAdvisor(system); ElementResults = ModelAdvisor.ResultDetail; ElementResults.Description = 'Display model configuration and checksum information'; % If running the Model Advisor on a subsystem, add note to description. if strcmp(system, model) == false ElementResults.Status = 'NOTE: The Model Advisor is reviewing a subsystem, but these results are based on root-level settings.'; ElementResults(end + 1) = ModelAdvisor.ResultDetail; end % 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); catch mdladvObj.setCheckResultStatus(false); end lbStr ='<br/>'; resultStr = ['Model Version: ' mdlver lbStr 'Author: ' mdlauthor lbStr ... 'Date: ' mdldate lbStr 'Model Checksum: ' mdlsum]; ElementResults(end).Status = resultStr; CheckObj.setResultDetails(ElementResults); end
Run the Custom Checks in the Model Advisor
In the Command Window, enter:
Advisor.Manager.refresh_customizations
Open the model
sldemo_fuelsys
by typing this command in the MATLAB command prompt:openExample('sldemo_fuelsys')
In the Modeling tab, select Model Advisor. A System Selector dialog opens. Click OK.
In the left pane, select By Product > Demo > Identify model configuration and checksum information.
Click Run Checks.
The check passes and displays the information.
In the left pane, select By Product > Demo > Check Constant block usage.
Click Run Checks.
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.
See Also
ModelAdvisor.Check
| ModelAdvisor.FormatTemplate
| ModelAdvisor.Check.CallbackContext