Manage Custom Attributes for Requirements by Using the Requirements Toolbox API
This example shows how to use the Requirements Toolbox™ API to create custom attributes for requirement sets and set custom attribute values for requirements.
Establish Requirement Set
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
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 = 1x4 cell
{'MyCheckboxAttribute'} {'MyComboboxAttribute'} {'MyDateTimeAttribute'} {'MyEditAttribute'}
atrb = inspectAttribute(rs,'MyComboboxAttribute')
atrb = struct with fields:
name: 'MyComboboxAttribute'
type: Combobox
description: ''
list: {'Unset' 'A' 'B' 'C'}
Set a Custom Attribute Value for a Requirement
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');
Set MyDateTimeAttribute
with the desired locale to ensure that the date and time is set in the correct format on systems in other locales. See Locale for more information.
localDateTimeStr = datestr(datetime('15-Jul-2018 11:00:00','Locale','en_US'),'Local'); setAttribute(req,'MyDateTimeAttribute',localDateTimeStr);
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
Edit Custom Attributes
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'}
Find Requirements That Match Custom Attribute Value
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">...' Keywords: {} Rationale: '' CreatedOn: 27-Feb-2017 10:15:38 CreatedBy: 'itoy' ModifiedBy: 'batserve' IndexEnabled: 1 IndexNumber: [] SID: 3 FileRevision: 46 ModifiedOn: 05-Sep-2024 22:41:51 Dirty: 1 Comments: [0x0 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 Requirement array with properties:
Type
Id
Summary
Description
Keywords
Rationale
CreatedOn
CreatedBy
ModifiedBy
IndexEnabled
IndexNumber
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 Requirement array with properties:
Type
Id
Summary
Description
Keywords
Rationale
CreatedOn
CreatedBy
ModifiedBy
IndexEnabled
IndexNumber
SID
FileRevision
ModifiedOn
Dirty
Comments
Index
Delete Custom Attributes
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 = 1x3 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 = 1x4 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 = 1x3 cell
{'MyCheckboxAttribute'} {'MyComboboxAttribute'} {'MyDateTimeAttribute'}
See Also
slreq.ReqSet
| addAttribute
| deleteAttribute
| updateAttribute
| inspectAttribute
| getAttribute
| setAttribute