Main Content

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

通过编程创建 Requirements Table 模块

自 R2022a 起

您可以通过编程方式创建和管理 Requirements Table 模块。在此示例中,您以编程方式在新模型中创建一个 Requirements Table模块,定义模块数据,添加需求,并访问需求属性。有关如何使用图形界面创建和管理 Requirements Table 模块的信息,请参阅 使用 Requirements Table 模块创建正式需求

创建一个 Requirements Table 模块

您添加的每个 Requirements Table模块都有一个关联的 RequirementsTable 对象。使用 slreq.modeling.create 函数在名为 myNewModel 的新模型中创建一个名为 Requirements Table 的模块,并将该模块分配给名为 tableRequirementsTable 对象。

table = slreq.modeling.create("myNewModel");

如果您有一个现有模型并想要添加 Requirements Table模块,请使用 add_block (Simulink) 函数并使用 slreq.modeling.find 和模块名称检索 RequirementsTable 对象。

add_block("reqmanage/Requirements Table",...
strcat(gcs,"/Requirements Table"));
table = slreq.modeling.find(gcs);

指定 Requirements Table 模块名称和列标题

Requirements Table 模块添加到模型后,使用点符号来修改 NameRequirementHeader属性。当您创建一个新的 Requirements Table模块时,该模块使用默认名称。通过将 Name属性修改为 My Requirements 来重命名该模块:

table.Name = "My Requirements";

您可以通过调整 RequirementHeader属性将预条件后条件操作列添加到模块中。在此示例中,指定模块包含两个先决条件列和一个后条件列:

table.RequirementHeaders.Preconditions = ["input1","input2"];
table.RequirementHeaders.Postconditions = "output";

您可以使用 hideRequirementColumnhideAssumptionColumn 函数隐藏需求选项卡和假设选项卡中不使用的列类型。此示例不使用动作或持续时间。使用 hideRequirementColumn 函数隐藏操作持续时间列:

hideRequirementColumn(table,"Actions");
hideRequirementColumn(table,"Duration");

注意

要删除列,请使用Requirements Table模块图形界面。

定义数据

除了向预条件和后条件添加数据之外,还必须通过定义数据来解析数据。目前,您尚未定义 input1input2output。要以编程方式定义数据,请在 RequirementsTable 对象上使用 addSymbol 函数。addSymbol 允许您调整数据的某些属性,包括数据名称、范围、数据类型和大小。如果想要设置其他属性,请使用图形界面中的符号窗格和属性检查器。有关更多信息,请参阅 在 Requirements Table 模块中定义数据

定义名为input1input2output的数据并指定它们作为输入数据。因为 output 是后条件中使用的唯一数据,所以必须为其启用 isDesignOutput属性。请参阅 isDesignOutput视为设计模型输出进行分析。指定output作为设计模型输出。

addSymbol(table,Name="input1",Scope="Input");
addSymbol(table,Name="input2",Scope="Input");
addSymbol(table,Name="output",Scope="Input",isDesignOutput=1);

Requirements Table模块出现,其中包含一个需求、三列以及列标题中的数据。

This image shows the Requirements Table block after entering the commands described up until this point. The table has one requirement, three columns, and the data are in each of the headers.

要修改数据,请使用 findSymbol 函数检索 Symbol 对象数组并使用点符号。请参阅检索数据并更改它

添加和修改需求

要向模块添加需求,请使用addRequirementRow函数。要添加假设,请使用addAssumptionRow函数。每个函数添加一行。要添加多行,您可以重复输入或使用for循环并在创建后调整属性。

向模块添加需求并指定摘要、预条件和后条件。

addRequirementRow(table,Summary="Requirement 2",...
Preconditions={'> 0',''});

第一个需求是空的。使用点符号来指定现有需求的属性。从表中检索需求作为RequirementRow对象并修改第一个需求。

rrow = getRequirementRows(table);
rrow(1).Summary = "Requirement 1";
rrow(1).Preconditions = {'0',''};
rrow(1).Postconditions = {'> 2'};

您还可以使用 addChild 函数将子项添加到需求或假设中。向第二个需求添加两个子项,以指定预条件和后条件。

addChild(rrow(2),Preconditions={'','> 0'},...
Postconditions={'> 5'});
addChild(rrow(2),Preconditions={'','<= 0'},...
Postconditions={'<= -1'});

要完成需求集,请使用 addRequirementRow 函数并将 rowType 指定为 "default" 来添加默认行。

addRequirementRow(table,rowType="default",...
Postconditions={'< 1'});

输入上述命令后,需求选项卡列出了新的需求:

This image shows the Requirements Table block after executing the programmatic commands shown above. It includes the two top-level requirements, two children requirements of the second requirement, and a default requirement.

如果您关闭了该模块,您可以使用 open_system 函数重新打开它:

open_system("myNewModel/My Requirements");

将需求作为 slreq.Requirement 对象进行管理

向模型添加 Requirements Table模块会为每个模块创建一个 slreq.ReqSet 对象,而创建需求会创建相应的 slreq.Requirement 对象。要从本例中讨论的表中检索对象,请将需求集加载到 Requirements Toolbox™ 中,并使用 slreq.load 函数检索 slreq.ReqSet 对象:

[~,reqSet] = slreq.load("myNewModel.slx");
您还可以在模型上使用 slreq.open,它会加载需求集并在需求编辑器中打开需求集。

检索 slreq.ReqSet 对象后,您可以像其他需求集一样检索属性。使用 find 检索 slreq.Requirement 对象。

requirements = find(reqSet,"Type","Requirement");

如果您只想检索最高层次结构级别的需求,请在 slreq.ReqSet 对象上使用 children 函数。然后,您可以使用每个 slreq.Requirement 对象上的 children 函数来访问每个需求的子需求。

如果 Requirements Table模块处于打开状态,则可以使用 slreq.getCurrentObject 函数检索 slreq.Requirement 对象。要从界面中检索 slreq.Requirement 对象,请选择模块或需求编辑器中的需求并输入 slreq.getCurrentObject

您只能在图形界面中调整需求层次。要添加或删除需求,请使用 addRequirementRowremoveRow

另请参阅

|

相关主题