SDMetrics home page
The Software Design Metrics tool for the UML
Sitemap

Counting OCL Expressions in UML Classes

January 9, 2014, Jürgen Wüst. Category: Tips & Tricks

In this post we’ll look at another real-life case study of how to adapt SDMetrics in order to solve a particular measurement problem. An SDMetrics user needed to count the OCL expressions in classes. SDMetrics does not produce this count out of the box, but needs a little tweaking.

In the UML2, an OCL expression is represented by a constraint whose specification is an opaque expression with OCL as its language. The following excerpt from an XMI file shows an OCL expression for a class:

<packagedElement xmi:type="uml:Class" xmi:id="id1" name="Cls1">
  <ownedRule xmi:type="uml:Constraint" xmi:id="id2" name="constr1">
    <specification xmi:type="uml:OpaqueExpression" xmi:id="id3">
      <language>OCL</language>
      <body>self.association().oclIsKindOf(Class)</body>
    </specification>
  </ownedRule>

OCL expressions may not only be defined for the class itself, but also for operations, properties, or other owned elements of the class. All these constraints should be included in the OCL count for the class. Therefore, the general strategy to define this metric in SDMetrics is to count all opaque expressions directly or indirectly owned by the class, where the language is OCL. However, we first need to extend SDMetrics’ XMI import to obtain all the required information from the model.

SDMetrics as shipped does not read the sub-elements of constraints, so the opaque expression elements owned by constraints are ignored by default. To change this, we must set the recurse attribute in the XMI transformation for constraints (cf. bottom of this section in the user manual):

<xmitransformation modelelement="constraint" 
  xmipattern="uml:Constraint" recurse="true"/>

Next, SDMetrics’ simplified UML metamodel does not define the attribute ”language” for opaque expressions. So we simply add it to the metamodel definition file:

<modelelement name="opaqueexpression">
  <attribute name="language" type="data" /> 
</modelelement>

We also have to create an XMI trigger for the new attribute in the XMI transformation file:

<xmitransformation modelelement="opaqueexpression" 
  xmipattern="uml:OpaqueExpression">
    <trigger name="language" 
      type="ctext" src="language" />
</xmitransformation>

This instructs SDMetrics to use the text of the child element “language” in the XMI file as the value for the “language” attribute of opaque expressions.

With these extensions to SDMetrics’ XMI import, we can then define the metric to count all opaque expressions in the class where the language is OCL:

<metric name="OCLExpressions" domain="class" category="Size">
  <description>The number of OCL expressions in the class.</description>
  <subelements target="opaqueexpression" condition="language='OCL'" />
</metric>

And that’s it! Yet another measurement problem for SDMetrics that could be solved with a few lines of XML code. The customization features work as advertised.