Define the Compile Option for Custom Checks

Depending on the implementation of your model and what you want your custom check to achieve, it is important that you specify the appropriate compile option to ensure that the correct information is evaluated by your custom check.

You use the ModelAdvisor.Check.CallbackContext property to define the compile option:

  • None specifies that the Model Advisor does not have to compile your model before analysis by your custom check.

  • PostCompile specifies that the Model Advisor must compile the model to update the model diagram and then simulate the model to execute your custom check.

  • PostCompileForCodegen specifies that the Model Advisor must compile and update the model diagram specifically for code generation, but does not simulate the model. Use this option for Model Advisor checks that analyze code generation readiness of the model.

Checks for Models That Are Not Compiled by the Model Advisor

For custom checks that do not require the Model Advisor to compile the model before execution of the check, in the check definition you specify the ModelAdvisor.Check.CallbackContext property as:

rec.CallbackContext = 'None';  
In this situation, the Model Advisor simulates the model. The Model Advisor does not compile the model.

Note

By default, the Model Advisor does not compile the model for custom checks. You do not have to include the ModelAdvisor.Check.CallbackContext property in the check definition.

This example shows a check definition that does not require the model to be compiled.

function defineModelAdvisorChecks
mdladvRoot = ModelAdvisor.Root;
% -----------------------------
% Sample Check: Check whose model does not need to be compiled
% -----------------------------

rec = ModelAdvisor.Check('exampleCheck2');
rec.Title = 'Non-compile check example';
rec.TitleID = 'custom.dtcCheck.NonCompile1';
rec.TitleTips = 'A custom check for a model that does not need to be compiled ';
rec.setCallbackFcn(@CheckNoCompile,'None','StyleOne');
rec.CallbackContext = 'None'; % Not compiled

mdladvRoot.publish(rec, 'Demo');

Checks That Require the Model to be Compiled and Simulated by the Model Advisor

For custom checks that require model compilation and simulation to properly check the implementation of the model, in the check definition you specify the ModelAdvisor.Check.CallbackContext property as:

rec.CallbackContext = 'PostCompile'; 

In this situation, the Model Advisor updates the model diagram and simulates the model. The Model Advisor does not flag modeling issues that fail during code generation because these issues do not affect the simulated model.

This example shows a check definition that requires a model to be compiled and simulated.

function defineModelAdvisorChecks
mdladvRoot = ModelAdvisor.Root;
% -----------------------------
% Sample Check: Check whose model must be compiled and simulated.
% -----------------------------

rec = ModelAdvisor.Check('exampleCheck3');
rec.Title = 'PostCompile check example';
rec.TitleID = 'custom.dtcCheck.Compile1';
rec.TitleTips = 'A custom check for a model that is compiled and simulated';
rec.setCallbackFcn(@CheckCompileSimulate,'None','StyleOne');
rec.CallbackContext = 'PostCompile'; % Compiled and simulated

mdladvRoot.publish(rec, 'Demo');

Checks That Evaluate Code Generation Readiness of the Model

For custom checks that evaluate code generation readiness, you must develop the model to generate code. In the check definition you specify the ModelAdvisor.Check.CallbackContext property as:

rec.CallbackContext = 'PostCompileForCodegen';

In this situation, the Model Advisor compiles the model and updates the model diagram specifically for code generation. The Model Advisor does not assume that the model is being simulated.

You can create custom Model Advisor checks that identify code generation setup issues in a model at an earlier stage, avoiding unexpected errors during code generation. For example, in this model, the Red enumeration in BasicColors and OtherColors are OK for use in a simulated model. In the generated code, however, these Red enumerations result in an enumeration clash. By using the 'PostCompileForCodegen' option, your custom Model Advisor check can identify this type of code generation setup issue.

The 'PostCompileForCodegen' option compiles the model for all variant choices. This compilation enables you to analyze possible issues present in the generated code for active and inactive variant paths in the model. An example is provided in Create Custom Check to Evaluate Active and Inactive Variant Paths from a Model.

This example shows a check definition that requires a model to be compiled for code generation

function defineModelAdvisorChecks
mdladvRoot = ModelAdvisor.Root;
% -----------------------------
% Sample Check: Check whose model is compiled for generated code.
% Model is not simulated.
% -----------------------------

rec = ModelAdvisor.Check('exampleCheck1');
rec.Title = 'PostCompileForCodegen check example';
rec.TitleID = 'custom.dtcCheck.CompileForCodegen1';
rec.TitleTips = 'A custom check for evaluating the generated code';
rec.setCallbackFcn(@CheckSingleToBoolConversion,'None','StyleOne');
rec.CallbackContext = 'PostCompileForCodegen'; % Compile for generated code

mdladvRoot.publish(rec, 'Demo');

Create Custom Check to Evaluate Active and Inactive Variant Paths from a Model

