以编程方式编写、导入、链接和申述需求
您可以使用 Requirements Toolbox™ API 来以编程方式编写、导入、链接和申述需求。该 API 提供了一种在需求编辑器中以交互方式执行这些操作的替代方法。有关以交互方式编写、导入、链接和申述需求的详细信息,请参阅基于需求来开发和验证 MATLAB 函数。
您可以在需求编辑器中编写、查看和编辑的项(例如需求、链接和申述)还具有等效的对象,您可以使用 Requirements Toolbox API 在 MATLAB® 命令行窗口中以编程方式访问这些对象。
此示例说明如何编写需求,从 Microsoft® Word 中导入额外的需求,然后以编程方式链接和申述需求。您只能在 Microsoft Windows® 平台上运行此示例。
编写需求
使用 slreq.new 函数创建要包含需求的需求集。
myReqSet = slreq.new("MyRequirementSet");该函数以 slreq.ReqSet 对象形式返回需求集对象。通过将 slreq.ReqSet 对象作为输入传递给 add 方法,向需求集添加一个父需求。
parentReq1 = add(myReqSet);
该方法以 slreq.Requirement 对象形式返回需求。向 Summary 和 Description 属性添加文本。使用圆点表示法访问这些属性。
parentReq1.Summary = "Parent Requirement 1"; parentReq1.Description = "This is the first parent requirement in the requirement set.";
向需求集添加第二个父需求。在添加需求时指定 Summary 和 Description 属性。
parentReq2 = add(myReqSet,Summary="Parent Requirement 2", ... Description="This is the second parent requirement in the requirement set.");
通过将 slreq.Requirement 对象作为输入传递给 add 方法,向第一个父需求添加两个子需求。
childReq1 = add(parentReq1,Summary="Child Requirement 1.1"); childReq2 = add(parentReq1,Summary="Child Requirement 1.2");
通过将 slreq.ReqSet 对象作为输入传递给 children 方法,返回需求集的父需求数组。
parentReqs = children(myReqSet)
parentReqs=1×2 Requirement array with properties:
1×1 Requirement 1×1 Requirement
通过将 slreq.Requirement 对象作为输入传递给 children 方法,返回第一个父需求的子需求数组。
childReqs = children(parentReq1)
childReqs=1×2 Requirement array with properties:
1×1 Requirement 1×1 Requirement
保存需求集。
save(myReqSet)
导入需求
需求设定 MyRequirementSpecification.docx 是一个 Microsoft Word 文档,其中包含按基本层次结构排列的六个需求。

