Contents > 8 Defining Custom Design Metrics and Rules > 8.1 Definition of Metrics > 8.1.4 Nesting

8.1.4 Nesting

The nesting procedure can be used to calculate the level of recursive nesting of elements. For instance, packages can be nested within other packages, classes can contain inner classes, states can be contained in (composite) states, and so on. With the nesting procedure, you can count the levels of nesting.

The nesting procedure has one required attribute, relation, specifying the relation which establishes the nesting relationship. Usually, this will be the "context" relation. For example:

<metric name="NestingLevel" domain="package">
  <nesting relation="context" />
The calculation procedure is as follows: retrieve the element that is referenced by the specified relation (here: context). If this element is "compatible", recursively calculate the metric for that element, increase by one, and return that value. Else (context element is not of the same type, e.g., top level packages defined in the namespace of the model), the metric yields 0.

An element is "compatible" with nesting metric m of domain type t if the type of the element is t, or if its type is a subtype or supertype of t and metric m is defined for the type.

For a typical package hierarchy, the results are (column NestingLevel):


Table 5: Package nesting metrics example

How does the nesting procedure is differ from projections using the nesting attribute? Projections navigate the specified relation in the opposite direction. A projection operates on all elements where the specified relation points to the current element for which the projection is calculated (navigation to the current element). The nesting procedure, on the other hand, simply navigates from the current element to the element referenced by the specified relation (navigation away from the current element). Consequently, a metric LNest defined as a projection as follows would count the levels of nesting within a package (as shown in column "LNest" of Table 5):

<metric name="LNest" domain="package">
  <description>Levels of package nesting in this package.</description>
  <projection relation="context" target="package" nesting="true" />