Fundamentals of the Eclipse Modeling Framework Dave Steinberg IBM Rational Software Toronto, Canada EMF Project Committer
Goal <ul><li>Learn about modeling and how the Eclipse Modeling Framework can help you write your application in significan...
Agenda <ul><li>What is Modeling? </li></ul><ul><li>Defining a Model with EMF </li></ul><ul><li>EMF Architecture </li></ul>...
Model Driven Architecture (MDA) <ul><li>A software architecture proposed by the OMG (Object Management Group) </li></ul><u...
MDA – Are We There Yet? <ul><li>Doubts exist about the ability of MDA to deliver on its promises: </li></ul><ul><ul><li>Am...
Enter EMF! <ul><li>EMF is a simple, pragmatic approach to modeling: </li></ul><ul><ul><li>Allows us to generate some of th...
Model Driven Development with EMF <ul><li>Contrary to the belief of many programmers, modeling is useful for more than jus...
What Does EMF Provide? <ul><li>Ecore Metamodel  </li></ul><ul><ul><li>Model of models, with which other models are defined...
EMF and Java 5.0 <ul><li>EMF 2.3 began exploiting the capabilities of Java 5.0 </li></ul><ul><ul><li>Generics </li></ul></...
EMF at Eclipse.org <ul><li>Foundation for the Eclipse Modeling Project </li></ul><ul><ul><li>EMF project incorporates core...
Agenda <ul><li>What is Modeling? </li></ul><ul><li>Defining a Model with EMF </li></ul><ul><li>EMF Architecture </li></ul>...
What is an EMF “Model”? <ul><li>Specification of an application’s data </li></ul><ul><ul><li>Object attributes </li></ul><...
Model Sources <ul><li>EMF models can be defined in (at least) three ways: </li></ul><ul><ul><li>Java Interfaces </li></ul>...
Java Interfaces <ul><li>Classes can be defined completely by a subset of members, supplemented by annotations </li></ul>pu...
UML Class Diagram <ul><li>Built-in support for Rational Rose ®  (.mdl) </li></ul><ul><li>UML2 support available with UML2 ...
XML Schema <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/X...
Unifying UML, XML and Java <ul><li>All three forms provide the same information </li></ul><ul><ul><li>Different visualizat...
Agenda <ul><li>What is Modeling? </li></ul><ul><li>Defining a Model with EMF </li></ul><ul><li>EMF Architecture </li></ul>...
Ecore <ul><li>EMF’s metamodel (model of a model) </li></ul>
Ecore <ul><li>Application models (e.g. purchase order model) are instances of Ecore </li></ul>EClass (name=&quot;PurchaseO...
Ecore <ul><li>Persistent format is XMI </li></ul><ul><li>Alternate format is EMOF (Essential MOF) XMI </li></ul><eClassifi...
Model Import and Generation <ul><li>Generator features: </li></ul><ul><ul><li>Customizable, JSP-like templates (JET) </li>...
Agenda <ul><li>What is Modeling? </li></ul><ul><li>Defining a Model with EMF </li></ul><ul><li>EMF Architecture </li></ul>...
Generated Model Code <ul><li>Interface and implementation for each modeled class </li></ul><ul><ul><li>Includes get/set ac...
Feature Accessors: Change Notification <ul><li>Efficient notification from set methods </li></ul><ul><ul><li>Observer desi...
Bidirectional Reference Handshaking public interface  PurchaseOrder { ... PurchaseOrder  getNext (); void  setNext (Purcha...
Bidirectional Reference Handshaking <ul><li>Multi-step procedure implemented using InternalEObject methods: </li></ul><ul>...
Bidirectional Reference Handshaking p1.setNext(p3); p2 p1 p2 p3 change notification  previous next previous next next prev...
Reflective EObject API <ul><li>All EMF classes implement EObject interface </li></ul><ul><li>Provides an efficient API for...
Reflective EObject API <ul><li>Efficient generated switch-based implementation of reflective methods </li></ul>public Obje...
Typesafe Enums <ul><li>Typesafe enum pattern for enumerated types </li></ul>public  enum  Status implements Enumerator { P...
Other Generated Artifacts <ul><li>Model </li></ul><ul><ul><li>Package (metadata) </li></ul></ul><ul><ul><li>Factory </li><...
<ul><li>The EMF generator is a merging generator </li></ul><ul><li>@generated elements are replaced/removed </li></ul><ul>...
<ul><li>Generated methods can be extended through redirection </li></ul>Regeneration and Merge public String  getName () {...
Agenda <ul><li>What is Modeling? </li></ul><ul><li>Defining a Model with EMF </li></ul><ul><li>EMF Architecture </li></ul>...
Model Persistence <ul><li>Persisted data is referred to as a resource </li></ul><ul><li>Data can be spread out among a num...
Resource Implementations <ul><li>EMF provides a generic XML resource implementation </li></ul><ul><li>Other resource imple...
Proxy Resolution and Demand Load proxyURI=&quot;p2.xml#p2&quot; proxyURI=&quot; p2.xml #p2&quot; p1 p1.xml PurchaseOrder p...
Change Notification <ul><li>Every EObject is also a notifier </li></ul><ul><ul><li>Sends notification whenever an attribut...
Reflection <ul><li>Setting an attribute using generated API: </li></ul><ul><li>Using reflective API: </li></ul>PurchaseOrd...
Dynamic EMF <ul><li>An Ecore model can also be defined at runtime using Ecore API or loaded from persistent form </li></ul...
Change Recording <ul><li>The change model represents changes to instances of any EMF model </li></ul>
Change Recording <ul><li>Change recorder </li></ul><ul><ul><li>Adapter that creates a change description based on notifica...
Validation <ul><li>Models can define named constraints and invariants for batch validation </li></ul><ul><ul><li>Invariant...
Agenda <ul><li>What is Modeling? </li></ul><ul><li>Defining a Model with EMF </li></ul><ul><li>EMF Architecture </li></ul>...
Relational Persistence <ul><li>EMF Technology (EMFT) project provides two emerging relational persistence technologies </l...
Teneo <ul><li>Provides EMF integration with existing object/relational mapping frameworks </li></ul><ul><ul><li>Hibernate ...
Teneo Hibernate O/R Mapping <ul><li>Hibernate data store sets up mapping and creates tables in the database </li></ul><ul>...
Teneo Hibernate Integration <ul><li>Data store provides session factory, which can be used for typical Hibernate transacti...
Teneo Hibernate Resource <ul><li>Hibernate resource implementation provides familiar EMF API </li></ul><ul><li>Handles ses...
Agenda <ul><li>What is Modeling? </li></ul><ul><li>Defining a Model with EMF </li></ul><ul><li>EMF Architecture </li></ul>...
Agenda <ul><li>What is Modeling? </li></ul><ul><li>Defining a Model with EMF </li></ul><ul><li>EMF Architecture </li></ul>...
Summary <ul><li>EMF is low-cost modeling for the Java mainstream </li></ul><ul><li>Leverages the intrinsic model in an app...
Resources <ul><li>EMF documentation in Eclipse Help </li></ul><ul><ul><li>Overviews, tutorials, API reference </li></ul></...
Legal Notices IBM, Rational, and Rational Rose are registered trademarks of International Business Machines Corp. in the U...
Upcoming SlideShare
Loading in …5
×

Eclipse World 2007: Fundamentals of the Eclipse Modeling Framework

1,393 views

Published on

EclipseWorld 2007 long talk discussing the fundamentals of EMF and introducing its CDO and Teneo persistence components.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,393
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Welcome…
  • I hope to help you learn about modeling and convince you that EMF can help you write just about any application in significantly less time, simply by leveraging the data model you’ve probably already defined (even though you might not know it yet).
  • Here’s our agenda. I’ll start with some philosophy…
  • What is modeling? The Object Management Group, an industry consortium, has tried to answer that question by proposing MDA – Model Driven Architecture…
  • MDA initiative began in 2001. Some people are wondering not just “are we there yet?” but “are we ever going to get there?”…
  • This is the environment in which EMF was introduced and took hold…
  • EMF’s take is that models are everywhere, and we just need to find them, extract them, and leverage them…
  • How? With all this…
  • Also, as of last summer’s Europa release, EMF provides support for Java 5.0… Generics support includes the ability to model generic types within Ecore.
  • Where does EMF fit into the Eclipse “big picture”? EMF was one of the first projects at Eclipse outside of the Eclipse Project itself, starting in 2002 as part of the Tools project. Since then, the modeling project has formed around it…
  • Now, let’s talk some more about models in EMF: what they consist of and where they come from.
  • I’ve mentioned that EMF’s view of modeling favours simplicity over expressiveness. But, to be more concrete, here’s what we mean by a “model” in EMF…
  • EMF is very much intended to be a unifying technology. It expects models to be expressed in any number of different ways, and aims to extract its core view of the model from those different representations. Out of the box, it supports what we consider to be the most important ones…  To dwell for a moment on the notion of equivalence of different model representations, let’s consider a simple, familiar example. Imagine we wanted to represent the data in a purchase order. The non-modeler would probably start by writing some Java interfaces…
  • An interface for each type, accessors for each attribute or association (reference). This really is a definition of the model. Sometimes, we’d like to express things that aren’t captured in these signatures…
  • A graphical representation of the model is more concise. Here, we’re using UML notation…
  • There’s another important way of coming at this: by asking “how do we want to instances of the model to look when they’re serialized?” That’s what XML Schema is all about… Notice that there’s additional information in this one…
  • To reiterate, these different forms all provide the same core information about the model, or structure, of the application’s data. EMF allows you to capture this information and use it to, among other things, generate code.
  • Let’s turn to EMF’s architecture. If EMF’s purpose is to capture the core information about an application’s data model, the big question is how…
  • Not surprisingly, it uses a model… Here is a simplified picture…
  • The way EMF defines your application model, such as the purchase order we discussed previously, is by instantiating Ecore…
  • Ecore is persisted as XML, using the OMG’s standard for metadata serialization, XML Metadata Interchange… EMOF is the OMG’s equivalent to Ecore. Based on experience with EMF, MOF was split into to two layers, where EMOF is the minimal core. Because of their similarity, EMF directly supports saving Ecore as EMOF XMI and loading EMOF XMI into Ecore.
  • Here’s the pretty block diagram that will hopefully tie everything together…
  • I’ve mentioned more than once that EMF lets you generate code from a model. But what does that code look like?
  • First off, for each modeled class, an interface/implementation pair are generated. Notice that the interface looks like the Java model representation… Using interfaces allows us support for multiple inheritance… Interfaces extend EObject, the EMF equivalent of java.lang.Object…
  • Looking in the implementation class at the accessors, we see an almost minimal implementation…
  • Things get a bit more complicated if you model bidirectional associations, in order to ensure that referential integrity is maintained. To illustrate…
  • EMF implements this using a handshaking pattern… It’s not possible to turn off this handshaking bahvior or stop it part-way, without significantly altering generated code.
  • Now, looking at the example, let’s follow the whole handshaking process. If we have three purchase orders arranged like this…
  • Earlier, I mentioned the EObject interface. Primarily, it provides the API for manipulating objects reflectively…
  • These generic accessor methods are implemented using an efficient switch, so as to only impose a small constant-time overhead… Metadata in EMF is represented in two ways…
  • EMF realizes enumerated types with Java 5 enums. Previously, a class-based type-safe enum pattern was used.
  • Obviously, there’s not enough time to talk about every artifact that EMF generates, so here’s a quick list. Notice that the code is divided into four projects…
  • As I mentioned in the intro, Ecore is simple by design. Instead of trying to provide a modeling vocabulary that’s as expressive as Java…
  • The generator also supports a pattern for extending generated methods, analogous to overriding a method in a subclass…
  • Let’s talk about how you can use all this code we generated…
  • We’ll start with EMF’s resource-based persistence framework. A resource in EMF is a container for a bunch of objects that will be persisted together. A resource set provides a context for multiple.. Demand-loading is used for cross-resource references…
  • Out of the box, EMF provides a generic XML resource and a number of different specializations of it…
  • To expand on EMF’s demand-load model for cross-resource references, let’s look at another simple example…
  • Switching gears, a few words on notification. As we saw when we were discussing generated setter implementations, change notification is built in…
  • The reflective EObject interface is a powerful mechanism for data integration, as it allows your application to discover the model for objects at runtime and modify them accordingly. If you know the model ahead of time…
  • Built upon the reflective API is dynamic EMF. Rather than generating code, you can define an Ecore model at runtime and instantiate it immediately, using a generic implementation. All of the behaviours of the generated code are emulated by this implementation. You can define the model…
  • EMF provides a facility for representing and recording changes to data. As we’re big believers in eating our own dog food…
  • A change recorder is a special adapter that attaches to an entire content tree and, based on the change notifications it receives…
  • The EMF core also has a built-in framework for validation of invariants and constraints… The API for invoking validation is the Diagnostician class…
  • As I mentioned at the beginning of the talk, the modeling project is building a whole stack of technologies on top of EMF. I thought that, for this audience in particular…
  • There are two emerging technologies in the EMFT project that provide support for database persistence in EMF…
  • I’m going to focus on Teneo today, just because of time pressures and because I’m more familiar with it… … with plans to include EclipseLink. It also allows you to use ordinary, unchanged generated model implementations…
  • I’ll focus on Hibernate just because it seems to be more popular, but the capabilities and APIs for JPOX are very much analgous… A Hibernate data store manages the O/R mapping and, in fact, creates it automatically at runtime…
  • The data store then provides a session factory, which can be used in typical Hibernate transactions…
  • Alternately, Teneo provides a Hibernate resource implementation, allowing you to use the ordinary EMF persistence API…
  • Eclipse World 2007: Fundamentals of the Eclipse Modeling Framework

    1. 1. Fundamentals of the Eclipse Modeling Framework Dave Steinberg IBM Rational Software Toronto, Canada EMF Project Committer
    2. 2. Goal <ul><li>Learn about modeling and how the Eclipse Modeling Framework can help you write your application in significantly less time, simply by leveraging the data model you’ve probably already defined (although you might not know it yet) </li></ul>
    3. 3. Agenda <ul><li>What is Modeling? </li></ul><ul><li>Defining a Model with EMF </li></ul><ul><li>EMF Architecture </li></ul><ul><li>Code Generation </li></ul><ul><li>Programming with EMF </li></ul><ul><li>Relational Persistence </li></ul><ul><li>Demo </li></ul><ul><li>Summary </li></ul>
    4. 4. Model Driven Architecture (MDA) <ul><li>A software architecture proposed by the OMG (Object Management Group) </li></ul><ul><li>Application specified in high-level, Platform Independent Model (PIM) </li></ul><ul><li>Transformation technologies used to convert PIM to Platform Specific Model (PSM), implementation code </li></ul><ul><li>Includes several open modeling standards: </li></ul><ul><ul><li>UML (Unified Modeling Language) </li></ul></ul><ul><ul><li>MOF (Meta-Object Facility) </li></ul></ul><ul><ul><li>XMI (XML Metadata Interchange) </li></ul></ul><ul><ul><li>CWM (Common Warehouse Model) </li></ul></ul>
    5. 5. MDA – Are We There Yet? <ul><li>Doubts exist about the ability of MDA to deliver on its promises: </li></ul><ul><ul><li>Ambitiousness of vision </li></ul></ul><ul><ul><li>Model expressiveness vs. complexity </li></ul></ul><ul><ul><li>Availability of implementations (“vaporware”) </li></ul></ul>
    6. 6. Enter EMF! <ul><li>EMF is a simple, pragmatic approach to modeling: </li></ul><ul><ul><li>Allows us to generate some of the code that we write over and over, paving the way for more complex systems (including more ambitious MDA tools) </li></ul></ul><ul><ul><li>Models are simple, but meant to be mixed with hand-written code </li></ul></ul><ul><ul><li>It’s real, proven technology (since 2002) </li></ul></ul>
    7. 7. Model Driven Development with EMF <ul><li>Contrary to the belief of many programmers, modeling is useful for more than just documentation </li></ul><ul><li>Almost every program we write manipulates some data model </li></ul><ul><ul><li>Defined using UML, XML Schema, some other definition language, or implicitly in Java ™ </li></ul></ul><ul><li>EMF aims to extract this intrinsic “model” and generate some of the implementation code </li></ul><ul><ul><li>Can be a tremendous productivity gain </li></ul></ul>
    8. 8. What Does EMF Provide? <ul><li>Ecore Metamodel </li></ul><ul><ul><li>Model of models, with which other models are defined </li></ul></ul><ul><li>Tools for importing models and generating code </li></ul><ul><li>Runtime model support </li></ul><ul><ul><li>Reflection, notification, dynamic definition </li></ul></ul><ul><li>Persistence framework </li></ul><ul><ul><li>XML/XMI resource implementations </li></ul></ul><ul><li>Validation framework </li></ul><ul><li>Change model and change recorders </li></ul><ul><li>EMF.Edit </li></ul><ul><ul><li>UI-independent viewing and editing support </li></ul></ul><ul><ul><li>Integrated workbench or RCP model editors </li></ul></ul><ul><li>And more all the time… </li></ul>
    9. 9. EMF and Java 5.0 <ul><li>EMF 2.3 began exploiting the capabilities of Java 5.0 </li></ul><ul><ul><li>Generics </li></ul></ul><ul><ul><li>Typesafe enums </li></ul></ul><ul><ul><li>Annotations </li></ul></ul><ul><ul><li>Enhanced for loop </li></ul></ul><ul><ul><li>Autoboxing </li></ul></ul><ul><li>EMF 2.3 and later provide 5.0-targeted runtime that only runs on a Java 5.0 or later VM </li></ul><ul><li>The code generator can also produce application code that runs on a 1.4 VM against the 2.2 runtime </li></ul><ul><li>This is a recognition that Java 5.0 is the way forward, but the transition need not be painful for the user </li></ul>
    10. 10. EMF at Eclipse.org <ul><li>Foundation for the Eclipse Modeling Project </li></ul><ul><ul><li>EMF project incorporates core and additional mature components: Query, Transaction, Validation </li></ul></ul><ul><ul><li>EMF Technology project incubates complementary components: CDO, Teneo, Compare, Search… </li></ul></ul><ul><ul><li>Other projects build on EMF: Graphical Modeling Framework (GMF), Model Development Tools (MDT), Model to Model Transformation (M2M), Model to Text Transformation (M2T) … </li></ul></ul><ul><li>Other uses: Web Tools Platform (WTP), Test and Performance Tools Platform (TPTP), Data Tools Platform (DTP), Business Intelligence and Reporting Tools (BIRT)… </li></ul><ul><li>Large open source user community </li></ul>
    11. 11. Agenda <ul><li>What is Modeling? </li></ul><ul><li>Defining a Model with EMF </li></ul><ul><li>EMF Architecture </li></ul><ul><li>Code Generation </li></ul><ul><li>Programming with EMF </li></ul><ul><li>Relational Persistence </li></ul><ul><li>Demo </li></ul><ul><li>Summary </li></ul>
    12. 12. What is an EMF “Model”? <ul><li>Specification of an application’s data </li></ul><ul><ul><li>Object attributes </li></ul></ul><ul><ul><li>Relationships (associations) between objects </li></ul></ul><ul><ul><li>Operations available on each object </li></ul></ul><ul><ul><li>Simple constraints (e.g. multiplicity) on objects and relationships </li></ul></ul><ul><li>Essentially, the Class Diagram subset of UML </li></ul>
    13. 13. Model Sources <ul><li>EMF models can be defined in (at least) three ways: </li></ul><ul><ul><li>Java Interfaces </li></ul></ul><ul><ul><li>UML Class Diagram </li></ul></ul><ul><ul><li>XML Schema </li></ul></ul><ul><li>Choose the one matching your perspective or skills and EMF can create the others, as well as the implementation code </li></ul>
    14. 14. Java Interfaces <ul><li>Classes can be defined completely by a subset of members, supplemented by annotations </li></ul>public interface PurchaseOrder { String getShipTo (); void setShipTo(String value); String getBillTo (); void setBillTo(String value); List<Item> getItems (); // containment } public interface Item { String getProductName (); void setProductName(String value); int getQuantity (); void setQuantity(int value) float getPrice (); void setPrice(float value); }
    15. 15. UML Class Diagram <ul><li>Built-in support for Rational Rose ® (.mdl) </li></ul><ul><li>UML2 support available with UML2 project </li></ul>
    16. 16. XML Schema <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot; targetNamespace=&quot;http://www.example.com/SimplePO&quot; xmlns:PO=&quot;http://www.example.com/SimplePO&quot;> <xsd:complexType name=&quot; PurchaseOrder &quot;> <xsd:sequence> <xsd:element name=&quot; shipTo &quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot; billTo &quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot; items &quot; type=&quot;PO:Item&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;unbounded&quot;/> </xsd:sequence> </xsd:complexType> <xsd:complexType name=&quot; Item &quot;> <xsd:sequence> <xsd:element name=&quot; productName &quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot; quantity &quot; type=&quot;xsd:int&quot;/> <xsd:element name=&quot; price &quot; type=&quot;xsd:float&quot;/> </xsd:sequence> </xsd:complexType> </xsd:schema>
    17. 17. Unifying UML, XML and Java <ul><li>All three forms provide the same information </li></ul><ul><ul><li>Different visualization/representation </li></ul></ul><ul><ul><li>The application’s data “model” or structure </li></ul></ul><ul><ul><li>Model importers can be added for different model representations (e.g. RDB Schema) </li></ul></ul><ul><li>From a model definition, EMF can generate… </li></ul><ul><ul><li>Java implementation code, including UI </li></ul></ul><ul><ul><li>XML Schemas </li></ul></ul><ul><ul><li>Eclipse “plug-in” artifacts </li></ul></ul>
    18. 18. Agenda <ul><li>What is Modeling? </li></ul><ul><li>Defining a Model with EMF </li></ul><ul><li>EMF Architecture </li></ul><ul><li>Code Generation </li></ul><ul><li>Programming with EMF </li></ul><ul><li>Relational Persistence </li></ul><ul><li>Demo </li></ul><ul><li>Summary </li></ul>
    19. 19. Ecore <ul><li>EMF’s metamodel (model of a model) </li></ul>
    20. 20. Ecore <ul><li>Application models (e.g. purchase order model) are instances of Ecore </li></ul>EClass (name=&quot;PurchaseOrder&quot;) EClass (name=&quot;Item&quot;) EAttribute (name=&quot;shipTo&quot;) EAttribute (name=&quot;billTo&quot;) EReference (name=&quot;items&quot;) eReferenceType EAttribute (name=“productName&quot;)
    21. 21. Ecore <ul><li>Persistent format is XMI </li></ul><ul><li>Alternate format is EMOF (Essential MOF) XMI </li></ul><eClassifiers xsi:type=&quot;ecore:EClass&quot; name=&quot; PurchaseOrder &quot;> <eStructuralFeatures xsi:type=&quot;ecore:EReference&quot; name=&quot; items &quot; eType=&quot;#//Item&quot; upperBound=&quot;-1&quot; containment=&quot;true&quot;/> <eStructuralFeatures xsi:type=&quot;ecore:EAttribute&quot; name=&quot; shipTo &quot; eType=&quot;ecore:EDataType http:...Ecore#//EString&quot;/> ... </eClassifiers>
    22. 22. Model Import and Generation <ul><li>Generator features: </li></ul><ul><ul><li>Customizable, JSP-like templates (JET) </li></ul></ul><ul><ul><li>JDT-integrated, command-line or Ant </li></ul></ul><ul><ul><li>Full support for regen and merge </li></ul></ul>Ecore Model UML XML Schema Java Model I M P O R T Java Edit Java Editor GENERATE
    23. 23. Agenda <ul><li>What is Modeling? </li></ul><ul><li>Defining a Model with EMF </li></ul><ul><li>EMF Architecture </li></ul><ul><li>Code Generation </li></ul><ul><li>Programming with EMF </li></ul><ul><li>Relational Persistence </li></ul><ul><li>Demo </li></ul><ul><li>Summary </li></ul>
    24. 24. Generated Model Code <ul><li>Interface and implementation for each modeled class </li></ul><ul><ul><li>Includes get/set accessors for attributes and references </li></ul></ul>public interface PurchaseOrder extends EObject { String getShipTo(); void setShipTo(String value); String getBillTo(); void setBillTo(String value); EList<Item> getItems(); } public class PurchaseOrderImpl extends EObjectImpl implements PurchaseOrder { ... }
    25. 25. Feature Accessors: Change Notification <ul><li>Efficient notification from set methods </li></ul><ul><ul><li>Observer design pattern </li></ul></ul>public String getShipTo() { return shipTo; } public void setShipTo(String newShipTo) { String oldShipTo = shipTo; shipTo = newShipTo; if (eNotificationRequired()) eNotify(new ENotificationImpl(this, ... ); }
    26. 26. Bidirectional Reference Handshaking public interface PurchaseOrder { ... PurchaseOrder getNext (); void setNext (PurchaseOrder value); PurchaseOrder getPrevious (); void setPrevious (PurchaseOrder value); } Bidirectional reference imposes invariant: po.getNext().getPrevious() == po
    27. 27. Bidirectional Reference Handshaking <ul><li>Multi-step procedure implemented using InternalEObject methods: </li></ul><ul><ul><li>eInverseRemove() </li></ul></ul><ul><ul><li>eInverseAdd() </li></ul></ul><ul><li>Framework list implementations and generated setters directly invoke them on target objects as needed </li></ul><ul><li>Notfifications accumulated using a NotificationChain and fired off at the end </li></ul><ul><ul><li>Not used to implement the handshaking </li></ul></ul>
    28. 28. Bidirectional Reference Handshaking p1.setNext(p3); p2 p1 p2 p3 change notification previous next previous next next previous next previous
    29. 29. Reflective EObject API <ul><li>All EMF classes implement EObject interface </li></ul><ul><li>Provides an efficient API for manipulating objects reflectively </li></ul><ul><ul><li>Used by framework (e.g. persistence framework, copy utility, editing commands) </li></ul></ul><ul><ul><li>Key to integrating EMF-based tools and applications </li></ul></ul>public interface EObject { Object eGet(EStructuralFeature sf); void eSet(EStructuralFeature sf, Object val); }
    30. 30. Reflective EObject API <ul><li>Efficient generated switch-based implementation of reflective methods </li></ul>public Object eGet(int featureID, ...) { switch (featureID) { case POPackage.PURCHASE_ORDER__ITEMS: return getItems(); case POPackage.PURCHASE_ORDER__SHIP_TO: return getShipTo(); case POPackage.PURCHASE_ORDER__BILL_TO: return getBillTo(); ... } ... }
    31. 31. Typesafe Enums <ul><li>Typesafe enum pattern for enumerated types </li></ul>public enum Status implements Enumerator { PENDING(0, &quot;Pending&quot;, &quot;Pending&quot;), BACK_ORDER(1, &quot;BackOrder&quot;, &quot;BackOrder&quot;), COMPLETE(2, &quot;Complete&quot;, &quot;Complete&quot;); public static final int PENDING_VALUE = 0; public static final int BACK_ORDER_VALUE = 1; public static final int COMPLETE_VALUE = 2; private final int value; private final String name; private final String literal; private Status(int value, String name, String literal) { ... } ... }
    32. 32. Other Generated Artifacts <ul><li>Model </li></ul><ul><ul><li>Package (metadata) </li></ul></ul><ul><ul><li>Factory </li></ul></ul><ul><ul><li>Switch utility </li></ul></ul><ul><ul><li>Adapter factory base </li></ul></ul><ul><ul><li>Validator </li></ul></ul><ul><ul><li>Custom resource </li></ul></ul><ul><ul><li>XML Processor </li></ul></ul><ul><li>Edit </li></ul><ul><ul><li>Item providers </li></ul></ul><ul><ul><li>Item provider adapter factory </li></ul></ul><ul><li>Editor </li></ul><ul><ul><li>Model Wizard </li></ul></ul><ul><ul><li>Editor </li></ul></ul><ul><ul><li>Action bar contributor </li></ul></ul><ul><ul><li>Advisor (RCP) </li></ul></ul><ul><li>Tests </li></ul><ul><ul><li>Test cases </li></ul></ul><ul><ul><li>Test suite </li></ul></ul><ul><ul><li>Stand-alone example </li></ul></ul><ul><li>Manifests, plug-in classes, properties, icons... </li></ul>
    33. 33. <ul><li>The EMF generator is a merging generator </li></ul><ul><li>@generated elements are replaced/removed </li></ul><ul><li>To preserve changes, mark @generated NOT </li></ul>Regeneration and Merge /** * <!-- begin-user-doc --> * <!-- end-user-doc --> * @generated */ public String getName() { return name; }
    34. 34. <ul><li>Generated methods can be extended through redirection </li></ul>Regeneration and Merge public String getName () { return format(getNameGen()); } /** * <!-- begin-user-doc --> * <!-- end-user-doc --> * @generated */ public String getNameGen () { return name; }
    35. 35. Agenda <ul><li>What is Modeling? </li></ul><ul><li>Defining a Model with EMF </li></ul><ul><li>EMF Architecture </li></ul><ul><li>Code Generation </li></ul><ul><li>Programming with EMF </li></ul><ul><li>Relational Persistence </li></ul><ul><li>Demo </li></ul><ul><li>Summary </li></ul>
    36. 36. Model Persistence <ul><li>Persisted data is referred to as a resource </li></ul><ul><li>Data can be spread out among a number of resources, becoming a resource set </li></ul><ul><li>Proxies represent referenced objects in other resources </li></ul>
    37. 37. Resource Implementations <ul><li>EMF provides a generic XML resource implementation </li></ul><ul><li>Other resource implementations are possible </li></ul>resource = resourceSet.createResource(...&quot;p1.xml&quot;...); resource.getContents().add(p1); resource.save(...); <PurchaseOrder> <shipTo> John Doe </shipTo> <next> p2.xml#p2 </next> </PurchaseOrder> p1.xml:
    38. 38. Proxy Resolution and Demand Load proxyURI=&quot;p2.xml#p2&quot; proxyURI=&quot; p2.xml #p2&quot; p1 p1.xml PurchaseOrder p2 = p1.getNext(); next p2 p2.xml next
    39. 39. Change Notification <ul><li>Every EObject is also a notifier </li></ul><ul><ul><li>Sends notification whenever an attribute or reference is changed </li></ul></ul><ul><ul><li>Observers can update views, dependent objects </li></ul></ul><ul><ul><li>Observers are also </li></ul></ul><ul><ul><li>adapters </li></ul></ul>Adapter adapter = ... purchaseOrder.eAdapters().add(adapter);
    40. 40. Reflection <ul><li>Setting an attribute using generated API: </li></ul><ul><li>Using reflective API: </li></ul>PurchaseOrder po = ... po. setBillTo (&quot;123 Elm St.&quot;); EObject po = ... EClass poClass = po.eClass(); po. eSet (poClass.getEStructuralFeature(&quot;billTo&quot;), &quot;123 Elm St.&quot;);
    41. 41. Dynamic EMF <ul><li>An Ecore model can also be defined at runtime using Ecore API or loaded from persistent form </li></ul><ul><ul><li>No generated code required </li></ul></ul><ul><ul><li>Dynamic implementation of reflective EObject API provides same runtime behavior as generated code </li></ul></ul><ul><ul><li>Can create dynamic subclasses of generated classes </li></ul></ul><ul><li>The framework treats all model instances the same, whether generated or dynamic </li></ul>
    42. 42. Change Recording <ul><li>The change model represents changes to instances of any EMF model </li></ul>
    43. 43. Change Recording <ul><li>Change recorder </li></ul><ul><ul><li>Adapter that creates a change description based on notifications, to describe reverse delta </li></ul></ul><ul><ul><li>Provides transaction capability </li></ul></ul>ChangeRecorder changeRecorder = new ChangeRecorder(resourceSet); try { // modifications within resource set } catch (Exception e) { changeRecorder.endRecording().apply(); }
    44. 44. Validation <ul><li>Models can define named constraints and invariants for batch validation </li></ul><ul><ul><li>Invariant: defined directly on class, as <<inv>> operation </li></ul></ul><ul><ul><li>Constraint: externally defined via a validator </li></ul></ul><ul><li>In either case, body must be hand-coded </li></ul><ul><li>Diagnostician walks a containment tree of objects, dispatching to package-specific validators </li></ul>Diagnostianostic diagnostic = Diagnostician.INSTANCE.validate(eObject); if (diagnostic.getSeverity() == Diagnostic.ERROR) ...
    45. 45. Agenda <ul><li>What is Modeling? </li></ul><ul><li>Defining a Model with EMF </li></ul><ul><li>EMF Architecture </li></ul><ul><li>Code Generation </li></ul><ul><li>Programming with EMF </li></ul><ul><li>Relational Persistence </li></ul><ul><li>Demo </li></ul><ul><li>Summary </li></ul>
    46. 46. Relational Persistence <ul><li>EMF Technology (EMFT) project provides two emerging relational persistence technologies </li></ul><ul><ul><li>Connected Data Objects (CDO): custom client-server based persistence framework for distributed shared EMF models </li></ul></ul><ul><ul><li>Teneo: EMF integration with existing persistence frameworks </li></ul></ul><ul><li>See http://www.eclipse.org/modeling/emft/ </li></ul>
    47. 47. Teneo <ul><li>Provides EMF integration with existing object/relational mapping frameworks </li></ul><ul><ul><li>Hibernate </li></ul></ul><ul><ul><li>JPOX/JDO </li></ul></ul><ul><li>Documentation at http://www.elver.org/ </li></ul>
    48. 48. Teneo Hibernate O/R Mapping <ul><li>Hibernate data store sets up mapping and creates tables in the database </li></ul><ul><li>Automatic mapping can be controlled via JPA/EJB3 annotations on the Ecore model or in a separate XML document </li></ul><ul><li>Custom mapping file can be specified instead </li></ul>HbDataStore ds = HbHelper.INSTANCE.createRegisterDataStore(&quot;POStore&quot;); ds.setEPackages(new EPackage[] { POPackage.eINSTANCE }); ds.initialize();
    49. 49. Teneo Hibernate Integration <ul><li>Data store provides session factory, which can be used for typical Hibernate transactions </li></ul><ul><li>EJB3 EntityManager API also available </li></ul>SessionFactory sf = ds.getSessionFactory(); Session session = sf.openSession(); session.beginTransaction(); List orders = session.createQuery(&quot;FROM PurchaseOrder&quot;).list(); ((PurchaseOrder)orders.get(0)).setShipTo(&quot;123 Elm St.&quot;); session.getTransaction().commit(); session.close();
    50. 50. Teneo Hibernate Resource <ul><li>Hibernate resource implementation provides familiar EMF API </li></ul><ul><li>Handles sessions and transactions under the covers </li></ul>URI uri = URI.createURI(&quot;hibernate://?dsname=POStore&quot;); Resource resource = resourceSet.createResource(uri); resource.getContents().add(po); resource.save(null);
    51. 51. Agenda <ul><li>What is Modeling? </li></ul><ul><li>Defining a Model with EMF </li></ul><ul><li>EMF Architecture </li></ul><ul><li>Code Generation </li></ul><ul><li>Programming with EMF </li></ul><ul><li>Relational Persistence </li></ul><ul><li>Demo </li></ul><ul><li>Summary </li></ul>
    52. 52. Agenda <ul><li>What is Modeling? </li></ul><ul><li>Defining a Model with EMF </li></ul><ul><li>EMF Architecture </li></ul><ul><li>Code Generation </li></ul><ul><li>Programming with EMF </li></ul><ul><li>Relational Persistence </li></ul><ul><li>Demo </li></ul><ul><li>Summary </li></ul>
    53. 53. Summary <ul><li>EMF is low-cost modeling for the Java mainstream </li></ul><ul><li>Leverages the intrinsic model in an application </li></ul><ul><ul><li>No high-level modeling tools required </li></ul></ul><ul><li>Mixes modeling with programming to maximize the effectiveness of both </li></ul><ul><li>Boosts productivity and facilitates integration </li></ul><ul><li>The foundation for model-driven development and data integration in Eclipse </li></ul>
    54. 54. Resources <ul><li>EMF documentation in Eclipse Help </li></ul><ul><ul><li>Overviews, tutorials, API reference </li></ul></ul><ul><li>EMF project Web site </li></ul><ul><ul><li>http://www.eclipse.org/modeling/emf/ </li></ul></ul><ul><ul><li>Downloads, documentation, FAQ, newsgroup, Bugzilla, wiki </li></ul></ul><ul><li>Eclipse Modeling Framework , by Frank Budinsky et al. </li></ul><ul><ul><li>ISBN: 0131425420 </li></ul></ul><ul><ul><li>Second edition coming soon… </li></ul></ul>
    55. 55. Legal Notices IBM, Rational, and Rational Rose are registered trademarks of International Business Machines Corp. in the United States, other countries, or both. Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. in the United States, other countries, or both. Other company, product, or service names may be trademarks or service marks of others.

    ×