Main Content

ModelAdvisor.FormatTemplate

用于格式化模型顾问分析结果的模板

说明

使用 ModelAdvisor.FormatTemplate 类在模型顾问的分析结果窗格中设置检查结果的格式,以便在您创建的检查之间保持一致的外观。可以将分析结果格式化为表格或列表。

创建对象

描述

obj = ModelAdvisor.FormatTemplate(type) 创建 ModelAdvisor.FormatTemplate 类的对象。type 是标识模板格式类型(列表或表格)的字符向量。

您必须将结果对象返回给模型顾问,才能在分析结果窗格中显示格式化的结果。

注意

在检查回调中使用 ModelAdvisor.FormatTemplate 类。

输入参量

全部展开

ModelAdvisor.FormatTemplate 的类型。

对象函数

addRow向模型顾问分析结果中添加表行
setCheckTextAdd description of check to result
setColTitles在模型顾问分析结果中添加表列标题
setInformationAdd description of subcheck to result
setListObj向模型对象添加超链接的列表
setRecActionAdd Recommended Action section and text
setRefLinkAdd See Also section and links
setSubBarAdd line between subcheck results
setSubResultStatusAdd status to the check or subcheck result
setSubResultStatusTextAdd text below status in result
setSubTitleAdd title for subcheck in result
setTableInfoAdd data to table
setTableTitleAdd title to table in Model Advisor analysis results

示例

