Main Content

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

定义自定义文档界面以直接链接到需求

如果您想要创建指向第三方应用中需求的直接链接,而Requirements Toolbox™并未提供内置支持,那么您可以定义自定义文档接口。要定义自定义文档接口,您需要创建一个函数,该函数指定在创建或导航直接链接时要使用的界面属性和回调函数。有关直接链接的更多信息,请参阅 直接链接到第三方应用程序中的需求

注意

如果您想使用 Requirements Toolbox 分析工具(例如实施状态、验证状态、变更跟踪和可追溯性矩阵),则必须导入第三方需求。有关更多信息,请参阅 导入和直接链接之间的差异。如果 Requirements Toolbox 没有为您的第三方应用程序提供内置导入支持,您可以从 ReqIF™ 文件导入需求,或者您可以定义自定义文档接口来导入这些需求。有关更多信息,请参阅 定义自定义文档接口以导入需求

定义自定义文档界面

要定义并使用自定义文档接口进行直接链接:

  1. 通过在 MATLAB® 命令行执行此代码来打开 myCustomDocInterface.m 文件:

    open(fullfile(matlabroot,"toolbox","slrequirements","slrequirements","linktype_examples","myCustomDocInterface.m"))
    docInterface变量代表文档接口对象。
    function docInterface = myCustomDocInterface 
        docInterface = ReqMgr.LinkType;

  2. 在 MATLAB 路径上保存 myCustomDocInterface.m 的副本。在 MATLAB 的编辑器选项卡中,点击保存 > 将副本另存为。将文件以您想要的名称保存到您想要的位置。

  3. 为文档界面对象定义这些属性:

    名称描述值类型示例值
    Registration需求文档接口名称,指定为字符向量。该属性值必须与文档接口函数名相同。您可以使用mfilename函数来指定该值。

    字符向量

    mfilename
    Label出向链接编辑器中自定义文档类型的标签,指定为字符向量。

    字符向量

    'My Custom Document Interface'
    IsFile

    基于文件的需求文档的指示器,指定为 logical 1 (true) 或 0 (false)。如果您的需求文档是基于文件的,则将值设置为 1;如果您的需求文档不是基于文件的,则将值设置为 0

    Logical 1 (true) or 0 (false)

    1
    Extensions允许基于文件的需求文档的文件类型扩展名,指定为字符向量单元元胞数组。对于非基于文件的需求文档,请将此属性留空。字符向量元胞数组{'.ppt','.pptx'}
    LocDelimeters

    指定需求文档中支持的位置标识符类型的字符,指定为以下一个或多个字符组合的字符数组:

    • ?:用于在文档中搜索文本

    • @:用作命名项,例如需求 ID

    • #:页码或项目编号

    • >:产品编号或行号

    • $:电子表格中的单元格地址

    此属性是可选的。

    包含以下一个或多个字符的字符数组:?@#>$'?@#'
    SelectionLinkLabel用于创建选择的直接链接的上下文菜单标签。如果为 SelectionLinkFcn 定义回调,则此属性是必需的,否则它是可选的。

    字符向量

    'Link to Selection in My Custom Document'

  4. 确定您的文档界面对象需要哪些回调函数。

    回调摘要必需还是可选?函数语法和说明
    NavigateFcn从 MATLAB 或 Simulink® 中的链接项导航到第三方应用中的需求的函数必需NavigateFcn(document,id)在第三方应用中打开document指定的需求文档到id指定的需求。
    ContentsFcn获取第三方应用中需求文档或者需求集合的目录的函数。该函数在“出向链接编辑器”的“文档索引”选项卡中显示目录。可选[labels,depths,ids] = ContentsFcn(document) 以元胞数组形式返回 document 指定的需求文档的需求信息。labels 包含一行需求摘要,ids 包含需求标识符,depths 包含层次结构中需求的级别。
    BrowseFcn当您点击“出向链接编辑器”中的“浏览”按钮时执行的函数。基于文件的需求域不需要此函数。
    • 对于非基于文件的需求文档是可选的,但是如果您未定义此函数,则必须在“出向链接编辑器”中手动输入文档的名称。

    • 如果 IsFile属性设置为 1,则不需要。

    BrowseFcn 打开浏览器查看需求文档。
    SelectionLinkFcn允许从上下文菜单创建直接链接到选择的函数可选。如果定义该函数,则必须定义SelectionLinkLabel属性。

    linkTargetInfo = SelectionLinkFcn(callerObject,make2way) 返回一个包含有关外部需求文档中当前选定需求的信息的结构体。输入的 callerObject 是调用 SelectionLinkFcn 函数的 MATLAB 或 Simulink 对象。该函数获取rmipref("BiDirectionalLinking")的值并将其用作make2way参量。如果 make2waytrue,则调用指示回调函数插入一个反向链接,从外部需求文档中当前选定的需求导航到 MATLAB 或 Simulink 中 callerObject 指定的对象。为了与 R2017b 之前开发的链接到第三方应用中的需求的接口兼容,linkTargetInfo结构体的字段必须是:

    • doc

    • id

    • linked

    • description

    • keywords

    • reqsys

    使用 rmi("createempty") 函数创建具有必填字段的结构体,然后根据自定义文档中的当前选择填写值。当您使用链接从 MATLAB 或 Simulink 导航到外部需求文档中的需求时,Requirements Toolbox 将 docid 字段的值存储为链接目标,并将值传递给 NavigateFcn 回调函数。

    DetailsFcn获取需求内容并将其包含在指定需求的需求报告中的函数可选。不需要创建链接和导航。[depths,info] = DetailsFcn(document,location,level) 从需求文档 document 中返回位置 location 的相关需求内容。该函数获取rmipref("ReportDocDetails")的值并将其用作level参量,以控制返回的需求内容量。例如,您可以选择让此方法在 level 为 0 时仅返回标签,在 level 为 1 时返回前几行,在 level 为 2 时返回全部内容。info 输出以元胞数组的形式返回有关需求的信息。depths 输出返回有关如何在层次结构中显示需求信息的信息。
    CreateURLFcn获取超链接以导航至需求报告中的链接需求的函数可选。不需要创建链接和导航。url = CreateURLFcn(document,documentURL,id) 返回 document 指定的外部需求文档中标识符 id 指定的需求的 URL,或者文档 URL documentURL
    UrlLabelFcn获取需求报告中需求导航 URL 的超链接文本的函数如果为 CreateURLFcn 定义回调,则必填label = UrlLabelFcn(document,id) 返回 document 指定的外部需求文档中标识符 id 指定的需求的标签。
    IsValidDocFcn检查需求文档是否存在的函数。使用此函数进行一致性检查。可选如果 document 指定的外部需求文档存在并且对于文档接口有效,则 tf = IsValidDocFcn(document,~) 返回 true
    IsValidIdFcn检查链接需求的 ID 是否存在于需求文档中的函数。使用此函数进行一致性检查。可选如果标识符 id 指定的需求存在于 document 指定的外部需求文档中,则 tf = IsValidIdFcn(document,id) 返回 true
    IsValidDescFcn检查直接链接存储的 Description属性是否与需求文档中的当前内容匹配的函数。使用此函数进行一致性检查。可选[tf,newDesc] = IsValidDescFcn(document,id,currDesc) 检查链接需求 currDescDescription属性的当前值是否与在 document 指定的外部需求文档中 id 指定的外部需求位置处找到的文本匹配。
    BacklinkCheckFcn检查外部需求文档中是否存在反向链接的函数。有关反向链接的更多信息,请参阅 管理外部需求文档中的导航反向链接可选

    [linkExists,newLinkURL] = BacklinkCheckFcn(mwArtifactName,mwItemId,extDoc,extReq) 检查外部需求文档 extDoc 中的外部需求 extReq 是否有指向工件mwArtifactNamemwItemId 指定的 MATLAB 或 Simulink 中的可链接项的反向链接。

    • 如果反向链接存在,linkExists 将返回 true,而 newLinkURL 将返回空。

    • 如果反向链接不存在,linkExists 将返回 false,而 newLinkURL 将包含要作为反向链接插入的 URL。

    BacklinkInsertFcn在外部需求文档中插入反向链接的函数。有关反向链接的更多信息,请参阅 管理外部需求文档中的导航反向链接如果为 BacklinkCheckFcn 定义回调函数则必填[navCmd,dispText] = BacklinkInsertFcn(extDoc,extReq,mwSourceArtifact,mwItemId,mwDomain) 使用工件文档接口 mwDomain 插入从外部需求文档 extDocextReq 指定的外部需求到 mwSourceArtifact 指定的工件中 mwItemId 指定的 MATLAB 或 Simulink 中的可链接项的反向链接。该函数返回反向链接使用的导航命令 navCmd 以及反向链接显示的超链接文本 dispText
    BacklinksCleanupFcn检查外部需求文档中过时的反向链接的函数。有关反向链接的更多信息,请参阅 管理外部需求文档中的导航反向链接可选,但如果未定义此函数,需求编辑器中的更新反向链接上下文菜单选项将无法检查和删除过时的反向链接
    • [countRemoved,countChecked] = BacklinksCleanupFcn(extDoc,mwSourceArtifact,mwLinksDataMap) 检查外部需求文档 extDoc 中指向 MATLAB 或 Simulink工件mwSourceArtifact 中的可链接项的反向反向链接,并删除 mwLinksDataMap 指定的 Requirements Toolbox 链接映射中没有对应前向链接的反向链接链接。

    • [countRemoved,countChecked] = BacklinksCleanupFcn(extDoc,mwSourceArtifact,mwLinksDataMap,saveBeforeCleanup) 在删除反向链接之前保存需求文档。

    BacklinkDeleteFcn删除外部需求文档中的反向链接的函数。有关反向链接的更多信息,请参阅 管理外部需求文档中的导航反向链接可选success = BacklinkDeleteFcn(extDoc,extReq,mwSourceArtifact,mwItemId) 从外部需求文档 extDoc 中删除 extReq 指定的外部需求的反向链接,该反向链接指向 mwSourceArtifact 指定的工件中 mwItemId 指定的 MATLAB 或 Simulink 中的可链接项。
    GetResultFcn当您链接到外部测试用例或测试结果时,获取验证状态的测试结果的函数。使用此函数来验证具有 Confirm 链接类型的出向链接的需求。可选

    result = GetResultFcn(link) 返回 link 链接到的测试用例或测试结果的验证状态结果。result 输出是一个包含以下字段的结构体:

    • status(必填):验证状态(PassedFailedUnexecuted

    • timestamp(可选):最近验证状态的日期和时间。值必须是 datetime 对象。

  5. 将文档界面所需的回调函数编写为主函数中的本地函数。注释掉或者删除不需要的回调函数。

  6. myCustomDocInterface.m将回调函数句柄分配给自定义文档接口对象的相应属性。如果有您未使用或删除的回调函数,请注释掉或删除将这些回调函数句柄分配给自定义文档接口对象属性的行。

  7. 使用rmi函数注册自定义文档接口。指定自定义文档接口名称作为第二个参量。

    rmi register myCustomDocInterface

您可以使用rmi函数取消注册自定义文档接口。

rmi unregister myCustomDocInterface

提示

文档接口定义的例子可以查看fullfile(matlabroot,"toolbox","slrequirements","slrequirements","linktype_examples")目录下内置的文档接口定义:

  • linktype_rmi_doors.m

  • linktype_rmi_excel.m

  • linktype_rmi_html.m

  • linktype_rmi_text.m

链接到 ABC 文件中的需求

此示例显示如何定义自定义文档接口,以便直接链接到扩展名为 .abc 的文本文件中的需求。

定义并注册自定义文档接口

abcInterface 函数定义一个自定义文档接口,可以链接到扩展名为 .abc 的文本文件。

type abcInterface
function docInterface = abcInterface

% Define custom document interface
docInterface = ReqMgr.LinkType;

% Define custom document interface properties
docInterface.Registration = mfilename;
docInterface.Label = 'ABC  file (for demonstration)';
docInterface.IsFile = 1;
docInterface.Extensions = {'.abc'};
docInterface.LocDelimiters = '>@';
% docInterface.Version = '';             % not required

% Assign custom document interface callback functions
docInterface.NavigateFcn = @NavigateFcn;
docInterface.ContentsFcn = @ContentsFcn;
end

%% Define callback functions
function NavigateFcn(filename,locationStr)
if ~isempty(locationStr)
    findId=0;
    switch(locationStr(1))
        case '>'
            lineNum = str2num(locationStr(2:end));
            openFileToLine(filename, lineNum);
        case '@'
            openFileToItem(filename,locationStr(2:end));
        otherwise
            openFileToLine(filename, 1);
    end
end
end

function openFileToLine(fileName, lineNum)
if lineNum > 0
    if matlab.desktop.editor.isEditorAvailable
        matlab.desktop.editor.openAndGoToLine(fileName, lineNum);
    end
else
    edit(fileName);
end
end

function openFileToItem(fileName, itemName)
reqStr = ['Requirement:: "' itemName '"'];
lineNum = 0;
fid = fopen(fileName);
i   = 1;
while lineNum == 0
    lineStr = fgetl(fid);
    if ~isempty(strfind(lineStr, reqStr))
        lineNum = i;
    end;
    if ~ischar(lineStr), break, end;
    i = i + 1;
end;
fclose(fid);
openFileToLine(fileName, lineNum);
end

function [labels, depths, locations] = ContentsFcn(filePath)
% Read the entire file into a variable
fid = fopen(filePath,'r');
contents = char(fread(fid)');
fclose(fid);

% Find all ok, reviewed by ngabriel 13a  the requirement items
fList1 = regexpi(contents,'\nRequirement:: "(.*?)"','tokens');

% Combine and sort the list
items = [fList1{:}]';
items = sort(items);
items = strcat('@',items);

if (~iscell(items) && length(items)>0)
    locations = {items};
    labels = {items};
else
    locations = [items];
    labels = [items];
end

depths = [];
end
%  Copyright 2024 The MathWorks, Inc.

使用rmi函数注册abcInterface

rmi register abcInterface

将需求直接链接到 Simulink 模块

AutopilotRequirements.abc 文件包含 aero_dap3dof Simulink®模型的自动驾驶仪系统的需求。

type AutopilotRequirements.abc
Requirement:: "Altitude Climb Control"

Altitude climb control is entered whenever:
|Actual Altitude- Desired Altitude | > 1500 

Units:
Actual Altitude - feet
Desired Altitude - feet

Description:

When the autopilot is in altitude climb 
control mode, the controller maintains a 
constant user-selectable target climb rate.  

The user-selectable climb rate is always a 
positive number if the current altitude is 
above the target altitude.  The actual target 
climb rate is the negative of the user 
setting. 

End of "Altitude Climb Control">


Requirement:: "Altitude Hold"

Altitude hold mode is entered whenever:
|Actual Altitude- Desired Altitude | < 
  30*Sample Period*(Pilot Climb Rate / 60) 

Units:
Actual Altitude - feet
Desired Altitude - feet
Sample Period - seconds
Pilot Climb Rate - feet/minute

Description:

The transition from climb mode to altitude 
hold is based on a threshold that is 
proportional to the Pilot Climb Rate.  

At higher climb rates the transition occurs 
sooner to prevent excessive overshoot. 

End of "Altitude Hold"


Requirement:: "Autopilot Disable"

Altitude hold control and altitude climb 
control are disabled when autopilot enable 
is false.

Description:

Both control modes of the autopilot 
can be disabled with a pilot setting.

ENd of "Autopilot Disable"


Requirement:: "Glide Slope Armed"

Glide Slope Control is armed when Glide 
Slope Enable and Glide Slope Signal 
are both true. 

Units:
Glide Slope Enable - Logical
Glide Slope Signal - Logical

Description:

ILS Glide Slope Control of altitude is only 
enabled when the pilot has enabled this mode 
and the Glide Slope Signal is true.  This indicates 
the Glide Slope broadcast signal has been 
validated by the on board receiver. 

End of "Glide Slope Armed"


Requirement:: "Glide Slope Coupled"

Glide Slope control becomes coupled when the control 
is armed and (Glide Slope Angle Error > 0) and 
 Distance < 10000 

Units:
Glide Slope Angle Error - Logical
Distance - feet

Description:

When the autopilot is in altitude climb control 
mode the controller maintains a constant user 
selectable target climb rate.  

The user-selectable climb rate is always a positive 
number if the current altitude is above the target 
altitude the actual target climb rate is the 
negative of the user setting. 

End of "Glide Slope Coupled"

打开aero_dap3dof模型。

open_system("aero_dap3dof.slx")

关闭阿波罗登月舱下降动画窗口。在 aero_dap3dof模型中,右键点击 Reaction Jet Control 子系统并选择需求 > 打开出向链接对话框。出向链接编辑器打开。点击新建来添加新的需求链接。

文档类型设置为 ABC file (for demonstration)

文档旁边,点击浏览并选择 AutopilotRequirements.abc 文件。选择文档索引选项卡并双击 @Altitude Hold。通过点击确定创建链接。验证链接是否按预期工作,方法:右键点击 Reaction Jet Control 子系统并选择 Requirements > 1. “高度保持”

取消注册自定义文档接口。

rmi unregister abcInterface

相关主题