Thursday, October 18, 2012

Enabling UUIDs in an EMF ECore model

Enabling UUIDs in an EMF ECore model

A while back we ran into the need to start using UUIDs in our ECore models. Googling a bit led me to this nice blog on the topic. But still one little piece of the puzzle was missing, so here I go with the full recipe and example code as well.

Update: The above link is broken, try this instead!

Adding UUIDs to your favorite model

We start out using the classic Library example. 
  1. Get started by cloning it from here - checkout the "master" branch.
  2. Open the /emf.uuid.library.example/model/emf.uuid.library.example.genmodel
  3. Generate All code
  4. Run the editor and create an example library model with some elements and save it
  5. Open the save library with Text Editor and note how the references are stored:

<?xml version="1.0" encoding="UTF-8"?>
<emf.uuid.library.example:Library xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:emf.uuid.library.example="https://github.com/fredrikattebrant/emf.uuid.library.example/1.0" name="The library">
  <writers books="//@books.0" name="Fredrik Attebrant"/>
  <books author="//@writers.0" title="Using UUIDs in ECore models"/>
</emf.uuid.library.example:Library>
  1. Now, to add support for using UUIDs instead, open the genmodel and select the Library element
  2. Set the Model > Resource Type property to XMI (btw, this was the missing piece in the recipe...)
  3. Regenerate the Model code
  4. Add org.eclipse.emf.ecore.xmi to the plug-in dependencies
  5. Follow the steps described in the blog:
    1. Create a LibraryResourceImpl class (Override the useUUIDs() method)
    2. Create a LibraryResourceFactory
    3. Hook into the org.eclipse.emf.ecore.extension_parser extension point
  6. Generate All code
  7. Run the Library Editor again
  8. Open the previously save library model
  9. Make a change and save the model
  10. Open it with Text Editor again and note how all elements now have UUIDs
  11. Note how references are now stored using the UUID of the referenced elements:
<?xml version="1.0" encoding="UTF-8"?>
<emf.uuid.library.example:Library xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:emf.uuid.library.example="https://github.com/fredrikattebrant/emf.uuid.library.example/1.0" xmi:id="_sQQSEBj-EeKqFILuf-WxiA" name="The UUID library">
  <writers xmi:id="_sQQSERj-EeKqFILuf-WxiA" books="_sQQSEhj-EeKqFILuf-WxiA" name="Fredrik Attebrant"/>
  <books xmi:id="_sQQSEhj-EeKqFILuf-WxiA" author="_sQQSERj-EeKqFILuf-WxiA" title="Using UUIDs in ECore models"/>
</emf.uuid.library.example:Library>

You'll find the example library models in the "librarytests" project and in the uuid branch, you'll find the completed example with UUIDs enabled.

Happy UUID-based modeling!

References


No comments:

Post a Comment