Atitit. Customizing the UML MOF EMF architecture Eclipse EMF Tutorial o7t

Source: Internet
Author: User
Tags serialization

Atitit. customizing the UML MOF EMF architecture Eclipse EMF Tutorial o7t

1. Meta-object mechanism (Mof,meta-object Facility) and struct 1

2. Meta-model diagram, Model Diagram, object graph UML Custom Modeling Language 2

3. Frame connection in 3- layer structure and structure 3

4. GEF and EMF 4

5. Create EMF Project - - Create an empty EMF proj 4

6. Build a Ecore Model 4

7. Emf,ecore 6

8. Build a jet build profile for Genmodel 7

9. Ecore and Genmodel. 8

10.8

11. Build the Project 9

12. Running the plug-in run as Eclipse app 11

13.11

14. Create a new custom model 11

15. Prevent overwriting of custom modifications. @generated Callout 13

16. Basic methods of customizing the application) 13

. EMF Issues ati Summary 14

18. Reference 14

1. Meta-object mechanism (Mof,meta-object Facility) and structure

Originated in Unified Modeling Language (UML). Object Management Institution (object Management group| OMG) requires a meta-model structure to define UML.

MOF is designed as a 4-level structure. At the top is the Meta model layer, the M3 layer. The M3 model is the language in which MOF builds meta-models (known as M2 models). The most obvious example of the M2 model is the UML meta-model, which describes UML. The M2 model describes the features of the M1 layer and the M1 layer, such as the UML model. The last layer is the M0 layer or the data layer. It describes real-world objects.

Author:: Old Wow's paw attilax Ayron, email:[email protected]

Reprint please indicate source: Http://blog.csdn.net/attilax

2. Meta-model diagram, Model Diagram, object graph UMLCustom Modeling Language

MOF 's four-tier model is: Meta model layer (M3), meta-model layer (M2), model layer (M1), runtime ( M0). The meta-model layer contains the elements required to define the modeling language; The meta-model layer defines the structure and syntax of a modeling language; The model layer defines a specific system model, and the runtime contains the state of a model's objects at run time.

This article deals with M2,M1, and M0 layers, and here is an example, first M2 layer, It can define the structure and syntax of a modeling language, such as:

This article deals with M2,M1, and M0 layers, here is an example, first of all M2 Layer , it can define the structure and syntax of a modeling language, for example:

This is a brief meta-model diagram, which indicates that the Class is made up of property and operation , which conforms to The concept of UML2.0, but we can also draw:

This way, theClass contains three elements:Attribute,associationend , and Operation, so that we can create a new metamodel, or create a small new model language. Although it does not conform to the UML2.0 specification.

The M1 layer model is the " model " we usually callit, which is a standardized representation of a system, typically used in a modeling language to represent a software system, such as the following car and human systems:

3. Frame connections in a 3-tier structure and structure

An application system can be divided into a typical 3-layer structure. In the UML mode and application[1] , the book is called the UI layer, the domain layer, the data layer respectively. Interfaces between the UI layer and the domain layer are connected through the application and application frameworks, and the interfaces of the domain layer and the data layer are connected through the persistence framework.

4. GEFand theEMF

The GEF (graphical editor framework) is a graphical editing framework that allows developers to visualize and edit models in a graphical manner to enhance the user experience. Using GEF, you can easily implement applications such as XML editors, UML class diagram editors, and so on.

The EMF (Eclipse modelling Framework) is an important part of the Eclipse MDA (model driven Architecture) that transforms the model into efficient, correct, and easy-to-customize Java code.

Model-driven development is easy with GEF and EMF (Model-driven DEVELOPMENT,MDD)

。 GEF is the graphical editing framework provided by Eclipse, which provides users with a graphical editing model to enhance the user experience. EMF is the framework that eclipse provides a model-driven application development framework that can produce code. EMF can create Java code based on the model specified in XML SCHEMA,UML or annotated Java, enabling graphical data editing, manipulation, reading, and serialization.

5. EstablishEMFProject--Create aempty EMF proj6. Establish aEcoreModel

The default can only be tree-shaped edits ...

