Main Content

Customize Model Advisor Configuration Programmatically

You can use the Model Advisor Configuration APIs to programmatically create new Model Advisor configurations and modify existing configurations to meet your compliance checking requirements. The API provides you with an easier and flexible way of programmatically creating and customizing configurations by:

  • Adding, removing, and organizing built-in and published custom checks in a hierarchy.

  • Specifying checks that you want to use for edit-time checking, as well as the checks included in the Model Advisor.

  • Disabling and enabling checks and folders.

You can save the organizational hierarchy in a JSON file which you can specify as the default Model Advisor configuration, or associate with a specific model.

This example shows how to use the Model Advisor configuration APIs to:

  • Create new custom configurations

  • Modify existing configurations to meet your current requirements.

  • Create new configurations for a set of predefined checks.

Create New Custom Model Advisor Configuration

Create a custom configuration that contains two folders containing some built-in MathWorks Advisory Board (MAB) and DO-178C/DO-331 standard modeling guidelines checks. Use the custom configuration as the default Model Advisor configuration and run a Model Advisor analysis on AdvisorCustomizationExample.slx.

1. Create a Model Advisor configuration object.

CfgObj = Advisor.Config;
Updating Model Advisor cache...
Model Advisor cache updated. For new customizations, to update the cache, use the Advisor.Manager.refresh_customizations method.

The object CfgObj provides you access to an editable blank configuration that you use to create the custom configuration.

2. Create two new folders named MAB Checks and IS Checks under the root node of the blank configuration by using the addFolder function.

FolderInstanceID = addFolder(CfgObj,'_SYSTEM','MAB Checks')
FolderInstanceID = 
'_SYSTEM_MAB Checks'
FolderInstanceID1 = addFolder(CfgObj,'_SYSTEM','IS Checks')
FolderInstanceID1 = 
'_SYSTEM_IS Checks'

When you create new folders, instances of the folders are instantiated in the configuration. The folder instances have IDs that represent the location of the folders in the configuration in the form _SYSTEM_Folder Hierarchy. addFolder returns the instance IDs of the new folders as outputs.

3. Add these built-in MathWorks Advisory Board (MAB) modeling guidelines checks under the MAB Checks folder using the addCheck function.

CheckInstanceID = addCheck(CfgObj,'_SYSTEM_MAB Checks','mathworks.jmaab.jc_0731')
CheckInstanceID = 
"_SYSTEM_MAB Checks_mathworks.jmaab.jc_0731"
CheckInstanceID1 = addCheck(CfgObj,'_SYSTEM_MAB Checks','mathworks.maab.jc_0061')
CheckInstanceID1 = 
"_SYSTEM_MAB Checks_mathworks.maab.jc_0061"
CheckInstanceID2 = addCheck(CfgObj,'_SYSTEM_MAB Checks','mathworks.jmaab.jc_0201')
CheckInstanceID2 = 
"_SYSTEM_MAB Checks_mathworks.jmaab.jc_0201"
CheckInstanceID3 = addCheck(CfgObj,'_SYSTEM_MAB Checks','mathworks.jmaab.jc_0211')
CheckInstanceID3 = 
"_SYSTEM_MAB Checks_mathworks.jmaab.jc_0211"

The addCheck function enables you to add checks available in the Model Advisor root. The root contains built-in checks for the installed products and published custom checks. When you add checks, instances of the checks are instantiated in the configuration. The check instances have IDs that represent the location of the check in the configuration in the form _SYSTEM_Check Hierarchy_Check ID. addCheck returns the instance IDs of the added checks as outputs.

4. Add these built-in DO-178C/DO-331 standard modeling guidelines checks under the IS Checks folder.

CheckInstanceID4 = addCheck(CfgObj,'_SYSTEM_IS Checks','mathworks.do178.MdlChecksum')
CheckInstanceID4 = 
"_SYSTEM_IS Checks_mathworks.do178.MdlChecksum"
CheckInstanceID5 = addCheck(CfgObj,'_SYSTEM_IS Checks','mathworks.hism.hisl_0036')
CheckInstanceID5 = 
"_SYSTEM_IS Checks_mathworks.hism.hisl_0036"
CheckInstanceID6 = addCheck(CfgObj,'_SYSTEM_IS Checks','mathworks.hism.hisl_0037')
CheckInstanceID6 = 
"_SYSTEM_IS Checks_mathworks.hism.hisl_0037"
CheckInstanceID7 = addCheck(CfgObj,'_SYSTEM_IS Checks','mathworks.hism.hisl_0040')
CheckInstanceID7 = 
"_SYSTEM_IS Checks_mathworks.hism.hisl_0040"

The addCheck function returns the instance IDs of the added checks as outputs.

5. Save the configuration to a JSON file named CustomConfiguration.json by using the saveConfiguration function.

saveConfiguration(CfgObj, 'CustomConfiguration.json');

The function saves the configuration file in your working directory. If you want to save the file in a different location, specify the desired absolute path.

6. To set CustomConfiguration.json as the default Model Advisor configuration, use the ModelAdvisor.setDefaultConfiguration function.

ModelAdvisor.setDefaultConfiguration ('CustomConfiguration.json');

7. Refresh the Model Advisor cache.

Advisor.Manager.refresh_customizations();

8. Open the AdvisorCustomizationExample.slx model.

open_system('AdvisorCustomizationExample.slx');

9. Run a Model Advisor analysis using an Advisor.Application object and the run method. Alternatively, you can use ModelAdvisor.run.

app = Advisor.Manager.createApplication();
RootModel = 'AdvisorCustomizationExample';
setAnalysisRoot(app,'Root',RootModel);
run(app);
Updating Model Advisor cache...
Model Advisor cache updated. For new customizations, to update the cache, use the Advisor.Manager.refresh_customizations method.

