Contents > 8 Defining Custom Design Metrics and Rules > 8.1 Definition of Metrics > 8.1.1 Projection > 8.1.1.3 Filter Attribute "target"

8.1.1.3 Filter Attribute "target"

To count only elements of a certain type in a relation or set, you can specify the optional "target" attribute:
<metric name="NumOps" domain="class"> 
  <description>The number of operations in the class.</description>
  <projection relation="context" target="operation" />
</metric>
This only counts elements of type "operation" whose "context" is the given class, i.e., the number of operations of the class.

Filtering for several types

If you want to count elements of several types, specify the additional types separated by a "|". For example, to count the classes, interfaces, and data types in a package, we write:

<metric name="NumEl" domain="package"> 
  <projection relation="context" target="class|interface|datatype" />
</metric>
Filtering for subtypes

In SDMetrics' metamodel for UML2, type "class" is the parent type of several subtypes such as "usecase", "actor", "component". The target filter target="class", however, only accepts elements of type "class". Elements whose type is a subtype of type "class" will not be accepted. Thus, the following metric "NumCls" only counts the classes in the package, not including actors, use cases, and so forth:

<metric name="NumCls" domain="package"> 
  <projection relation="context" target="class" />
</metric>
To also accept elements of direct or indirect subtypes of the type, put a "+" prefix in front of the type name:
<metric name="NumClsTypeEl" domain="package"> 
  <projection relation="context" target="+class" />
</metric>
This will count all classes, actors, usecases, components, and other elements in the package whose type is a direct or indirect subtype of "class".

You can combine subtype filtering with filtering for several types. For example, target="+class|interface|+datatype" will accept classes and datatypes as well as their subtypes, and interfaces, but not any subtypes of "interface".