Tuesday, October 01, 2013

How to apply EMF profiles to models

In a previous post, we demonstrated how to create EMF profiles based on the example of the famous library metamodel. Once we have created our profile, we want to apply them to corresponding models. In this post, we demonstrate step-by-step how this is done using EMF Profiles.

What we have so far

As shown in our previous post, we created a profile, named MyExtendedLibrary, for the library metamodel. In particular, we wanted to be able to store whether a book is an ebook and, if it is an ebook, in which format it is available (such as ePub or PDF). Additionally, we wanted to be able to annotate books with tags. Therefore, we created a profile containing two stereotypes EBook and TaggedBook, both applicable to instances of Book. The resulting profile is depicted below. Once we created this profile correctly (i.e., it is valid and contains no errors), it is automatically registered to the local profile registry; ready to be applied to library models.

Applying the profile

Assume we have a small library model, that is, an instance of the library metamodel, consisting of two books, named "My Book" and "Another Book". To apply our profile to this model, we first open the model in the Sample Reflective Ecore Editor, or in any other EMF-based modeling editor if available (e.g., a GMF-based editor).

Next, we open the Profile Application View using Window > Show View > Other... > EMF Profiles > EMF Profile Applications. Once the profile application view is visible, again select the library model to be annotated in the modeling editor. The profile application view will now detect automatically that we selected a modeling editor that is supported by EMF profiles and activates the buttons for creating new and loading existing profile applications (the button with the "window plus" icon and the button with the "green plus" icon) in the profile application view.

To create a new profile application, click the button with the "window plus" icon (it should say Apply Profile in the tool tip). This will open up a wizard in which we first have to select the location where we want to save the profile application information. Note that the additional information that is saved in profile applications is stored in a separate file. The extension of profile application files is pa.xmi. Select a location and a file name for the profile application file and click Next >.

In the next step, we choose the profile that we want to apply to the model. Therefore, the wizard lists all profiles that are available in the local profile registry. In this list, we may now select the MyExtendedLibrary profile and hit Finish.

The wizard now creates the empty profile application file for us and shows the profile application in the profile application view.

Now we can start applying stereotypes to our small library model. Therefore, we right-click the model element to which we want to apply a stereotype and select Apply Stereotype in the popup menu. Note that this command is only activated in the popup menu, if there is a stereotype available in the loaded profile that can be applied to the selected model element. In our example, this should be the case for all books that we have in our library model, since the MyExtendedLibrary profile contains two stereotypes that extend the class Book. So let us right-click the book My Book and click Apply Stereotype. This brings up a window in which we can select the stereotype to be applied; let us choose the stereotype <<EBook>> and click Ok.

The stereotype <<EBook>> is now applied to My Book. Thus, a new item indicating this stereotype application is added to the profile application view. Note that only those stereotype applications are shown in the view that are applied to the model element currently selected in the modeling editor (e.g., My Book). If you prefer to see all currently applied stereotypes in the currently loaded profile application file, irrespectively of which model element is selected, unselect the Pin View with Selected Element button in the profile application view.

Since we not only want to apply stereotypes, but also add additional information in terms of tagged values (attributes and references introduced by the stereotypes), we can now select the stereotype application in the profile application view and inspect the standard Eclipse Properties view. This properties view now shows all attributes and references of the applied stereotype; in our case this is for instance the format attribute, for which we can select the value PDF or EPUB, as specified in the MyExtendedLibrary profile. Note that we can apply both stereotypes in this profile only once for each book according to the specified multiplicity of the extension relationship (cf. MyExtendedLibrary profile).

Once we finished applying stereotypes, we can save the profile application by clicking the Save button in the profile application view and closing the modeling editor showing our library model. Of course, we can still load this profile application (or several at the same time) later. This is done by opening the library model and by clicking the Load Profile Application button (the one with the "green plus" icon). This brings up a wizard in which we select the profile application file to be loaded. Modifying the loaded profile application in terms of adding or deleting stereotype applications or modifying their attributes and references works similarly as with new profile applications.

The profile application file

One major advantage of EMF Profiles is that profile application files are common EMF models and, as a result, they can be processed with any EMF-based technology, such as model transformation tools, model-to-code generation frameworks, etc. So lets have a brief look at how these models look like.

Basically, profile application models consist of one container object of type Profile Application and a list of instances of the stereotypes that we applied to the respective model. The model element to which the stereotype has been applied is referred to through the reference appliedTo and the additional values (e.g., the format attribute of the EBook stereotype) are common EMF attributes or references. Loading profile applications as normal EMF models enables us to apply any processing techniques imaginable for common EMF models.

We hope you enjoyed reading this blog post and invite you to try EMF profiles for your needs. If you have any questions or comments, don't hesitate to contact us or post a comment below.

1 comment:

  1. I am new to EMF and I have been looking for something like this for some time now. EMF Profile fills a urgent need for people like me who are looking for a simple and effective way to extend EMF based models.

    However; I am wondering about how to create a stereotype that has more complex internal structure. I added a reference to my Stereotype but when I went to apply it to a model, there was no way for me to specify the values for the these references.

    Is there a sample that shows more than tagged stereotypes? Perhaps a stereotype that refers to or contains more complex data?

    Thanks in advance!