Location: cellLib @ e653d60e211e / Scripts / encapNew.m

Author:
WeiweiAi <wai484@aucklanduni.ac.nz>
Date:
2022-04-13 16:28:32+12:00
Desc:
Add Matlab scripts to build cellML models
Permanent Source URI:
https://models.cellml.org/workspace/6bc/rawfile/e653d60e211e14e8a4b552af1bc280317474fe43/Scripts/encapNew.m

function comp=encapNew(comp,encap,chd,idx)
% get indexes
cmnames=string(extractfield(comp,'name'));%unique
if length(cmnames)>length(unique(cmnames))
    disp('There are repetitions of component names')
    return
end
[~,idxesChd,~]=intersect(cmnames,chd);
[~,idxEncap,~]=intersect(cmnames,encap);
nChd=length(chd);
comp(idxEncap).children=chd';
for i=1:nChd
    idxChd=idxesChd(i);
    %% copy the non-overlapped parameters of the child to the parent
    idxes_encap=find(comp(idxEncap).vars(:,idx.vctg)=='para');
    idxes_chd=comp(idxChd).vars(:,idx.vctg)=='para';
    vars_Encap=comp(idxEncap).vars(idxes_encap,:);
    vars_chd=comp(idxChd).vars(idxes_chd,:);
    if ~(isempty(vars_Encap) && isempty(vars_chd)) % at least one has para
        if isempty(vars_Encap) || isempty(vars_chd)
            varsMatch=[];
            vars_nomatch_chd=vars_chd;
        else %both have para
            [varsMatch,idxMatch_encap,idxMatch_ichd]=intersect(vars_Encap(:,idx.var),vars_chd(:,idx.alias));
            nomatch_chd=setdiff((1:size(vars_chd,1)),idxMatch_ichd);
            vars_nomatch_chd=vars_chd(nomatch_chd,:);
        end
        if (~isempty(varsMatch))
            comp(idxEncap).vars(idxes_encap(idxMatch_encap),idx.priv)='out';
        end
        if ~isempty(vars_nomatch_chd)
            vars_nomatch_chd(:,idx.pub)='in';
            vars_nomatch_chd(:,idx.priv)='out';
            vars_nomatch_chd(:,idx.var)=vars_nomatch_chd(:,idx.alias);
            comp(idxEncap).vars=[vars_nomatch_chd;comp(idxEncap).vars;];%creat
        end
    end
    %% find the outputs of the parent for the inputs of the child
    idxes_encap=find(comp(idxEncap).vars(:,idx.vctg)=='out');
    idxes_chd=comp(idxChd).vars(:,idx.vctg)=='in';
    vars_Encap=comp(idxEncap).vars(idxes_encap,:);
    vars_chd=comp(idxChd).vars(idxes_chd,:);
    if ~(isempty(vars_Encap) && isempty(vars_chd)) % at least one is not empty
        if isempty(vars_Encap) || isempty(vars_chd)
            varsMatch=[];
        else %neither is empty
            [varsMatch,idxMatch_encap,~]=intersect(vars_Encap(:,idx.var),vars_chd(:,idx.alias));
        end
        % connect the outputs of the parent to the inputs of the child
        if (~isempty(varsMatch))
            comp(idxEncap).vars(idxes_encap(idxMatch_encap),idx.priv)='out';
        end
    end
    %% find the inputs of the parent for the outputs of the child
    idxes_encap=find(comp(idxEncap).vars(:,idx.vctg)=='in');
    idxes_chd=comp(idxChd).vars(:,idx.vctg)=='out';
    vars_Encap=comp(idxEncap).vars(idxes_encap',:);
    vars_chd=comp(idxChd).vars(idxes_chd',:);
    if ~(isempty(vars_Encap) && isempty(vars_chd)) % at least one is not empty
        if isempty(vars_Encap) || isempty(vars_chd)
            varsMatch=[];
        else %neither is empty
            [varsMatch,idxMatch_encap,~]=intersect(vars_Encap(:,idx.var),vars_chd(:,idx.alias));
        end
        % connect the inputs of the parent to the outputs of the child
        if (~isempty(varsMatch))
            comp(idxEncap).vars(idxes_encap(idxMatch_encap),idx.priv)='in';
            comp(idxEncap).vars(idxes_encap(idxMatch_encap),idx.pub)='out';
            comp(idxEncap).vars(idxes_encap(idxMatch_encap),idx.vctg)='out';
        end
    end
end
end