Documentation

Define Custom Checks

About Custom Checks

You can create a custom check to use in the Model Advisor. Creating custom checks provides you with the ability to specify which conditions and configuration settings the Model Advisor reviews.

You define custom checks in one or more functions that specify the properties of each instance of the ModelAdvisor.Check class. Define one instance of this class for each custom check that you want to add to the Model Advisor, and register the custom check.

    Tip   You can add a check to multiple folders by creating a task.

Contents of Check Definitions

When you define a Model Advisor check, it contains the information listed in the following table.

ContentsDescription
Check ID (required)Uniquely identifies the check. The Model Advisor uses this id to access the check.
Handle to check callback function (required)Function that specifies the contents of a check.
Check name (recommended)Creates a name for the check that the Model Advisor displays.
Check properties (optional)Creates a user interface with the check. When adding checks as tasks, the Model Advisor uses the task properties instead of the check properties, except for Visible and LicenseName.
Input Parameters (optional)Adds input parameters that request input from the user. The Model Advisor uses the input to perform the check.
Action (optional)Adds automatic fixing action.
Explore Result button (optional)Adds the Explore Result button that the user clicks to open the Model Advisor Result Explorer.

Display and Enable Checks

You can create a check and specify how it appears in the Model Advisor. You can define when to display a check, or whether a user can select or clear a check using the Visible, Enable, and Value properties of the ModelAdvisor.Check class.

    Note:   When adding checks to the Model Advisor as tasks, specify these properties in the ModelAdvisor.Task class. If you specify the properties in both ModelAdvisor.Check and ModelAdvisor.Task, the ModelAdvisor.Task properties take precedence, except for the Visible and LicenseName properties.

Modify the behavior of the Visible, Enable, and Value properties in a process callback function. The following chart illustrates how these properties interact.

Define Where Custom Checks Appear

Specify where the Model Advisor places custom checks using the following guidelines:

  • To place a check in a new folder in the Model Advisor root, use the ModelAdvisor.Group class.

  • To place a check in a new folder in the By Task folder, use the ModelAdvisor.FactoryGroup class.

  • To place a check in the By Product folder, use the ModelAdvisor.Root.publish method. If the By Product folder is not displayed in the Model Advisor window, select Show By Product Folder from the Settings > Preferences dialog box.

Check Definition Function

This example shows a function that defines the custom checks associated with the callback functions described in Create Callback Functions and Results. The check definition function returns a cell array of custom checks to be added to the Model Advisor.

The check definitions in the example use the tasks described in Define Custom Tasks.

% Defines custom Model Advisor checks
function defineModelAdvisorChecks

% Sample check 1: Informational check
rec = ModelAdvisor.Check('mathworks.example.configManagement');
rec.Title = 'Informational check for model configuration management';
setCallbackFcn(rec, @modelVersionChecksumCallbackUsingFT,'None','StyleOne');
rec.CallbackContext = 'PostCompile';
mdladvRoot = ModelAdvisor.Root;
mdladvRoot.register(rec);

% Sample check 2: Basic Check with Pass/Fail Status
rec = ModelAdvisor.Check('mathworks.example.unconnectedObjects');
rec.Title = 'Check for unconnected objects';
setCallbackFcn(rec, @unconnectedObjectsCallbackUsingFT,'None','StyleOne');
mdladvRoot = ModelAdvisor.Root;
mdladvRoot.register(rec);

% Sample Check 3: Check with Subchecks and Actions
rec = ModelAdvisor.Check('mathworks.example.optimizationSettings');
rec.Title = 'Check safety-related optimization settings';
setCallbackFcn(rec, @OptmizationSettingCallback,'None','StyleOne');
% Define an automatic fix action for this check
modifyAction = ModelAdvisor.Action;
setCallbackFcn(modifyAction, @modifyOptmizationSetting);
modifyAction.Name = 'Modify Settings';
modifyAction.Description = ['Modify model configuration optimization' ...
                            ' settings that can impact safety.'];
modifyAction.Enable = true;
setAction(rec, modifyAction);
mdladvRoot = ModelAdvisor.Root;
mdladvRoot.register(rec);

Define Check Input Parameters

With input parameters, the you can request input before running the check. Define input parameters using the ModelAdvisor.InputParameter class inside a custom check function. You must define one instance of this class for each input parameter that you want to add to a Model Advisor check.

