Cyclomatic complexity status -3 in "*.mdl"-models

3 views (last 30 days)
Dear all,
I have a script, that analyzes lots of Simulink models. One of the things to analyze is the cyclomatic complexity (mathworks.metrics.CyclomaticComplexity). Many of the .mdl-files, I want to analyze won't return results. It seems to work fine with any .slx-file, though.
E.g. I apply the following code with the model https://github.com/motahhir/MATLAB-Simulink-Model-of-Photovoltaic-Cell-Panel-and-Array-/SIMULINK PV CIRCUIT MODEL/pv_model_simulink_R2006b.mdl (The model is compressed in the .rar-file)
metric_engine = param_list.metric_engine;
setAnalysisRoot(metric_engine, 'Root', modelName)
execute(metric_engine, 'mathworks.metrics.CyclomaticComplexity');
res_col = getMetrics(metric_engine, 'mathworks.metrics.CyclomaticComplexity');
disp(res_col)
and it produces:
MetricID: 'mathworks.metrics.CyclomaticComplexity'
Status: -3
Category: 'Uncategorized'
Outdated: 0
Results: [1×0 slmetric.metric.Result]
While for other models like https://github.com/motahhir/MATLAB-Simulink-Model-of-Photovoltaic-Cell-Panel-and-Array-/Mathematical Model/pv_cell_effects_of_temp.mdl it returns a working result with status 0:
MetricID: 'mathworks.metrics.CyclomaticComplexity'
Status: 0
Category: 'Compliant'
Outdated: 0
Results: [1×3 slmetric.metric.Result]
Is there a way to also get a working cyclometic complexity result for these .mdl-files, or is mathworks.metrics.CyclomaticComplexity just buggy?

Answers (1)

Colin Branch
Colin Branch on 25 Nov 2020
Greetings,
I believe the problem encountered is due to errors with the model. I analyzing the models specified using R2020b and initially observed the same result along with some error messages.
Commands executed:
metric_engine = slmetric.Engine()
setAnalysisRoot(metric_engine,'Root','pv_model_simulink_R2006b');
execute(metric_engine, 'mathworks.metrics.CyclomaticComplexity');
res_col = getMetrics(metric_engine, 'mathworks.metrics.CyclomaticComplexity');
disp(res_col)
Output:
Engine with properties:
AnalysisRoot: ''
AnalyzeLibraries: 1
ModelReferencesSimulationMode: 'AllModes'
Warning: Error evaluating parameter 'Resistance' in 'pv_model_simulink_R2006b/Rp*Nss//Npp'
> In slmetric/Engine/execute (line 253)
Warning: Unrecognized function or variable 'Rp'.
> In slmetric/Engine/execute (line 253)
Warning: Variable 'Rp' has been deleted from base workspace.
Suggested Actions:
Undo the deletion. - Fix
Load a file into base workspace. - Fix
Create a new variable. - Fix
> In slmetric/Engine/execute (line 253)
Warning: Error evaluating parameter 'Resistance' in 'pv_model_simulink_R2006b/Rs*Nss//Npp'
> In slmetric/Engine/execute (line 253)
Warning: Unrecognized function or variable 'Rs'.
> In slmetric/Engine/execute (line 253)
Warning: Variable 'Rs' has been deleted from base workspace.
Suggested Actions:
Undo the deletion. - Fix
Load a file into base workspace. - Fix
Create a new variable. - Fix
> In slmetric/Engine/execute (line 253)
Warning: Error evaluating parameter 'slope' in 'pv_model_simulink_R2006b/Test ramp 0 < V < Vocn'
> In slmetric/Engine/execute (line 253)
Warning: Unrecognized function or variable 'Vocn'.
> In slmetric/Engine/execute (line 253)
Warning: Variable 'Vocn' has been deleted from base workspace.
Suggested Actions:
Undo the deletion. - Fix
Load a file into base workspace. - Fix
Create a new variable. - Fix
> In slmetric/Engine/execute (line 253)
ResultCollection with properties:
MetricID: 'mathworks.metrics.CyclomaticComplexity'
Status: -3
Category: 'Uncategorized'
Outdated: 0
Results: [1×0 slmetric.metric.Result]
The error messages indicate that 'pv_model_simulink_R2006b' failed to be processed to be due to undefined variables. Looked at the file structure, included with the model are two possible sources for these variables: MSX60_Specification.m or pv_model_example_(15x2array).mat. Without additional context it it is not clear in what context one would be used or another. Inititializing the workspace with these variables solves the error and allows the metric results to be generated successfully.
metric_engine =
Engine with properties:
AnalysisRoot: ''
AnalyzeLibraries: 1
ModelReferencesSimulationMode: 'AllModes'
ResultCollection with properties:
MetricID: 'mathworks.metrics.CyclomaticComplexity'
Status: 0
Category: 'Compliant'
Outdated: 0
Results: [1×1 slmetric.metric.Result]
One way to ensure that Models have their necessary workspace initialized is to specify the data source for the model's workspace in the model properties. Change Model Workspace Data

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!