Define the Ecore model, I prefer the graphical way, because it looks very intuitive, so I use the Omondo company's ECLIPSEUML plug-in to draw class diagram, the free version of this plugin on their website download, pay attention to download the appropriate version of Eclipse. Of course rose is also good and more stable, but it's not free and can only be used in Windows. Unlike Rose, ECLIPSEUML has special support for EMF, and after installing this plugin, I can create an "EMF Class Diagram" in the project. This will create both an. ecd file and a. ecore file, and the Eclipseuml Editor's class diagram information will be stored in these two files, the former is mainly graphic content, while the latter is the real model information, note that none of the two files are lost, it is best to back up frequently.

Editing a class diagram in ECLIPSEUML is simple, and it is important to note that if there is an association between two objects, carefully consider whether the association is a "containment" relationship (typically in UML with a black diamond), and if a class is not contained in any other class, the instance of the class is not saved to the file. The category in Example 2 is included in the shop and product is included in the category. In this way, all objects are directly or indirectly contained by the shop object. In other words, if you use shop as the "root", all objects can be saved to the file.

EMF Wraps Java primitives and some common classes, such as Int->eint,java.lang.integer->einteger, Java.util.list->elist, and so on, So when defining the properties of a class to use these EMF types, there are certainly ways to use the custom type (which will be used later). The model in our example is quite simple, there are only three business objects (for more intuitive, we have added a namedelement interface), and now the online store class diagram is shown in the first version 2.

7. Emf,ecore

The original goal of the project was to implement the MOF specification (Meta object Facility) of the OMG (Object Management Group). In the process of the project, EMF developers re-processed the MOF specification based on a number of practical experiences, extracting some core element sets (MOF 2.0 called Emof (Essential MOF)). It can be said that EMF is a Java efficient implementation of the MOF core subset, and the implementation of the MOF core subset in EMF is called Ecore.


Figure 1. Ecore class Hierarchy: This image shows the complete class hierarchy of Ecore metadata.

EMF is used to define and implement a structured data model. The so-called data model is a set of related classes used to process the data that needs to be processed in the application.

Benefits of using EMF

1. Code generation. Generate all the code for the available data model based on the definition of the model. EMF generates easy-to-read code according to template files, which can be customized by template files. You can modify the generated code, and the changes are preserved when you regenerate the class. The generated code requires a rational rose model file, annotated Java interface or XML Schema definition (in development)

2. Meta data. You can use the program to query the model structure to get more information than the standard Java BeanInfo or reflection. EMF also supports accessing and updating models.

3. Default serialization. EMF can load model instances from XMI files, or you can save model instances to XMI files (XMI files are XML-formatted files). You can also save files in different formats. The file format XML schema is still in development.

4. Connection between files. You can save and edit data in multiple files

5. Editor. EMF also generates a model editor and a reflection editor that allows you to browse and edit EMF model files using only the metadata of the model.

The Eclipse Modeling Framework (EMF) is a Java open source framework and code generation tool-used to build tools and other applications based on a structured model. While the Eclipse platform provides a robust integration framework at both the user interface and the file level, EMF strengthens this capability to enable good-grained data sharing between tools and applications. EMF can quickly generate simple, correct, and effective Java code for your model. Given a model, EMF can automatically generate Java source code-the source allows you to create, query, update, serialize, crossdress, validate, and track changes to your model instances.

Similar to other Java binding frameworks, such as JAXB or XMLBeans, given a model, EMF can generate Java source code-it allows you to create, query, update, crossdress, and serialize instances of your model. While most Java binding frameworks support only one model class, such as XML Schemas, EMF supports generating code from XML schemas, UML class diagrams (Rational Rose or UML2), and annotated Java interfaces. In addition to the model code, EMF can also generate a complete application-it includes a customizable editor.

The model used to describe the EMF model, called Ecore, is essentially a subset of UML class diagrams, a simple model of the classes and data applied. When Ecore is established, EMF uses a design that implements the separation from the interface and automatically generates 1 interfaces and an implementation class for each class (Eclass) in the Ecore.

