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!
Update: The above link is broken, try this instead!
Adding UUIDs to your favorite model
We start out using the classic Library example.
- Get started by cloning it from here - checkout the "master" branch.
- Open the /emf.uuid.library.example/model/emf.uuid.library.example.genmodel
- Generate All code
- Run the editor and create an example library model with some elements and save it
- 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>
- Now, to add support for using UUIDs instead, open the genmodel and select the Library element
- Set the Model > Resource Type property to XMI (btw, this was the missing piece in the recipe...)
- Regenerate the Model code
- Add org.eclipse.emf.ecore.xmi to the plug-in dependencies
- Follow the steps described in the blog:
- Create a LibraryResourceImpl class (Override the useUUIDs() method)
- Create a LibraryResourceFactory
- Hook into the org.eclipse.emf.ecore.extension_parser extension point
- Generate All code
- Run the Library Editor again
- Open the previously save library model
- Make a change and save the model
- Open it with Text Editor again and note how all elements now have UUIDs
- 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
- https://github.com/fredrikattebrant/emf.uuid.library.example
- http://serdom.eu/ser/2007/03/05/how-to-enable-uuid-in-emf-generated-model-to-get-copypaste-working (Update: link is broken - try #3 below instead)
- http://eioki.eu/2007/03/05/how-to-enable-uuid-in-emf-generated-model-to-get-copypaste-working
No comments:
Post a Comment