Main Content

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.

  1. Create a temporary working directory.

  2. In MATLAB, on the Home tab, click Simulink.

  3. In the Simulink Start page, click System Composer and select Architecture Model.

  4. Add three Component blocks.

  5. Connect the outport of one Component block to the inport ports of the other two Component blocks as shown in this image.

    Simple architecture model

  6. On the Modeling tab, click Interface Editor.

  7. Create two data interfaces with the names interface0 and interface1.

  8. Open the Property Inspector.

  9. For Component, click the OutBus port. In the Interface section of the Property Inspector, for the Name field, select interface0.

  10. For Component1, click the InBus port. In the Interface section of the Property Inspector, for the Name field, select interface1.

  11. For Component2, click the InBus port. In the Interface section of the Property Inspector, for the Name field, select interface0.

  12. 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.

  1. To register the custom 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, defineCheck is the check definition function. Create the sl_customization function and save it to your working folder.

    function sl_customization(cm)
    cm.addModelAdvisorCheckFcn(@defineCheck);
  2. Create the check definition function. Inside the function, create a ModelAdvisor.Check object and specify the Check ID as an 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 PortMismatch 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 a defineCheck function and include the code below in it. Save the defineCheck 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
  3. Create a class that derives from the ModelAdvisor.EdittimeCheck abstract base class. For this example, create a class file named PortMismatch. Copy the code below into the PortMismatch.m file. Then, create a folder named +MyEditTimeChecks and save the PortMismatch.m file in that folder. The class must be in a folder that has the same name as the package name.

    The PortMismatch class defines two methods: PortMismatch and blockDiscovered. The PortMismatch method sets the CheckId and TraversalType properties. This check has a traversal type of edittimecheck.TraversalTypes.ACTIVEGRAPH because the check must check newly added and edited blocks and affected blocks in the same subsystem or model. The blockDiscovered 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.

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

    Advisor.Manager.refresh_customizations
  2. Open the Model Advisor Configuration Editor by clicking the Modeling tab and selecting Model Advisor > Configuration Editor or by entering this command at the command prompt:

    Simulink.ModelAdvisor.openConfigUI;
  3. 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.

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

  5. 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');
    
  6. To view the edit-time warnings, click the signal highlighted in yellow.

    Architecture model with edit-time warning

    The connector between the Component and Component1 blocks produces a warning because the data interface names in each port do not match.

See Also

| |

Related Topics