The code in this section demonstrates some scenarios with Metric Tool.

(i) Printing all Metric names and their values of all elements under the package “Design” by using the “System Metrics” suite.

#import('metrics', 'com.nomagic.reportwizard.tools.MetricsTool')
#set ($package = $report.findElementInCollection($Package, "Design"))
#set ($metricsResults = $metrics.calculate('System Metrics', $package))
Package $package.name contains the following metrics
#foreach ($metricName in $metricsResults.metricNames)
$metricName value $metricsResults.getValue($package, $metricName)
#end

(ii) Creating a hierarchy of elements of a project and printing all Metric names and their values by using the “System Metrics” suite.

#import('metrics', 'com.nomagic.reportwizard.tools.MetricsTool')
#macro (next $e)
#set ($metricsResults = $metrics.calculate('System Metrics', $e))
Element $e.name
#foreach ($metricName in $metricsResults.metricNames)
$metricName value $metricsResults.getValue($e, $metricName)
#end
#foreach ($child in $e.ownedElement)
#next($child)
#end
#end
#next ($project.model)

(iii) Creating a Metric table for Classes by using the “System Metrics” suite and formatting the result by using two-decimal digits.

#import('metrics', 'com.nomagic.reportwizard.tools.MetricsTool')
#set ($metricsResults = $metrics.calculate('System Metrics', $Class))
#foreach ($cls in $Class)
Class $cls.name
#set ($elementMetric = $metricsResults.getMetrics($cls))
#foreach ($name in $elementMetric.metricNames)
$name : $elementMetric.getValueAsString($name, '##.00')
#end
#end