Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Web Development

A Web Service for Calculating the Metrics of UML Class Diagrams

Software developers are always thankful for tools that do not interfere too much with their work habits. Software metrics can offer some feedback on the quality and results of design decisions, and help developers move towards an engineering approach to software. Metrics are a useful resource to help determine whether software meets desired quality characteristics. Although many types of metrics have been devised, design metrics for object orientation have reached one of the highest levels of maturity because their relationship to lower maintenance efforts, lower defect rates, and other benefits has been demonstrated repeatedly in studies. As such, basic design concepts such as encapsulation, inheritance, polymorphism, and class complexity are known indicators of software quality.

As UML becomes a de facto standard notation for software documentation, effective design metrics increasingly must work with UML diagrams as input. Some tools have the ability to calculate design metrics from UML diagrams, but this activity can benefit from relying on Web services created with an open-source solutions to calculate the metrics. The Web service we created uses SOAP messages, typically conveyed using HTTP, with XML serialization. Using this technique, a Web service that implements the calculation of UML metrics can be inserted in any development environment and serve as a source of early feedback to architects and developers.

XMI and UML diagrams

Most UML modeling tools are capable of importing and exporting models or diagrams (see a sample UML class diagram in Figure 1) using the XMI format. XMI is an XML dialect that includes tags to represent UML diagrams and their components. Listing One presents a sample XMI code extract that corresponds to the class diagram in Figure 1 (exported as an Example.xmi file).

Figure 1: Class diagram example.

The tag <packagedElement> with the attribute type=Class is used to represent classes, while <ownedOperation>, <ownedAttribute>, and <generalization> represent attributes, operations, and inherence associations. This example is pretty good because UML class diagrams are most-oftent used as documentation and they are totally equivalent to the structure of code.

<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmi:version="2.1" xmlns:uml="http://schema.omg.org/spec/UML/2.1"  
 <uml:Model xmi:type="uml:Model" xmi:id="themodel" name="example">
  <packagedElement xmi:type="uml:Package" xmi:id="example" name ="example">
  <packagedElement xmi:type="uml:Class" name="Person" xmi:id="person" visibility="public">
   <ownedAttribute xmi:type="uml:Property" name="Name" xmi:id="name" visibility="private">
    <type xmi:type="uml:PrimitiveType"href="http://schema.omg.org/spec/UML/2.1/uml.xml#String"/>
   <ownedAttribute xmi:type="uml:Property" name="Age" xmi:id="age" visibility="private">
    <type xmi:type="uml:PrimitiveType" href="http://schema.omg.org/spec/UML/2.1/uml.xml#Integer"/>
   <ownedOperation xmi:type="uml:Operation" name="SetName" xmi:id="setname" visibility="public" 
    <ownedParameter name="return" xmi:id="return_1" direction="return">
     <type xmi:type="uml:Class" xmi:idref="datatype_0"/>
    <ownedParameter xmi:type="uml:Parameter" name="Name" xmi:id="op_param_1" direction="in">
     <type xmi:type="uml:PrimitiveType" href="http://schema.omg.org/spec/UML/2.1/uml.xml#String"/>
   <ownedOperation xmi:type="uml:Operation" name="GetName" xmi:id="getname" visibility="public" 
    <ownedParameter name="return" xmi:id="return_2" direction="return">
     <type xmi:type="uml:PrimitiveType" href="http://schema.omg.org/spec/UML/2.1/uml.xml#String"/>
  <packagedElement xmi:type="uml:Class" name="Driver" xmi:id="driver" visibility="public">
   <generalization xmi:type="uml:Generalization" xmi:id="gene_person" general="person"/>
   <ownedAttribute xmi:type="uml:Property" name="LicenceDate" xmi:id="licencedate" 
    <type xmi:type="uml:PrimitiveType" href="http://schema.omg.org/spec/UML/2.1/uml.xml#String"/>
   <ownedAttribute xmi:type="uml:Property" name="MyCars" xmi:id="attrib_mycars" 
    visibility="private" association="mycars" aggregation="none">
    <type xmi:type="uml:Class" xmi:idref="car"/>
     <lowerValue xmi:type="uml:LiteralString" xmi:id="multi_lowmultipli" value="*"/>
     <upperValue xmi:type="uml:LiteralString" xmi:id="multi_up" value="*"/>
   <ownedOperation xmi:type="uml:Operation" name="ObtainLicence" xmi:id="obtainlicence" 
    visibility="public" isAbstract="false">
    <ownedParameter name="return" xmi:id="return_3" direction="return">
     <type xmi:type="uml:PrimitiveType" 
    <ownedParameter xmi:type="uml:Parameter" name="date" xmi:id="param_date" direction="in">
     <type xmi:type="uml:PrimitiveType" href="http://schema.omg.org/spec/UML/2.1/uml.xml#String"/>
    <ownedOperation xmi:type="uml:Operation" name="BuyCar" xmi:id="buycar" visibility="public" 
     <ownedParameter xmi:type="uml:Parameter" name="newCar" xmi:id="param_newcar" direction="in">
      <type xmi:type="uml:Class" xmi:idref="car"/>
   <packagedElement xmi:type="uml:Class" name="Car" xmi:id="car" visibility="package">
    <ownedAttribute xmi:type="uml:Property" name="Model" xmi:id="model" visibility="private">
     <type xmi:type="uml:PrimitiveType" href="http://schema.omg.org/spec/UML/2.1/uml.xml#String"/>
    <ownedAttribute xmi:type="uml:Property" name="Price" xmi:id="price" visibility="private">
     <type xmi:type="uml:Class" xmi:idref="datatype_1"/>
   <packagedElement xmi:type="uml:Association" xmi:id="mycars" visibility="private">
    <memberEnd xmi:idref="attrib_mycars"/>
    <ownedEnd xmi:type="uml:Property" xmi:id="end_mycars" association="mycars" 
     visibility="private" type="driver" aggregation="none" isNavigable="false"/>
    <memberEnd  xmi:idref="end_mycars"/>
   <packagedElement xmi:type="uml:Dependency" xmi:id="dep_driver_car" client="driver" 
  <packagedElement xmi:type="uml:DataType" xmi:id="datatype_1" name="float"/>
  <packagedElement xmi:type="uml:DataType" xmi:id="datatype_0" name="void"/>

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.