function outData = impcblock_ForwardingTable(inData)
% Use the Forwarding Table to define the mapping from old "mpclib" blocks
% to new blocks.  Any links to old library blocks will be updated when you
% open a model containing links to the library with this Forwarding Table.
% If you select identical old and new library block names and paths, the
% Forwarding Table automatically populates library version numbers.  You
% can optionally define a transformation function to update old links
% parameter data using a MATLAB file on the path. Transforming old links
% parameter data for the new library block enables you to load old links
% and preserve parameter data.    

% Author(s): Rong Chen
% Copyright 2018 The MathWorks, Inc. 

outData.NewBlockPath = ''; % No change in the library block path
outData.NewInstanceData = [];
instanceData = inData.InstanceData;

% Replace "lims_inport" with 4 "umin/umax/ymin/ymax" inports
[HasLims, Index] = ismember('lims_inport',{instanceData.Name});
if HasLims
    LimsValue = instanceData(Index).Value;
    instanceData(end+1).Name = 'umin_inport';
    instanceData(end).Value = LimsValue;
    instanceData(end+1).Name = 'umax_inport';
    instanceData(end).Value = LimsValue;
    instanceData(end+1).Name = 'ymin_inport';
    instanceData(end).Value = LimsValue;
    instanceData(end+1).Name = 'ymax_inport';
    instanceData(end).Value = LimsValue;
    instanceData(Index) = [];
end

% Forward "cost_outport" to "return_cost"
[HasCost, Index] = ismember('cost_outport',{instanceData.Name});
if HasCost
    CostValue = instanceData(Index).Value;
    instanceData(end+1).Name = 'return_cost';
    instanceData(end).Value = CostValue;
    instanceData(Index) = [];
end

% Forward "return_sequence" to "return_mvseq"
[HasSequence, Index] = ismember('return_sequence',{instanceData.Name});
if HasSequence
    SequenceValue = instanceData(Index).Value;
    instanceData(end+1).Name = 'return_mvseq';
    instanceData(end).Value = SequenceValue;
    instanceData(Index) = [];
end

% Remove obsolete parameters
instanceData = removeObsoleteRefMd(instanceData,'ref_from_ws','ref');
instanceData = removeObsoleteParameter(instanceData,'ref_signal_name');
instanceData = removeObsoleteRefMdPreview(instanceData,'ref_preview','ref');
instanceData = removeObsoleteRefMd(instanceData,'md_from_ws','md');
instanceData = removeObsoleteParameter(instanceData,'md_signal_name');
instanceData = removeObsoleteRefMdPreview(instanceData,'md_preview','md');
instanceData = removeObsoleteParameter(instanceData,'from_project');
instanceData = removeObsoleteParameter(instanceData,'project_file');
instanceData = removeObsoleteParameter(instanceData,'project_name');
instanceData = removeObsoleteParameter(instanceData,'task_name');
instanceData = removeObsoleteParameter(instanceData,'is_designfromscratch');

% return
outData.NewInstanceData = instanceData;

function instanceData = removeObsoleteParameter(instanceData,name)
[HasObsolete, Index] = ismember(name,{instanceData.Name});
if HasObsolete
    instanceData(Index) = [];
end

function instanceData = removeObsoleteRefMd(instanceData,name,signal)
[HasObsolete, Index] = ismember(name,{instanceData.Name});
if HasObsolete
    Value = instanceData(Index).Value;
    if strcmp(Value,'on')
        fprintf(ctrlMsgUtils.message('MPC:utility:SignalFromWorkspaceNotSupported',signal));
    end
    instanceData(Index) = [];
end

function instanceData = removeObsoleteRefMdPreview(instanceData,name,signal)
[HasObsolete, Index] = ismember(name,{instanceData.Name});
if HasObsolete
    Value = instanceData(Index).Value;
    if strcmp(Value,'on')
        fprintf(ctrlMsgUtils.message('MPC:utility:PreviewFromWorkspaceNotSupported',signal));
    end
    instanceData(Index) = [];
end
