以编程方式修复断开的链接
在此步骤中,当链接源的指定 ID 不再存在时,您使用 Requirements Toolbox™ 编程接口来修复断开的链接。
检查断开的链接
打开slvnvdemo_powerwindowController_stateflow
模型,它也会加载其关联的链接集。
model = "slvnvdemo_powerwindowController_stateflow";
open_system(model)
该模型与slvnvdemo_powerwindowController
模型类似,但它使用具有逻辑的 Stateflow 图来实现Truth Table
和Truth Table1
模块。
打开需求编辑器。
slreq.editor
点击显示链接即可查看链接。由于缺少 Truth Table 模块,链接集中指向这些模块的四个链接已断开。
修复断开的链接
获取 slvnvdemo_powerwindowController_stateflow
链接集的句柄。将链接集中的链接分配给数组。
ls = slreq.find(Type="LinkSet");
linksArray = getLinks(ls);
通过创建包含断开链接的数组来检查链接集中的每个链接的链接源是否已解析。如果链接源未解析,则将其添加到brokenLinks
数组中。
j = 1; for i = 1:numel(linksArray) link = linksArray(i); if ~link.isResolvedSource brokenLinks(j) = link; j = j+1; end end
打开 slvnvdemo_powerwindowController
模型。
oldModel = "slvnvdemo_powerwindowController";
open_system(oldModel)
对于每个断开的链接,使用链接源 ID 从 slvnvdemo_powerwindowController
模型中获取模型元素名称。
repairedLinks = 0; for i = 1:numel(brokenLinks) link = brokenLinks(i); elementSID = link.source.id; elementHandle = Simulink.ID.getHandle(strcat(oldModel,elementSID)); elementPath = getfullname(elementHandle); elementName = strrep(convertCharsToStrings(elementPath),oldModel+"/","");
在 slvnvdemo_powerwindowController_stateflow
模型中, Truth Table
模块被 Stateflow 图 Chart
替换,而 Truth Table1
模块被 Stateflow 图 Chart1
替换。使用setSource
将源更改为相应的 Stateflow 图。
switch elementName case "Truth Table" newPath = model+"/Chart"; case "Truth Table1" newPath = model+"/Chart1"; end newHandle = getSimulinkBlockHandle(newPath); setSource(link,newHandle); repairedLinks = repairedLinks + 1; end
显示已修复链接的数量。
disp("Fixed "+num2str(repairedLinks)+" links in "+model+".slmx");
Fixed 4 links in slvnvdemo_powerwindowController_stateflow.slmx