Showing posts with label EMF profiles. Show all posts
Showing posts with label EMF profiles. Show all posts

Monday, June 30, 2014

Actions in EMF Profiles

Introduction

Since the Eclipse Modeling Framework (EMF) does not provide any native extension mechanism, the project EMF Profiles was invented as a solution for this problem. So called stereotypes can be used to extend and enrich meta-models. Once a stereotype is defined for a meta-model, it can be applied to models that conform to the respective meta-model. This mechanism, however, does not allow to extend models with additional semantic functionality (i.e., behavior), it only allows to annotate models and to add additional information to them.

The possibility to add semantic functionality to stereotypes in terms of stereotype actions enables developers to invoke the functionality of applied stereotypes in the context of annotated models in order to automate time-consuming and repetitive tasks automatically. Some usage examples include stereotypes and respective actions for model-to-code transformation (Entity Model to SQL, Entity Model to Annotated Java classes) or model-to-model transformation, for example pulling up members to super classes or automatically resolve conflicts in models that have conflict information attached in terms of stereotypes.

Our aim was to invent an easy-to-use extension mechanism for adding such semantic functionality to stereotypes in EMF Profiles.

Creating EMF Profile Actions

The first step is to add an implementation of the action functionality.
  1. Create a meta-model
  2. Create an EMF profile package including stereotypes and actions
  3. Implement and register action handlers

Using EMF Profile Actions

Once a profile is extended with actions, a modeler can take advantage of the provided functionality of the stereotypes.
  1. Create a model based on the meta-model
  2. Apply stereotypes using EMF profiles
  3. Execute actions of the applied stereotypes

Exemplary Use Case: ORM Mapper

For our example we want to create an Object-relational mapper (ORM). The generated function, an automatic creation of SQL code, will be minimal, due to demonstration purpose. In the next steps we create an EMF profile with an action and apply the profile to an existing model for executing the action.

Structure of our exemplary Use Case

Step 1

  1. For the meta-model Ecore can be reused as data modeling language.
  2. Next we have to create an EMF Profile "ORM_Mapper_EMFProfilePlugin" with the required annotations necessary to perform the ORM Mapper functionality including the definition of two stereotypes: Entity and Column. The stereotypes will be applied to EClass instances. The figure shows the finished EMF Profile model. Next we define the action CreateSQL, this action is now available for the whole EMF Profile Project and also requires an identifier. The action must be set in the properties of the desired stereotype, in our case the stereotype Entity.

    EMF Profile with Action CreateSQL

  3. In order to execute an action, a Java class has to implement the Action Handler interface in an Eclipse plug-in project, the "ORM_Mapper_ActionPlugin". It contains the code for the automatic generation of the SQL statements.

    Action Handler Class for generating SQL Code


    For the handler registration the extension point needs to be contributed so that the action identifier is bound to the handling class.
Extension Point with Action Handler Class and Action ID

Step 2

  1. In a further Eclipse instance with the registered EMF Profile, a model, the "ORM_Mapper_model", with a corresponding application of the defined EMF Profile from step 1 has to be created.
  2. The stereotypes can easily be applied through the tree-based Ecore editor.
  3. Model with applied Stereotpyes

  4. Now the option to execute our action is provided and are prompted to select from a list, after right-clicking our class "Dog".

    Available actions for the selected object

     The action has exported the create statements for our model into the "create" file.
Output of the create file


Links to the project:

GitHub-Repository: https://github.com/elexx/emf-profiles


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.

Monday, April 29, 2013

How to create an EMF Profile

Whenever you want to store additional information to EMF-based models and you cannot or don't want to change the respective metamodel, EMF Profiles provide a convenient solution for extending metamodels in a non-intrusive and lightweight manner; comparable to how UML profiles allow to extend UML. For extending a metamodel, all you have to do is to create a profile, which can then be applied to models in order to store the additional information you want to add to the original model. In the following, we show how profiles are created step by step.

Extending the library metamodel

For illustrating the process of creating a profile, we extend the well-known library example metamodel. This metamodel basically contains three classes: the class Library, Writer, and Book.

