%REFRESHSOURCEARTIFACTPATH update link set created with the legacy Requirements
% Management Interface (RMI) to enable analysis by the digital thread.
%
% refreshSourceArtifactPath(LINKSET) loads the model associated with the passed
%  linkset, updates the link set, and saves changes. LINKSET is a full
%  file path to the .slmx file of the link set.
%

% Copyright 2021-2022 The MathWorks, Inc.
function refreshSourceArtifactPath(linksetFilePath)
    linksetFilePath = convertCharsToStrings(linksetFilePath);

    if exist(linksetFilePath, "file") ~= 2
        error("'%s' doesn''t exist or is no valid link file.", linksetFilePath);
    end

    [~,~,ext]=fileparts(linksetFilePath);
    if ~strcmp(ext, ".slmx")
        error("'%s' doesn''t exist or is no valid link file.", linksetFilePath);
    end

    ls = slreq.find("Type", "LinkSet", "Filename", linksetFilePath);

    if isempty(ls)
        ls = slreq.load(linksetFilePath);
    end

    [~, mdlName, ~] = fileparts(ls.Artifact);

    % make sure link set is closed
    slreq.clear();

    load_system(mdlName);

    ls = slreq.find("Type", "LinkSet", "Filename", linksetFilePath);
    if isempty(ls)
        error("Linkset '%s' is not associated to model '%s'", linksetFilePath, mdlName);
    end

    % temporarily modify link set description and save link set file
    temp = ls.Description;
    ls.Description = "TEMP";
    ls.Description = temp;
    ls.save();

    slreq.clear();
    close_system(mdlName);
end
