Tuesday, March 30, 2010

Converting UML Files to RDF via Java/EMF Objects and SPARQLMotion

TopBraid 3.3 comes with two new SPARQLMotion modules that can be used to convert arbitrary EMF files (including UML and XSD) to RDF. The underlying mechanism is very simple and extremely flexible:
  1. Eclipse is able to load .uml files into a MOF-based Java object model called EMF.
  2. SPARQLMotion provides a new module sml:ImportJavaObjectsFromEMFFile that takes a UML file as argument and loads its EMF Java objects into a SPARQLMotion variable.
  3. SPARQLMotion provides a new module sml:ConvertJavaObjectsToRDF that can traverse arbitrary Java object graphs to create RDF objects with the same structure.
  4. Now that we have the UML file represented in RDF, we can apply arbitrary transformations, in particular SPIN rules (SPARQL CONSTRUCTs) to create whatever ontology we like.
In an example SPARQLMotion script, this process looks as follows:

In the import step, we have to specify the file name (here: an example UML file) and the name of an output variable, that allows subsequent steps to access the EMF objects:

Next, we instruct the SPARQLMotion engine to convert the Java objects to RDF. This will start at the object specified by the variable uml, and then call all public getXY and isXY methods to collect the properties of the objects. The algorithm will include adjacent Java objects, as long as they are from a list of classes and packages specified under sml:javaClass:

As the output of this step, the module has produced an RDF graph containing instances as well as class and property definitions, all derived from the Java objects using reflection. All Java classes are mapped into a default namespace (abbreviated with the prefix "uml" in the screenshot below):

A TopBraid Composer class diagram shows that the generated classes also have RDF properties associated with them, all automatically derived from the Java objects:

Now that we have those RDF/OWL classes, we can attach SPARQL CONSTRUCT queries as SPIN rules to them. (Here, we use a property umlspin:mappingRule which is a subproperty of spin:rule which has max iteration count set to 1 to speed up the mapping):

These two example SPIN rules traverse the generated UML classes starting at the current class ?this, and create corresponding OWL classes and rdfs:subClassOf relationships between them. Of course, additional mapping rules are needed to represent the other UML elements such as associations and attributes. In our example SPARQLMotion script above, those SPIN rules are loaded from a generic mapping file umlspin.ttl. Note that the rules above call a user-defined SPIN function umlspin:getOWLClass that creates a URI resource from a namespace and a UML name.

With those rules in place, we just need to run the SPIN rules engine (TopSPIN) and get an OWL class hierarchy from whatever has been defined in the UML file:

The approach presented here is not completely out-of-the-box yet, but you can see that it offers a great deal of flexibility because developers can add or change any number of SPIN rules to fine tune the mapping. For example, in one case you may want to create owl:ObjectProperties from UML associations, while in other cases a reified object might be a better choice.

The SPIN based importer raises the level of abstraction and makes it possible to define complex mappings without having to work with a programming language like Java. This empowers a larger group of people to contribute mapping rules. The UML/MOF world consists of many related standards, and the SPIN/SPARQLMotion-based mapping enables the domain experts of those standards (e.g. mechanical engineers) to fine tune the importers.

Finally, other EMF-based importers exist for formats such as XML Schema, and the approach presented above would of course also work for any other Java object model, as long as it provides access to its properties using getXY and isXY methods. You can basically connect your own native Java objects into an RDF triple store by providing a bit of glue code (as a SPARQLMotion module similar to the EMF loader above).