全部折叠

  1. 下列 sl_customization 文件包含的代码可以创建 ft1ft2 两个对象并使用它们来设置在表格和列表中运行检查的结果的格式。结果标识模型中的模块。

    function sl_customization(cm)
    
    % register custom checks
    cm.addModelAdvisorCheckFcn(@defineModelAdvisorChecks);
    
    % register custom factory group 
    cm.addModelAdvisorTaskFcn(@defineModelAdvisorTasks);
    
    end
    
    
    % -----------------------------
    % defines Model Advisor Checks
    % -----------------------------
    function defineModelAdvisorChecks
    
    % Define and register a sample check 
    rec = ModelAdvisor.Check('mathworks.example.SampleDetailStyle');
    rec.Title = 'Sample check for Model Advisor using the ModelAdvisor.FormatTemplate';
    setCallbackFcn(rec, @SampleDetailStyleCallback,'None','DetailStyle');
    
    mdladvRoot = ModelAdvisor.Root;
    mdladvRoot.register(rec);
    
    end
    
    
    % -----------------------------
    % defines Model Advisor Tasks
    % -----------------------------
    function defineModelAdvisorTasks
    mdladvRoot = ModelAdvisor.Root;
     
    % --- sample factory group
    rec = ModelAdvisor.FactoryGroup('com.mathworks.sample.factorygroup');
    rec.DisplayName='My Group 1';
    rec.Description='Demo Factory Group';
    rec.addCheck('mathworks.example.SampleDetailStyle');
    mdladvRoot.publish(rec); % publish inside By Group list
    
    end
    
    
    % -----------------------------
    % Sample Check With Subchecks Callback Function
    % -----------------------------
    function [ResultDescription] = SampleDetailStyleCallback(system, CheckObj)
    
    % Initialize variables
    ElementResults = ModelAdvisor.ResultDetail.empty();
    
    % Perform the check actions
    allBlocks = find_system(system);
    [ResultDescription] = getFormattedTemplate(allBlocks);
    
    % Perform the subcheck actions - Result Details - Table
    if length(allBlocks) == 1
        % Add result details for detailed style check
        ElementResults(end + 1) = ModelAdvisor.ResultDetail;
        ElementResults(end).ViolationType = 'warn';
        ElementResults(end).Description =  ['Find and report all blocks in a table. '...
            '(setInformation method - Description of what the subcheck reviews)'];
        ElementResults(end).Status =  ['The model does not contain blocks. '...
            '(setSubResultStatusText method - Description of result status)'];   
    else  
        for i=1:numel(allBlocks)
            ElementResults(end+1) = ModelAdvisor.ResultDetail;
            ElementResults(end).ViolationType = 'pass';
            ElementResults(end).Format = 'Table';       
            ModelAdvisor.ResultDetail.setData(ElementResults(end),'SID',allBlocks{i});
            ElementResults(end).Description =  ['Find and report all blocks in a table. '...
                '(setInformation method - Description of what the subcheck reviews)'];
            ElementResults(end).Status = ['The model contains blocks. '...
                '(setSubResultStatusText method - Description of result status)'];
        end    
    end
    
    % Perform the subcheck actions  - Result Details - List
    if length(allBlocks) == 1
         ElementResults(end+1) = ModelAdvisor.ResultDetail;
         ElementResults(end).ViolationType = 'warn';
         ElementResults(end).Description =  ['Find and report all blocks in a table. '...
            '(setInformation method - Description of what the subcheck reviews)'];
         ElementResults(end).Status =  ['The model does not contain blocks. '...
            '(setSubResultStatusText method - Description of result status)'];    
    else   
        for i= 1:numel(allBlocks) 
            ElementResults(end+1) = ModelAdvisor.ResultDetail;
            ElementResults(end).ViolationType = 'pass';       
            ModelAdvisor.ResultDetail.setData(ElementResults(end),'SID',allBlocks{i});
            ElementResults(end).Description =  ['Find and report all blocks in a list. '...
                '(setInformation method - Description of what the subcheck reviews)'];
            ElementResults(end).Status = ['The model contains blocks. '...
                '(setSubResultStatusText method - Description of result status)'];  
        end    
    end
    
    %Set check result details
    CheckObj.setResultDetails(ElementResults);
    
    end
    
    function [ResultDescription] = getFormattedTemplate(allBlocks)
    ResultDescription={};
    
    % Create FormatTemplate object for first subcheck, specify table format
    ft1 = ModelAdvisor.FormatTemplate('TableTemplate');
    
    % Add information describing the overall check
    setCheckText(ft1, ['Find and report all blocks in the model. '...
        '(setCheckText method - Description of what the check reviews)']);
    
    % Add information describing the subcheck
    setSubTitle(ft1, 'Table of Blocks (setSubTitle method - Title of the subcheck)');
    setInformation(ft1, ['Find and report all blocks in a table. '...
        '(setInformation method - Description of what the subcheck reviews)']);
    
    % Add See Also section for references to standards
    setRefLink(ft1, {{'Standard 1 reference (setRefLink method)'},
        {'Standard 2 reference (setRefLink method)'}});
    
    % Add information to the table
    setTableTitle(ft1, {'Blocks in the Model (setTableTitle method)'});
    setColTitles(ft1, {'Index (setColTitles method)',
        'Block Name (setColTitles method)'});
    
    
    if length(allBlocks) == 1
        % Add status for subcheck
        setSubResultStatus(ft1, 'Warn');
        setSubResultStatusText(ft1, ['The model does not contain blocks. '...
            '(setSubResultStatusText method - Description of result status)']);
        setRecAction(ft1, {'Add blocks to the model. '...
            '(setRecAction method - Description of how to fix the problem)'});    
    else
        % Add status for subcheck
        setSubResultStatus(ft1, 'Pass');
        setSubResultStatusText(ft1, ['The model contains blocks. '...
            '(setSubResultStatusText method - Description of result status)']);
        for inx = 2 : length(allBlocks)
            % Add information to the table
            addRow(ft1, {inx-1,allBlocks(inx)});
        end    
    end
    
    % Pass table template object for subcheck to Model Advisor
    ResultDescription{end+1} = ft1;
    
    % Create FormatTemplate object for second subcheck, specify list format
    ft2 = ModelAdvisor.FormatTemplate('ListTemplate');
    
    % Add information describing the subcheck
    setSubTitle(ft2, 'List of Blocks (setSubTitle method - Title of the subcheck)');
    setInformation(ft2, ['Find and report all blocks in a list. '...
        '(setInformation method - Description of what the subcheck reviews)']);
    
    % Add See Also section for references to standards
    setRefLink(ft2, {{'Standard 1 reference (setRefLink method)'},
        {'Standard 2 reference (setRefLink method)'}});
    
    % Last subcheck, suppress line
    setSubBar(ft2, false);
    
    % Perform the subcheck actions
    if length(allBlocks) == 1
        % Add status for subcheck
        setSubResultStatus(ft2, 'Warn');
        setSubResultStatusText(ft2, ['The model does not contain blocks. '...
            '(setSubResultStatusText method - Description of result status)']);
        setRecAction(ft2, {'Add blocks to the model. '...
            '(setRecAction method - Description of how to fix the problem)'});
       
    else
        % Add status for subcheck
        setSubResultStatus(ft2, 'Pass');
        setSubResultStatusText(ft2, ['The model contains blocks. '...
            '(setSubResultStatusText method - Description of result status)']);
        % Add information to the list
        setListObj(ft2, allBlocks);
    end
    
    % Pass list template object for the subcheck to Model Advisor
    ResultDescription{end+1} = ft2;
    
    end
    
  2. sl_customization 文件保存到您的工作目录中。

  3. 在 MATLAB 命令行窗口中,输入:

    Advisor.Manager.refresh_customizations

  4. 打开一个模型。

  5. 建模选项卡中,选择模型顾问

  6. 按任务 > My Group 1文件夹中,选择 Sample check for Model Advisor using ModelAdvisor.FormatTemplate

  7. 点击运行此检查

    下图显示了检查通过时模型顾问中显示的输出。

    Model Advisor output when the check passes

    下图显示了检查发出警告时模型顾问中显示的输出。

    Model Advisor check output when the check warns

备选方法

当您定义 ModelAdvisor.Check 对象时,对于 CallbackStyle 属性,如果指定 DetailStyle,则不必使用 ModelAdvisor.FormatTemplate API 或其他格式 API 来格式化模型顾问报告中显示的结果。DetailStyle 还允许您按模块、子系统或建议的操作来查看结果。

如果默认格式不符合您的需要,请使用 ModelAdvisor.FormatTemplate API 或其他格式 API。ModelAdvisor.FormatTemplate 类在您创建的检查之间提供一致的外观。

版本历史记录

在 R2009a 中推出