10. The Model Advisor uses the CustomConfiguration.json custom configuration for the analysis. Generate an HTML report by using generateReport and open it in a web browser.

report = generateReport(app);
web(report);

In the report, you see that out of 8 checks in the MAB Checks and IS Checks folders, these 3 checks have warnings:

  • IS Checks > Check safety-related diagnostic settings for saving (Check ID mathworks.hism.hisl_0036)

  • IS Checks > Check safety-related model referencing settings (Check ID mathworks.hism.hisl_0037)

  • MAB Checks > Check the display attributes of block names (Check ID mathworks.maab.jc_0061)

Modify Existing Model Advisor Configuration

In this example, use the Advisor.Config object and its functions to modify the configuration CustomConfiguration.json created in Create New Custom Model Advisor Configuration. Then use the customized configuration to check the compliance of AdvisorCustomizationExample.slx.

1. Create a Model Advisor configuration object.

CfgObj = Advisor.Config;

2. To load the CustomConfiguration.json file, use the loadConfig function.

loadConfig(CfgObj,'CustomConfiguration.json')

3. For this example, to see the effect of customization, delete the IS Checks folder from the configuration and remove the check in MAB Checks folder that previously issued a warning from the Model Advisor analysis.

To delete the IS Checks folder instance _SYSTEM_IS Checks and the checks in it from the configuration, use the delete function.

delete(CfgObj,'_SYSTEM_IS Checks');

To remove the Check the display attributes of block names check instance _SYSTEM_MAB Checks_mathworks.maab.jc_0061 from the Model Advisor analysis, use the deselect function.

deselect(CfgObj,'_SYSTEM_MAB Checks_mathworks.maab.jc_0061');

You can determine if the check instance is included in the Model Advisor analysis by using the isSelected function.

isSelected(CfgObj,'_SYSTEM_MAB Checks_mathworks.maab.jc_0061')
ans = logical
   0

4. Disable the check instance _SYSTEM_MAB Checks_mathworks.jmaab.jc_0201 by using the disable function.

disable(CfgObj,'_SYSTEM_MAB Checks_mathworks.maab.jc_0061');

Similarly, disable the selected checks and folder instances that are included in the analysis.

disable(CfgObj,'_SYSTEM_MAB Checks_mathworks.jmaab.jc_0731');
disable(CfgObj,'_SYSTEM_MAB Checks_mathworks.jmaab.jc_0201');
disable(CfgObj,'_SYSTEM_MAB Checks_mathworks.jmaab.jc_0211');
disable(CfgObj,'_SYSTEM_MAB Checks');

Disabling prevents unintentional removal or inclusion of check or folder instances from or to the Model Advisor analysis. To include or exclude a disabled check or folder instance to or from the analysis, you must first enable the instance by using the enable function.

5. Save the customization to the CustomConfiguration.json file.

saveConfiguration(CfgObj,'CustomConfiguration.json');

6. Refresh the Model Advisor cache.

Advisor.Manager.refresh_customizations();

7. Open the model AdvisorCustomizationExample.slx model.

open_system('AdvisorCustomizationExample.slx');

8. Run a Model Advisor analysis.

run(app);

The Model Advisor uses the default CustomConfiguration.json custom configuration for analysis. Generate an HTML report and open it in a web browser.

report = generateReport(app);
web(report);

You see that the model passes the three checks and one check does not run, which matches the customization.

Create Custom Model Advisor Configurations for Sets of Predefined Checks

If you want to create a new custom configuration for a set of checks without organizing them in a hierarchy, you can easily create one by using an Advisor.config object and the createFromCheckList function. This example shows how to create a custom configuration for a predefined checklist and associate it with a model. Associating a custom configuration with a model means the Model Advisor uses the configuration each time the model is opened.

1. Create a checklist for the desired checks by defining a cell array of their check IDs. For this example, create a check list for these MAB Modeling Guidelines checks:

checkList = {'mathworks.jmaab.jc_0222', 'mathworks.jmaab.jc_0241',...
             'mathworks.jmaab.jc_0201','mathworks.jmaab.jc_0211'};

2. Create a Model Advisor configuration object.

CfgObj = Advisor.Config;
Updating Model Advisor cache...
Model Advisor cache updated. For new customizations, to update the cache, use the Advisor.Manager.refresh_customizations method.

The object CfgObj provides you access to an editable blank configuration that you use to create the custom configuration.

3. Add the checks whose IDs are defined in checkList under the root node of the blank configuration by using the createFromCheckList function.

createFromCheckList(CfgObj,checkList);

You can further customize it by creating new folders and adding checks in them by using the addFolder and addCheck functions.

4. Save the configuration to a JSON file named ConfigforCheckList.json.

saveConfiguration(CfgObj, 'ConfigforCheckList.json');

The function saves the file in your working directory.

5. Create and save a new model named newModel.

new_system("newModel");
save_system("newModel");

6. Open the Model Advisor for the model newModel.

modeladvisor("newModel");

7. Associate the configuration file ConfigforCheckList.json with the model newModel.

ModelAdvisor.setModelConfiguration("newModel","ConfigforCheckList.json");

If close and reopen the model, the Model Advisor uses the associated configuration each time.

Limitations

The Model Advisor programmatic workflow has these limitations:

  • When loading an older Model Advisor configuration, loadConfig does not warn you of missing or incompatible checks.

  • You cannot programmatically upgrade an older Model Advisor configuration to the newer version of MATLAB or validate it for compatibility. Use Model Advisor Configuration Editor to upgrade or validate older configurations.

Clean Up

close_system ("newModel", 0);
delete CustomConfiguration.json;
delete ConfigforCheckList.json;
delete newModel.slx;
rmdir 'slprj' s

See Also

|

Related Topics