使用 Requirements Toolbox API 管理需求的自定义属性
此示例显示如何使用 Requirements Toolbox™ API 为需求集创建自定义属性并为需求设置自定义属性值。
建立需求集
加载描述巡航控制系统的需求文件 crs_req_func_spec,并将其分配给变量。
rs = slreq.load('crs_req_func_spec');添加每种类型的自定义属性
将每种类型的自定义属性添加到需求集。创建一个带有描述的 Edit 自定义属性。
addAttribute(rs,'MyEditAttribute','Edit','Description',... 'You can enter text as the custom attribute value.')
创建一个 Checkbox 类型属性并将其 DefaultValue 属性设置为 true。
addAttribute(rs,'MyCheckboxAttribute','Checkbox', ... 'DefaultValue',true)
创建 Combobox 自定义属性。因为第一个选项必须是 'Unset',所以添加选项 'Unset', 'A', 'B', and 'C'。
addAttribute(rs,'MyComboboxAttribute','Combobox', ... 'List',{'Unset','A','B','C'})
创建 DateTime 自定义属性。
addAttribute(rs,'MyDateTimeAttribute','DateTime')
检查需求集定义的自定义属性。获取有关 MyComboboxAttribute 的信息以查看您添加的选项。
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'}
为需求设置自定义属性值
在需求集找到一个需求,然后为您创建的所有四个自定义属性设置自定义属性值。
req = find(rs,'Type','Requirement','SID',3); setAttribute(req,'MyEditAttribute', ... 'Value for edit attribute.'); setAttribute(req,'MyCheckboxAttribute',false); setAttribute(req,'MyComboboxAttribute','B');
使用所需的区域设置设置 MyDateTimeAttribute,以确保在其他区域的系统上以正确的格式设置日期和时间。查看 Locale 以了解更多信息。
localDateTimeStr = datestr(datetime( ... '15-Jul-2018 11:00:00','Locale','en_US'),'Local'); setAttribute(req,'MyDateTimeAttribute', ... localDateTimeStr);
查看属性值。
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
编辑自定义属性
为链接集定义自定义属性后,您可以对自定义属性进行有限的更改。
为 MyCheckboxAttribute 和 MyComboboxAttribute 添加描述,并更改 MyComboboxAttribute 的选项列表。由于您无法更新 Checkbox 属性的默认值,因此您只能更新 MyCheckboxAttribute 的描述。查看更改。
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'}
查找与自定义属性值匹配的需求
在需求集搜索所有将 'MyEditAttribute' 设置为 '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: 'batserve'
IndexEnabled: 1
IndexNumber: []
SID: 3
FileRevision: 46
ModifiedOn: 01-Feb-2025 14:19:10
Dirty: 1
Comments: [0×0 struct]
Index: '1.2'
在需求集搜索所有将 'MyCheckboxAtribute' 设置为 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
在需求集搜索所有将 'MyComboboxAttribute' 设置为 '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
删除自定义属性
您可以使用 deleteAttribute 来删除属性。但是,由于此示例中创建的自定义属性已分配给需求,因此必须将 'Force' 设置为 true 才能删除这些属性。删除 'MyEditAttribute' 并确认更改。
deleteAttribute(rs,'MyEditAttribute', ... 'Force',true); rs.CustomAttributeNames
ans = 1×3 cell
{'MyCheckboxAttribute'} {'MyComboboxAttribute'} {'MyDateTimeAttribute'}
添加新的自定义属性,但不为需求设置任何需求自定义属性值。
addAttribute(rs,'NewEditAttribute','Edit'); rs.CustomAttributeNames
ans = 1×4 cell
{'MyCheckboxAttribute'} {'MyComboboxAttribute'} {'MyDateTimeAttribute'} {'NewEditAttribute'}
因为 'NewEditAttribute' 没有被任何需求使用,所以可以通过将 deleteAttribute 设置为 'Force' 来将其与 false 一起删除。确认更改。
deleteAttribute(rs,'NewEditAttribute', ... 'Force',false); rs.CustomAttributeNames
ans = 1×3 cell
{'MyCheckboxAttribute'} {'MyComboboxAttribute'} {'MyDateTimeAttribute'}
另请参阅
slreq.ReqSet | addAttribute | deleteAttribute | updateAttribute | inspectAttribute | getAttribute | setAttribute