Monday, December 10, 2007

The EMF Framework

The EMF Framework
In addition to simply increasing your productivity, building your application using EMF provides several other benefits, such as model change notification, persistence support including default XMI serialization, and a most efficient reflective API for manipulating EMF objects generically. Most important of all, EMF provides the foundation for interoperability with other EMF-based tools and applications.

2.5.1 Notification and Adapters
In Section 2.4.1, we saw that every generated EMF class is also a Notifier, that is, it can send notification whenever an attribute or reference is changed. This is an important property, allowing EMF objects to be observed, for example, to update views or other dependent objects.

Notification observers (or listeners) in EMF are called adapters because in addition to their observer status, they are often used to extend the behavior (that is, support additional interfaces without subclassing) of the object they're attached to. An adapter, as a simple observer, can be attached to any EObject (for example, PurchaseOrder) by adding to its adapter list like this:

Adapter poObserver = ...
aPurchaseOrder.eAdapters().add(poObserver);

After doing this, the notifyChanged() method will be called, on poObserver, whenever a state change occurs in the purchase order (for example, if the setBillTo() method is called), as shown in Figure 2.7.

Figure 2.7. Calling the notifyChanged() method.


Unlike simple observers, attaching an adapter as a behavior extension is normally done using an adapter factory. An adapter factory is asked to adapt an object with an extension of the required type, something like this:

PurchaseOrder aPurchaseOrder = ...
AdapterFactory somePOAdapterFactory = ...
Object poExtensionType = ...
if (somePOAdapterFactory.isFactoryForType(poExtensionType)) {
Adapter poAdapter =
somePOAdapterFactory.adapt(aPurchaseOrder, poExtensionType);
...
}

Often, the poExtensionType represents some interface supported by the adapter. For example, the argument could be the actual java.lang.Class for an interface of the chosen adapter. The returned adapter can then be downcast to the requested interface, like this:

POAdapter poAdapter =
(POAdapter)somePOAdapterFactory.adapt(someObject,
POAdapter.class);

If the adapter of the requested type is already attached to the object, then adapt() will return the existing adapter; otherwise it will create a new one. In EMF, the adapter factory is the one responsible for creating the adapter; the EMF object itself has no notion of being able to adapt itself. This approach allows greater flexibility to implement the same behavioral extension in more than one way. If instead the object were asked to adapt itself, it could only ever return one implementation for a given extension type.

As you can see, an adapter must be attached to each individual EObject that it wants to observe. Sometimes, you may be interested in being informed of state changes to any object in a containment hierarchy, a resource, or even any of a set of related resources. Rather than requiring you to walk through the hierarchy and attach your observer to each object, the EMF framework provides a very convenient adapter class, EContentAdapter, which can be used for this purpose. It can be attached to a root object, a resource, or even a resource set, and it will automatically attach itself to all the contents. It will then receive notification of state changes to any of the objects and it will even respond to content change notifications itself, by attaching or detaching itself as appropriate.

Adapters are used extensively in EMF as observers and to extend behavior. They are the foundation for the UI and command support provided by the EMF.Edit framework, as we will see in Chapter 3. We'll also look at how they work in much more detail in Chapter 13.

2.5.2 Object Persistence
The ability to persist, and reference other persisted model objects, is one of the most important benefits of EMF modeling; it's the foundation for fine-grain data integration between applications. The EMF framework provides simple, yet powerful, mechanisms for managing object persistence.

As we've seen earlier, core models are serialized using XMI. Actually, EMF includes a default XMI serializer that can be used to persist objects generically from any model, not just Ecore. Even better, if your model is defined using an XML Schema, EMF allows you to persist your objects as an XML instance document conforming to that schema. The EMF framework, combined with the code generated for your model, handles all this for you.

Above and beyond the default serialization support, EMF allows you to save your objects in any persistent form you like. In this case you'll also need to write the actual serialization code yourself, but once you do that the model will transparently be able to reference (and be referenced by) objects in other models and documents, regardless of how they're persisted.

