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

8.1.1.6 Filter Attribute "scope"

For a detailed analysis of the modularity of a system it is interesting to take the scope of elements into account. For instance, any sufficiently large design will usually be organized in a hierarchy of packages. Dependency relationships or associations between classes in the same package would appear less critical than links between classes from different packages.

With the "scope" attribute, you can filter for elements that fulfill a condition for the scope:

<metric name="SameScope_SupplierClasses" domain="class"> 
  <description>The number of supplier classes of a client 
  class defined in the same scope.</description>
  <projection relation="depclient" target="dependency"
    element="depsupplier" eltype="class" scope="same"/>
</metric>
For a given client class, this projection only accesses supplier classes that are in the same scope as the current class. You can specify one of the following values for the "scope" attribute: Example package hierarchy
Figure 35: Example package hierarchy

In the above cases, we compared the scope of two elements. There are four more values we can specify for scope: idem/notidem, and containedin/notcontainedin. These test if the element for which the metric is calculated is/is not the scope of the projected element.

For example, we want to define a package coupling metric that counts the number of stimuli sent to instances of classes in the package, from instances of classes outside the package (i.e., stimuli that cross the package boundary).

We assume we have already defined for packages the set "StimRecvSet" of stimuli received by object instances of classes in the package. We need to check, for each stimulus in that set, if type of the sender is a class that is not defined in the current package:

<metric name="StimRecv_ex" domain="package">
   <description>The number of stimuli received by object instances 
                 of classes outside the package.</description>
   <projection relset="StimRecvSet" target="stimulus"
     element="stimsender.objtype.context" scope="notidem" />
</metric>
The element attribute accesses the package of the class of the instance that sent the stimuli. The scope attribute value "notidem" only accepts elements that are not identical to the current element for which the metric is calculated. So we only count stimuli where the context of the class of the sender is not the current package - i.e. stimuli sent from outside the package.

To further illustrate the new scope comparisons, consider again the example package hierarchy in Figure 35. The additional values for attribute scope are:

Note that in the definition of metric "StimRecv_ex" above, we could not use the scope value "other" or any of the values explained previously. To see why, take again the example of package1.1: the scope of package1.1 is package1, and it is this package1 against which all comparisons are made! For example, the elements that are in the same scope as package1.1 are Class1 and Class2.