Now assume, we need to add additional information to library models without having to change the metamodel and, thus, without the need to re-generate the model-, edit-, and editor code. In particular, we want 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 want to be able to annotate books with tags.

Creating a profile project

To create a new profile project, we start the dedicated wizard for profile projects by selecting File > New > Other and select EMF Profile > EMF Profile Project.


After clicking Next, we enter a name for the profile project, as well as additional information about the profile, such as its name and its namespace URI. For the name and the namespace URI, it is recommendable to apply the same naming conventions as for metamodels.


After hitting the Finish button, the wizard creates the EMF profile project and opens the created empty EMF profile diagram, which has been initialized with the information provided in the wizard already, as can be seen in the Properties view.

Modeling the profile

A profile consists of stereotypes, which extend EClasses of metamodels. In our scenario, we want to store additional information to instances of the class Book of the library metamodel. Thus, we will create stereotypes that extend the class Book. Therefore, we first have to import the class Book. To do that, we use the popup menu (right-click the canvas of the profile diagram editor). In the popup menu, we now select Import Metamodel Element... and choose the extlibrary metamodel and, in the next step, the EClass Book. As a result, we obtain a shortcut to Book in our profile diagram canvas.



Now, we can start creating stereotypes using the palette in the profile diagram editor. To store whether a book (i.e., an instance of the class Book) is an ebook, we create the stereotype EBook. As we want to apply this stereotype to instances of Book, we create an Extension relationship from the created stereotype to the imported class Book. Extension relationships have a multiplicity (e.g., 0..1, 0..*, 2..4, etc.). This multiplicity specifies how often a stereotype can be (and must be) applied within one profile application to an instance of the extended class (e.g., Book). In our scenario, the stereotype EBook should not be mandatory, since we might have books that are not ebooks and, thus, should not be annotated with the stereotype EBook. Thus, we use a lower-bound multiplicity of 0. Further, it wouldn't make any sense to apply the stereotype EBook multiple times to the same book. To prohibit multiple applications, we set the upper-bound multiplicity to 1.


Stereotypes may carry additional data. One way to allow for adding additional data is to add tagged values to stereotypes. Since we want to store the format of an ebook, we add the tagged value named format to the stereotype EBook. Tagged values are basically attributes and can have primitive Ecore types, such as EString, EBoolean, ect., as well as custom EEnum types. We want to store whether an EBook is available in the format of EPUB or PDF; thus, we create a new EEnum named EBookFormat with two literals, EPUB and PDF, and set the type of the attribute format to EBookFormat.

Side note: Although this isn't needed in our example, stereotypes may, in general, also contain cross references to existing model elements (in the base model or in the profile application), as well as containment references (i.e., the stereotype application owns additional model elements). For creating references, just use the palette in the diagram editor and set the properties of the added reference accordingly. You can also use inheritance among stereotypes, if needed.

As mentioned above, we also want to be able to annotate books with tags. Therefore, we add another stereotype named TaggedBook, which also extends the class Book, and add a tagged value named tags of type EString to the created stereotype. To enable multiple tags for one book, we may set tags to be multi-valued (i.e., an upper-bound of -1 in the properties of the tagged value) and/or set the multiplicity of the stereotype to be 0..*, which specifies that the stereotype TaggedBook may be applied arbitrarily often to one instance of a book.

Registered profiles

Having finished the profile, we may validate it and save it. If the profile is valid, it will be registered automatically to the local profile registry. You may inspect all currently registered profiles using the Registered EMF Profiles view, which can be added using Window > Show View > Other... > EMF Profiles > Registered EMF Profiles.


As can be seen in the screenshot above, the created profile has been registered correctly. Note that you can still modify your profile; it will be synchronized automatically with the local profile registry, as long as your profile is valid. If it is invalid, it will disappear from the registry until you fix the validation error.

Once your profile is available in the local registry you can apply it to models that conform to the extlibrary metamodel. This, however, will be covered in future posts... so make sure to stay tuned.