When we looked at the properties of a generated model class in Section 2.4.1, we pointed out that there are two methods related to persistence: eContainer() and eResource(). To understand how they work, let's start with the following example:

PurchaseOrder aPurchaseOrder =
POFactory.eINSTANCE.createPurchaseOrder();
aPurchaseOrder.setBillTo("123 Maple Street");

Item aItem = POFactory.eINSTANCE.createItem();
aItem.setProductName("Apples");
aItem.setQuantity(12);
aItem.setPrice(0.50);

aPurchaseOrder.getItems().add(aItem);

Here we've created a PurchaseOrder and an Item using the generated classes from our purchase order model. We then added the Item to the items reference by calling getItems().add().

Whenever an object is added to a containment reference, which items is, it also sets the container of the added object. So, in our example, if we were to call aItem.eContainer() now, it would return the purchase order, aPurchaseOrder.[10] The purchase order itself is not in any container, so calling eContainer() on it would return null. Note also that calling the eResource() method on either object would also return null at this point.

[10] Notice how this implies that a containment association is implicitly bidirectional, even if, like the items reference, it is declared to be one-way. We'll discuss this issue in more detail in Chapter 9.

Now, to persist this pair of objects, we need to put them into a resource. Interface Resource is used to represent a physical storage location (for example, a file). To persist our objects all we need to do is add the root object (that is, the purchase order) to a resource like this:

Resource poResource = ...
poResource.getContents().add(aPurchaseOrder);

After adding the purchase order to the resource, calling eResource() on either object will return poResource. The item (aItem) is in the resource via its container (aPurchaseOrder).

Now that we've put the two objects into the resource, we can save them by simply calling save()on the resource. That seems simple enough, but where did we get the resource from in the first place? To understand how it all fits together we need to look at another important interface in the EMF framework: ResourceSet.

A ResourceSet, as its name implies, is a set of resources that are accessed together, in order to allow for potential cross-document references among them. It's also the factory for its resources. So, to complete our example, we would create the resource, add the purchase order to it, and then save it something like this[11] :

[11] If you're wondering about the call to File.getAbsolutePath(), it's used to ensure that we start with an absolute URI that will allow any cross document references that we may serialize to use relative URIs, guaranteeing that our serialized document(s) will be location independent. URIs and cross-document referencing are described in detail in Chapter 13.

ResourceSet resourceSet = new ResourceSetImpl();
URI fileURI =
URI.createFileURI(new File("mypo.xml").getAbsolutePath());
Resource poResource = resourceSet.createResource(fileURI);
poResource.getContents().add(aPurchaseOrder);
poResource.save(null);

Class ResourceSetImpl chooses the resource implementation class using an implementation registry. Resource implementations are registered, globally or local to the resource set, based on a URI scheme, file extension, or other possible criteria. If no specific resource implementation applies for the specified URI, then EMF's default XMI resource implementation will be used.

Assuming that we haven't registered a different resource implementation, then after saving our simple resource, we'd get an XMI file, mypo.xml, that looks something like this:

xmlns:xmi="http://www.omg.org/XMI"
xmlns:simplepo="http:///simplepo.ecore"
billTo="123 Maple Street">



Now that we've been able to save our model instance, let's look at how we would load it again. Loading is also done using a resource set like this:

ResourceSet resourceSet = new ResourceSetImpl();
URI fileURI =
URI.createFileURI(new File("mypo.xml").getAbsolutePath());
Resource poResource = resourceSet.getResource(fileURI, true);
PurchaseOrder aPurchaseOrder =
(PurchaseOrder)poResource.getContents().get(0);

Notice that because we know that the resource has our single purchase order at its root, we simply get the first element and downcast.

The resource set also manages demand-load for cross-document references, if there are any. When loading a resource, any cross-document references that are encountered will use a proxy object instead of the actual target. These proxies will then be resolved lazily when they are first used.

