Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

algorithm

指定度量数据分析的逻辑

说明

指定度量算法分析的逻辑。对于库链接和外部 MATLAB 文件组件,不调用以自定义形式创建的度量算法。

示例

Result = algorithm(Metric,Component) 指定度量算法分析的逻辑。

输入参数

全部展开

为新度量定义的模型度量类。

用于度量分析的 Advisor.component.Component 的实例。

输出参数

全部展开

算法数据,以 slmetric.metric.Result 对象的数组形式返回。

示例

全部展开

此示例说明如何使用 algorithm 方法创建非虚拟模块计数度量。

通过使用 createNewMetricClass 函数,创建一个名为 nonvirtualblockcount 的度量类。该函数在当前工作文件夹中创建 nonvirtualblockcount.m 文件。

className = 'nonvirtualblockcount'; 
slmetric.metric.createNewMetricClass(className);

打开并编辑度量算法文件 nonvirtualblockcount.m。文件包含一个空的度量算法方法。

edit(className);

将以下代码复制并粘贴到 nonvirtualblockcount.m 文件中。保存 nonvirtualblockcount.m。该代码提供用于对非虚拟模块进行计数的度量算法。

classdef nonvirtualblockcount < slmetric.metric.Metric
    % nonvirtualblockcount calculate number of non-virtual blocks per level.
    % BusCreator, BusSelector and BusAssign are treated as non-virtual.
    properties
        VirtualBlockTypes = {'Demux','From','Goto','Ground', ...
            'GotoTagVisiblity','Mux','SignalSpecification', ...
            'Terminator','Inport'};
    end
    
    methods
    function this = nonvirtualblockcount()
        this.ID = 'nonvirtualblockcount';
        this.Version = 1;
        this.CompileContext = 'None';
        this.Description = 'Algorithm that counts nonvirtual blocks per level.';
        this.ComponentScope = [Advisor.component.Types.Model, ...
            Advisor.component.Types.SubSystem];
    end

    function res = algorithm(this, component)
        % create a result object for this component
        res = slmetric.metric.Result();	

        % set the component and metric ID
        res.ComponentID = component.ID;
        res.MetricID = this.ID;

        % use find_system to get all blocks inside this component
        blocks = find_system(getComponentSource(component), ...
            'FollowLinks','on', 'SearchDepth', 1, ...
            'Type', 'Block', ...
            'FollowLinks', 'On');

        isNonVirtual = true(size(blocks));

        for n=1:length(blocks)
            blockType = get_param(blocks{n}, 'BlockType');

            if any(strcmp(this.VirtualBlockTypes, blockType))
                isNonVirtual(n) = false;
            else
                switch blockType
                    case 'SubSystem'
                        % Virtual unless the block is conditionally executed
                        % or the Treat as atomic unit check box is selected.
                        if strcmp(get_param(blocks{n}, 'IsSubSystemVirtual'), ...
                                'on')
                            isNonVirtual(n) = false;
                        end
                    case 'Outport'
                        % Outport: Virtual when the block resides within
                        % any SubSystem block (conditional or not), and 
                        % does not reside in the root (top-level) Simulink window.
                        if component.Type ~= Advisor.component.Types.Model
                            isNonVirtual(n) = false;
                        end
                    case 'Selector'
                        % Virtual only when Number of input dimensions 
                        % specifies 1 and Index Option specifies Select 
                        % all, Index vector (dialog), or Starting index (dialog).
                        nod = get_param(blocks{n}, 'NumberOfDimensions');
                        ios = get_param(blocks{n}, 'IndexOptionArray');

                        ios_settings = {'Assign all', 'Index vector (dialog)', ...
                            'Starting index (dialog)'};

                        if nod == 1 && any(strcmp(ios_settings, ios))
                            isNonVirtual(n) = false;
                        end
                    case 'Trigger'
                        % Virtual when the output port is not present.
                        if strcmp(get_param(blocks{n}, 'ShowOutputPort'), 'off')
                            isNonVirtual(n) = false;
                        end
                    case 'Enable'
                        % Virtual unless connected directly to an Outport block.
                        isNonVirtual(n) = false;

                        if strcmp(get_param(blocks{n}, 'ShowOutputPort'), 'on')
                            pc = get_param(blocks{n}, 'PortConnectivity');

                            if ~isempty(pc.DstBlock) && ...
                                    strcmp(get_param(pc.DstBlock, 'BlockType'), ...
                                    'Outport')
                                isNonVirtual(n) = true;
                            end
                        end
                end
            end
        end

        blocks = blocks(isNonVirtual);

        res.Value = length(blocks);
    end
    end
end

版本历史记录

在 R2016a 中推出