SDMetrics advertizes itself as “The Software Design Metrics tool for the UML”. SDMetrics has a flexible XMI parser to import UML models, as XMI is the most common UML model interchange format. But XMI is not limited to UML model interchange. It can be used to serialize any MOF-based models to XML. Therefore, SDMetrics too is actually not restricted to analyzing UML models. With its customizable XMI import and user-definable metrics, we can use SDMetrics to measure structural properties for any MOF-based models. That’s what we’ll do in this post. All we’ll need is SDMetrics (the Demo Version will do), and a text editor to create some XML files.
KDM – the Knowledge Discovery Meta-model
To stay within the realm of software measurement, we’ll analyze KDM models. KDM – the Knowledge Discovery Meta-model – is a MOF-based meta-model for representing information related to existing software, its elements, associations, and operational environments. It is an OMG standard, and a core part of the OMG’s Architecture-driven Modernization initiative. You can find out more about KDM here or here.
To get an idea what a KDM model looks like, here’s some sample Java code and an excerpt of a XMI file of the KDM model for that code. You can find the full example in Section 12.8.2 of the current KDM 1.3 standard, or here.
package flip; interface IFlip { public int flip(int i); } public class Foo implements IFlip { public int flip(int i) { return i * -1; } }
<?xml version="1.0" encoding="UTF-8"?> <kdm:Segment xmi:version="2.1" xmlns:xmi="http://www.omg.org/XMI" xmlns:action="http://kdm.omg.org/action" xmlns:code="http://kdm.omg.org/code" xmlns:kdm="http://kdm.omg.org/kdm" name="Interface Example"> <model xmi:id="id.0" xmi:type="code:CodeModel"> <codeElement xmi:id="id.1" xmi:type="code:Package" name="flip"> <codeElement xmi:id="id.2" xmi:type="code:ClassUnit" name="Foo"> <codeRelation xmi:id="id.3" xmi:type="code:Implements" to="id.21" from="id.2"/> <codeElement xmi:id="id.4" xmi:type="code:MethodUnit" name="flip" type="id.23"> <codeRelation xmi:id="id.5" xmi:type="code:ImplementationOf" to="id.22" from="id.4"/> <entryFlow xmi:id="id.6" to="id.10" from="id.4"/> <codeElement xmi:id="id.7" xmi:type="code:Signature" name="flip"> <parameterUnit xmi:id="id.8" name="i" type="id.53"/> <parameterUnit xmi:id="id.9" type="id.53" kind="return"/> </codeElement> <codeElement xmi:id="id.10" xmi:type="action:ActionElement" name="d1" kind="Multiply"> <codeElement xmi:id="id.11" xmi:type="code:Value" name="-1" type="id.53"/> <codeElement xmi:id="id.12" xmi:type="code:StorableUnit" name="t5" type="id.53" kind="register"/> <actionRelation xmi:id="id.13" xmi:type="action:Reads" to="id.8" from="id.10"/> <actionRelation xmi:id="id.14" xmi:type="action:Reads" to="id.11" from="id.10"/> <actionRelation xmi:id="id.15" xmi:type="action:Writes" to="id.12" from="id.10"/> <actionRelation xmi:id="id.16" xmi:type="action:Flow" to="id.17" from="id.10"/> </codeElement> ...
We have previously seen how to extend SDMetrics’ simplified UML meta-model, or how to define metrics for UML profiles. The basic steps always are the same:
- add new UML meta-model elements
- provide XMI transformations to extract the new data from the XMI file
- define the metrics and or design rules for the new model elements.
To prepare SDMetrics for KDM models, we follow the same three steps. The only difference is that instead of adding to an existing UML meta-model, we have to start from scratch.
Define the KDM meta-model for SDMetrics
SDMetrics uses a simplified meta-model for UML that does not cover the entire UML standard in every last detail. It just contains the parts that are relevant to the metrics and rules it checks.
Likewise, our simplified KDM meta-model for SDMetrics will just contain a few KDM meta-types we’re interested in. The KDM model above has elements such as packages, classes (called class units), methods (called method units) with signatures, and so on. For these elements, we create a meta-model definition file, like so:
<?xml version="1.0"?> <!DOCTYPE sdmetricsmetamodel SYSTEM 'metamodel.dtd'> <sdmetricsmetamodel version="2.3"> ... <modelelement name="package" /> <modelelement name="classunit" /> <modelelement name="methodunit"> <attribute name="kind">The kind of method.</attribute> </modelelement> ...
For for space reasons, I’m only showing an excerpt of the meta-model definition file here. I have prepared a Zip archive for download (just 3KB) that contains the entire meta-model definition file (file kdmmeta.xml) for about 20 KDM meta-types.
Provide XMI transformations
XMI transformations instruct SDMetrics what information to extract from the XMI file. Essentially, this is a mapping from the “official” KDM meta-model to our simplified version. Writing the XMI transformation file is straightforward: just create an XMI transformation for every meta-model element, and XMI triggers for every attribute.
The XMI transformations for the meta-model elements of the excerpt above look like this:
<?xml version="1.0"?> <!DOCTYPE xmitransformations SYSTEM 'xmitrans.dtd'> <xmitransformations version="2.3"> ... <xmitransformation modelelement="package" xmipattern="code:Package" recurse="true" /> <xmitransformation modelelement="classunit" xmipattern="code:ClassUnit" recurse="true" /> <xmitransformation modelelement="methodunit" xmipattern="code:MethodUnit" recurse="true"> <trigger name="kind" type="attrval" attr="kind" /> </xmitransformation> ...
Again, you can find the entire set of XMI transformations in file kdmxmi.xml in the Zip archive.
Define metrics for KDM
Now we’re ready to define KDM metrics with the SDMetricsML. As a simple example, this metric counts the number of method units in a class unit:
<metric name="NumMethods" domain="classunit" category="Size"> <description>The number of method units in the class unit.</description> <projection relation="context" target="methodunit" /> </metric>
The file kdmmetrics.xml in the Zip archive contains this and a few more example metrics. If you want to try them out for yourself, unpack the Zip archive, and register the XML and XMI files with SDMetrics. Just drag and drop the four extracted files somewhere into SDMetrics window or the project file settings dialog, SDMetrics will figure out what to do with them. The project file settings should look something like this:
Then close the dialog, hit the “Calculate” button, et voilà, metrics for KDM models, calculated by SDMetrics.