Or from an example to see the use of EMF and the role of it. Suppose our application is an online store that has these objects in the system: Store shop, category category, product product, where categories can contain subcategories. Now we use EMF to build an application that can manage categories and products. If you do not have EMF installed in eclipse, download the EMF SDK for your eclipse version in ECLIPSE.ORG/EMF and recommend that you download all the ones that are included. After installation, the category of EMF is added to the New dialog box, as shown in Figure 1:
Figure 1 EMF categories in the new wizard
where "EMF Model" is created from the existing model file Genmodel model, which is designed to generate code; If you already have a model (such as a. mdl file or an. xsd file, etc.), you can choose New EMF Project. This will require an existing model file to be provided in the later part of the wizard, because we intend to build the model from scratch, so choose "Empty EMF Project" to provide a plug-in name just like the normal plugin. We named the store project as Com.my.shop and finished the wizard by pressing finish. There is no code in this newly built project, just meta-inf/manifest. The MF file defines dependencies on EMF-related plug-ins.
Let's start by defining the Ecore model, I prefer a graphical approach because it looks intuitive, so I use the Omondo company's ECLIPSEUML plugin to draw class diagrams, and the free version of this plugin is downloaded on their website, paying attention to downloading the appropriate version of Eclipse for you. Of course rose is nice and more stable, but it's not free, and it can only be used in Windows. Unlike Rose, ECLIPSEUML has special support for EMF, and after installing the plugin, I can create a new "EMF Class Diagram" in the project, creating a. ecd file and a. ecore file at the same time, ECLIPSEUML edited class diagram information will be saved in these two files, the former is mainly graphic aspects, and the latter is the real model information, note that the two files are not lost any one, it is best to regularly back up.
Editing a class diagram in ECLIPSEUML is simple, note that if there is an association between two objects, consider carefully whether the association is a "contained" relationship (typically represented in a black diamond in UML), and if a class is not contained in any other class, the instance of the class is not saved to the file. For example, in Figure 2, category is included in the shop and product is included in the category. In this way, all objects are included directly or indirectly by the shop object. In other words, if you take the shop as the "root", all objects can be saved to the file.
The EMF wraps the Java base type and some common classes, such as int->eint,java.lang.integer->einteger and Java.util.list->elist, and so on, So when you define the properties of a class, you use these EMF types, and of course you have the option of using a custom type (which you'll use later). The model in our example is fairly simple, with a total of only three business objects (we added a namedelement interface for more intuitive purposes), and now the first version of the online store class diagram is shown in Figure 2.
Figure 2 Online Store class diagram (version 1.0)