使用 slreq.import 函数,将需求作为外部需求的引用(称为引用需求)导入。通过将需求作为引用需求导入,您可以继续在 Microsoft Word 中管理需求。
[count,filePath,myImportedReqSet] = slreq.import("MyRequirementSpecification.docx", ... AsReference=true);
该函数会返回导入的引用需求的数量,需求集的文件路径,以及需求集的 slreq.ReqSet 对象。通过将 slreq.ReqSet 对象作为输入传递给 children 方法,获取需求集的导入节点的句柄。
topRef = children(myImportedReqSet);
该导入节点以 slreq.Reference 对象形式返回。通过将 slreq.Reference 对象作为输入传递给 children 方法,返回需求集的父需求数组。
parentRefs = children(topRef)
parentRefs=1×2 Reference array with properties:
1×1 Reference 1×1 Reference
该方法将导入节点下的引用需求作为 slreq.Reference 对象返回。
打开 Word 文档。
winopen("MyRequirementSpecification.docx")在 Parent Requirement 1 下,将现有文本替换为以下文本:This is the first parent requirement in the requirement set. 保存 Word 文档,然后将其关闭。
检查与导入节点关联的 Word 文档自文档导入以来是否发生了更改。
tf = hasNewUpdate(topRef)
tf = logical
1
更新需求集。
[status,changeList] = updateReferences(myImportedReqSet,topRef)
status = 'Update completed. Refer to Comments on Import1.'
changeList =
'Updated: Parent Requirement 1. Properties: description
'
保存需求集。
save(myImportedReqSet)
创建链接
使用 slreq.createLink 函数,在具有摘要 Parent Requirement 1 的两个需求之间创建链接。
parentRef1 = parentRefs(1); myLink = slreq.createLink(parentRef1,parentReq1);
该函数将链接作为 slreq.Link 对象返回。
当您使用需求编辑器界面创建链接时,Requirements Toolbox 会确定哪个项是源,哪个项是目标。当您使用 slreq.createLink 函数创建链接时,必须指示哪个项是源,哪个项是目标。当您在需求与设计项或测试项之间创建链接时,请将需求设置为目标。Requirements Toolbox 会确定链接类型。
通过将 slreq.Link 对象作为输入传递给 linkSet 方法,返回该链接的链接集对象。
myLinkSet1 = linkSet(myLink);
该函数将链接集对象作为 slreq.LinkSet 对象返回。
保存链接集。
save(myLinkSet1);
申述需求
如果您的需求集中的需求不需要实现或验证,您可以对将它们从实现和验证状态中排除进行申述。
对具有实现摘要 Parent Requirement 1 的需求作出申述。
通过将 slreq.ReqSet 对象作为输入传递给 addJustification 方法,向 MyRequirementSet 需求集添加申述。使用 justifyImplementation 方法,在需求与申述之间创建类型为 Implement 的链接。
jt1 = addJustification(myReqSet); implLink = justifyImplementation(parentReq1,jt1);
addJustification 方法会将申述作为 slreq.Justification 对象返回。当您以编程方式添加申述时,Requirements Toolbox 会将申述存储在父申述下。如果父申述不存在,该软件会创建一个父申述。通过将 slreq.Justification 对象作为输入传递给 parent 方法,返回父申述。
parentJust = parent(jt1);
接下来,对具有验证摘要 Parent Requirement 1 的需求进行申述。
使用 addJustification 方法,向 MyRequirementSet 需求集添加申述。使用 justifyVerification 方法,在需求与申述之间创建类型为 Implement 的链接。
jt2 = addJustification(myReqSet); verifLink = justifyVerification(parentReq1,jt2);
保存需求集。
save(myReqSet)
返回链接的链接集,然后进行保存。
myLinkSet2 = linkSet(verifLink); save(myLinkSet2);
查找加载的 Requirements Toolbox 对象
您可以通过搜索所有加载的对象,或者在需求集或链接集中搜索对象,来查找加载的 Requirements Toolbox 对象。
使用 slreq.find 函数查找加载的需求集。
loadedReqSets = slreq.find(Type="ReqSet")loadedReqSets=1×2 ReqSet array with properties:
1×1 ReqSet 1×1 ReqSet
使用 slreq.find 函数查找加载的需求。然后,通过将 slreq.ReqSet 对象作为输入传递给 find 方法,在 MyRequirementSet 需求集中查找加载的需求。比较结果。
loadedRequirements1 = slreq.find(Type="Requirement")loadedRequirements1=1×4 Requirement array with properties:
1×1 Requirement 1×1 Requirement 1×1 Requirement 1×1 Requirement
loadedRequirements2 = find(myReqSet,Type="Requirement")loadedRequirements2=1×4 Requirement array with properties:
1×1 Requirement 1×1 Requirement 1×1 Requirement 1×1 Requirement
结果是相同的,因为 MyRequirementSet 需求集是包含 slreq.Requirement 对象的唯一加载的需求集。
使用 slreq.find 函数查找加载的链接。然后,通过将 slreq.LinkSet 对象作为输入传递给 find 方法,在第二个链接集中查找加载的链接。比较结果。
loadedLinks1 = slreq.find(Type="Link")loadedLinks1=1×3 Link array with properties:
1×1 Link 1×1 Link 1×1 Link
loadedLinks2 = find(myLinkSet2)
loadedLinks2=1×2 Link array with properties:
1×1 Link 1×1 Link
结果是不同的,因为两个 slreq.LinkSet 对象都包含 slreq.Link 对象。
清除和加载需求集以及链接集
您可以从内存中清除单个需求集。通过将 slreq.ReqSet 对象作为输入传递给 close 方法,关闭 MyRequirementSpecification 需求集。
close(myImportedReqSet)
当您关闭某个需求集时,Requirements Toolbox 也会关闭该需求集的已注册链接集,除非包含该链接集的链接源或目标的另一个工件仍处于已加载状态。搜索仍处于已加载状态的链接集。
loadedLinkSets = slreq.find(Type="LinkSet")loadedLinkSets=1×2 LinkSet array with properties:
1×1 LinkSet 1×1 LinkSet
您无法从内存中清除单个链接集。请用 slreq.clear 函数清除所有加载的需求集和链接集。使用 slreq.find 函数确认已从内存中清除链接集。
slreq.clear
loadedLinkSets = slreq.find(Type="LinkSet")loadedLinkSets =
0×0 LinkSet array with properties:
Description
Filename
Artifact
Domain
Revision
Dirty
CustomAttributeNames
Show all accessible properties of LinkSet
从内存中清除需求和链接后,保留在工作区中的变量将不再可用。使用 clear 清空工作区。
clear
使用 slreq.load 函数加载 MyRequirementSet 需求集。
myReqSet = slreq.load("MyRequirementSet");加载需求集时还会加载需求集的已注册链接集。
loadedLinkSets = slreq.find(Type="LinkSet")loadedLinkSets=1×2 LinkSet array with properties:
1×1 LinkSet 1×1 LinkSet
另请参阅
类
slreq.ReqSet|slreq.Requirement|slreq.Reference|slreq.Justification|slreq.LinkSet|slreq.Link