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

    function [pEq,NewExpressions,SubFeatureDetails] = parse(obj,sys,block,Feature)
%CGSL2EXPR Initiating function call to parse a Simulink diagram
%
%  [pEq,NewExpressions,SubFeatureDetails] = parse(obj,sys,block,Feature)
%  Inputs:
%    sys     - Handle to starting level of the SL Diagram
%    block   - Handle to an outport of the Simulink Diagram
%    Feature - pointer to feature 
%  Outputs:
%    pEq     - pointer to equation for feature
%    NewExpressions - array of new expression pointers
%    SubFeatureDetails - structure with Feature and NewExpressions pointers for any new sub features. Sub
%                        features are created from subsystems.
%  SubFeatures are only created if the caller requests the third output SubFeatureDetails.

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

% Store the starting point of the parse
% this is used to scope searches for datastores etc

if nargin<3
    f = find_system( sys, 'SearchDepth', 1, 'BlockType', 'Outport' );
    block = get_param(f{1},'handle');
end
if nargin<4
    obj.PointerList = mbcpointer(0,1);
    obj.BlockList = zeros(0,1);
    obj.NewPointers = true(0,1);
else
    % find existing pointers
    obj.Feature = Feature;
    Existing = obj.PointerList( obj.NewPointers );
    NonVars = ~ismember(Existing,listptrs( info(obj.Project.getdd) ));
    Existing = Existing(NonVars);
end

obj.MultiLevel = nargout>2;

if ischar(block)
    try
        block = get_param(block,'handle');
    catch %#ok<CTCH>
        f = find_system( sys, 'SearchDepth', 1, 'Name', block );
        block = get_param(f{1},'handle');
    end
end

obj.StartLevel = get_param(sys,'handle');
set_param(block,'selected','off');

try
    in = findSrcBlocks(obj,block);
catch %#ok<CTCH>
    obj.assert('The outport block must be connected to a single source block',block);
end

pEq = [];
NewExpressions = [];
SubFeatureDetails = [];

if isscalar(in)
    try
        pEq = parseBlock(obj, in, block, [] );
        if ~isnull(pEq)
            % Check for algebraic loops
           AssertLoops(pEq.info);
        else
            obj.assert('No strategy equation detected.',block);
        end
        
        % list of pointers created during parsing
        NewExpressions = obj.PointerList( obj.NewPointers );
        NonVars = ~ismember(NewExpressions,listptrs( info(obj.Project.getdd) ));
        NewExpressions = NewExpressions(NonVars);
        
        [SubFeatureDetails,NewExpressions] = processFeatures(obj,pEq,NewExpressions);
        NewExpressions = intersect( unique([pEq;pEq.getptrsnosf]), NewExpressions);
        
        if nargin>3
            % overwrite userdata pointer with feature when parsing multiple features/ouports
            set(in,'UserData',Feature)
            % update list of pointers/blocks so sub feature is used rather than equation
            b = obj.BlockList(obj.PointerList==pEq);
            obj.BlockList(obj.PointerList==pEq)= -1;
            % add feature to list with block
            for i=1:length(b)
                addToList(obj,Feature,b(i),false)
            end
            
            NewExpressions = setdiff(NewExpressions,Existing);
            if ~isempty(SubFeatureDetails)
                obj.SubFeatures = union(obj.SubFeatures,[SubFeatureDetails.Feature]);
            end
        end
        
    catch ME
        % Free any pointers created during parsing
        cleanup(obj)
        rethrow(ME)
    end
else
    obj.assert('The outport block must be connected to a single source block',block);
end

function [SubFeatureDetails,NewExpressions] = processFeatures(obj,pEq,NewExpressions)
%processFeatures determine details of any subfeatures created during parsing

exprlist = postorder(info(pEq));
exprlist = removeDuplicates(exprlist);
isfeature = parrayeval( exprlist, @(f) isa(f,'cgfeature'), {}, @true);
pFeats = exprlist(isfeature);
% remove features already processed
pFeats( ismember(pFeats,obj.SubFeatures) ) = [];
NewExpressions = setdiff(NewExpressions,pFeats);

SubFeatureDetails = struct('Feature',{},'Equation',{},'NewExpressions',{});
for i=1:length(pFeats)
    ind = pFeats(i)==obj.PointerList;
    if any(ind) && any(obj.NewPointers(ind))
        % feature is new
        pEq = pFeats(i).get('equation');
        subfeat.Feature = pFeats(i);
        subfeat.Equation = pEq;
        subfeat.NewExpressions = intersect( unique([pEq;pEq.getptrsnosf]), NewExpressions);
        NewExpressions = setdiff(NewExpressions,subfeat.NewExpressions);

        SubFeatureDetails = [SubFeatureDetails subfeat]; %#ok<AGROW>
    end
end

function elist = postorder(E)

inp = infoarray(getinputs(E));
elist= cell(1,length(inp));
for i=1:length(inp)
    elist{i}= postorder(inp{i});
end
elist = [elist{:},address(E)];