Main Content

ModelAdvisor.ResultDetail.setData

Class: ModelAdvisor.ResultDetail
Namespace: ModelAdvisor

Associate Model Advisor check result with block or signal

Description

ModelAdvisor.ResultDetail.setData(resultObj,blockID) associates the Model Advisor check result, resultObj, with the Simulink® block, blockID. You can use this method inside your custom check to return information about blocks that violate your check.

example

ModelAdvisor.ResultDetail.setData(resultObj,'SID',blockID) is the same as the syntax above.

example

ModelAdvisor.ResultDetail.setData(resultObj,'Signal',signalHandle) associates the Model Advisor check result, resultObj, with the Simulink signal, signalHandle.

example

Input Arguments

expand all

Model Advisor check result, specified as a ModelAdvisor.ResultDetail object.

Simulink block, specified as a string or character vector that contains the Simulink Identifier (SID) for the block.

Example: 'myModel/myBlock'

Data Types: char | string

Simulink signal, specified as a double that represents the signal handle.

Example: 280.0009

Data Types: double

Examples

expand all

Create a custom Model Advisor check that checks whether block names appear below the blocks.

Open the model.

openExample('AdvisorCustomizationExample')

Model with edit-time check violations

Save the model to your working folder. Close the model.

To register the edit-time check, create an sl_customization function. The sl_customization function accepts one argument, a customization manager object. To register the custom check, use the addModelAdvisorCheckFcn method. The input to this method is a handle to the check definition function. For this example, defineDetailStyleCheck is the check definition function. Create the sl_customization function and save it to your working folder.

function sl_customization(cm)
cm.addModelAdvisorCheckFcn(@defineDetailStyleCheck);

Create the check definition function. For this example, create a function file named defineDetailStyleCheck.m. Copy the following code to defineDetailStyleCheck.m and save the function to your working folder.

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

The check definition function contains ModelAdvisor.Check and ModelAdvisor.Action objects that define the check actions and a fix. For more details on these aspects of the code, see Fix a Model to Comply with Conditions that You Specify with the Model Advisor.

The defineDetailStyleCheck function contains a DetailStyleCallback callback function. To return the blocks whose names do not appear below them, the DetailStyleCallback function uses the find_system function.

When violationBlks is empty, the code creates one ModelAdvisor.ResultDetail object, ElementResults. ElementResults specifies information about the passing check that appears in the Model Advisor.

When the find_system function returns a list of blocks that violate the check, ElementResults is an array of ModelAdvisor.ResultDetail objects. The array contains one object for each block that violates the check. Each object contains information about the blocks that appears in the Model Advisor.

Refresh the Model Advisor to update the cache with the new check on the path.

Advisor.Manager.refresh_customizations

Open the AdvisorCustomizationExample model.

In Simulink, open the Model Advisor by clicking the Modeling tab and selecting Model Advisor.

In the left pane, select By Product > Demo > Check whether block names appear below blocks and click Run Checks.

To address the warning, click Fix.

Create a custom edit-time check that checks whether signals that connect to Outport blocks have labels.

Open the model.

openExample('AdvisorCustomizationExample')

Model with edit-time check violations

Save the model to your working folder. Close the model.

To register the custom edit-time check, create an sl_customization function and save it to your working folder.

function sl_customization(cm)
cm.addModelAdvisorCheckFcn(@defineCheck);

Create the check definition function. Inside the function, create a ModelAdvisor.Check object and specify the check ID as the input argument. Then, specify the ModelAdvisor.Check Title and CallbackHandle properties. The CallbackHandle property is the name of the class that you create to define the edit-time check. For this example, MyEditTimeChecks is the package name and SignalLabel is the class name. Then, publish the check to a new folder in the Model Advisor. For this example, the folder name is Demo: Edit-Time Checks. Create a defineCheck function and include the following code. Save the defineCheck function to your working folder.

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');

Create a class that derives from the ModelAdvisor.EdittimeCheck abstract base class. For this example, create a class file named SignalLabel.m. Copy the following code into the SignalLabel.m file and save it to the +MyEditTimeChecks folder.

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

For more information on the code in the ModelAdvisor.EdittimeCheck class, see Define Edit-Time Checks to Comply with Conditions That You Specify with the Model Advisor.

The blockDiscovered method contains an algorithm that defines check violations. Unlike custom checks that appear only in the Model Advisor, this algorithm does not contain the find_system function. The blockDiscovered method takes block handles as inputs and traverses the model, so you do not need the find_system function for custom edit-time checks.

For each model element that violates the check, the code creates a ModelAdvisor.ResultDetail object. For this example, because the violations are on signals, the check must use parameters on the line handles of blocks. To find signals with violations, you must specify the LineHandles option. Specifically, for signals that connect to Outport blocks, this algorithm checks whether the Name signal parameter has a value. Then, because the violation is on a signal, the algorithm highlights the signal by creating a violation object with the Type property value set to Signal.

Refresh the Model Advisor to update the cache with the new checks on the path.

Advisor.Manager.refresh_customizations

Open the AdvisorCustomizationExample model.

In Simulink, open the Model Advisor Configuration Editor by clicking the Modeling tab and selecting Model Advisor > Configuration Editor.

Create a custom configuration that consists of the custom edit-time check by deleting every folder except the DEMO: Edit Time Checks folder.

Save the configuration as my_config.json. When prompted to set this configuration as the default, click No.

Close the Model Advisor Configuration Editor.

Set the custom configuration to the my_config.json file by clicking the Modeling tab and selecting Model Advisor > Edit-Time Checks. In the Configuration Parameters dialog box, set the Model Advisor configuration file parameter to the path of the configuration file.

Turn on edit-time checking by selecting the Edit-Time Checks parameter. Close the Model Configuration Parameters dialog box.

To view the edit-time warning, click the signal highlighted in yellow. The signal connecting to the Outport block produces a warning because it does not have a label.

This image shows the Model Advisor report and indicates which ModelAdvisor.ResultDetail properties populate each section:

Model Advisor Result Details

Version History

Introduced in R2018b

expand all