This example shows how to use the Simulink® Requirements™ API to create custom attributes for requirement sets and set custom attribute values for requirements.
Load the requirement file crs_req_func_spec
, which describes a cruise control system, and assign it to a variable.
rs = slreq.load('crs_req_func_spec');
Add a custom attribute of each type to the requirement set. Create an Edit
custom attribute with a description.
addAttribute(rs,'MyEditAttribute','Edit','Description',... 'You can enter text as the custom attribute value.')
Create a Checkbox
type attribute and set its DefaultValue
property to true
.
addAttribute(rs,'MyCheckboxAttribute','Checkbox','DefaultValue',true)
Create a Combobox
custom attribute. Because the first option must be 'Unset'
, add the options 'Unset', 'A', 'B', and 'C'
.
addAttribute(rs,'MyComboboxAttribute','Combobox','List',{'Unset','A','B','C'})
Create a DateTime
custom attribute.
addAttribute(rs,'MyDateTimeAttribute','DateTime')
Check the defined custom attributes for the requirement set. Get information about MyComboboxAttribute
to see the options you added.
rs.CustomAttributeNames
ans = 1×4 cell
{'MyCheckboxAttribute'} {'MyComboboxAttribute'} {'MyDateTimeAttribute'} {'MyEditAttribute'}
atrb = inspectAttribute(rs,'MyComboboxAttribute')
atrb = struct with fields:
name: 'MyComboboxAttribute'
type: Combobox
description: ''
list: {'Unset' 'A' 'B' 'C'}
Find a requirement in the requirement set, and set the custom attribute value for all four custom attributes that you created.
req = find(rs,'Type','Requirement','SID',3); setAttribute(req,'MyEditAttribute','Value for edit attribute.'); setAttribute(req,'MyCheckboxAttribute',false); setAttribute(req,'MyComboboxAttribute','B'); setAttribute(req,'MyDateTimeAttribute','15-Jul-2018 11:00:00');
View the attribute values.
getAttribute(req,'MyEditAttribute')
ans = 'Value for edit attribute.'
getAttribute(req,'MyCheckboxAttribute')
ans = logical
0
getAttribute(req,'MyComboboxAttribute')
ans = 'B'
getAttribute(req,'MyDateTimeAttribute')
ans = datetime
15-Jul-2018 11:00:00
After you define a custom attribute for a link set, you can make limited changes to the custom attribute.
Add a description to MyCheckboxAttribute
and MyComboboxAttribute
, and change the list of options for MyComboboxAttribute
. Because you cannot update the default value of Checkbox
attributes, you can only update the description of MyCheckboxAttribute
. View the changes.
updateAttribute(rs,'MyCheckboxAttribute','Description',... 'The checkbox value can be true or false.'); updateAttribute(rs,'MyComboboxAttribute','Description',... 'Choose an option from the list.','List',{'Unset','1','2','3'}); atrb2 = inspectAttribute(rs,'MyCheckboxAttribute')
atrb2 = struct with fields:
name: 'MyCheckboxAttribute'
type: Checkbox
description: 'The checkbox value can be true or false.'
default: 1
atrb3 = inspectAttribute(rs,'MyComboboxAttribute')
atrb3 = struct with fields:
name: 'MyComboboxAttribute'
type: Combobox
description: 'Choose an option from the list.'
list: {'Unset' '1' '2' '3'}
Search the requirement set for all requirements where 'MyEditAttribute' is set to 'Value for edit attribute.'
req2 = find(rs,'Type','Requirement','MyEditAttribute','Value for edit attribute.')
req2 = Requirement with properties: Type: 'Functional' Id: '#3' Summary: 'Avoid repeating commands' Description: '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">↵<html><head><meta name="qrichtext" content="1" /><style type="text/css">↵p, li { white-space: pre-wrap; }↵</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:10pt; font-weight:400; font-style:normal;">↵<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">If the following switch operations are repeated, the system should output <span style=" font-style:italic;">NoRequest</span> the second time and after as long as the same switch is enabled:</p>↵<ul style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; -qt-list-indent: 1;"><li style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Cancel</li>↵<li style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Cruise</li>↵<li style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Set</li>↵<li style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Resume</li></ul>↵<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></body></html>' Keywords: {} Rationale: '' CreatedOn: 27-Feb-2017 10:15:38 CreatedBy: 'itoy' ModifiedBy: 'ahoward' SID: 3 FileRevision: 46 ModifiedOn: 15-Jul-2020 10:36:52 Dirty: 1 Comments: [0×0 struct] Index: '1.2'
Search the requirement set for all requirements where 'MyCheckboxAtribute'
is set to true
.
reqsArray = find(rs,'Type','Requirement','MyCheckboxAttribute',true)
reqsArray=1×69 object
1×69 Requirement array with properties:
Type
Id
Summary
Description
Keywords
Rationale
CreatedOn
CreatedBy
ModifiedBy
SID
FileRevision
ModifiedOn
Dirty
Comments
Index
Search the requirement set for all requirements where 'MyComboboxAttribute'
is set to 'Unset'
.
reqsArray2 = find(rs,'Type','Requirement','MyComboboxAttribute','Unset')
reqsArray2=1×70 object
1×70 Requirement array with properties:
Type
Id
Summary
Description
Keywords
Rationale
CreatedOn
CreatedBy
ModifiedBy
SID
FileRevision
ModifiedOn
Dirty
Comments
Index
You can use deleteAttribute
to delete attributes. However, because the custom attributes created in this example are assigned to requirements, you must set 'Force'
to true
to delete the attributes. Delete 'MyEditAttribute'
and confirm the change.
deleteAttribute(rs,'MyEditAttribute','Force',true); rs.CustomAttributeNames
ans = 1×3 cell
{'MyCheckboxAttribute'} {'MyComboboxAttribute'} {'MyDateTimeAttribute'}
Add a new custom attribute, but don't set any requirement custom attribute values for requirements.
addAttribute(rs,'NewEditAttribute','Edit'); rs.CustomAttributeNames
ans = 1×4 cell
{'MyCheckboxAttribute'} {'MyComboboxAttribute'} {'MyDateTimeAttribute'} {'NewEditAttribute'}
Because 'NewEditAttribute'
is not used by any requirements, you can delete it with deleteAttribute
by setting 'Force'
to false
. Confirm the change.
deleteAttribute(rs,'NewEditAttribute','Force',false); rs.CustomAttributeNames
ans = 1×3 cell
{'MyCheckboxAttribute'} {'MyComboboxAttribute'} {'MyDateTimeAttribute'}
Clear the open requirement sets without saving changes and close the open models without saving changes.
slreq.clear;
bdclose all;
addAttribute
| deleteAttribute
| getAttribute
| inspectAttribute
| setAttribute
| slreq.ReqSet
| updateAttribute