模型度量数据聚合
通过分析汇总的模型度量数据,您可以更好地了解模型及其组件的大小、复杂性和可读性。聚合度量数据可在 slmetric.metric.Result 对象的 AggregatedValue 和 AggregatedMeasures 属性中找到。AggregatedValue 属性聚合度量标量值。AggregatedMeasures 属性聚合度量度量(即有关度量值的详细信息)。
模型度量聚合的工作原理
模型度量的实现定义了度量如何聚合组件层次结构中的数据。对于 MathWorks 模型度量,slmetric.metric.Metric 类定义了模型度量聚合。此类包含 AggregationMode 属性,该属性具有以下选项:
Sum:返回组件层次结构中所有子组件的Value属性和Value属性的总和。返回组件层次结构中所有子组件的Meaures属性和Measures属性的总和。Max:返回组件层次结构中其子组件的Value属性和Value属性的最大值。返回组件层次结构中其子组件的Measures属性和Measures属性的最大值。None:度量值不进行聚合。
您可以在 模型度量 中找到 MathWorks 模型度量及其 AggregationMode 属性设置的描述。对于自定义度量,作为 algorithm 方法的一部分,您可以定义度量如何聚合数据。有关详细信息,请参阅创建非虚拟模块计数的自定义模型度量。
此图显示了软件如何汇总模型层次结构中各个组件的度量数据。父模型位于层级结构的顶端。这些组成部分可以包括以下几类:
模型
Subsystem 模块
图
MATLAB Function 模块
受保护模型

在图中,AggregationMode 是 Sum,层次结构中的模型和组件各有一个 Value 和一个 AggregatedValue。父模型或组件的 AggregatedValue 是其 Value 和每个直接子组件的 AggregatedValue 的总和。例如,在此图中,父模型的 AggregatedValue 为 75。父模型的 AggregatedValue 计算方法是:父模型的 Value、6 加上每个直接子组件的 AggregatedValue、33、17 和 19 的总和。
访问汇总度量数据
本示例展示了如何在度量引擎中以编程方式收集度量数据,然后访问聚合的度量数据。
加载
sldemo_auto_climatecontrol模型。openExample('sldemo_auto_climatecontrol')创建一个
slmetric.Engine对象并设置分析根。metric_engine = slmetric.Engine(); setAnalysisRoot(metric_engine,'Root',... 'sldemo_auto_climatecontrol','RootType','Model');
收集输入输出模型度量的数据。
execute(metric_engine,'mathworks.metrics.IOCount');获取返回一个
slmetric.metric.ResultCollection对象数组res_col的模型度量数据。指定AggregationDepth的输入参量。res_col = getMetrics(metric_engine,'mathworks.metrics.IOCount',... 'AggregationDepth','All');
AggregationDepth输入参量有两个选项:All和None。如果您不希望getMetrics方法聚合度量和值,请指定None。显示结果。
metricData ={'MetricID','ComponentPath','Value',... 'AggregatedValue','Measures','AggregatedMeasures'}; cnt = 1; for n=1:length(res_col) if res_col(n).Status == 0 results = res_col(n).Results; for m=1:length(results) disp(['MetricID: ',results(m).MetricID]); disp([' ComponentPath: ',results(m).ComponentPath]); disp([' Value: ',num2str(results(m).Value)]); disp([' Aggregated Value: ',num2str(results(m).AggregatedValue)]); disp([' Measures: ',num2str(results(m).Measures)]); disp([' Aggregated Measures: ',... num2str(results(m).AggregatedMeasures)]); metricData{cnt+1,1} = results(m).MetricID; metricData{cnt+1,2} = results(m).ComponentPath; metricData{cnt+1,3} = results(m).Value; tdmetricData{cnt+1,4} = results(m).Measures; metricData{cnt+1,5} = results(m).AggregatedMeasures; cnt = cnt + 1; end else disp(['No results for:',res_col(n).MetricID]); end disp(' '); end
结果如下:
MetricID: mathworks.metrics.IOCount ComponentPath: sldemo_auto_climatecontrol Value: 0 Aggregated Value: 9 Measures: 0 0 0 0 Aggregated Measures: 5 4 0 0 MetricID: mathworks.metrics.IOCount ComponentPath: sldemo_auto_climatecontrol/AC Control Value: 6 Aggregated Value: 6 Measures: 5 1 0 0 Aggregated Measures: 5 1 0 0 MetricID: mathworks.metrics.IOCount ComponentPath: sldemo_auto_climatecontrol/External Temperature in Celsius Value: 1 Aggregated Value: 1 Measures: 0 1 0 0 Aggregated Measures: 0 1 0 0 MetricID: mathworks.metrics.IOCount ComponentPath: sldemo_auto_climatecontrol/Heat from occupants Value: 1 Aggregated Value: 1 Measures: 0 1 0 0 Aggregated Measures: 0 1 0 0 MetricID: mathworks.metrics.IOCount ComponentPath: sldemo_auto_climatecontrol/Heater Control Value: 8 Aggregated Value: 8 Measures: 5 3 0 0 Aggregated Measures: 5 3 0 0 MetricID: mathworks.metrics.IOCount ComponentPath: sldemo_auto_climatecontrol/Interior Dynamics Value: 3 Aggregated Value: 3 Measures: 2 1 0 0 Aggregated Measures: 2 1 0 0 MetricID: mathworks.metrics.IOCount ComponentPath: sldemo_auto_climatecontrol/More Info Value: 0 Aggregated Value: 0 Measures: 0 0 0 0 Aggregated Measures: 0 0 0 0 MetricID: mathworks.metrics.IOCount ComponentPath: sldemo_auto_climatecontrol/Subsystem Value: 2 Aggregated Value: 2 Measures: 1 1 0 0 Aggregated Measures: 1 1 0 0 MetricID: mathworks.metrics.IOCount ComponentPath: sldemo_auto_climatecontrol/Subsystem1 Value: 2 Aggregated Value: 2 Measures: 1 1 0 0 Aggregated Measures: 1 1 0 0 MetricID: mathworks.metrics.IOCount ComponentPath: sldemo_auto_climatecontrol/Temperature Control Chart Value: 9 Aggregated Value: 9 Measures: 5 4 0 0 Aggregated Measures: 5 4 0 0 MetricID: mathworks.metrics.IOCount ComponentPath: sldemo_auto_climatecontrol/User Setpoint in Celsius Value: 1 Aggregated Value: 1 Measures: 0 1 0 0 Aggregated Measures: 0 1 0 0
对于输入输出度量,AggregationMode 为 Max。对于每个组件,AggregatedValue 和 AggregatedMeasures 属性是其自身及其子组件的最大输入数和输出数。例如,对于 sldemo_auto_climatecontrol,AggregatedValue 属性为 9,即 sldemo_auto_climatecontrol/Temperature Control Chart 组件值。
另请参阅
slmetric.metric.Metric | slmetric.Engine | slmetric.metric.Result | slmetric.metric.ResultCollection