In our simple example, we actually have no cross-document references; the purchase order contains the item, so they are both in the same resource. Imagine, however, that we had modeled items as a non-containment reference like this (Figure 2.8):

Figure 2.8. items as a simple reference.


Notice the missing black diamond on the PurchaseOrder end of the association, indicating a simple reference as opposed to a by-value aggregation (containment reference). If we make this change using Java annotations instead of UML, the getItems() method would need to change to this:

/**
* @model type="Item"
*/
List getItems();

Now that items is not a containment reference, we'll need to explicitly call getContents().add() on a resource for the item, just like we previously did for the purchase order. Now, however, we have the option of adding it to the same resource as the purchase order, or to a different one. If we choose to put the items into separate resources, then demand loading would come into play, as shown in Figure 2.9.

Figure 2.9. Resource set demand-loading of resources.


In the diagram, Resource 1 (which could contain our purchase order, for example) contains cross-document references to Resource 2 (for example, containing our item). When we load Resource 1 by calling getResource() for "uri 1", any references to objects in Resource 2 (that is, "uri 2") will simply be set to proxies. A proxy is an uninitialized instance of the target class, but with the actual object's URI stored in it. Later, when we access the object, for example by calling aPurchaseOrder.getItems().get(0), Resource 2 will be demand loaded and the proxy will be resolved (that is, replaced with the target object).

Demand loading, proxies, and proxy resolution are very important features of the EMF framework. We'll explore them in greater detail in Chapters 9 and 13.

2.5.3 The Reflective EObject API
As we observed in Section 2.4.1, every generated model class implements the EMF base interface, EObject. Among other things, EObject defines a generic, reflective API for manipulating instances:

public interface EObject
{
Object eGet(EStructuralFeature feature);
void eSet(EStructuralFeature feature, Object newValue);

boolean eIsSet(EStructuralFeature feature);
void eUnset(EStructuralFeature feature);

...
}

We can use this reflective API, instead of the generated methods, to read and write the model. For example, we can set the shipTo attribute of the purchase order like this:

aPurchaseOrder.eSet(shipToAttribute, "123 Maple Street");

We can read it back like this:

String shipTo = (String)aPurchaseOrder.eGet(shipToAttribute);

We can also create a purchase order reflectively, by calling a generic create method on the factory like this:

EObject aPurchaseOrder =
poFactory.create(purchaseOrderClass);

If you're wondering where the metaobjects, purchaseOrderClass and shipToAttribute, and the poFactory come from, the answer is that you can get them using generated static accessors like this:

POPackage poPackage = POPackage.eINSTANCE;
POFactory poFactory = POFactory.eINSTANCE;
EClass purchaseOrderClass = poPackage.getPurchaseOrder();
EAttribute shipToAttribute =
poPackage.getPurchaseOrder_ShipTo();

The EMF code generator also generates efficient implementations of the reflective methods. They are slightly less efficient than the generated getShipTo() and setShipTo() methods (the reflective methods dispatch to the generated ones through a generated switch statement), but they open up the model for completely generic access. For example, the reflective methods are used by the EMF.Edit framework to implement a full set of generic commands (for example, AddCommand, RemoveCommand, SetCommand) that can be used on any model. We'll talk more about this in Chapter 3.

Notice that in addition to the eGet() and eSet() methods, the reflective EObject API includes two more methods: eIsSet() and eUnset(). The eIsSet() method can be used to find out if an attribute is set or not, while eUnset() can be used to unset or reset it. The generic XMI serializer, for example, uses eIsSet() to determine which attributes need to be serialized during a resource save operation. We'll talk more about the "unset" state, and its significance on certain models, in Chapters 5 and 9.

2.5.4 Dynamic EMF
Until now, we've only ever considered the value of EMF in generating implementations of models. Sometimes, we would like to simply share objects without requiring generated implementation classes to be available. A simple interpretive implementation would be good enough.

A particularly interesting characteristic of the reflective API is that it can also be used to manipulate instances of dynamic, non-generated, classes. Imagine if we hadn't created the purchase order model or run the EMF generator to produce the Java implementation classes in the usual way. Instead, we simply create the core model at runtime, something like this:

