www.gusucode.com > mbcexpr 工具箱 matlab 源码程序 > mbcexpr/@cgslparser/parseBlock.m

    function ptr = parseBlock(obj,b,prevblock,LoopBlocks) 
%parseBlock Main recursive function for parsing Simulink Diagrams
%
% ptr = parseBlock(obj,b,prevblock,LoopBlocks)

%  Copyright 2007-2016 The MathWorks, Inc. and Ford Global Technologies, Inc.

% get block name
blockname=get_param(b,'name');
blockname=fliplr(deblank(fliplr(blockname)));
blockname=strrep(blockname,' ','_');
blockname=strrep(blockname,char(10),'_');
blockname=strrep(blockname,'/','over');
if isempty(blockname)
    blockname = ['empty_',num2str(floor(rand*100))];
end

[ptr,blk] = getPointer(obj,b);
if isnull(ptr) && blk==-1
    % need to add block to list before processing to prevent infinite
    % recursion
    obj.addToList(ptr,b);
    
    BlockInputs= findSrcBlocks(obj,b);
    pInputs = cell( size(BlockInputs) );
    LoopBlocks =[b,LoopBlocks];
    % parse each of the input blocks
    for i = 1:length(BlockInputs)
        % recursive call to parse each of the input blocks
        if any(BlockInputs(i)==LoopBlocks)
            % check whether we are in a loop
            posLoop = find(BlockInputs(i)==LoopBlocks,1,'first');
            % found loop
            % report whole loop 
            currentLoop= LoopBlocks(1:posLoop);
            blknames = [get(currentLoop,{'Parent'}) get(currentLoop,{'Name'})]';
            LoopList = sprintf('  * %s/%s\n',blknames{:});
            % check whether user would like to break the loop
            pInputs{i} = breakLoop(obj,BlockInputs(i),LoopList,currentLoop);
            if isnull(pInputs{i})
                obj.assert(sprintf('Unresolved inputs. The likely cause is a loop defined by:\n%s',LoopList),BlockInputs(i))
            end
                
        else
            % recursive call to process each of the input blocks
            pInputs{i} = parseBlock(obj,BlockInputs(i),get_param(b,'handle'),LoopBlocks);
        end

    end
    
    
    % get parsing function for block
    fParse = parsefunc(obj.SLBlocks,b);
    if isempty(fParse)
        % unknown block type
        ptr = unknown(obj,b,blockname,prevblock);
    else
        % process current block
        ptr = fParse(obj,b,blockname,pInputs);
        
        sys = get(b,'Parent');
        if obj.MultiLevel && ... % switch to enable hierarchy
                ~isequal(sys,get(prevblock,'Parent')) &&... % build a subfeature if previous block is at a different level
                ~strcmp(get(prevblock,'BlockType'),'Outport') && ... % don't build a subsystem for the whole outport
                ~ptr.isa('cgvariable') % don't build a subsystem for a variable
            
            % make a sub feature if necessary
            ptr = createSubFeature(obj,b,ptr);
        end
    end
    
end
    

function pInput = breakLoop(obj,bInput,LoopList,currentLoop)
%breakLoop offer to break Simulink loop

% break loop near outport if possible
sys = get(bInput,'Parent');
outports = find_system(sys,'SearchDepth',1,...
    'LookUnderMasks','on',...
    'BlockType','Outport');
featname='';
for i=1:length(outports)
    port = get_param(outports{i},'Handle');
    src = findSrcBlocks(obj,port);
    if any(src == currentLoop)
        % current block is the source block of this output
        bInput = currentLoop(src == currentLoop);
        bInput = bInput(1);
        featname = get(port,'Name');
        break
    end
end
pInput = mbcpointer(1);
if isempty(featname)
    % can't break loop at subsystem boundary
    featname = get(bInput,'Name');
end

% create new variable previousOutportNane
varname = sprintf('previous_%s',mbcMakeValidName(featname));

% make sure this is a new variable
varname = obj.Project.uniquename(varname);
hilite_system(bInput)
% ask whether user wants to break the loop
resp = questdlg(sprintf('CAGE has detected a loop consisting of:\n%sDo you want to break this loop with an external variable %s?',...
    LoopList,varname),...
    'CAGE Strategy','Yes','No','No');
hilite_system(bInput,'none')
if strcmp(resp,'Yes')
    % add new variable as input
    pInput = addVariable(obj,-1,varname);
end