Contents > 8 Defining Custom Design Metrics and Rules > 8.1 Definition of Metrics > 8.1.1 Projection > 8.1.1.7 Attributes "sum" and "stat"

8.1.1.7 Attributes "sum" and "stat"

You are not restricted to just counting elements in a relation. You can evaluate a metric expression (see Section 8.5.2 "Metric Expressions") for the elements in a relation, and add up the values, or return the maximum or minimum value. For example:
<metric name="NumOps" domain="package"> 
  <projection relation="context" target="class" sum="NumOps" />
</metric>
The expression "NumOps" is evaluated for each class in the package. This metric returns the number of operations in the classes in a package.

To return the maximum or minimum of the values evaluated for the sum expression, specify the additional attribute "stat". The stat attribute can take values "max", "min" or "sum" (default value). For example:

<metric name="MaxNumOps" domain="package"> 
  <description>The maximum number of operations in a class
    of the package.</description>
  <projection relation="context" target="class" 
              sum="NumOps" stat="max" />
</metric>
When you combine "sum" with the "element" and "eltype" attributes, the sum will be taken over the elements returned by those attributes. For example:
<metric name="SupplierOperations" domain="class"> 
  <description>The number of public operations in the supplier
  classes of a client class.</description>
  <projection relation="depclient" target="dependency"
    element="depsupplier" eltype="class" sum="NumPubOps"/>
</metric>