The interfaces in the Eobject inherit the set interfaces in these EMF, as Java.lang.Object in Java. EMF also generates an interface and implementation class for each model that corresponds to the factory and package. Where **factory inherits from Efactory, providing create*** methods for the creation of each class in the model. In Jstudio, the use of EMF to achieve business model object building and code generation, reduce the workload of developers, improve efficiency, and the generated code is concise and efficient.

8. EstablishGenmodelof theJetbuild configuration file

The next generation of code will be generated. EMF uses Jet to generate code using a template ( described previously in Jet), so to convert the Ecore model into a Genmodel model that can be exploited by jet, the specific operation is to press CTRL + N to create a new EMF In the first step of the wizard, you specify the name Shop.genmodel, the second step is to import from the Ecore model, and the third step selects the Ecore model file (Shop.ecore), so that the default Genmodel model is established, and on the basis of this model you can do some customization work, such as the description information of each attribute, and so on.

With the Genmodel model, there is only one step away from getting the Java code available. Open the Shop.genmodel file and click the right-click menu on the root node (see Figure 3), if you just want to generate the model code select "Generate Model", If you need the. Edit code (EMF provides some itemprovider and adapterfactory to help implement the editor) and the available editor, select "Generate All" is the most convenient, and this is also the way to choose in the example. The EMF code generator generates a plug-in project (com.my.shop/shop.edit/shop.editor/shop.tests) for the model,. Edit, editor, and test code, with the first three being dependent on the former. In this case, even without EMF projects, it is recommended to separate the model and interface using different plug-in projects, which has many benefits, mainly because the flexibility is greatly improved.


Figure 3 Choose to generate some or all of the code from the menu

9. Ecoreand theGenmodel. 10.

EMF is built on the basis of two meta-models, namely


Ecore: Mainly used to define the structure of the model, and provide a root object (as if the root node of the XML file) to represent the model to be displayed, the child node of the object is the package (packet), the child node is class (classes), The child node of class is attribute (the class's attribute field), so that the entire tree structure represents the EMF model structure to be represented.

Genmodel: Mainly contains information about code generation (e.g., file generation path and filename), and some control parameters to control how the code is generated.

11. Build the Project

EMF uses a model-driven development approach that allows us to generate the following things based on the model using EMF, without coding.
1, the EMF entity class corresponding to the model.
2, a variety of Itemprovider model adapter classes based on the entity class, through which the EMF model can be well bound to the JFace component, the specific description later.
3,editor-plugin project, which can be understood as a plug-in project based on EMF applications.
4, Test bundle.

1 entity classes: Like Hibernate ORM for our generated entity classes, EMF also generates such entity classes for us, and these entity classes contain more advanced features (such as: When the class attribute state changes, the entity class has the notification function)

2 Adapter classes: The model structure of EMF can be very complex (such as possibly containing multiple entity classes, and the class structure is different), so it becomes very difficult to use these entity classes directly when the view component is used (to determine which entity class, how to display the view for that entity class, etc...), Therefore, EMF generates some adapter classes for these entity classes, allowing the adapter class to design the display of the entity class by implementing the specified interface, and the view only needs to select the appropriate adaptation class.

3 Adapter Factory: Above, the view avoids direct interaction with the entity class through the adapter class, but how does the view get the appropriate adapter class? That is through the adapter factory class.

The model is defined as follows:


From the model definition, the library is the root object provided by Ecore to represent the model.

First look at the Model entity classes that EMF generates for us (assuming that these entity classes are distributed among Org.emf.demo bundles)
EMF will generate 3 package for us, namely:
Org.emf.demo.library: An interface containing entity classes, a factory for creating entity classes (Libraryfactory), and a utility class for accessing model metadata (Librarypackage)


Org.emf.demo.library.impl: Implementation class for Entity class interface, implementation class for entity class Factory (Libraryfactryimpl), Librarypackage implementation Class (Librarypackageimpl)


Org.emf.demo.library.util: Adapter factory Class (Libraryadapterfactory) for this model
Note: Libraryfactory and librarypackage are singleton patterns that can be used to get an instance of an object through its member variable einstance.

12. Run the PluginRun as Eclipse app13.14. Create a new custom model

。 EMF generates a new wizard for us, which can be used to generate a new shop instance, and note that in the third step you choose the shop class as the root type. As shown in the editor's run-screen 4, its appearance has yet to be improved, but the functionality is enough for us to edit the categories and products in the online store. In future posts, we will make some changes to the Ecore model of the online store and the interface of the editor.

In the EMF auto-generated editor, only Treeviewer is provided as an edit control, and the automatically generated code allows Treeviewer to handle redo&undo operations.


Figure 4 The default editor interface

15. Prevent overwriting of custom modifications. @generatedCallout

Note: The variables or methods generated by EMF will contain @generated annotations, and EMF will only overwrite methods that contain @generated annotations when performing the regenerate operation, and the methods you add manually will not be overwritten.

.basic methods for customizing applications)

