ModelAdvisor.ResultDetail 类
命名空间: ModelAdvisor
定义检查结果详细信息
描述
在自定义检查编写算法中使用 ModelAdvisor.ResultDetail
类的对象来提供有关检查结果的详细信息。对于仅在模型顾问中运行的检查,请使用 setResultDetails
方法将这些结果与 ModelAdvisor.Check
对象相关联,并在 ModelAdvisor.Check.setCallbackFcn
函数中指定 'DetailStyle
' 作为回调样式。
属性
Data
— 模块标识符或信号线句柄
string
违反自定义检查的每个模块的模块标识符或信号的信号线句柄,指定为字符串。
数据类型: char
Type
— 数据类型
SID
(默认) | Signal
数据类型,指定为:
SID
– 检查违规是否发生在模块上Signal
– 检查违规是否发生在信号上
数据类型: enum
Description
— 检查结果的描述
string
检查结果的描述,指定为字符串。
数据类型: char
Title
— 标题
string
检查结果的标题,指定为字符串。
数据类型: char
Information
— 其他信息
string
有关检查结果的其他信息,指定为字符串。
数据类型: char
Status
— 状态消息
string
在模型顾问中显示的状态消息,指定为字符串。
数据类型: char
RecAction
— 建议采取的操作
string
为修复检查而建议采取的操作,指定为字符串。
数据类型: char
ViolationType
— 检查结果的严重性
"warn"
(默认) | "pass"
| "fail"
| "info"
检查结果的严重性,指定为 "pass"
、"fail"
、"info"
或 "warn"
。
数据类型: char
方法
公共方法
ModelAdvisor.ResultDetail.setData | Associate Model Advisor check result with specific block or signal |
示例
定义针对模块违规的自定义模型顾问检查
创建自定义模型顾问检查,用于检查模块名称是否出现在模块下方。
打开模型。
openExample('AdvisorCustomizationExample')
将模型保存到您的工作文件夹中。关闭模型。
要注册编辑时检查,请创建一个 sl_customization
函数。sl_customization
函数接受一个参量,即自定义管理器对象。要注册自定义检查,请使用 addModelAdvisorCheckFcn
方法。此方法的输入是检查定义函数的句柄。对于此示例,defineDetailStyleCheck
是检查定义函数。创建 sl_customization
函数并将其保存到工作文件夹中。
function sl_customization(cm)
cm.addModelAdvisorCheckFcn(@defineDetailStyleCheck);
创建检查定义函数。对于此示例,创建名为 defineDetailStyleCheck.m
的函数文件。将以下代码复制到 defineDetailStyleCheck.m
,并将该函数保存到工作文件夹中。
function defineDetailStyleCheck % Create a ModelAdvisor.Check object and set the properties. rec = ModelAdvisor.Check('com.mathworks.sample.detailStyle'); rec.Title = 'Check whether block names appear below blocks'; rec.TitleTips = 'Check position of block names'; rec.setCallbackFcn(@DetailStyleCallback,'None','DetailStyle'); % Create a ModelAdvisor.Action object for setting a fix operation. myAction = ModelAdvisor.Action; myAction.setCallbackFcn(@ActionCB); myAction.Name='Make block names appear below blocks'; myAction.Description='Click the button to place block names below blocks'; rec.setAction(myAction); % Publish the check to the Demo group. mdladvRoot = ModelAdvisor.Root; mdladvRoot.publish(rec, 'Demo'); end % ----------------------------- % This callback function uses the DetailStyle CallbackStyle type. % ----------------------------- function DetailStyleCallback(system, CheckObj) % Get the Model Advisor object. mdladvObj = Simulink.ModelAdvisor.getModelAdvisor(system); % Find the blocks whose names do not appear below the block. violationBlks = find_system(system, 'Type','block',... 'NamePlacement','alternate',... 'ShowName', 'on'); if isempty(violationBlks) ElementResults = ModelAdvisor.ResultDetail; ElementResults.ViolationType = 'info'; ElementResults.Description = 'Identify blocks where the name is not displayed below the block.'; ElementResults.Status = 'All blocks have names displayed below the block.'; 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 = 'Identify blocks where the name is not displayed below the block.'; ElementResults(i).Status = 'The following blocks have names that do not display below the block:'; ElementResults(i).RecAction = 'Change the location such that the block name is below the block.'; end mdladvObj.setCheckResultStatus(false); mdladvObj.setActionEnable(true); end CheckObj.setResultDetails(ElementResults); end % ----------------------------- % This action callback function changes the location of the block names. % ----------------------------- function result = ActionCB(taskObj) mdladvObj = taskObj.MAObj; checkObj = taskObj.Check; resultDetailObjs = checkObj.ResultDetails; for i=1:numel(resultDetailObjs) block=Simulink.ID.getHandle(resultDetailObjs(i).Data); set_param(block,'NamePlacement','normal'); end result = ModelAdvisor.Text('Changed the location such that the block name is below the block.'); mdladvObj.setActionEnable(false); end
该检查定义函数包含用于定义检查操作和修复的 ModelAdvisor.Check
和 ModelAdvisor.Action
对象。有关该代码这些方面的更多详细信息,请参阅修复模型以符合您使用模型顾问指定的条件。
defineDetailStyleCheck
函数包含 DetailStyleCallback
回调函数。为返回名称未出现在其下方的模块,DetailStyleCallback
函数使用 find_system
函数。
当 violationBlks
为空时,该代码将创建一个 ModelAdvisor.ResultDetail
对象 ElementResults
。ElementResults
指定有关模型顾问中显示的通过检查的信息。
当 find_system
函数返回违反检查的模块的列表时, ElementResults
会是一个 ModelAdvisor.ResultDetail
对象数组。每个违反检查的模块在该数组中都有一个对应的对象。每个对象都包含有关模型顾问中显示的模块的信息。
刷新模型顾问以使用路径上的新检查更新缓存。
Advisor.Manager.refresh_customizations
打开 AdvisorCustomizationExample
模型。
通过点击建模选项卡并选择模型顾问来打开模型顾问。
在左窗格中,选择按产品 > 演示 > 检查模块名称是否出现在模块下方,然后点击运行检查。
要解决警告,请点击修复。
定义针对信号违规的自定义编辑时检查
创建自定义编辑时检查,用于检查连接到 Outport 模块的信号是否具有标签。
打开模型。
openExample('AdvisorCustomizationExample')
将模型保存到您的工作文件夹中。关闭模型。
要注册自定义编辑时检查,请创建 sl_customization
函数并将其保存到工作文件夹中。
function sl_customization(cm)
cm.addModelAdvisorCheckFcn(@defineCheck);
创建检查定义函数。在该函数中,创建一个 ModelAdvisor.Check
对象并将检查 ID 指定为输入参量。然后,指定 ModelAdvisor.Check
Title
和 CallbackHandle
属性。CallbackHandle
属性是您为定义编辑时检查而创建的类的名称。对于此示例,MyEditTimeChecks
是包名称,SignalLabel
是类名。然后,将检查发布到模型顾问中的新文件夹。对于此示例,文件夹名称为 Demo: Edit-Time Checks
。创建一个 defineCheck
函数并包含以下代码。将 defineCheck
函数保存到工作文件夹中。
function defineCheck rec = ModelAdvisor.Check("advisor.edittimecheck.SignalLabel"); rec.Title = 'Check that signals have labels if they are to propagate those labels'; rec.CallbackHandle = 'MyEditTimeChecks.SignalLabels'; mdladvRoot = ModelAdvisor.Root; mdladvRoot.publish(rec,'DEMO: Edit-Time Checks');
创建一个从 ModelAdvisor.EdittimeCheck
抽象基类派生的类。对于此示例,请创建一个名为 SignalLabel.m
的类文件。将以下代码复制到 SignalLabel.m
文件中,然后将该文件保存到 +MyEditTimeChecks
文件夹中。
classdef SignalLabels < ModelAdvisor.EdittimeCheck methods function obj=SignalLabels(checkId) obj=obj@ModelAdvisor.EdittimeCheck(checkId); obj.traversalType = edittimecheck.TraversalTypes.BLKITER; end function violation = blockDiscovered(obj, blk) violation = []; ports = get_param(blk,'Ports'); lh = get_param(blk, 'LineHandles'); if strcmp(get_param(blk,'BlockType'),'Outport') for j = 1 : ports(1) if lh.Inport(j) ~= -1 % failure case: no connection allsources = get_param(lh.Inport(j),'SrcPortHandle'); hiliteHandle = get_param(lh.Inport(j), 'DstPortHandle'); if (isempty(allsources) ~= 0) || (isempty(find(allsources==-1,1)) ~= 0) lh_obj = get_param(lh.Inport(j),'Object'); if isempty(lh_obj.Name) if strcmp(lh_obj.signalPropagation,'off') == 1 allsources_parent = get_param(allsources,'Parent'); if strcmp(get_param(allsources_parent,'BlockType'),'Inport') buscreator_outputs = get_param(allsources_parent,'IsBusElementPort'); else buscreator_outputs = 'off'; end if ~strcmp(buscreator_outputs,'on') violation = ModelAdvisor.ResultDetail; ModelAdvisor.ResultDetail.setData(violation, 'Signal',hiliteHandle); violation.Description ='The model does not adhere to the signal label modeling guideline.'; violation.CheckID = obj.checkId; violation.Title = 'Signal Label Missing'; violation.Information = 'This check verifies the presence of signal label.'; violation.Status = 'The following signals do not have a label:'; end end end end end end end end end end
有关 ModelAdvisor.EdittimeCheck
类中代码的详细信息,请参阅Define Edit-Time Checks to Comply with Conditions That You Specify with the Model Advisor。
blockDiscovered
方法包含用于定义检查违规的算法。与仅在模型顾问中显示的自定义检查不同,此算法不包含 find_system
函数。blockDiscovered
方法将模块句柄作为输入并遍历模型,因此您不需要使用 find_system
函数进行自定义编辑时检查。
对于违反检查的每个模型元素,代码会创建一个 ModelAdvisor.ResultDetail
对象。对于此示例,由于违规发生在信号上,因此检查必须使用有关模块的信号线句柄的参数。要查找存在违规情形的信号,必须指定 LineHandles
选项。具体而言,对于连接到 Outport 模块的信号,此算法会检查 Name
信号参数是否具有值。然后,因为违规发生在信号上,此算法通过创建 Type
属性值设置为 Signal
的违规对象来突出显示该信号。
刷新模型顾问以使用路径上的新检查更新缓存。
Advisor.Manager.refresh_customizations
打开 AdvisorCustomizationExample
模型。
点击建模选项卡并选择模型顾问 > 配置编辑器,以打开模型顾问配置编辑器。
删除除 DEMO:Edit Time Checks 文件夹之外的每个文件夹,创建包含自定义编辑时检查的自定义配置。
将配置保存为 my_config.json
。当系统提示将此配置设置为默认配置时,点击否。
关闭模型顾问配置编辑器。
点击建模选项卡并选择模型顾问 > 编辑时检查,将自定义配置设置为 my_config.json
文件。在“配置参数”对话框中,在模型顾问配置文件参数中指定配置文件指定的路径。
选择编辑时检查参数来打开编辑时检查。关闭“模型配置参数”对话框。
要查看编辑时警告,请点击以黄色突出显示的信号。连接到 Outport 模块的信号会引发警告,因为它没有标签。
下图显示了模型顾问报告中的 ModelAdvisor.ResultDetail
属性:
版本历史记录
在 R2018b 中推出R2023b: 将会删除 IsInformer
和 IsViolation
属性
在以后的版本中将会删除 ModelAdvisor.ResultDetail
类的 IsInformer
和 IsViolation
属性。请改用 ViolationType
属性来指定模型顾问检查结果的严重性。
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)