Main Content

Define Model Advisor Checks for Supported and Unsupported Blocks and Parameters

You can create Model Advisor checks that check whether blocks use specific block or parameter values. You can specify constraints for:

  • Supported or unsupported block parameter values

  • Supported or unsupported model parameter values

  • Supported or unsupported blocks

  • Whether blocks or parameters meet a combination of constraints

You can also use the addPreRequisiteConstraintID function to add prerequisite constraints that must pass before Model Advisor checks the actual constraint. You can check your model against these constraints as you edit or by running the checks from the Model Advisor.

Example

The sldemo_bounce model simulates a ball bouncing on Earth. In this example, you create two Model Advisor checks consisting of constraints, then check the model against those constraints.

ex_bouncing_ball_model.png

Create a Check for Supported or Unsupported Block Parameters

First, create a Model Advisor check that contains three block parameter constraints, c1, c2, and c3, that specify the supported and unsupported block parameter values.

1. Define a new function.

function constraints = createConstraints_Check1()
end

2. Inside the function, create two block parameter constraints, c1 and c2.

function constraints = createConstraints_Check1()

    c1=Advisor.authoring.PositiveBlockParameterConstraint;
    c1.ID='ID_1';
    c1.BlockType='Gain';
    c1.ParameterName='Gain';
    c1.SupportedParameterValues={'-0.7'};
    c1.ValueOperator='eq'; % equal to
    
    c2=Advisor.authoring.NegativeBlockParameterConstraint;
    c2.ID='ID_2';
    c2.BlockType='InitialCondition';
    c2.ParameterName='Value';
    c2.UnsupportedParameterValues={'0'};
    c2.ValueOperator='le'; % less than or equal to

end

Constraint c1 specifies that a Gain block must have a value equal to -0.7. Constraint c2 specifies that an Initial Condition block with a value less than or equal to zero is unsupported.

3. Create a positive block constraint, c3, and set constraints equal to a cell array of constraints c1, c2, and c3.

function constraints = createConstraints_Check1()

    c1=Advisor.authoring.PositiveBlockParameterConstraint;
    c1.ID='ID_1';
    c1.BlockType='Gain';
    c1.ParameterName='Gain';
    c1.SupportedParameterValues={'-0.7'};
    c1.ValueOperator='eq'; % equal to
    
    c2=Advisor.authoring.NegativeBlockParameterConstraint;
    c2.ID='ID_2';
    c2.BlockType='InitialCondition';
    c2.ParameterName='Value';
    c2.UnsupportedParameterValues={'0'};
    c2.ValueOperator='le'; % less than or equal to
    
    c3=Advisor.authoring.PositiveBlockTypeConstraint;
    c3.ID='ID_3';
    s1=struct('BlockType','Constant','MaskType','');
    s2=struct('BlockType','SubSystem','MaskType','');
    s3=struct('BlockType','InitialCondition','MaskType','');
    s4=struct('BlockType','Gain','MaskType','');
    s5=struct('BlockType','Memory','MaskType','');
    s6=struct('BlockType','SecondOrderIntegrator','MaskType','');
    s7=struct('BlockType','Terminator','MaskType','');
    c3.SupportedBlockTypes={s1;s2;s3;s4;s5;s6;s7;};
    
    constraints = {c1,c2,c3};

end

Constraint c3 specifies the supported blocks. constraints is a cell array of the block constraints.

4. Define a new Model Advisor check by creating another function, check1. Use the function Advisor.authoring.createBlockConstraintCheck to create a Model Advisor check, rec, with these block constraints. Then use mdladvRoot.register(rec) to register the block constraints check with the Model Advisor.

function check1()

    rec = Advisor.authoring.createBlockConstraintCheck('mathworks.check_0001',...
                                             'Constraints',@createConstraints_Check1);
    
    rec.Title = 'Example 1: Check three block parameter constraints';
    rec.TitleTips = 'Example check three block parameter constraints';
    
    mdladvRoot = ModelAdvisor.Root;
    mdladvRoot.publish(rec,'Example: My Group')


end

function constraints = createConstraints_Check1()

    c1=Advisor.authoring.PositiveBlockParameterConstraint;
    c1.ID='ID_1';
    c1.BlockType='Gain';
    c1.ParameterName='Gain';
    c1.SupportedParameterValues={'-0.7'};
    c1.ValueOperator='eq'; % equal to
    
    c2=Advisor.authoring.NegativeBlockParameterConstraint;
    c2.ID='ID_2';
    c2.BlockType='InitialCondition';
    c2.ParameterName='Value';
    c2.UnsupportedParameterValues={'0'};
    c2.ValueOperator='le'; % less than or equal to
    
    c3=Advisor.authoring.PositiveBlockTypeConstraint;
    c3.ID='ID_3';
    s1=struct('BlockType','Constant','MaskType','');
    s2=struct('BlockType','SubSystem','MaskType','');
    s3=struct('BlockType','InitialCondition','MaskType','');
    s4=struct('BlockType','Gain','MaskType','');
    s5=struct('BlockType','Memory','MaskType','');
    s6=struct('BlockType','SecondOrderIntegrator','MaskType','');
    s7=struct('BlockType','Terminator','MaskType','');
    c3.SupportedBlockTypes={s1;s2;s3;s4;s5;s6;s7;};
    
    constraints = {c1,c2,c3};

end

Create a Check for a Composite Constraint

