www.gusucode.com > slvnv 源码程序 matlab案例代码 > slvnv/CreateModelMetricsUsingAPIsExample.m

    %% Create Model Metric for Nonvirtual Block Count
% This example shows how to use the metric APIs to create a metric for
% counting nonvirtual blocks in a model.  After creating the metric, the example shows
% how to access and export the results.  
%% Create Metric Class
% Using the |createNewMetricClass| function, create a new metric algorithm class
% named |nonvirtualblockcount|. The function creates file |nonvirtualblockcount.m|
% in the current working folder. The file contains an empty metric algorithm
% method and a constructor implementation.
% For this example, create a working directory.
mkdir C:work
cd C:work

className = 'nonvirtualblockcount'; 
slmetric.metric.createNewMetricClass(className);
%% Create Nonvirtual Block Count Metric
% To create the metric algorithm, open the file that the  
% |createNewMetricClass| function created.  Using the |slmetric.metric.Metric.algorithm| 
% method, add the metric logic to the file.  
% For example, to edit file, use the command edit(className).
%%
% For this example, the file |nonvirtualblockcount_orig.m| contains the logic to
% create a metric that counts the nonvirtual blocks.
copyfile(fullfile(matlabroot,'examples','slvnv','nonvirtualblockcount_orig.m'),...
    'nonvirtualblockcount_orig.m','f');
%%
% Close the files. Copy and save |nonvirtualblockcount_orig.m| to |nonvirtualblockcount.m|.
copyfile('nonvirtualblockcount_orig.m','nonvirtualblockcount.m');
%%
% Register the new metric. For this example, register the nonvirtual block
% count metric.
id_metric = slmetric.metric.registerMetric(className);
%% Collect Metrics
% 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 |sf_car|
% model.
%% 
% Load the |sf_car| model.
model = 'sf_car';
load_system(model);
%%
% Create a metric engine object and set the analysis root.
e = slmetric.Engine();
setAnalysisRoot(e,'Root',model,'RootType','Model');

%%
% Collect metric data for the nonvirtual block count metric.
execute(e);
rc = getMetrics(e,id_metric);
%% Display and Export Results
% To access the metrics for your model, use instances of |slmetric.metric.Result|. 
% In this example, display the nonvirtual block count metrics for the |sf_car| 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
%%
% To export all 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='MyXMLfile.xml';
exportMetrics(e,filename);
%%
% For this example, unregister the nonvirtual block count metric.
slmetric.metric.unregisterMetric(id_metric);
%%
% Close the model sf_car.
clear;
bdclose('all');