www.gusucode.com > mbcexpr 工具箱 matlab 源码程序 > mbcexpr/+cgsimfill/@EvalPoints/checkLink.m

    function [ok, msg] = checkLink(V, pVar, pLink)
%CHECKLINK Check if it is ok to make a link.
%
%   [OK, MSG] = CHECKLINK(V, PVAR, PLINK)

%   Copyright 2006-2011 The MathWorks, Inc.

ok = true;
msg = '';

if ~V.AllowLoops
    % pLink cannot depend on the variable we are linking.
    if ismember(pVar,pLink.getinports)
        ok = false;
        msg = sprintf( 'Can''t make link because %s is an input to %s.', pVar.getname, pLink.getname );
        return;
    end
    
    [nExpr, E] = iGetDependentsInfo( V );
    for j=1:nExpr
        % make sure we are not using a circular link
        if ismember(pVar,E(j).Source) && ismember(pLink, E(j).Dependents)
            ok = false;
            msg = sprintf( 'Linking %s to %s is not allowed.', pLink.getname, pVar.getname );
            return;
        end
    end
end
if any(ismember(pLink.getinports,V.LinkedVariables))
    % can't have inputs to defined as a link 
    ok = false;
    UsedLinks=ismember(V.LinkedVariables,pLink.getinports);
    msg = sprintf('The inputs %s of %s are already defined as a link.',...
        mbcListString(pveceval(V.LinkedVariables(UsedLinks),@getname)), pLink.getname );
    return
end
pAllLinks = V.Links;
for i=1:length(pAllLinks)
    %check that pVar is not an input to existing links
    if ismember(pVar,pAllLinks(i).getinports)
        ok = false;
        msg = sprintf('%s cannot be used as a link variable as it is an input to the link expression %s.',pVar.getname,pAllLinks(i).getname);
        return
    end
end


% If we are still going we can attempt to create the link in the
% ValueStore.  We don't allow links to be made to linkinputs.
[isInput, inputType] = pFindInput(V, pVar);
if isInput && ~strcmp( inputType, 'expression' )
    ok = false;
    msg = sprintf( 'Can''t link inputs.' );
end

% -------------------------------------------------------------------------
function [nExpr, E] = iGetDependentsInfo( V )

exprCons = V.Constraint;

% Create a temporary pointer to put the constraint into so we can
% concatenate with the expressions pointers.
tmpConPtr = mbcpointer( size( exprCons ) );
for n = 1:length( tmpConPtr )
    tmpConPtr(n) = xregpointer( [] );
    tmpConPtr(n).info = exprCons(n);
end

pExpr= [V.Expressions, tmpConPtr];
nExpr= length(pExpr);

E= struct('Source',pveceval(pExpr,@getsource),'Dependents',[]);
for j=1:nExpr
    E(j).Dependents= setdiff([pExpr(j) getAllInputs(pExpr(j).info)],E(j).Source);
end

% Need to clean up the tmpConPtr
freeptr( tmpConPtr );