EMF is a modeling framework launched by the Eclipse organization. It can help us transform models (UML, XSD, etc.) into robust and rich Java code. By using EMF, the program we write can get a robust model layer for free, which is usually more robust than our own hand-written model layer. In fact, there are many commercial products that use EMF as their model layer. Because of the widespread use of EMF, the ECLIPSE organization has launched numerous peripheral modules for it.
1 Introduction
Because EMF (the Eclipse Modeling Framework) is widely used in the Java camp, users urgently need more EMF based functionality. As a result, the Eclipse organization has launched numerous peripheral modules for it. For example, the currently more mature GEF (graphical Editing framework) and GMF (graphical modeling framework) can help users develop an EMF based graphical editor. In fact, the new technology based on EMF is far more than GEF and GMF. The EMFT (Eclipse Modeling Framework Technology) is a specialized project that eclipse is dedicated to developing new technologies based on EMF. The EMF query that we're going to introduce today is a subassembly of EMFT. We can use it to query the EMF model, which reduces the difficulty of dealing with complex models.
2 Establishing the Library model
In the article introducing EMF, the most common example is the Library sample. The UML diagram of the library model looks like this: (The Ecore model and source code used in this article are in the attachment Emfquery.zip)
Fig. 1 Library model
As we can see, the library example is fairly simple, and it contains only three classes: library, Writer, book, and a Bookcategory enumeration type. When using EMF, we first need to get a ecore model that will be used to define the metadata of the user model, such as the library model. We can create a Ecore model from scratch, or we can import it from another model. If UML is modeled using products from the rational family, then we can export UML models directly to the Ecore model in rational products. Alternatively, we can convert to the Ecore model by creating XSD documents or annotated Java files and using EMF-led wizards.
After we get the Ecore model, we create a Java project in eclipse. In the New Java Project Wizard, we set the name of the project to Test.emf.query, and we should choose to detach the source code directory and the output directory. We create a new model directory in the new Test.emf.query project and save the Library.ecore file to this directory.
To generate the Java implementation of the model, we first need to use the EMF-provided wizard to convert the. Ecore model to the. Genmodel model. This can be done by using the New EMF Model Wizard as shown in Figure 2.
Figure 2 Creating a new project using the New EMF Model Wizard