Main Content

Define the Compile Option for Custom Model Advisor Checks

Depending on the implementation of your model and what you want your custom check to achieve, it is important that you specify the correct compile option. You specify the compile option for the check definition function of a ModelAdvisor.Check object by setting the CallbackContext property as follows:

  • None specifies that the Model Advisor does not have to compile your model before analysis by your custom check. None is the default setting of the CallbackContext property.

  • 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. The Model Advisor does not flag modeling issues that fail during code generation because these issues do not affect the simulated model.

  • 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 the code generation readiness of the model. This option is not supported for custom edit-time checks.

Checks that Evaluate the Code Generation Readiness of the Model

You can create custom Model Advisor checks that enable the Model Advisor engine to identify code generation setup issues in a model at an earlier stage so you can avoid 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.

Class definitions for BasicColors and OtherColors, showing the enumeration clash from Red enumeration

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.

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 instead of PostCompile as the value for the ModelAdvisor.Check.CallbackContext property when generating code from the model is the final objective.

Update Model to Analyze All Variant Choices

For the Model Advisor to evaluate active and inactive paths in a variant system, you must set the Variant activation time parameter to Code compile or startup for the variant blocks (Variant Sink, Variant Source, and Variant Subsystem). You must also set the System target file configuration parameter to ert.tlc.

Note: Selecting this option can affect the execution time and increase 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 set the Variant activation time parameter to Code compile.

  3. Save the model to your local working folder.

Create an sl_customization Function

In your working folder, create this sl_customization function and save it.

function sl_customization(cm)

% register custom checks 
cm.addModelAdvisorCheckFcn(@defineModelAdvisorCheck);

% -----------------------------
% defines Model Advisor Checks
% -----------------------------
function defineModelAdvisorCheck
CheckSingleToBoolConversion;

The sl_customization function accepts a customization manager object. The customization manager object includes the addModelAdvisorCheckFcn method for registering custom checks. The input to this method is a handle to a function (defineModelAdvisorCheck). This function contains a call to the check definition function that corresponds to the custom check.

Open and inspect the check definition function,CheckSingleToBoolConversion.m:

function CheckSingleToBoolConversion
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(@DetailStyleCallback,'None','DetailStyle');
rec.CallbackContext = 'PostCompileForCodegen'; % Compile for Code Generation

mdladvRoot.publish(rec, 'Demo');

end
    
function DetailStyleCallback(system, CheckObj)

mdladvObj = Simulink.ModelAdvisor.getModelAdvisor(system);

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

if isempty(violationBlks)
    ElementResults = ModelAdvisor.ResultDetail;
    ElementResults(1,numel(violationBlks))=ModelAdvisor.ResultDetail;
    ElementResults.Description = 'This check looks for data type conversion blocks that convert single data to boolean data';
    ElementResults.Status = 'Check has passed. No data type conversion blocks that convert single data to boolean were found.';
    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 = 'This check looks for data type conversion blocks that convert single data to boolean data';
        ElementResults(i).Status = 'Check has failed. The following data type conversion blocks convert single data to boolean:';
        ElementResults(i).RecAction =  'Modify the model to avoid converting data type from single to boolean';
    end
    mdladvObj.setCheckResultStatus(false);
    mdladvObj.setActionEnable(true);
end
CheckObj.setResultDetails(ElementResults);
end

For more information on creating custom checks, see Define Custom Model Advisor Checks.

Open Model Advisor and Execute Custom Check

Before 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. In the Modeling tab, select Model Advisor. A System Selector - Model Advisor dialog box opens. Click OK. The Model Advisor opens.

  3. In the left pane, select By Product > Demo > Check to identify Single to Bool conversion.

  4. 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. The inactive variant paths appear dimmed.

Review the Model Advisor Results

Review the check analysis results in the Model Advisor. Click the hyperlinks 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 systems.

check_compile_code_gen_maresults.png

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.

check_compile_code_gen_maresults_postcompile.png

See Also

|

Related Topics