www.gusucode.com > sldv工具箱matlab源码程序 > sldv/sldv/+Sldv/+xform/@RepMdlInfo/RepMdlInfo.m

    
%   Copyright 2008-2012 The MathWorks, Inc.

classdef RepMdlInfo < Sldv.xform.MdlInfo
    %REPMDLINFO Capture the model properties and blocks for block
    %replacements 
    
    properties (Hidden)
        %LibForConfigSS - Library that includes the replacement blocks for
        %rules that are set to config ss. The library name will be stored to
        %the replacement model as a model parameter.
        LibForConfigSS = ''; 
        
        %SubSystemsToReplace - Cell array of subsystems to replace
        SubSystemsToReplace = {};
        
        %BuiltinBlksToReplace - Cell array of built-in blks to replace
        BuiltinBlksToReplace = {};                         
               
        %CloseLoadedForModelReference - True if the models that are loaded
        %to construct the model reference tree should be closed. Set this
        %attribute to true if sldvcompat or sldvrun requires those models
        %to refer later. 
        CloseLoadedForModelReference = true;       
        
        %DiagnosticsToTurnOff - Cell array of model Diagnostic parameters
        %to turn off. If a parameter is set to error on the root level
        %model reference item, and it is not set to error on any child,
        %then it will be a part of this list. Otherwise, the inlined model
        %may not update because of the diagnostic errors.
        DiagnosticsToTurnOff = {};                
    end
    
    properties (Access = private)
        %BuiltinBlkRepRulesTree - Compound for block replacement rules for
        %built-in block types. 
        BuiltinBlkRepRulesTree = [];
        
        %MdlRefBlkRepRulesTree - Compound for block replacement rules for
        %model reference blocks.
        MdlRefBlkRepRulesTree = [];
        
        %SubSystemRepRulesTree - Compound for block replacement rules for
        %subsystems. 
        SubSystemRepRulesTree = [];         
        
        %BlockTypesActiveRulesBltBlk - Cell array of block types of the 
        %replacement rules for built-in blocks.
        BlockTypesActiveRulesBltBlk = {};
        
        %OriginalDiagnosticsSetToError - Cell array of diagnostic parameters that
        %are set to error on the original root level model.
        OriginalDiagnosticsSetToError = {};
    end
    
    methods
        function obj = RepMdlInfo(objH, RepRules, varargin)
            obj = obj@Sldv.xform.MdlInfo(objH,varargin{:});
            obj.MdlRefBlkRepRulesTree = RepRules(1);
            
            if numel(RepRules) > 1
                obj.SubSystemRepRulesTree = RepRules(2);
                obj.BuiltinBlkRepRulesTree = RepRules(3); 
                obj.genBlockTypesActiveRulesBltBlk;
            end
        end
        
        function delete(obj)
           if ~obj.CloseLoadedForModelReference
               % Parent class constructor will not find a single model to
               % close. 
               obj.MdlsLoadedForMdlRefTree = {};
           end
        end               
    end       
    
    methods (Access = protected)        
        function mdlRefBlkNode = constructMdlRefBlkTreeNode(obj, blockH, parentNode)  
            if nargin<3
                parentNode = [];
            end
            mdlRefBlkNode = Sldv.xform.RepMdlRefBlkTreeNode(blockH);
            if ~isempty(parentNode)                
                mdlRefBlkNode.connectUp(parentNode)
                obj.findDialogParamtersToTurnOff(mdlRefBlkNode);
            else
                obj.OriginalDiagnosticsSetToError = ...
                    Sldv.xform.RepMdlInfo.getDiagnosticsParams(blockH, {'error'});
            end
        end
        
        function subsystemNode = constructSubSystemTreeNode(obj, blockH, ...
                parentNode, genSSCompiledInfo, busObjectList)
            if nargin<5
                busObjectList = [];
            end            
            if nargin<4
                genSSCompiledInfo = false;
            end
            if nargin<3
                parentNode = [];
            end
            subsystemNode = Sldv.xform.RepSubSystemTreeNode(blockH,...
                obj.LibForConfigSS,obj.BuiltinBlkRepRulesTree,genSSCompiledInfo);
            if ~isempty(parentNode)                
                subsystemNode.connectUp(parentNode);
            end
            if genSSCompiledInfo
                obj.constructSubSystemCompIOinfo(subsystemNode,busObjectList);  
            end                 
        end
        
        function constructSubSystemCompIOinfo(obj, blockInfo, busObjectList) 
            if ~isempty(blockInfo.Up)
                blockInfo.constructCompIOInfo(busObjectList);
                blockInfo.checkSampleTimeInheritance;
                isReplaceableInModelStructure = blockInfo.availableForReplacement;
                if isReplaceableInModelStructure || ...
                        blockInfo.ReplacementInfo.UnderSelfModifMaskException
                    obj.SubSystemRepRulesTree.canReplace(blockInfo);
                    if blockInfo.ReplacementInfo.IsReplaceable
                        % Store the model reference block in the FIFO queue of
                        % subsystems to replace
                        obj.SubSystemsToReplace{end+1} = blockInfo;                    
                    end                    
                end
            else
                % Top node is the block diagram. Set it to be replaceable
                blockInfo.ReplacementInfo.IsReplaceable = false;
            end
        end
        
        function constructSubsystemTreeBuiltinBlks(obj, allBlksH, allBlkTypes, systemTable, busObjectList)
            %constructSubsystemTreeBuiltinBlks Generates the information of
            %blocks that belongs (directly under) to a subsystem located in the
            %subsystem tree.            
            blkRepTypesForBltRules = obj.BlockTypesActiveRulesBltBlk;
            if isempty(intersect(blkRepTypesForBltRules,allBlkTypes))
                return;
            end            
            for idx=1:length(allBlksH)
                matchedRule = strmatch(allBlkTypes{idx},blkRepTypesForBltRules,'exact');
                if ~isempty(matchedRule)
                    obj.constructBuiltinBlk(allBlksH(idx), systemTable, busObjectList);
                end
            end
        end
    end
    
    methods (Access = private)        
        function genBlockTypesActiveRulesBltBlk(obj)
            activeRules = obj.BuiltinBlkRepRulesTree.getChildBlkRepRules(true);
            ruleBlockTypes = cell(length(activeRules),1);
            for idx = 1:length(activeRules)
                currentRule = activeRules{idx};  
                ruleBlockTypes{idx} = currentRule.BlockType;  
            end            
            obj.BlockTypesActiveRulesBltBlk = unique(ruleBlockTypes);
        end
        
        function findDialogParamtersToTurnOff(obj, mdlRefBlkNode)
            if ~isempty(obj.OriginalDiagnosticsSetToError)
                paramsReferenced = ...
                    Sldv.xform.RepMdlInfo.getDiagnosticsParams(mdlRefBlkNode.RefMdlName, ...
                    {'none','warning'});
                obj.DiagnosticsToTurnOff  = ...
                    union(obj.DiagnosticsToTurnOff,intersect(paramsReferenced,obj.OriginalDiagnosticsSetToError));
            end
        end        
    end
    
    methods (Access = private, Static) 
        function params = getDiagnosticsParams(model, acceptedValues)
            currConfigSet = getActiveConfigSet(model);
            diagnosticConfig = currConfigSet.getComponent('Diagnostics');
            propsDiagnostic = diagnosticConfig.getPossibleProperties;
            targetPropValues = {'none','warning','error'};
            allindex = zeros(1,length(propsDiagnostic));
            for idx=1:length(propsDiagnostic)
                allowedValues = diagnosticConfig.getPropAllowedValues(propsDiagnostic{idx});
                if length(allowedValues) == length(targetPropValues) && ...
                        all(strcmp(allowedValues',targetPropValues)) && ...
                        any(strcmp(get_param(model, propsDiagnostic{idx}),acceptedValues))
                    allindex(idx) = 1;                
                end
            end
            params = propsDiagnostic(find(allindex)); %#ok<FNDSB>
        end
    end
    
end