Main Content

Programmatically Repair Broken Links

In this step, you use the Requirements Toolbox™ programmatic interface to repair broken links when the specified ID of the link sources no longer exists.

Examine Broken Links

Open the slvnvdemo_powerwindowController_stateflow model, which also loads its associated link set.

model = "slvnvdemo_powerwindowController_stateflow";
open_system(model)

The model is similar to the slvnvdemo_powerwindowController model, but it implements the Truth Table and Truth Table1 blocks by using Stateflow charts that have logic.

stateflow-model.png

Open the Requirements Editor.

slreq.editor

Click Show Links to view the links. Because the Truth Table blocks are missing, the four links in the link set that point to those blocks are broken.

broken-links.png

Repair Broken Links

Get a handle to the slvnvdemo_powerwindowController_stateflow link set. Assign the links from the link set to an array.

ls = slreq.find(Type="LinkSet");
linksArray = getLinks(ls);

Check if the link source is resolved for each link in the link set by creating an array that contains the broken links. If the link source is unresolved, add it to the brokenLinks array.

j = 1;
for i = 1:numel(linksArray)
    link = linksArray(i);
    if ~link.isResolvedSource
        brokenLinks(j) = link;
        j = j+1;
    end
end

Open the slvnvdemo_powerwindowController model.

oldModel = "slvnvdemo_powerwindowController";
open_system(oldModel)

For each broken link, use the link source ID to get the model element name from the slvnvdemo_powerwindowController model.

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+"/","");

In the slvnvdemo_powerwindowController_stateflow model, the Truth Table block was replaced with the Stateflow chart Chart, while the Truth Table1 block was replaced with the Stateflow chart Chart1. Use setSource to change the source to the corresponding Stateflow chart.

    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

Display the number of repaired links.

disp("Fixed "+num2str(repairedLinks)+" links in "+model+".slmx");
Fixed 4 links in slvnvdemo_powerwindowController_stateflow.slmx

See Also

Apps

Functions

Related Topics