Main Content

本页采用了机器翻译。点击此处可查看英文原文。

使用 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 = 1x4 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

编辑自定义属性

为链接集定义自定义属性后,您可以对自定义属性进行有限的更改。

MyCheckboxAttributeMyComboboxAttribute 添加描述,并更改 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">...'
        Keywords: {}
       Rationale: ''
       CreatedOn: 27-Feb-2017 10:15:38
       CreatedBy: 'itoy'
      ModifiedBy: 'batserve'
    IndexEnabled: 1
     IndexNumber: []
             SID: 3
    FileRevision: 46
      ModifiedOn: 20-Apr-2024 08:26:52
           Dirty: 1
        Comments: [0x0 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 = 1x3 cell
    {'MyCheckboxAttribute'}    {'MyComboboxAttribute'}    {'MyDateTimeAttribute'}

添加新的自定义属性,但不为需求设置任何需求自定义属性值。

addAttribute(rs,'NewEditAttribute','Edit');
rs.CustomAttributeNames
ans = 1x4 cell
    {'MyCheckboxAttribute'}    {'MyComboboxAttribute'}    {'MyDateTimeAttribute'}    {'NewEditAttribute'}

因为 'NewEditAttribute' 没有被任何需求使用,所以可以通过将 'Force' 设置为 false 来将其与 deleteAttribute 一起删除。确认更改。

deleteAttribute(rs,'NewEditAttribute','Force',false);
rs.CustomAttributeNames
ans = 1x3 cell
    {'MyCheckboxAttribute'}    {'MyComboboxAttribute'}    {'MyDateTimeAttribute'}

清理

清除打开的需求集而不保存更改,并关闭打开的模型而不保存更改。

slreq.clear;
bdclose all;

另请参阅

| | | | | |

相关示例

详细信息