Contents > 8 Defining Custom Design Metrics and Rules > 8.5 Expression Terms > 8.5.2 Metric Expressions > 8.5.2.2 Special Operators

8.5.2.2 Special Operators

The following special operators help the navigation between model elements.

1. The "dot" operator

In an expression term, it is sometimes useful to refer to attribute or metric values of related elements. This can be accomplished with the dot operator.

For example, to access the name of an operation's owner, we simply write "context.name". The left hand side expression "context" refers to the owner of the operation, which has to be some model element (e.g., a class). For that model element, the right hand side expression "name" is evaluated, which yields the value of its attribute "name".

In general, evaluating expression a.b for a model element e works as follows:

2. The "upto" operator

If we want to determine the package in which a class resides, we usually access it via its "context" attribute. However, the owner of a class need not be a package. For an inner class, for example, the "context" attribute yields its containing outer class.

The "upto" operator in the expression "context upto (typeof(self)='package')" follows the "context" attribute, across several model elements if necessary, until it encounters a model element of type package.

In general, evaluation of the expression a upto b for a model element e works as follows:

  1. Evaluate metric expression a for model element e. Let e' denote the result of this evaluation.
  2. If e' is not a model element, an empty string is returned as value of a upto b.
  3. Evaluate condition expression b for model element e'.
  4. If the result of b is true, then e' is returned as result of the metric expression a upto b.
  5. Otherwise, set e=e' and repeat from step 1.
3. The "topmost" operator

Similar to the "upto" operator, the "topmost" operator successively evaluates a condition expression for a chain of model elements. Whereas the "upto" operator returns the first element that fulfills the condition expression, the "topmost" operator returns the last element in the chain that fulfills the condition expression.

The "topmost" operator in the expression "context topmost (typeof(self)='package')" yields the top-level package that contains the element.

In general, evaluation of the expression a topmost b for a model element e works as follows:

  1. Set result to be the empty string.
  2. Evaluate metric expression a for model element e. Let e' denote the result of this evaluation.
  3. If e' is not a model element, return result as value of a upto b.
  4. Evaluate condition expression b for model element e'.
  5. If the result of b is true, set result=e'.
  6. Set e=e' and repeat from step 2.