%

%   Copyright 2016 The MathWorks, Inc.

function [status, outData]  = extendSLDVData(this)
        
        [mode, showUI, modelH, fileName] = readInputOptions(this);
        runopts = configureModelOptions(mode, modelH, fileName);
        [status, outData] = runSLDVAnalysis(modelH, runopts, showUI);

end

function [mode, showUI, modelH, fileName] = readInputOptions(this)
    fileName = this.sldvData;
    mode = this.extendOpts.mode;
    showUI = this.extendOpts.showUI;
    if ~ishandle(this.modelH)
        modelH = get_param(this.modelH, 'Handle');
    else
        modelH = this.modelH;
    end
end

function runopts = configureModelOptions(mode, modelH, fileName)
    if strcmp(mode,'COV_EXTN_MODE')
        runopts = configureSLDVOptsForCoverageExtension(modelH, fileName);
    else
        runopts = configureSLDVOptsForTestCaseExtension(modelH, fileName);
    end
end

function [status, outData] = runSLDVAnalysis(modelH, runopts, showUI)
        outData = [];
        [status, fileNames] = sldvrun(modelH, runopts, showUI);
        if ~isempty(fileNames.DataFile)
            outData = load(fileNames.DataFile);
        end
end

function runopts = configureSLDVOptsForCoverageExtension (modelH, dataFile)
    designRunopts = sldvoptions(modelH);
    runopts = designRunopts.deepCopy;
    runopts.IgnoreCovSatisfied = 'on';
    runopts.CoverageDataFile = dataFile;
end

function runopts = configureSLDVOptsForTestCaseExtension (modelH, dataFile)
    % Set up options to either extend the existing testcases or ignore objectives
    % already covered by given testcases.
    designRunopts = sldvoptions(modelH);
    runopts = designRunopts.deepCopy;
    runopts.ExtendExistingTests = 'on';
    runopts.ExistingTestFile = dataFile;
end  