EMF series (II. Generating applications from models)

Source: Internet
Author: User

Let's take a look at the usage and functions of EMF from an example. Assume that our application is an online store with these objects in the system: Shop shop, category,
Product, which can contain subcategories. Now we use EMF to generate an application that can manage categories and products from start to end.Program. If you haven't installed
EMF: Download the emf sdk for your eclipse.org/emf. we recommend that you download all the packages. After installation, the EMF category is added to the new dialog box, as shown in 1:


Figure 1 EMF category in the new Wizard

The "EMF model" is used to create a genmodel from an existing model file. This model is used to generateCodeIf you already have a model (such as a. MDL file or a. XSD file ),
Project, the existing model file is required in the later part of the Wizard. Because we plan to create this model from the beginning, select "Empty EMF
Project, just like creating a common plug-in, to provide a plug-in name, we name this store projectCom. My. ShopAnd then press finish to complete the wizard. Currently, this newly created
The project does not contain any code, but defines dependencies on EMF-related plug-ins in the META-INF/manifest. MF file.

The following describes how to define the ecore model. I prefer the graphical method. Because it looks intuitive, I use the eclipseuml plug-in of omondo to draw class diagrams. The free version of this plug-in is downloaded from their website, download the appropriate version of Eclipse. Of course, Rose is also good and more stable, but it
It is not free and can only be used in windows. Unlike rose, eclipseuml has special support for EMF.
Yes. After installing this plug-in, we can create a new "EMF class" in the project.
In this way, a. MCM file and A. ecore file are created. The class chart information edited by eclipseuml is saved in these two files.
And the latter is the real model information. Do not lose any of these two files. It is best to back up the files frequently.

It is very easy to edit a class chart in eclipseuml. It should be noted that if there is an association between two objects, you should carefully consider whether the Association is an "include" relationship (generally in UML, the Black Diamond table is used
If a class is not included in any other class, the instance of this class will not be saved to the file. In Example 2, category is included in shop, and product is included in
Category. In this way, all objects are directly or indirectly contained by shop objects. In other words, if shop is used as the "root", all objects can be saved to files.

EMF encapsulates Basic Java types and some common classes, such as int-> Eint, java. Lang. integer-> einteger
And java. util. List-> elist. Therefore, you must use these EMF types when defining class attributes. Of course, you can also use custom types (which will be used later ). Me
In our example, the model is quite simple. There are only three business objects in total (to be more intuitive, we have added a namedelement interface ).
The first version 2 of the store class diagram is shown.


Figure 2 online store diagram (version 1.0)

The code will be generated later. EMF uses jet to generate code using the template (previously introduced jet). Therefore, we need to convert the ecore model into a genmodel that can be used by jet, the specific operation is to press Ctrl + n to create an EMF
Model: Specifies the name in the first step of the wizard.Shop. genmodel, Step 2 select import from ecore model, step 3 select ecore model file
(Shop. ecore). In this way, the default genmodel model is created, and some customization work can be done on the basis of this model, such as the description information of each attribute.

With the genmodel model, it is only one step away from the available Java code. Open the shop. genmodel file and right-click the root node (see figure 3 ).
Select generate model for model generation code
Code ", if you need the. Edit code (EMF provides itemprovider and adapterfactory to help implement the editor) and the available Editor, select
Select "generate"
"All" is the most convenient, which is also the method selected in the example. The EMF code generator generates a plug-in project for each model,. Edit, editor, and test code.
(COM. My. Shop/shop. Edit/shop. Editor/shop. Tests), the first three are the relationship between the latter and the former. Insert a sentence here, even if there is no
For EMF projects, we also recommend that you separate models from interfaces using different plug-in projects. This has many advantages, mainly because the flexibility is greatly improved.


Figure 3 select generate part or all of the code from the menu

Now we can run it to see the effect. Note that we haven't even written a sentence of code. EMF generates a new Wizard (New
You can use this wizard to generate a new shop instance. Note that you must select the root type as the shop class in step 3. As shown in interface 4 of the editor, although its appearance needs to be improved, its functions have been
It is enough for us to edit the category and product in the online store. In future posts, we will make some modifications to the ecore model and Editor interface of the online store.


Figure 4 default editor page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.