Define Custom Edit-Time Checks that Fix Issues in Architecture Models
This example shows how to create a custom edit-time check that runs on architecture models that you create using System Composer™. Edit-time checks help you catch issues earlier in the model design review process. The custom edit-time check in this example produces a warning if the names of connecting block port interface names do not match. For more information on the process for authoring custom checks, see Define Custom Model Advisor Checks.
Create a Simple Architecture Model
Create a simple architecture model with mismatched data interface names in ports that share a connector.
Create a temporary working directory.
In MATLAB, on the Home tab, click Simulink.
In the Simulink Start page, click System Composer and select Architecture Model.
Add three Component blocks.
Connect the outport of one Component block to the inport ports of the other two Component blocks as shown in this image.
On the Modeling tab, click Interface Editor.
Create two data interfaces with the names
interface0
andinterface1
.Open the Property Inspector.
For
Component
, click theOutBus
port. In the Interface section of the Property Inspector, for the Name field, selectinterface0
.For
Component1
, click theInBus
port. In the Interface section of the Property Inspector, for the Name field, selectinterface1
.For
Component2
, click theInBus
port. In the Interface section of the Property Inspector, for the Name field, selectinterface0
.Save the model to your working directory. For this example, the model name is
myModel.slx
.
Create the Custom Edit-Time Check
Create a check that detects the mismatched data interface names in ports that share the same connector while a user is editing a model.
To register the custom edit-time check, create an
sl_customization
function. Thesl_customization
function accepts one argument, a customization manager object. To register the custom check, use theaddModelAdvisorCheckFcn
method. The input to this method is a handle to the check definition function. For this example,defineCheck
is the check definition function. Create thesl_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 an input argument. Then, specify theModelAdvisor.Check
Title
andCallbackHandle
properties. TheCallbackHandle
property is the name of the class that you create to define the edit-time check. For this example,MyEditTimeChecks
is the namespace andPortMismatch
is the class name. Then, publish the check to a new folder in the Model Advisor. For this example, the folder name is System Composer Edit-time Check. For this example, create adefineCheck
function and include the code below in it. Save thedefineCheck
function to your working folder.function defineCheck rec = ModelAdvisor.Check("advisor.edittimecheck.SystemComposerPortMismatch"); rec.Title = 'Check port mismatch for system composer components'; rec.CallbackHandle = 'MyEditTimeChecks.PortMismatch'; mdladvRoot = ModelAdvisor.Root; mdladvRoot.publish(rec,'System Composer Edit-time Check'); end
Create a class that derives from the
ModelAdvisor.EdittimeCheck
abstract base class. For this example, create a class file namedPortMismatch
. Copy the code below into thePortMismatch.m
file. Then, create a folder named+MyEditTimeChecks
and save thePortMismatch.m
file in that folder. The class must be in a folder that has the same name as the namespace.The
PortMismatch
class defines two methods:PortMismatch
andblockDiscovered
. ThePortMismatch
method sets theCheckId
andTraversalType
properties. This check has a traversal type ofedittimecheck.TraversalTypes.ACTIVEGRAPH
because the check must check newly added and edited blocks and affected blocks in the same subsystem or model. TheblockDiscovered
method contains an algorithm that checks whether the port interface names match.classdef PortMismatch < ModelAdvisor.EdittimeCheck methods function obj=PortMismatch(checkId) obj=obj@ModelAdvisor.EdittimeCheck(checkId); obj.traversalType = edittimecheck.TraversalTypes.ACTIVEGRAPH; end function violationArray = blockDiscovered(obj,blk) violationArray = []; blkHdl = get_param(blk, 'Handle'); archMdl = systemcomposer.arch.Model(bdroot(blk)); comp = archMdl.lookup('SimulinkHandle',blkHdl); if isa(comp, 'systemcomposer.arch.Component') for i = 1:length(comp.Ports) compPort = comp.Ports(i); if strcmp(compPort.Direction, 'Output') srcInterfaceName = compPort.InterfaceName; for j = 1:length(compPort.Connectors) connector = compPort.Connectors(j); destPort = connector.DestinationPort; destInterfaceName = destPort.InterfaceName; if(~strcmpi(srcInterfaceName, destInterfaceName)) hiliteHandle = destPort.SimulinkHandle; violation = ModelAdvisor.ResultDetail; ModelAdvisor.ResultDetail.setData(violation, 'Signal',hiliteHandle); violation.CheckID = obj.checkId; violation.Description = 'Connected port interface names should be the same.'; violation.Title = 'Port Interface Mismatch'; violation.ViolationType = 'warn'; violationArray = [violationArray violation]; %#ok<AGROW> end end end end else compPort = comp; if strcmp(compPort.Direction, 'Output') srcInterfaceName = compPort.InterfaceName; for j = 1:length(compPort.Connectors) connector = compPort.Connectors(j); destPort = connector.DestinationPort; destInterfaceName = destPort.InterfaceName; if(~strcmpi(srcInterfaceName, destInterfaceName)) hiliteHandle = destPort.SimulinkHandle; violation = ModelAdvisor.ResultDetail; ModelAdvisor.ResultDetail.setData(violation,'Signal',hiliteHandle); violation.CheckID = obj.checkId; violation.Description = 'Connected port interface names should be the same.'; violation.Title = 'Port Interface Mismatch'; violation.ViolationType = 'warn'; violationArray = [violationArray violation]; %#ok<AGROW> end end end end end end end
Create a Custom Edit-Time Check Configuration
Create a custom configuration consisting of the edit-time check. Associate the
configuration with the myModel.slx
model.
Refresh the Model Advisor to update the cache with the new check on the path.
Advisor.Manager.refresh_customizations
Open the Model Advisor Configuration Editor by clicking the Modeling tab and selecting Model Advisor > Customize Edit-Time Checks or by entering this command at the command prompt:
Simulink.ModelAdvisor.openConfigUI;
Create a custom configuration that consists only of the custom edit-time check. Select the Product > System Composer Edit-time Checksfolder and then delete the other folders. Save the configuration as
sc_config.json
. Close the Model Advisor Configuration Editor.Set the custom configuration to the
sc_config.json
file by clicking the Modeling tab and selecting Model Advisor > Edit-Time Checks. In the Configuration Parameters dialog box that opens, specify the path to the configuration file in the Model Advisor configuration file parameter. Alternatively, enter this command at the command prompt:ModelAdvisor.setModelConfiguration('myModel', 'sc_config.json');
Turn on edit-time checking by selecting the Model Advisor > Edit Time configuration parameter. Alternatively, you can enter this command at the command prompt:
edittime.setAdvisorChecking('myModel','on');
To view the edit-time warnings, click the signal highlighted in yellow.
The connector between the
Component
andComponent1
blocks produces a warning because the data interface names in each port do not match.
See Also
ModelAdvisor.EdittimeCheck
| ModelAdvisor.Check
| ModelAdvisor.ResultDetail