Manage Custom Attributes for Links by Using the Requirements Toolbox API
This example shows how to use the Requirements Toolbox™ API to create and manage custom attributes for link sets and set custom attribute values for links.
Establish Link Set
Load the crs_req
requirement file, which describes a cruise control system. Find the link set named crs_req
and assign it to a variable.
slreq.load('crs_req'); ls = slreq.find('Type','LinkSet','Name','crs_req')
ls = LinkSet with properties: Description: 'crs_req' Filename: '/tmp/Bdoc24b_2725827_3713282/tpfec5b6fa/slrequirements-ex23809012/crs_req.slmx' Artifact: '/tmp/Bdoc24b_2725827_3713282/tpfec5b6fa/slrequirements-ex23809012/crs_req.slreqx' Domain: 'linktype_rmi_slreq' Revision: 11 Dirty: 0 CustomAttributeNames: {'Target Speed Change'}
Delete a Custom Attribute
There is an existing custom attribute in the link set called Target Speed Change
. Delete the custom attribute and confirm the results by checking the existing custom attribute names for the link set.
deleteAttribute(ls,'Target Speed Change','Force',true); ls.CustomAttributeNames
ans = 0x0 empty cell array
Add a Custom Attribute of Each Type
Add a custom attribute of each type to the link set. Create an Edit
custom attribute with a description.
addAttribute(ls,'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(ls,'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(ls,'MyComboboxAttribute','Combobox','List',{'Unset','A','B','C'})
Create a DateTime
custom attribute.
addAttribute(ls,'MyDateTimeAttribute','DateTime')
Check the custom attributes for the link set. Get information about MyComboboxAttribute
to see the options you added to the Combobox
attribute.
ls.CustomAttributeNames
ans = 1x4 cell
{'MyCheckboxAttribute'} {'MyComboboxAttribute'} {'MyDateTimeAttribute'} {'MyEditAttribute'}
atrb = inspectAttribute(ls,'MyComboboxAttribute')
atrb = struct with fields:
name: 'MyComboboxAttribute'
type: Combobox
description: ''
list: {'Unset' 'A' 'B' 'C'}
Set a Custom Attribute Value for a Link
Find a link in the link set and set the custom attribute value for all four custom attributes that you created.
lk = find(ls,'SID',3); setAttribute(lk,'MyEditAttribute','Value for edit attribute.'); setAttribute(lk,'MyCheckboxAttribute',false); setAttribute(lk,'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(lk,'MyDateTimeAttribute',localDateTimeStr);
View the attribute values.
getAttribute(lk,'MyEditAttribute')
ans = 'Value for edit attribute.'
getAttribute(lk,'MyCheckboxAttribute')
ans = logical
0
getAttribute(lk,'MyComboboxAttribute')
ans = 'B'
getAttribute(lk,'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
, then 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(ls,'MyCheckboxAttribute','Description',['The checkbox value can be' ... ' true or false.']); updateAttribute(ls,'MyComboboxAttribute','Description',['Choose an option from the ' ... 'list.'],'List',{'Unset','1','2','3'}); atrb2 = inspectAttribute(ls,'MyCheckboxAttribute')
atrb2 = struct with fields:
name: 'MyCheckboxAttribute'
type: Checkbox
description: 'The checkbox value can be true or false.'
default: 1
atrb3 = inspectAttribute(ls,'MyComboboxAttribute')
atrb3 = struct with fields:
name: 'MyComboboxAttribute'
type: Combobox
description: 'Choose an option from the list.'
list: {'Unset' '1' '2' '3'}
Find Links That Match Custom Attribute Value
Search the link set for all links where 'MyEditAttribute'
is set to 'Value for edit attribute.'
lk2 = find(ls,'MyEditAttribute','Value for edit attribute.')
lk2 = Link with properties: Type: 'Derive' Description: '#8: Set Switch Detection' Keywords: {} Rationale: '' CreatedOn: 20-May-2017 13:14:40 CreatedBy: 'itoy' ModifiedOn: 05-Sep-2024 22:41:49 ModifiedBy: 'batserve' Revision: 5 SID: 3 Comments: [0x0 struct]
Search the link set for all links where MyCheckboxAttribute
is set to true
.
lkArray = find(ls,'MyCheckboxAttribute',true)
lkArray=1×11 Link array with properties:
Type
Description
Keywords
Rationale
CreatedOn
CreatedBy
ModifiedOn
ModifiedBy
Revision
SID
Comments
Search the link set for all links where MyComboboxAttribute
is set to 'Unset'
.
lkArray2 = find(ls,'MyComboboxAttribute','Unset')
lkArray2=1×12 Link array with properties:
Type
Description
Keywords
Rationale
CreatedOn
CreatedBy
ModifiedOn
ModifiedBy
Revision
SID
Comments
Delete Custom Attributes
You can use deleteAttribute
to delete attributes. However, because the custom attributes created in this example are assigned to links, you must set Force
to true
to delete the attributes. Delete MyEditAttribute
and confirm the change.
deleteAttribute(ls,'MyEditAttribute','Force',true); ls.CustomAttributeNames
ans = 1x3 cell
{'MyCheckboxAttribute'} {'MyComboboxAttribute'} {'MyDateTimeAttribute'}
Add a new custom attribute, but don't set any custom attribute values for links.
addAttribute(ls,'NewEditAttribute','Edit'); ls.CustomAttributeNames
ans = 1x4 cell
{'MyCheckboxAttribute'} {'MyComboboxAttribute'} {'MyDateTimeAttribute'} {'NewEditAttribute'}
Because NewEditAttribute
is not used by any links, you can delete it with deleteAttribute
by setting Force
to false
. Confirm the change.
deleteAttribute(ls,'NewEditAttribute','Force',false); ls.CustomAttributeNames
ans = 1x3 cell
{'MyCheckboxAttribute'} {'MyComboboxAttribute'} {'MyDateTimeAttribute'}
See Also
slreq.LinkSet
| addAttribute
| updateAttribute
| inspectAttribute
| deleteAttribute
| getAttribute
| setAttribute