Next, create a Model Advisor check that contains three block parameter constraints cc1, cc2, and cc. Constraints cc1 and cc2 specify which block parameters are supported and constraint cc is a composite constraint which contains cc1 and cc2.

1. Define a new function.

function constraints = createConstraints_Check2()
end

2. Create two block parameter constraints, cc1 and cc2, and a composite constraint, cc. Set constraints equal to a cell array of constraints cc1, cc2, and cc.

function constraints = createConstraints_Check2()

    cc1=Advisor.authoring.PositiveBlockParameterConstraint;
    cc1.ID='ID_cc1';
    cc1.BlockType='SecondOrderIntegrator';
    cc1.ParameterName='UpperLimitX';
    cc1.SupportedParameterValues={'inf'};
    cc1.ValueOperator='eq'; % equal to
    
    cc2=Advisor.authoring.PositiveBlockParameterConstraint;
    cc2.ID='ID_cc2';
    cc2.BlockType='SecondOrderIntegrator';
    cc2.ParameterName='LowerLimitX';
    cc2.SupportedParameterValues={'0.0'};
    cc2.ValueOperator='eq'; % equal to
    
    cc=Advisor.authoring.CompositeConstraint;
    cc.addConstraintID('ID_cc1');
    cc.addConstraintID('ID_cc2');
    cc.CompositeOperator='and'; % Model Advisor checks multiple constraints

    constraints = {cc1,cc2,cc};

end

Constraint cc1 specifies that for a Second-Order Integrator block, the Upper limit x parameter must have a value equal to inf. Constraint cc2 additionally specifies that the Lower limit x parameter must have a value equal to zero. Constraint cc specifies that for this check to pass, both cc1 and cc2 must pass. constriants is a cell array of the block constraints.

3. Define a new Model Advisor check in a new function, check2. Use the function Advisor.authoring.createBlockConstraintCheck to create a Model Advisor check for the block constraints defined by the function createConstraints_Check2.

function check2()

    rec = Advisor.authoring.createBlockConstraintCheck('mathworks.check_0002',...
                                             'Constraints',@createConstraints_Check2);
    
    rec.Title = 'Example 2: Check three block parameter constraints';
    rec.TitleTips = 'Example check three block parameter constraints';
    
    mdladvRoot = ModelAdvisor.Root;
    mdladvRoot.publish(rec,'Example: My Group')



end

function constraints = createConstraints_Check2()

    cc1=Advisor.authoring.PositiveBlockParameterConstraint;
    cc1.ID='ID_cc1';
    cc1.BlockType='SecondOrderIntegrator';
    cc1.ParameterName='UpperLimitX';
    cc1.SupportedParameterValues={'inf'};
    cc1.ValueOperator='eq';
    
    cc2=Advisor.authoring.PositiveBlockParameterConstraint;
    cc2.ID='ID_cc2';
    cc2.BlockType='SecondOrderIntegrator';
    cc2.ParameterName='LowerLimitX';
    cc2.SupportedParameterValues={'0.0'};
    cc2.ValueOperator='eq';
    
    cc=Advisor.authoring.CompositeConstraint;
    cc.addConstraintID('ID_cc1');
    cc.addConstraintID('ID_cc2');
    cc.CompositeOperator='and';
    
    constraints = {cc1,cc2,cc};

end

Create and Run Model Advisor Checks

1. To register the new checks, use an sl_customization.m file. For this example, rename the sl_customization_DefineChecks function and file to sl_customization.

function sl_customization(cm)

    % register custom checks.
    cm.addModelAdvisorCheckFcn(@check1);
    cm.addModelAdvisorCheckFcn(@check2);
    

2. At the command prompt, create the Example 1: Check block parameter constraints and Example 2: Check block parameter constraints checks by typing this command:

Advisor.Manager.refresh_customizations

3. At the command prompt, open the model sldemo_bounce.

open_system('sldemo_bounce')

4. In the Modeling tab, select Model Advisor to open the Model Advisor.

5. In the left pane, select By Product > Example: My Group.

6. Click Run Checks.

The Example 1: Check three block parameter constraints check produces a warning because the Gain block has a value of -0.8. The Example 2: Check three block parameter constraints check passes because the Second-Order Integrator block meets both constraints.

Create Model Advisor Edit-Time Checks using Constraints

You can use edit-time checking to highlight blocks with block constraint violations in the model canvas. You can choose which Model Advisor checks evaluate during edit-time checking by selecting the desired checks in the Model Advisor Configuration Editor and saving a custom configuration.

1. To open the Model Advisor Configuration Editor, open the Model Advisor and select Open > Open Configuration Editor.

2. The checks you created appear in the By Product > Example: My Group.

3. For this example, delete all folders except for the Example: My Group folder.

block_configuration.png

4. Click Save and save the configuration as my_config.json.

5. In the dialog box, click No because you do not want to set this configuration as the default configuration.

6. In the Simulink model editor, on the Modeling tab, click Model Advisor > Edit-Time Checks.

7. For the Model advisor configuration file parameter, click Browse, and select my_config.json.

8. Select the Edit-Time Checks parameter and close the Configuration Parameters dialog box.

9. Click on the highlighted block in the model to view the edit-time warning.

You can edit the block parameter values from the edit-time check diagnostics window by clicking the Fix button or by clicking the hyperlinks of the unsupported parameter to open the Block Parameters window.

See Also

| | | | | |

Related Topics