Skip navigation links
www.sdmetrics.com

Package com.sdmetrics.model

Provides the XMI import of UML models and access to the elements of the imported model.

See: Description

Package com.sdmetrics.model Description

Provides the XMI import of UML models and access to the elements of the imported model.

Tutorial - how to parse an XMI file and access the model elements

The following code snippets take you through the steps to parse an XMI file, and write out all model elements extracted from the XMI file to the console.

  1. Required imports:
    import java.util.Collection;
    import java.util.List;
    
    import sdmetrics.model.MetaModel;
    import sdmetrics.model.MetaModelElement;
    import sdmetrics.model.Model;
    import sdmetrics.model.ModelElement;
    import sdmetrics.model.XMIReader;
    import sdmetrics.model.XMITransformations;
    import sdmetrics.util.XMLParser;
    
  2. Have your metamodel, XMI transformation, and XMI input files ready:
    String metaModelURL = ...;  // metamodel definition to use
    String xmiTransURL = ...;   // XMI tranformations to use
    String xmiFile = ...;       // XMI file with the UML model
    
  3. Read the metamodel
    XMLParser parser = new XMLParser();
    MetaModel metaModel = new MetaModel();
    parser.parse(metaModelURL, metaModel.getSAXParserHandler());
    
    You do not have to use the SAX parser provided by class XMLParser, you may just as well use a org.xml.sax.XMLReader that you created yourself.

  4. Read the XMI transformation file
    XMITransformations trans=new XMITransformations(metaModel);
    parser.parse(xmiTransURL, trans.getSAXParserHandler());
    
  5. Read the XMI file with the UML model
    Model model = new Model(metaModel);
    XMIReader xmiReader = new XMIReader(trans, model);
    parser.parse(xmiFile, xmiReader);
    
  6. Optionally, specify element filters to get rid of standard libraries or 3rd party APIs
    String[] filters = { "#.java", "#.javax", "#.org.xml" };
    model.setFilter(filters, false, true);
    
    At this point, you can already start calculating metrics for the elements in the model. The tutorial for package com.sdmetrics.metrics describes how. The remainder of this tutorial shows how to access the elements in the model.

  7. Access the UML model
    The following example writes all model elements accepted by the element filter to the console, along with the values of their attributes.
    // iterate over all model element types in the metamodel
    for (MetaModelElement type : metaModel) {
       System.out.println("Elements of type: " + type.getName());
       
       // iterate over all model elements of the current type
       List<ModelElement> elements = model.getAcceptedElements(type);
       for (ModelElement me : elements) {
          System.out.println("  Element: " + me.getFullName() + " ");
          
          // write out the value of each attribute of the element
          Collection<String> attributeNames = type.getAttributeNames();
          for (String attr : attributeNames) {
             System.out.print("     Attribute '" + attr);
             if (type.isSetAttribute(attr))
                System.out.println("' has set value "
                      + me.getSetAttribute(attr));
             else if (type.isRefAttribute(attr)) {
                System.out.print("' references ");
                ModelElement referenced = me.getRefAttribute(attr);
                System.out.println((referenced == null) ? "nothing"
                      : referenced.getFullName());
             } else
                System.out.println("' has value: "
                      + me.getPlainAttribute(attr));
          }
       }
    }
    
Skip navigation links
www.sdmetrics.com