Main Content

getGroupName

Class: slmetric.metric.ResultDetail
Package: slmetric.metric

Obtain the name for a group of slmetric.metric.ResultDetail objects

Description

Obtain the name of a group of slmetric.metric.ResultDetail objects. Calling the execute method collects metric data. Calling getMetrics accesses the slmetric.metric.Result objects which include the slmetric.metric.ResultDetail objects. Apply the getGroupName method to the slmetric.metric.ResultDetail object.

example

groupName = getGroupName(mrd) obtains the name for the slmetric.metric.ResultDetail object mrd.

Input Arguments

expand all

Calling the slmetric.Engine.execute method creates the slmetric.metric.Result objects, which include the slmetric.metric.ResultDetail objects.

Output Arguments

expand all

Name for a group of slmetric.metric.ResultDetail objects

Examples

expand all

Use the getGroupName and getGroupIdentifier methods to obtain the name and identifier for a group of clones.

Open the example model.

open_system([docroot '\toolbox\simulink\examples\ex_clone_detection.slx']);

Save the example model to your current working folder.

Call the execute method. Apply the getMetrics method for the mathworks.metric.CloneDetection metrics.

metric_engine = slmetric.Engine();
setAnalysisRoot(metric_engine,'Root','ex_clone_detection','RootType','Model');
execute(metric_engine);
rc = getMetrics(metric_engine,'mathworks.metrics.CloneDetection');

For each slmetric.metric.Result object, display the ComponentPath. For each slmetric.metric.ResultDetail object, display the clone group name and identifier.

for n=1:length(rc.Results)
    if rc.Results(n).Value > 0
	for m=1:length(rc.Results(n).Details)
	  disp(['ComponentPath: ',rc.Results(n).ComponentPath]);
          disp(['Group Name: ',rc.Results(n).Details(m).getGroupName]);
          disp(['Group Identifier: ',rc.Results(n).Details(m).getGroupIdentifier]);
        end
    else
        disp(['No results for ComponentPath: ',rc.Results(n).ComponentPath]);
    end
    disp(' ');
end

The results show that the model contains one clone group, CloneGroup1, which contains two clones.

Use the setGroup method to group detailed results. When you create a custom model metric, you apply this method as part of the algorithm method.

Using the createNewMetricClass function, create a metric class named DataStoreCount. This metric counts the number of Data Store Read and Data Store Write blocks and groups them together by the corresponding Data Store Memory block. The createNewMetricClass function creates a file, DataStoreCount.m, in the current working folder. The file contains a constructor and empty metric algorithm method. For this example, make sure that you are working in a writable folder.

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

To write the metric algorithm, open the DataStoreCount.m file and add the metric to the file. For this example, you can create the metric algorithm by copying this logic into the DataStoreCount.m file.

classdef DataStoreCount < slmetric.metric.Metric
    % Count the number of Data Store Read and Data Store Write
    % blocks and correlate them across components.
    
    methods
        function this = DataStoreCount()
            this.ID = 'DataStoreCount';
            this.ComponentScope = [Advisor.component.Types.Model, ...
                Advisor.component.Types.SubSystem];
            this.AggregationMode = slmetric.AggregationMode.Sum;
            this.CompileContext = 'None';
            this.Version = 1;
            this.SupportsResultDetails = true;
            
            %Textual information on the metric algorithm
            this.Name = 'Data store usage';
            this.Description = 'Metric that counts the number of Data Store Read and Write'; 
                  'blocks and groups them by the corresponding Data Store Memory block.';
            
        end
        
        function res = algorithm(this, component)
            % Use find_system to get all blocks inside this component.
            dswBlocks = find_system(getPath(component), ...
                'SearchDepth', 1, ...
                'BlockType', 'DataStoreWrite');
            dsrBlocks = find_system(getPath(component), ...
                'SearchDepth', 1, ...
                'BlockType', 'DataStoreRead');          
            
            % Create a ResultDetail object for each data store read and write block.
			% Group ResultDetails by the data store name.
            details1 = slmetric.metric.ResultDetail.empty();
            for i=1:length(dswBlocks)
                details1(i) = slmetric.metric.ResultDetail(getfullname(dswBlocks{i}),...
                            get_param(dswBlocks{i}, 'Name'));
		   groupID = get_param(dswBlocks{i},'DataStoreName');
		   groupName = get_param(dswBlocks{i},'DataStoreName');
                details1(i).setGroup(groupID, groupName);                
                details1(i).Value = 1;
            end
            
            details2 = slmetric.metric.ResultDetail.empty();
            for i=1:length(dsrBlocks)
                details2(i) = slmetric.metric.ResultDetail(getfullname(dsrBlocks{i}),...
                   get_param(dsrBlocks{i}, 'Name'));
                groupID = get_param(dsrBlocks{i},'DataStoreName');
				groupName = get_param(dsrBlocks{i},'DataStoreName');
                details2(i).setGroup(groupID, groupName);
                details2(i).Value = 1;
            end
            
            res = slmetric.metric.Result();
            res.ComponentID = component.ID;
            res.MetricID = this.ID;
            res.Value = length(dswBlocks)+ length(dsrBlocks);
            res.Details = [details1 details2];
        end
    end
end

In the DataStoreCount metric class, the SupportsResultDetail method is set to true. The metric algorithm contains the logic for the setGroup method.

Now that your new model metric is defined in DataStoreCount.m, register the new metric.

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

To collect metric data on models, use instances of slmetric.Engine. Using the getMetrics method, specify the metric that you want to collect. For this example, specify the data store count metric for thesldemo_mdlref_dsm model.

Load the sldemo_mdlref_dsm model.

model = 'sldemo_mdlref_dsm';
load_system(model);

Create a metric engine object and set the analysis root.

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

Collect metric data for the Data Store count metric.

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

For each slmetric.metric.Result object, display the ComponentPath. For each slmetric.metric.ResultDetails object, display the Data Store group name and identifier.

for n=1:length(rc.Results)
    if rc.Results(n).Value > 0
	for m=1:length(rc.Results(n).Details)
	  disp(['ComponentPath: ',rc.Results(n).ComponentPath]);
          disp(['Group Name: ',rc.Results(n).Details(m).getGroupName]);
          disp(['Group Identifier: ',rc.Results(n).Details(m).getGroupIdentifier]);
        end
    else
        disp(['No results for ComponentPath: ',rc.Results(n).ComponentPath]);
    end
    disp(' ');
end

Here are the results.

ComponentPath: sldemo_mdlref_dsm
Group Name: ErrorCond
Group Identifier: ErrorCond
 
No results for ComponentPath: sldemo_mdlref_dsm/More Info1
 
ComponentPath: sldemo_mdlref_dsm_bot
Group Name: RefSignalVal
Group Identifier: RefSignalVal
 
ComponentPath: sldemo_mdlref_dsm_bot2
Group Name: ErrorCond
Group Identifier: ErrorCond
 
ComponentPath: sldemo_mdlref_dsm_bot/PositiveSS
Group Name: RefSignalVal
Group Identifier: RefSignalVal
 
ComponentPath: sldemo_mdlref_dsm_bot/NegativeSS
Group Name: RefSignalVal
Group Identifier: RefSignalVal

For this example, unregister the data store count metric.

slmetric.metric.unregisterMetric(id_metric);

Close the model.

clear;
bdclose('all');
Introduced in R2017b