RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Discover the Eclipse Modeling Framework (EMF) and Its Dynamic Capabilities

Given a model, EMF can generate Java source code that will allow you to create, query, update, serialize, deserialize, validate, and track changes to instances of your models. EMF provides an efficient reflective API and allows you to work with dynamic, non-generated, models.

he Eclipse Modeling Framework (EMF) is a Java open source framework and code-generation facility for building tools and other applications based on a structured model. While the Eclipse Platform provides a powerful integration framework at the UI and file level, EMF enhances this capability to enable fine-grained data sharing among tools and applications.

Similar to other Java binding frameworks, e.g. JAXB or XMLBeans, given a model, EMF can generate Java source code that will allow you to create, query, update, deserialize, and serialize instances of your models. While the majority of Java binding frameworks support just one class of models, for example XML Schema, EMF supports generating code from XML Schema, UML class diagrams (Rational Rose or UML2), and annotated Java interfaces. In addition to the model code, EMF can also generate a complete application that includes a customizable editor.

The EMF-generated code has a built-in change notification mechanism and supports cross-document references. EMF provides a reflective API to access instances of your models and allows you to dynamically create models. EMF supports validation of model constraints. EMF provides powerful code generation tools that support regeneration of models and merging with user written code.

In this article, we'll explain just what the EMF is, take a look at the basic architecture.

EMF started out as an implementation of the Object Management Group's (OMG) Meta Object Facility (MOF) specification, which standardizes a metamodel for object oriented analysis and design. Over time, EMF was used to implement a large set of tools and thus evolved into an efficient Java implementation of a core subset of the MOF API.

The MOF-like core metamodel (model of a model) in EMF is called Ecore. In the current proposal for MOF 2.0, there is a similar subset of the MOF model, called Essential MOF (EMOF), which is separated out. There are small, mostly naming differences between Ecore and EMOF and therefore EMF can transparently read and write serializations of EMOF, allowing standard interchange of data between tools.

Today EMF's use is widespread. For example, EMF is used to implement the open source XML Schema Infoset Model (XSD), Service Data Objects (SDO), UML2, and Web Tools Platform (WTP) projects at Eclipse. In addition EMF is used in commercial products, such as Omondo EclipseUML and IBM Rational and WebSphere products.

Ecore and the Reflective API
One of the key interfaces in EMF is EObject, which is conceptually equivalent to java.lang.Object. All modeled objects, generated or not, implement this interface in order to provide several important features:

  • Similarly to Java's Object.getClass(), using the eClass() method, you can retrieve the metadata of the instance, i.e., its EClass.
  • On any EMF modeled object you can use the reflective API (eGet(), eSet()) to access its data. This is conceptually equivalent to Java's java.lang.reflect.Method.invoke() method, though much more efficient.
  • From any instance object you can get its container (parent) using the eContainer() method.
  • EObject also extends Notifier, which allows you to monitor all changes to the object's data.

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

As mentioned previously, EMF has its own simple metadata called Ecore. Figure 1 shows the complete class hierarchy of the Ecore metadata. In Figure 1, you can see that EPackage contains information about model classes (EClass) and data types (EDataType). EClass represents a modeled class, and specifies the attributes and references representing the data of instances. EAttribute represents simple data, specified by an EDataType. EReference represents an association between classes; its type is an EClass. EFactory contains methods to create model elements.

To find out more about EMF and Ecore please read the online overview or purchase the Eclipse Modeling Framework (EMF). The EMF Web site has several documents describing how to generate Java code from an XML Schema or UML diagram using EMF.

The next section describes an example which uses Ecore to create a simple model for companies, and then uses dynamic EMF to create, serialize and deserialize instances of this model. If you wish to follow along and you are already an Eclipse user, download and install the EMF 2.1 SDK or any newer version available on the EMF download site. Otherwise, you can download the standalone package, which includes EMF jars that do not have any dependencies on Eclipse and can be used in a standalone application.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date