Contents > 7 SDMetrics Metamodel and XMI Transformation Files > 7.2 XMI Transformation Files > 7.2.3 Tips on Writing XMI Transformations > 7.2.3.6 Conditional XMI Transformations

7.2.3.6 Conditional XMI Transformations

The XMI transformations we have discussed so far were unconditional: they are used whenever an XMI element matching the transformation's XMI pattern is encountered.

It is possible to make the use of an XMI transformation conditional. You may specify a condition for the attributes of the XMI element that must be fulfilled for a XMI transformation to become effective. With this feature, you can filter certain XMI elements, or you can conditionally map one XMI element onto different metamodel element types, depending on the values of its attributes.

For example, the MagicDraw™ UML modeling tool uses a proprietary XMI extension to encode diagram information as follows:

<mdElement elementClass = 'DiagramData' xmi.id = 'ID2546'>
  <parentID xmi.idref = 'ID0002' />
  <type>Class Diagram</type>
  <mdElement elementClass = 'DiagramView' xmi.id = 'ID2547'>
    <elementID xmi.idref = 'ID2546' />
    <zoomFactor xmi.value = '1.0' />
    <mdOwnedViews>
      <mdElement elementClass = 'ClassView' xmi.id = 'ID2548'>
       <elementID xmi.idref = 'ID00dd' />
       <geometry>165, 16, 358, 60</geometry>
      </mdElement>
      <mdElement elementClass = 'ClassView' xmi.id = 'ID2549'>
       <elementID xmi.idref = 'ID15f2' />
       <geometry>40, 170, 600, 111</geometry>
      </mdElement>
      ...
The XML element mdElement represents both diagrams and diagram elements, indicated by the value of attribute elementClass. The value "DiagramData" denotes a diagram (and we can then extract additional information on diagram type and owner). A value other than "DiagramData" denotes a diagram element (which contains a cross-reference to the UML model element it represents). To define diagram-specific metrics, we must be able distinguish diagrams from diagram elements:
<xmitransformation modelelement="diagram" xmipattern="mdElement" 
                   condition="elementClass='DiagramData'" recurse="true">
  <trigger name="style" type="ctext" src="type" />
  <trigger name="context" type="cattrval" src="parentID" attr="xmi.idref" />
</xmitransformation>

<xmitransformation modelelement="diagramelement" xmipattern="mdElement" 
                   condition="elementClass!='DiagramData'">
  <trigger name="element" type="cattrval" src="elementID" attr="xmi.idref" />
</xmitransformation>
The first xmitransformation for model element "diagram" specifies a condition that the "mdElement" XML element must have an attribute elementClass of value "DiagramData". The second xmitransformation for model element "diagramelement" picks up all mdElement instances with an elementClass different from "DiagramData". As a result, "mdElement" DiagramData elements are stored as "diagram" metamodel elements, all others are stored as instances of "diagramelement".

SDMetrics uses the following strategy when searching a suitable XMI transformation for an XML element in the XMI source file:

The condition you specify is a Boolean expression using operands as described in Section 8.5.4 "Condition Expressions". Identifiers (such as elementClass, see Section 8.5.1.2 "Identifiers") refer to XML attributes. Values of XML attributes can be compared to string constants, which are enclosed in single quotes (see Section 8.5.1.1 "Constants").