Posts Tagged ‘ Model Driven Architecture

Model Driven Engineering (MDE) with Eclipse

Model driven engineering (MDE) is a development method that emphasizes creating software from high level models and abstractions. This can increase productivity, reduce programming errors and facilitate software refactoring to better accommodate future changes and new requirements.

In essence you can define your model (for example in UML) and generate code from it, thereby building the skeleton of your application (and sometimes your entire application) from it. You can then spend your time customizing the generated code to fit your requirements, thereby spending your time on your most important programming challenges.

The Eclipse IDE has a set of modeling plugins (Eclipse Modeling Project) that allow you to create models, generate Java code from them. In addition it offers technology for persisting models (XML or using O/R mapping), querying, validating, visualizing, transforming them (and more).

Here is a quick tutorial to give you a taste of MDE in action.

Download Eclipse Modeling Tools from http://www.eclipse.org/downloads/

Launch Eclipse and create a new workspace.

Create a new EMF Empty Project.

And give it a name. Here I use com.tamerconsulting.example:

In your project create a new “Ecore Diagram” file. An Ecore file is the core EMF file that represents your model. You can create an Ecore file using UML (which we will do here) or from various other inputs (such as annotated Java files).

Choose to create a new model (which is the default). It must end with a .ecore extension.

Open the diagram file. This is where we will create our UML model. As an example we will create a Company object, which contains departments. And each department will contain employees. This is pretty straight forward and intentionally simplistic, just for the sake of demonstration. The diagram editor has a toolbox with objects and connections.

Click the EClass icon in the toolbox and click on the diagram. This will create a new object. Call it Company by editing its name on the diagram or in the Eclipse properties view.

Similarly, create a Department object and an Employee object.

Once you are done, create an EReference connection between Company and Department. Then create a connection between Department and Employee. To do so, click on the EReference connection in the toolbox, then drag a line between the two objects you want to connect.

Then go to the property sheet and set the Upper Bounds to *. This will ensure that a company can have many departments, and that each department can have many employees. The label “0..*” should appear on the link, showing you that this link is a one-to-many relationship. Also check the box “Is Containment”. This means that the parent object will contain its children (the company will contain the departments and the departments will contain their employees). You should end up with a diagram as such:

Now it’s time to create attributes for each objects. We will add a “name” attribute to the Company and Department objects. For the Employee objects we will have 3 attributes: first name, last name, and role.

To add an attribute select the EAttribute tool from the toolbox palette and click in the top compartment of an object on the diagram.

Place an attribute in the Company object, and call it “name”. Set its type to EString. An EString is an EMF wrapper around a Java string.

Finish setting up the attributes in a similar way for the Department and Employee objects similarly, by using the EAttribute tool on the palette. Don’t put spaces in the attribute names, to avoid confusing EMF :)

The end result should look like this:

At this point you are done defining your model. Now we are going to generate Java code that represents this model, without writing a single line of code ourselves. This is the fun part.

Create an EMF Generator Model file.

Give it a name that end with the extension .genmodel

Now you need to associate your Generator Model with your Ecore model:

On the following screen select “Browse Workspace…” to find your Ecore model:

Click “Finish”. You should now have a new genmodel file in your workspace. This file will remain synchronized with your ecore model. If you open your genmodel file you will see the following:

Click on the Model package to select it. In the Properties View set the “Base Package” to the package name you would like your generate code in.

Save your genmodel. Right click the root element (Model) and select “Generate Model Code”.

Now feel free to expand the src folder in your project and explore the source code that was generated. You will notice 3 packages. The model package contain abstract version of your objects (in the form of Java interfaces) as well as an abstract factory. You also notice a Package class which contains constants. In the second package (model.impl) you will find actual implementations of your model objects. For example the object CompanyImpl implements the interface Company and has a concrete implementation of methods such as getName(), setName(), getDepartments(). The last package (model.util) contains utility classes which we will ignore for now.

We could actually stop here and use the generated code as is. For example we could create a Main class that uses those objects and does something with them.

Create a Main class with a public static main function:

First we get a reference to the factory by calling ModelFactory.eInstance (the ModelFactory uses a singleton pattern).

Then we create a Employee object, and set its properties (first name, last name, role). We then create a department using the factory. We set its name and add the employee to it.

However we could take things a step further and generate an Eclipse plugin that has a wizard for creating a model instance and allows editing it in its own editor.

Let’s go back to the genmodel and right-click the root element. This time select “Generate All”.

Now you will have new projects in your workspace. Open the editor project and locate the plugin.xml file. Open it and go to the extensions tab. Change the name of the newWizard to Company Model.

Also change the editor name to Company Editor, as shown below.

Now it’s time to run our plugins and try out our wizard and editor. Right-click your editor plugin and select “Run As> Eclipse Application”. This will launch another instance of Eclipse containing your plugins. If you have done Eclipse plugin development in the past, you should be familiar with this process.

Create a new empty project. This is because our model file needs to be contained in a project.

Go to the file “New” menu and locate the “Example EMF Model Creation Wizards”. You will notice a new wizard called “Company Model”. Select it, and press next.

On the next screen you will select which root object you would like to instantiate. Select “Company”. Click “Finish”

This will create a Company Model file. Those files can be open with your generated “Company Editor”.

Your custom editor has several tabs. One of them is the “Selection” tab. It shows you your model in a tree. You can right click the Company root object and create a child element (a Department). You can set the properties of the Company, Department or Employees in the Eclipse properties view.

Create a department:

Create an Employee:

Save your model. It will be persisted in XML. Also, instead of opening it with your custom Company Editor, you can open it in a text editor to examine it.

THE END.

I hope this tutorial has given you a taste of what Model Driven Engineering using Eclipse. You can define your model in UML, and have a working editor without writing a single line of code. Of course there is a lot I did not cover in this tutorial. The genmodel file can be highly customized. You can also use your ecore model to define constraints in your model using OCL. I addition you can use GMF to generate a diagram editor. Such an editor would allow you edit a company model by placing Employees and Departments on a canvas.

If you have any questions or comments, feel free to post them!

Tamer