This example shows the creation of a custom Model Advisor check that evaluates active and inactive variant paths from a variant system model. The example provides Model Advisor results that demonstrate why you use PostCompileForCodegen versus PostCompile as the value for the ModelAdvisor.Check.CallbackContext property when generating code from the model is your final objective. See Define the Compile Option for Custom Checks.

Update Model to Analyze All Variant Choices

For the Model Advisor to evaluate active and inactive paths in a variant system, you must enable the Analyze all choices during update diagram and generate preprocessor conditionals option for the variant blocks (Variant Sink, Variant Source, and Variant Subsystem, Variant Model).

Note: Selecting this option can affect the execution time, thereby increasing the time it takes for the Model Advisor to evaluate the model.

  1. Open the example model ex_check_compile_code_gen.

  2. For each Variant Source block, open the block parameters and select the Analyze all choices during update diagram and generate preprocessor conditionals option.

  3. Save the model to your local working folder.

Update sl_customization.m File

In your working folder, update the sl_customization.m file. Save your changes. If you are asked if it is ok to overwrite the file, click OK.

function sl_customization(cm)

% --- register custom checks
cm.addModelAdvisorCheckFcn(@defineModelAdvisorChecks);

end

% --- defineModelAdvisorChecks function
function defineModelAdvisorChecks
mdladvRoot = ModelAdvisor.Root;

rec = ModelAdvisor.Check('exampleCheck1');
rec.Title = 'Check to identify SINGLE to BOOL conversions';
rec.TitleID = 'custom.dtcCheck.CompileForCodegen1';
rec.TitleTips = 'Custom check to identify SINGLE to BOOL conversions';
rec.setCallbackFcn(@CheckSingleToBoolConversion,'None','StyleOne');
rec.CallbackContext = 'PostCompileForCodegen'; % Compile for Code Generation

mdladvRoot.publish(rec, 'Demo');

end

% --- creates SimpleCallback function
function result = CheckSingleToBoolConversion(system)

mdladvObj = Simulink.ModelAdvisor.getModelAdvisor(system);
result={};
dtcBlks = find_system(system, 'BlockType', 'DataTypeConversion');
for ii = numel(dtcBlks):-1:1
    dtcBlk = dtcBlks{ii};
    compDataTypes = get_param(dtcBlk, 'CompiledPortDataTypes');
    if isempty(compDataTypes)
        dtcBlks(ii) = [];
        continue;
    end
    if ~(strcmp(compDataTypes.Inport, 'single') && strcmp(compDataTypes.Outport, 'boolean'))
        dtcBlks(ii) = [];
        continue;
    end
end

ft = ModelAdvisor.FormatTemplate('ListTemplate');
ft.setInformation(['This check looks for data type conversion blocks that'...
    ' convert single data to boolean data']);
if ~isempty(dtcBlks)
    ft.setSubResultStatusText(['Check has failed. The following '...
        'data type conversion blocks convert single data to boolean:']);
    ft.setListObj(dtcBlks);
    ft.setSubResultStatus('warn');
    ft.setRecAction('Modify the model to avoid converting data type from single to boolean');
    mdladvObj.setCheckResultStatus(false); 
else
    ft.setSubResultStatusText(['Check has passed. No data type conversion blocks '...
        'that convert single data to boolean were found.']);
    ft.setSubResultStatus('pass');
    mdladvObj.setCheckResultStatus(true); 
end
ft.setSubBar(0);
result{end+1} = ft;

end

function result = dummy(~)
result={};
end

Open Model Advisor and Execute Custom Check

Prior to opening the Model Advisor and running the custom check, you must refresh the Model Advisor check information cache. In the MATLAB Command Window, enter:

Advisor.Manager.refresh_customizations

To open the Model Advisor and execute the custom check:

  1. Open your saved model.

  2. From the model window, select Analysis > Model Advisor > Model Advisor.

  3. A System Selector — Model Advisor dialog box opens. Click OK. The Model Advisor window opens.

  4. In the left pane, select By Product > Demo > Check to identify SINGLE to BOOL conversion. If the By Product folder is not displayed in the Model Advisor window, select Settings > Preferences > Show By Product Folder.

  5. Right-click the check and select Run This Check. The Model Advisor compiles the model and executes the check. The Model Advisor updates the model diagram, with the inactive variant paths appearing as dimmed.

Review the Model Advisor Results

Review the check analysis results in the Model Advisor. Click the hyperlink path to open the violating block in the model editor.

In this example, because you defined the compile option in the sl_customization.m file as

rec.CallbackContext = 'PostCompileForCodegen';

the Model Advisor generates warnings for the Data Type Conversion blocks in the active paths and the inactive paths of the Variant system.

If you defined the compile option in the sl_customization.m file as

rec.CallbackContext = 'PostCompile';

the results include only the Data Type Conversion blocks in the active path.

See Also

|

Related Topics