StarUML model validation using Schematron

Main benefits of Continuous Integration practice are improved quality and team communication. The former is achieved by automated compilation with testing and the latter by publishing the results. Nowadays successful building any bigger system is not possible without both.

In one of my recent posts I presented how to transform StarUML model to XML Schema outside the tool. Implementation dependant model generation corresponds to compilation. Testing can be implemented as validation. In this case literal and structural analysis of UML file is fairly simple owing to its XML format.

There is nothing to invent – there is an ISO standard also known as Schematron. It is simple language that defines rules of assertions that are applicable in specific document context. Conditions are written in pluggable expression languages – XPath is default one. Default implementation of the standard compiles tests into XSLT stylesheet and then executes against a document.

Model will be verified on three levels - document conformance (if it is StarUML document), model quality (defined for model itself) and transformation profile constraints (schema in that case). List of rules (not planned as complete):

  1. Document conformance
  2. Model general quality
    • class cannot extend itself
    • abstract classes should have abstract prefix
    • class name should be longer than 5 chars (after dropping abstract prefix)
    • attribute must have defined type
    • attribute must have defined multiplicity
  3. Schema transformation profile
    • class must extend one class at most

First point of the list looks as follow:

<!-- DOCUMENT CONFORMANCE -->
<sch:pattern>
  <sch:title>Verifying StarUML document conformance</sch:title>
  <sch:rule context="/" id="document-conformance" role="fatal">
    <sch:assert test="XPD:PROJECT[@version='1']/XPD:BODY">
      Document not conforms StarUML structure of version 1
    </sch:assert>
  </sch:rule>
</sch:pattern>

And point three:

<!-- SCHEMA TRANSFORMATION PROFILE -->
<sch:pattern>
  <sch:title>
    Validating constraints for schema transformation profile - non multiple extending
  </sch:title>
  <sch:rule id="xsd-profile.single-extending"
      context="XPD:OBJ[@type='UMLClass' and child::XPD:ATTR[@name='#Generalizations']]">
    <sch:assert
      test="XPD:ATTR[@name='#Generalizations'] and XPD:ATTR[@name='#Generalizations']='1'">
      Class <sch:value-of select="XPD:ATTR[@name='Name']"/> must not extend more than one class
      (but extends <sch:value-of select="XPD:ATTR[@name='#Generalizations']"/>)
    </sch:assert>
  </sch:rule>
</sch:pattern>

To test out implemented Schematron rules I prepared the worst model I can as pictured below.

Incorrect class diagram

When no includes nor abstract rules is used execution has two phases - validator compilation and validation itself. Apache Ant target controlling this process looks as follow:

<target name="validation-of-model">
  <!-- compilation -->
  <xslt
      basedir="src/schematron"
      style="src/schematron/iso_svrl_for_xslt2.xsl"
      in="src/validation/star-uml-schema-rules.sch"
      out="target/schematron/validator.xsl">
    <factory name="net.sf.saxon.TransformerFactoryImpl" />
  </xslt>
  <!-- validation -->
  <xslt
      basedir="src/schematron"
      style="target/schematron/validator.xsl"
      in="src/model/validation.uml"
      out="target/validation.svrlt">
    <factory name="net.sf.saxon.TransformerFactoryImpl" />
  </xslt>
</target>

Result is saved in XML document that shows fired rules, assertion results in schematron document sequence order. Adding simple XSLT allow to generate nice looking report. Using xmltask I described few days ago you can simply signal if there is any error.

Complete example you can download from my files site.

0 komentarze:

Post a Comment