主要内容

Define the Compile Option for Custom Model Advisor Checks

根据模型的实现以及您想要的自定义检查实现的目标,重要的是要指定正确的编译选项。您为检查的检查定义功能指定编译选项ModelAdvisor.Checkobject by setting theCallbackContextproperty as follows:

  • 没有任何specifies that the Model Advisor does not have to compile your model before analysis by your custom check.没有任何是默认设置的设置CallbackContextproperty.

  • PostCompilespecifies 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指定模型顾问必须专门为代码生成编译和更新模型图,但不会模拟模型。使用此选项进行模型顾问检查,以分析模型的代码生成准备就绪。自定义编辑时间检查不支持此选项。金宝app

检查评估模型代码生成准备就绪的检查

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, theRedenumeration in基础颜色OtherColorsare OK for use in a simulated model. In the generated code, however, theseRedenumerations 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”选择编译所有var模型iant 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创建自定义检查以评估模型的活动和非活动变体路径

创建自定义检查以评估模型的活动和非活动变体路径

此示例显示了自定义模型顾问检查的创建,该检查评估了从变体系统模型中评估活动和不活跃的变体路径。该示例提供了模型顾问结果,以证明您为什么使用PostCompileForCodegeninstead ofPostCompile作为modelAdvisor.check.callbackContextproperty when generating code from the model is the final objective.

Update Model to Analyze All Variant Choices

为了使模型顾问评估变体系统中的主动和非活动路径,您必须设置Variant activation timeparameter to代码编译或者startupfor the variant blocks (变体水槽,变体源, 和Variant Subsystem, Variant Model)。您还必须设置System target file配置参数到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 modelex_check_compile_code_gen

  2. For each Variant Source block, open the block parameters and set theVariant activation timeparameter to代码编译

  3. 将模型保存到本地工作文件夹。

Create ansl_customization功能

在您的工作文件夹中,创建这个sl_customization功能并保存它。

函数SL_CUSTOMIZATION(CM)%寄存器自定义检查CM.AddModelAdvisorCheckfcn(@definemodeladvisorcheck);%-----------------------------------%定义模型顾问检查%----------------------------------------------函数decenemodeladvisorcheck checksingletoboolConversion;

Thesl_customization功能接受自定义管理器对象。自定义管理器对象包括addModelAdvisorCheckFcn注册自定义检查的方法。此方法的输入是函数的句柄(denemodeladvisorcheck)。此功能包含对与自定义​​检查相对应的检查定义功能的调用。

打开并检查检查定义功能,CheckSingleToBoolConversion.m:

功能checkSingletOboolConversion mdladvroot = modelAdvisor.root;rec = modelAdvisor.check('exampleCheck1');rec.Title ='Check to identify Single to Bool conversions';rec.titleid ='custom.dtcCheck.CompileForCodegen1';rec.TitleTips =“自定义检查以识别单身到布尔转换”;rec.setCallbackFcn(@DetailStyleCallback,'None',“细节样式”);rec.CallbackContext =“ PostCompileForCodegen”;%编译代码生成mdladvRoot.publish(rec,'Demo');end功能DetailStyleCallback(system, CheckObj) mdladvObj = Simulink.ModelAdvisor.getModelAdvisor(system); violationBlks = find_system(system,'BlockType',“ datatypeconversion');forii = numel(afirationblks):-1:1 dtcblk = afirationblks {ii};compDatatypes = get_param(dtcblk,'CompiledPortDataTypes');如果isempty(compDataTypes) violationBlks(ii) = [];continue;end如果~(strcmp(compDataTypes.Inport,'single') && strcmp(compDataTypes.Outport,“布尔人”))违法行为(ii)= [];continue;endend如果Isempty(afterationblks)elementResults = modelAdvisor.ResultDetail;elementResults(1,numel(afirationblks))= modelAdvisor.ResultDetail;elementResults.isInformer = true;elementResults.Description ='This check looks for data type conversion blocks that convert single data to boolean data';ElementResults.Status ='检查已经过去了。找不到将单个数据转换为布尔值的数据类型转换块。”;mdladvObj.setCheckResultStatus(true);别的fori=1:numel(violationBlks) ElementResults(1,i) = ModelAdvisor.ResultDetail;endfori=1:numel(ElementResults) ModelAdvisor.ResultDetail.setData(ElementResults(i),'sid',违反{i});elementResults(i).description ='This check looks for data type conversion blocks that convert single data to boolean data';ElementResults(i).Status ='检查失败了。以下数据类型转换块将单个数据转换为布尔值:';elementResults(i).recation =“修改模型以避免将数据类型从单个转换为布尔值”;endmdladvObj.setCheckResultStatus(false); mdladvObj.setActionEnable(true);endCheckObj.setResultDetails(ElementResults);end

For more information on creating custom checks, see定义自定义模型顾问检查

Open Model Advisor and Execute Custom Check

在打开模型顾问并运行自定义检查之前,您必须刷新模型顾问检查信息缓存。在MATLAB命令窗口中,输入:

Advisor.manager.refresh_customizations

打开模型顾问并执行自定义检查:

  1. Open your saved model.

  2. 在里面Modeling选项卡,选择Model Advisor。ASystem Selector - Model Advisor对话框打开。点击好的。The Model Advisor opens.

  3. 在左窗格中,选择By Product > Demo > Check to identify Single to Bool conversion

  4. Right-click the check and select运行此检查。模型顾问编译模型并执行检查。模型顾问更新模型图。非活动变体路径显得昏暗。

查看模型顾问结果

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

在此示例中,因为您在sl_customization.mfile as

rec.CallbackContext = 'PostCompileForCodegen';

模型顾问在活动路径和变体系统的不活动路径中为数据类型转换块生成警告。

If you defined the compile option in thesl_customization.mfile as

rec.callbackContext ='PostCompile';

结果仅包括活动路径中的数据类型转换块。

See Also

|

相关话题