Share Port Constraints Across Multiple Masked Blocks
This example shows how to create a port constraint and share it across masks of different blocks in the same model or different models. The port constraint definition is saved in an XML file and can be associated with ports across multiple masked blocks. The XML file must be available on the MATLAB® path.
Port constraints enable you to validate compile-time signal attributes on the input and output ports of a masked block without having to write validation code. Sharing of port constraints eliminates the need to repeatedly update multiple blocks, making maintenance and management of constraints more efficient. Defining a shared constraint in an XML file enables you to compare the constraint definition with a previous version.
Explore the Model
Open the model slexMaskSharedPortConstraint
. The model has two subsystem blocks, Engine_1
and Engine_2
. The shared port constraint ScalarInt8SharedConstraint
is saved in an XML file sharedPortChecks.xml
. The constraint validates whether the signals at input ports at index 1
of the subsystem blocks are int8
, scalar
, and real
at compile time.
open_system("slexMaskSharedPortConstraint");
Create Shared Port Constraint
To create a port constraint ScalarInt8SharedConstraint
in an XML file sharedPortChecks.xml
:
1. Create a Subsystem block with one input port In1
port and one output port Out1
. Name the block Engine_1
.
2. Create a mask on the block Engine_1
.
3. In the Mask Editor, create a checkbox parameter BatteryCheck
. The parameter BatteryCheck
is created to add the parameter condition to the constraint in the later steps.
4. In the Constraints tab, click New and enter sharedPortChecks
as the name of the XML file.
5. In the toolstrip, click Port to create a port constraint. Specify Constraint name as ScalarInt8SharedConstraint
and set the rule for Data Type as int8
, Complexity as real
, and Dimension as scalar
.
6. Set the Parameter Conditions. Click Add to select the parameter BatteryCheck
in Parameter Name and set the value to on
. The signal at the port is validated only when the parameter condition is satisfied.
7. Select error from the Diagnostic Level list and enter a diagnostic message in the Diagnostic Message box.
8. Create a port identifier for the input port with index 1
. Click Port Identifiers from the tool strip. In the Port Identifiers section, click Add. Specify Name as Eng_1_Inport_1
, select Input from the Type list, select index from the Identifier Type list, and specify Identifier(s) as 1
.
9. To associate the port at index 1
with the constraint using the port identifier Eng_1_Inport_1
, select the port identifier Eng_1_Inport_1
in Associations.
10. Save the mask.
Share Port Constraint
The port constraint ScalarInt8SharedConstraint
is created in the XML file sharedPortChecks.xml
in the previous section. To associate this port constraint with another subsystem block Engine_2
in the example model:
1. Create a subsystem block with one input port In1
with index 2
and one output port Out1
with index 2
. Name the block as Engine_2
.
2. Create a mask on the subsystem block Engine_2
.
3. In the Mask Editor, create a checkbox parameter Battery Check
.
4. In the Constraints tab, click Load and select the XML file sharedPortChecks.xml
to load the port constraint ScalarInt8SharedConstraint
.
5. Create a port identifier for the input port with index 1
. Click Port Identifiers from the tool strip. In the Port Identifiers section, click Add. Specify Name as Eng_2_Inport_1
, select Input from the Type list, select index from the Identifier Type list, and specify Identifier(s) as 1
.
6.To associate the port constraint to the port identifier, select the port Eng_2_Inport_1
in Associations.
Note: The port constraint definition is shared across blocks but you must associate port identifiers and association for each block.
Validate Port Constraint
To validate the associated port constraints:
1. Double click the input port In1
of the Engine_1
block.
2. In the Signal Attributes tab, change the Data type to int16
and click OK
3. Simulate the model. The Diagnostic Viewer displays the message.
Data at 'Input Port 1' of block 'slexSharedPortConstraint/Engine_1' is invalid. Caused by: Expected data types for the port must be int8. However, the port data type is int16. 'The signal at the battery component is incorrect'. Component:Simulink | Category:Model error An error occurred while propagating data type 'int16' from 'slexSharedPortConstraint/In1', 'Output Port 1'. Component:Simulink | Category:Model error
4. Reset the Data type to int8
and click OK so that the validation is successful for the block Engine_2
.
5. To demonstrate sharing of port constraints with the block Engine_2
, double-click the input port In1
of the Engine_2
block.
6. In the Signal Attributes tab, change the signal attribute to int16
and click OK.
7. Simulate the model. The Diagnostic Viewer displays the message.
Data at 'Input Port 1' of block 'slexSharedPortConstraint/Engine_2' is invalid. Caused by: Expected data types for the port must be int8. However, the port data type is uint16. 'The signal at the battery component is incorrect'. Component:Simulink | Category:Model error An error occurred while propagating data type 'uint16' from 'slexSharedPortConstraint/In2', 'Output Port 1'. Component:Simulink | Category:Model error