EPackage poPackage = EcoreFactory.eINSTANCE.createEPackage();

EClass purchaseOrderClass = EcoreFactory.eINSTANCE.createEClass();
purchaseOrderClass.setName("PurchaseOrder");
poPackage.getEClassifiers().add(purchaseOrderClass);

EClass itemClass = EcoreFactory.eINSTANCE.createEClass();
itemClass.setName("Item");
poPackage.getEClassifiers().add(itemClass);

EAttribute shipToAttribute =
EcoreFactory.eINSTANCE.createEAttribute();
shipToAttribute.setName("shipTo");
shipToAttribute.setEType(EcorePackage.eINSTANCE.getEString());
purchaseOrderClass.getEAttributes().add(shipToAttribute);

// and so on ...

Here we have an in-memory core model, for which we haven't generated any Java classes. We can now create a purchase order instance and initialize it using the same reflective calls as we used in the previous section:

EFactory poFactory = poPackage.getEFactoryInstance();
EObject aPurchaseOrder = poFactory.create(purchaseOrderClass);
aPurchaseOrder.eSet(shipToAttribute, "123 Maple Street");

Because there is no generated PurchaseOrderImpl class, the factory will create an instance of EObjectImpl instead. EObjectImpl provides a default dynamic implementation of the reflective API. As you'd expect, this implementation is slower than the generated one, but the behavior is exactly the same.

An even more interesting scenario involves a mixture of generated and dynamic classes. For example, assume that we had generated class PurchaseOrder in the usual way and now we'd like to create a dynamic subclass of it.

EClass subPOClass = EcoreFactory.eINSTANCE.createEClass();
subPOClass.setName("SubPO");
subPOClass.getESuperTypes().add(POPackage.getPurchaseOrder());
poPackage.getEClassifiers().add(subPOClass);

If we now instantiate an instance of our dynamic class SubPO, then the factory will detect the generated base class and will instantiate it instead of EObjectImpl. The significance of this is that any accesses we make to attributes or references that come from the base class will call the efficient generated implementations in class PurchaseOrderImpl:

String shipTo = aSubPO.eGet(shipToAttribute);

Only features that come from the derived (dynamic) class will use the slower dynamic implementation.

The most important point of all this is that, when using the reflective API, the presence (or lack thereof) of generated implementation classes is completely transparent. All you need is the core model in memory. If generated implementation classes are (later) added to the class path, they will then be used. From the client's perspective, the only thing that will change will be the speed of the code.

2.5.5 Foundation for Data Integration
The last few sections have shown various features of the EMF framework that support sharing of data. Section 2.5.1 described how change notification is an intrinsic property of every EMF object, and how adapters can be used to support open-ended extension. In Section 2.5.2, we showed how the EMF persistence framework uses Resources and ResourceSets to support cross-document referencing, demand-loading of documents, and arbitrary persistent forms. Finally, in Sections 2.5.3 and 2.5.4 we saw how EMF supports generic access to EMF models, including ones that may be partially or completely dynamic (that is, without generated implementation classes).

In addition to these features, the EMF framework provides a number of convenience classes and utility functions to help manage the sharing of objects. For example, a utility class for finding object cross-references (EcoreUtil.CrossReferencer and its subclasses) can be used to find any uses of an object (for example, to cleanup references when deleting the object) and any unresolved proxies in a resource, among other things.

All these features, combined with an intrinsic property of modeling—that it provides a higher-level description that can more easily be shared—provide all the needed ingredients to foster fine-grain data integration. While Eclipse itself provides a wonderful platform for integration at the UI and file level, EMF builds on this capability to enable applications to integrate at a much finer granularity than would otherwise be possible. We've seen how EMF can even be used to share data reflectively, even without using the EMF code generation support. Whether dynamic or generated, EMF models are the foundation for fine-grain data integration in Eclipse.

No comments: