function copy_reqinfo(theSource,theDest)
%COPY_REQINFO copies all the requirements info from the source to destination
% COPY_REQINFO(source,destination) copies all the req info from the source
% to destination. This is to be used when copying subsystems from one model
% to another.
%
% For example to copy only the "Airflow calculation" subsystem in
% fuelsys_docreq/fuel rate controller/Airflow calculation
% to
% untitled/Airflow calculation
%
% copy_reqinfo(sprintf('fuelsys_docreq/fuel rate\ncontroller/Airflow calculation'),...
% 'untitled/Airflow calculation');
%
% The code is intended to be robust in that it should handle
% partially copied subsystems too. For example if the Integrator block is
% removed from the destination, all the other requirements should still be
% copied.
%
% Note: The destination block has to be copied separately.
%
%% Find all the objects that could have Req.
modelH=get_param(bdroot(theSource),'Handle');
modelObj = find(slroot, '-isa', 'Simulink.BlockDiagram', '-and', 'Handle', modelH);
slObjs = find(modelObj, '-isa', 'Simulink.BlockDiagram',...
'-or', '-isa', 'Simulink.Block');
sfObjs = find(modelObj, '-isa', 'Stateflow.Transition',...
'-or', '-isa', 'Stateflow.State',...
'-or', '-isa', 'Stateflow.Box',...
'-or', '-isa', 'Stateflow.Function',...
'-or', '-isa', 'Stateflow.EMFunction',...
'-or', '-isa', 'Stateflow.TruthTable',...
'-or', '-isa', 'Stateflow.Chart');
modelHDest=get_param(bdroot(theDest),'Handle');
modelObjDest = find(slroot, '-isa', 'Simulink.BlockDiagram', '-and', 'Handle', modelHDest);
%% Loop on slObjs
for idx=1:length(slObjs)
thisReq=rmi('get',slObjs(idx));
if ~isempty(thisReq)
thisSoP=get(slObjs(idx),'Path');
thisSoN=get(slObjs(idx),'Name');
[st,ed]=regexp(thisSoP,theSource);
if ~isempty(st)
thisDest=[theDest thisSoP(ed+1:end)];
thisDestSo=find(modelObjDest,'Path',thisDest,'-and','Name',thisSoN);
if ~isempty(thisDestSo)
rmi('set',thisDestSo,thisReq);
end
end
end
end
%% Loop on sfObjs
%%*********
showWarn=true; % TODO: Take out when all SF cases tested
%%*********
for idx=1:length(sfObjs)
thisReq=rmi('get',sfObjs(idx));
if ~isempty(thisReq)
thisSoP=get(sfObjs(idx),'Path');
switch class(sfObjs(idx))
case {'Stateflow.State' 'Stateflow.Box' 'Stateflow.Function' ....
'Stateflow.TruthTable' 'Stateflow.EMFunction'} % TODO: Only properly checked Stateflow.State
prop='Name';
case 'Stateflow.Transition' % TODO: Prove that this is a unique property
prop='LabelPosition';
otherwise % TODO: Check if this is still needed.
warning(['Not support ' class(sfObjs(idx)) ' yet'])
% If you get this warnind, find a unique property and code
% for it :-).
continue
end
thisSoN=get(sfObjs(idx),prop);
[st,ed]=regexp(thisSoP,strrep(theSource,char(10),' '));
if ~isempty(st)
%%*********
if showWarn % TODO: Take out when all SF cases tested
warning('Not all cases for Stateflow have been fully tested');
showWarn=false;
end
%%*********
thisDestP=[theDest thisSoP(ed+1:end)];
thisDestP=strrep(thisDestP,char(10),' '); % Replace new lines. TODO: Check if this is always true.
thisDestSo=find(modelObjDest,'Path',thisDestP,'-and',prop,thisSoN);
if length(thisDestSo)>1 % TODO: Check if this is needed.
error('Should never ger here');
end
if ~isempty(thisDestSo)
rmi('set',thisDestSo,thisReq);
end
end
end
end