The editor's role is to edit model instances, such as when you define a product in the store, and the editor lets you create these items (such as a Canon digital camera), or where the role of the model is reflected.

If the requirement is not high, you build a good editor the entire application is finished, your project can be finished, because you can already use it to manage your store.

However, most of the cases we are not satisfied with the generated application, such as that the generated icons are not good-looking, the menu order is unreasonable, and even the entire interface is not satisfied, and so on. Therefore, it is necessary to customize the generated application, that is, the transformation, until fully meet the requirements.

With the help of EMF, you can generate a complete application without having to write a line of code yourself, are you interested in EMF? However, the generated application looks like it came from the same mold, a multi-page editor, an outline view, and a property page, which of course does not meet everyone's needs. Do not worry, as long as the mechanism to understand the EMF, according to our requirements to modify the application is not a very difficult thing.

Let's take a look at what EMF has generated for us. According to the previous action, EMF should generate a total of four plug-in projects: Com.my.shop, Com.my.shop.edit, Com.my.shop.editor and Com.my.shop.tests, the last of which is the framework code that facilitates us to write unit tests, so let's focus on the top three items for a moment.

The first project is the model part, which mainly contains the Java interface and the default implementation class code for each type of Ecore model (EClass, which is called EClass in the Ecore meta-model, attribute is called Eattribute). For example, Product.java and Productimpl.java, respectively, are placed in Com.my.shop and Com.my.shop.impl packages; a factory class (Shopfactory) creates a new model instance using the factory schema; The Package Class (Shoppackage) maintains information about the Metamodel, provides a bunch of static member variables, and also generates the two classes of Shopadapterfactory and Shopswitch, which are optional, and they both serve to sell a xiaoguanzi for the time being.

The second item is the. Edit section, which contains a series of itemprovider classes to facilitate the display of these model objects in the various viewer (viewer) JFace, taking Categoryitemprovider as an example, It implements Istructureditemcontentprovider, Itreeitemcontentprovider, and iitemlabelprovider these interfaces, noting that the "Item" in these interface names is removed The provider you need in JFace, you can imagine these provider with the word "Item" as the jface corresponding provider packaging. With these Provider, it is convenient to use JFace Treeviewer, Tableviewer and other viewers in the application. (In one of the previous GEF posts , the EMF was used to construct the model, which was only part of the model, because the JFace viewer was not used.) So depending on your application, you can only generate parts of the model to use, and the edit section relies on the model part, and vice versa. )

The third item is the editor, which relies on the. Edit section, which mainly contains several UI-related classes. The EMF generated for us by this editor has two uses: one is that we can not start from scratch, but on the basis of this editor to make their own editor, and the other is to show how to use the first two items in the application of those classes, The editor contains so many tabs to demonstrate the use of various viewers. Here's how to customize the generated application.

. EMFthe problemATISummary

The Web doesn't seem to work.

18. Reference

EMF Introduction Series (Ii. from Model generation application) - octal - Blog Park . htm

EMF Introduction Series (Iii. basic methods for customizing applications) - octal - Blog Park . htm

EMF Introduction Series (Viii. validation of models) - octal - Blog Park . htm

Out of Eclipse environments using EMF- octal - Blog Park . htm

Build the Eclipse plug-in to Browse content in the EMF model . htm

Related Article

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.