Specify the layout of input parameters with the following methods.

MethodDescription

ModelAdvisor.Check.setInputParametersLayoutGrid

Specifies the size of the input parameter grid.

ModelAdvisor.InputParameter.setRowSpan

Specifies the number of rows the parameter occupies in the Input Parameter layout grid.

ModelAdvisor.InputParameter.setColSpan

Specifies the number of columns the parameter occupies in the Input Parameter layout grid.

This example shows how to define input parameters that you add to a custom check. You must include input parameter definitions inside a custom check definition. The following code, when included in a custom check definition, creates three input parameters.

rec = ModelAdvisor.Check('com.mathworks.sample.Check1');
rec.setInputParametersLayoutGrid([3 2]);
% define input parameters
inputParam1 = ModelAdvisor.InputParameter;
inputParam1.Name = 'Skip font checks.';
inputParam1.Type = 'Bool';
inputParam1.Value = false;
inputParam1.Description = 'sample tooltip';
inputParam1.setRowSpan([1 1]);
inputParam1.setColSpan([1 1]);
inputParam2 = ModelAdvisor.InputParameter;
inputParam2.Name = 'Standard font size';
inputParam2.Value='12';
inputParam2.Type='String';
inputParam2.Description='sample tooltip';
inputParam2.setRowSpan([2 2]);
inputParam2.setColSpan([1 1]);
inputParam3 = ModelAdvisor.InputParameter;
inputParam3.Name='Valid font';
inputParam3.Type='Combobox';
inputParam3.Description='sample tooltip';
inputParam3.Entries={'Arial', 'Arial Black'};
inputParam3.setRowSpan([2 2]);
inputParam3.setColSpan([2 2]);
rec.setInputParameters({inputParam1,inputParam2,inputParam3});

The Model Advisor displays these input parameters in the right pane, in an Input Parameters box.

Define Model Advisor Result Explorer Views

A list view provides a way for users to fix check warnings and failures using the Model Advisor Result Explorer. Creating a list view allows you to:

  • Add the Explore Result button to the custom check in the Model Advisor window.

  • Provide the information to populate the Model Advisor Result Explorer.

This example shows how to define list views. You must make the Explore Result button visible using the ModelAdvisor.Check.ListViewVisible property inside a custom check function, and include list view definitions inside a check callback function. You must define one instance of this class for each list view that you want to add to a Model Advisor Result Explorer window.

The following code, when included in a check definition function, adds the Explore Result button to the check in the Model Advisor.

rec = ModelAdvisor.Check('com.mathworks.sample.Check1');
% add 'Explore Result' button
rec.ListViewVisible = true;

The following code, when included in a check callback function, provides the information to populate the Model Advisor Result Explorer.

mdladvObj = Simulink.ModelAdvisor.getModelAdvisor(system);
mdladvObj.setCheckResultStatus(true);

% define list view parameters
myLVParam = ModelAdvisor.ListViewParameter;
myLVParam.Name = 'Invalid font blocks'; % the name appeared at pull down filter
myLVParam.Data = get_param(searchResult,'object')';
myLVParam.Attributes = {'FontName'}; % name is default property
mdladvObj.setListViewParameters({myLVParam});

Define Check Actions

An action provides a way for you to specify an action that the Model Advisor performs to fix a Model Advisor check. When you define an action, the Model Advisor window includes an Action box below the Analysis box.

You define actions using the ModelAdvisor.Action class inside a custom check function. You must define:

  • One instance of this class for each action that you want to take.

  • One action callback function for each action.

This example shows the information you need to populate the Action box in the Model Advisor. Include this in the check definition function.

rec = ModelAdvisor.Check('mathworks.example.optimizationSettings');
% Define an automatic fix action for this check
modifyAction = ModelAdvisor.Action;
modifyAction.setCallbackFcn(@modifyOptmizationSetting);
modifyAction.Name = 'Modify Settings';
modifyAction.Description = ['Modify model configuration optimization' ...
                            ' settings that can impact safety'];
modifyAction.Enable = true;
rec.setAction(modifyAction);

The Model Advisor, in the right pane, displays an Action box.

See Also

| | | | | |

Related Examples

More About

Was this topic helpful?