Create a Custom Model Metric for Nonvirtual Block Count

This example shows how to use the model metric API to create a custom model metric for counting nonvirtual blocks in a model. After creating the metric, you can collect data for the metric, access the results, and export the results.

Create Metric Class

To create a custom model metric, use the slmetric.metric.createNewMetricClass function to create a new metric class derived from the base class slmetric.metric.Metric. The slmetric.metric.createNewMetricClass function creates a file that contains a constructor and an empty metric algorithm method.

1. For this example, make sure that you are in a writeable folder and create a new metric class named nonvirtualblockcount.

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

2. Write the metric algorithm into the slmetric.metric.Metric method, algorithm. The algorithm calculates the metric data specified by the Advisor.component.Component class. The Advisor.component.Types class specifies the types of model objects for which you can calculate metric data. For this example, the file nonvirtualblockcount_orig.m contains the logic to create a metric that counts the nonvirtual blocks. Copy this file to the nonvirtualblockcount.m file.

copyfile nonvirtualblockcount_orig.m nonvirtualblockcount.m f

When creating a custom metric, you must set the following properties of the slmetric.metric.Metric class:

  • ID: Unique metric identifier that retrieves the new metric data.

  • Name: Name of the metric algorithm.

  • ComponentScope: Model components for which the metric is calculated.

  • CompileContext: Compile mode for metric calculation. If your model requires model compilation, specify PostCompile. Collecting metric data for compiled models slows performance.

  • ResultCheckSumCoverage: Specify whether you want the metric data regenerated if source file and Version have not changed.

  • AggregationMode: How the metric algorithm aggregates metric data.

  • AggregateComponentDetails: Returns all detailed results or aggregates detailed results of the component.

Optionally, you can set these additional properties:

  • Description: Description of the metric.

  • Version: Metric version.

3. Now that your new model metric is defined in nonvirtualblockcount.m, you can register the new metric in the metric repository.

[id_metric,err_msg] = slmetric.metric.registerMetric(className);

Collect Metric Data

To collect metric data on models, use instances of slmetric.Engine. Using the getMetrics method, specify the metrics you want to collect. For this example, specify the nonvirtual block count metric for the sldemo_mdlref_bus model.

1. Load the sldemo_mdlref_bus model.

model = 'sldemo_mdlref_bus';
load_system(model);

2. Create a metric engine object and set the analysis root.

metric_engine = slmetric.Engine();
setAnalysisRoot(metric_engine,'Root',model,'RootType','Model');

3. Collect metric data for the nonvirtual block count metric.

execute(metric_engine);
rc = getMetrics(metric_engine,id_metric);

Display and Export Results

To access the metrics for your model, use instance of slmetric.metric.Result. In this example, display the nonvirtual block count metrics for the sldemo_mdlref_bus model. For each result, display the MetricID, ComponentPath, and Value.

for n=1:length(rc)
    if rc(n).Status == 0
        results = rc(n).Results;

        for m=1:length(results)
            disp(['MetricID: ',results(m).MetricID]);
            disp(['  ComponentPath: ', results(m).ComponentPath]);
            disp(['  Value: ', num2str(results(m).Value)]);
            disp(' ');
        end
    else
        disp(['No results for:',rc(n).MetricID]);
    end
    disp(' ');
end
MetricID: nonvirtualblockcount
  ComponentPath: sldemo_mdlref_bus
  Value: 13
 
MetricID: nonvirtualblockcount
  ComponentPath: sldemo_mdlref_bus/More Info3
  Value: 0
 
MetricID: nonvirtualblockcount
  ComponentPath: sldemo_mdlref_bus/More Info4
  Value: 0
 
MetricID: nonvirtualblockcount
  ComponentPath: sldemo_mdlref_bus/More Info1
  Value: 0
 
MetricID: nonvirtualblockcount
  ComponentPath: sldemo_mdlref_bus/More Info2
  Value: 0
 
MetricID: nonvirtualblockcount
  ComponentPath: sldemo_mdlref_counter_bus
  Value: 2
 
MetricID: nonvirtualblockcount
  ComponentPath: sldemo_mdlref_counter_bus/COUNTER
  Value: 6
 
MetricID: nonvirtualblockcount
  ComponentPath: sldemo_mdlref_counter_bus/COUNTER/Counter
  Value: 3
 
MetricID: nonvirtualblockcount
  ComponentPath: sldemo_mdlref_counter_bus/COUNTER/Counter/ResetCheck
  Value: 4
 
MetricID: nonvirtualblockcount
  ComponentPath: sldemo_mdlref_counter_bus/COUNTER/Counter/ResetCheck/NoReset
  Value: 2
 
MetricID: nonvirtualblockcount
  ComponentPath: sldemo_mdlref_counter_bus/COUNTER/Counter/ResetCheck/Reset
  Value: 3
 
MetricID: nonvirtualblockcount
  ComponentPath: sldemo_mdlref_counter_bus/COUNTER/Counter/SaturationCheck
  Value: 5
 
MetricID: nonvirtualblockcount
  ComponentPath: sldemo_mdlref_counter_bus/COUNTER/LimitsProcess
  Value: 1
 
MetricID: nonvirtualblockcount
  ComponentPath: sldemo_mdlref_counter_bus/More Info1
  Value: 0
 
MetricID: nonvirtualblockcount
  ComponentPath: sldemo_mdlref_counter_bus/More Info2
  Value: 0
 
 

To export the metric results to an XML file, use the exportMetrics method. For each metric result, the XML file includes the ComponentID, ComponentPath, MetricID, Value, AggregatedValue, and Measure.

filename='MyMetricData.xml';
exportMetrics(metric_engine,filename);

For this example, unregister the nonvirtual block count metric.

slmetric.metric.unregisterMetric(id_metric);

Close the model.

clear;
bdclose('all');

Limitations

Custom metric algorithms do not support the path property on component objects:

  • Linked Stateflow charts

  • MATLAB Function blocks

Custom metric algorithms do not follow library links.

Copyright 2019 The MathWorks, Inc.

See Also

| | | | |

Related Topics