Model Handling with EMF
Model handling with EMF
An introduction to the Eclipse Modeling Framework
ATLAS group (INRIA & LINA), University of Nantes France http://www.sciences.univ-nantes.fr/lina/atl/
-1-
Eclipse ECESIS Project
Model Handling with EMF
Context of this work
The present courseware has been elaborated in the context of the MODELWARE European IST FP6 project (http://www.modelware-ist.org/). Co-funded by the European Commission, the MODELWARE project involves 19 partners from 8 European countries. MODELWARE aims to improve software productivity by capitalizing on techniques known as Model-Driven Development (MDD). To achieve the goal of large-scale adoption of these MDD techniques, MODELWARE promotes the idea of a collaborative development of courseware dedicated to this domain. The MDD courseware provided here with the status of open source software is produced under the EPL 1.0 license.
-2-
Eclipse ECESIS Project
Model Handling with EMF
Outline
Model-Driven Engineering The Eclipse platform Handling models with EMF
Principles Model-Driven Architecture Operating on models
-3-
Eclipse ECESIS Project
Model Handling with EMF
MDE Principles
Current engineering approaches MDE approach
Models not part of engineering processes (documentation) Models as first class entities Need for dedicated tools System real world situation Model abstraction of a system
MDE basic concepts
Metamodel rules to define an abstraction
-4Eclipse ECESIS Project
Describe a given aspect of the system
Model Handling with EMF
Model-Driven Architecture
conformsTo
meta
Metametamodel
Metametamodel element
MOF
M3
meta conformsTo
Metamodel element
M2
Metamodel
meta conformsTo
Relational metamodel
UML metamodel
System
repOf
Model element
M1
Model
-5-
Eclipse ECESIS Project
Model Handling with EMF
Model-Driven Architecture: Example
conformsTo
MOF Metametamodel
source
Association
destination
Class
conformsTo
Relational Metamodel
Table
+ col
Column
* {ordered}
name: String + owner
+ keyOf 1..*
name: String
+ key *
+ type
Type
name: String
conformsTo
System
repOf
BookId
Relational Model
Title
PagesNb
AuthorId
Book
-6-
Eclipse ECESIS Project
Model Handling with EMF
Operating on Models
Model persistence
Loading/saving
Model edition
Creation/deletion/modification conformsTo/meta relations
Model navigation
-7-
Eclipse ECESIS Project
Model Handling with EMF
Outline
Model-Driven Engineering The Eclipse platform
Handling models with EMF
Plug-in architecture Platform architecture
-8-
Eclipse ECESIS Project
Model Handling with EMF
What is Eclipse?
Eclipse is a universal platform for integrating
development tools Open, extensible architecture based on plug-ins
Plug-in development environment Java development tools Eclipse Platform Standard Java2 Virtual Machine PDE JDT Platform Java VM
-9-
Eclipse ECESIS Project
Model Handling with EMF
Eclipse Plug-in Architecture (1/2)
Plug-in
Big example: HTML editor Small example: Action to create zip files
- smallest unit of Eclipse function
Extension Extension
point - named entity for collecting contributions - a contribution
Example: extension point for workbench preference UI Example: specific HTML editor preferences
- 10 -
Eclipse ECESIS Project
Model Handling with EMF
Eclipse Plug-in Architecture (2/2)
Each plug-in
Contributes to 1 or more extension points Optionally declares new extension points Depends on a set of other plug-ins Contains Java code libraries and other files May export Java-based APIs for downstream plug-ins Lives in its own plug-in subdirectory
Details spelled out in the plug-in
Manifest declares contributions Code implements contributions and provides API plugin.xml file in root of plug-in subdirectory
- 11 -
manifest
Eclipse ECESIS Project
Model Handling with EMF
Eclipse Platform Architecture
Eclipse Platform Runtime is micro-kernel
All functionality supplied by plug-ins
Eclipse Platform Runtime handles start up
Discovers plug-ins installed on disk Matches up extensions with extension points Builds global plug-in registry Caches registry on disk for next time
- 12 -
Eclipse ECESIS Project
Model Handling with EMF
Outline
Model-Driven Engineering The Eclipse platform Handling models with EMF
EMF framework Model persistence Model edition & navigation
- 13 -
Eclipse ECESIS Project
Model Handling with EMF
EMF Framework (1/2)
Object oriented framework
Edition of new EMF models
Ecore metametamodel XMI as canonical representation UML model XML schema annotated Java code
Import of existing models
Export of Ecore-based models
Java code generation
Accessor methods Operations skeletons
- 14 -
Eclipse ECESIS Project
Model Handling with EMF
EMF Framework (2/2)
Generation of programmatic facilities for generic
model handling
Model package class accessing model metadata
EClass bookClass = libraryPackage.getBook(); Book
Title: String PagesNb: int Category:String
EAttribute titleAttribute = libraryPackage.getBook_Title();
Model factory class instantiating modeled classes
Book myBook = libraryFactory.createBook();
- 15 -
Eclipse ECESIS Project
Model Handling with EMF
Model Persistence
EMF Persistence Interface
URI (Uniform Resource Identifier)
Standard for resource identification/location Common interface for different storage types
Resource
ResourceSet
Interface for Resource collections
- 16 -
Eclipse ECESIS Project
Model Handling with EMF
Model Persistence: URI Interface
URI = three parts string
Scheme Scheme-specific part Fragment
scheme
file, jar, platform, etc.
is scheme-dependent identifies a part of the contents of the resource specified by the
Examples
The library.xml resource
First book in the library.xml resource
platform:/resource/project/library.xml
platform:/resource/project/library.xml#//@books.0
- 17 -
Eclipse ECESIS Project
Model Handling with EMF
Model Persistence: Resource Interface (1/2)
EMF Resource interface
Common API for different storage types Persistent container of EObjects
EObject = base object of the EMF framework
Resource location identified by an URI
Resource types identified by URI extensions
EMF implementations
XMLResource XMIResource (default)
- 18 -
Eclipse ECESIS Project
Model Handling with EMF
Model Persistence: Resource Interface (2/2)
Resource interface (excerpt)
void load(Map /*option*/); void save(Map /*option*/); void unload();
Interactions with persistent storage
Updating contents
void getContents().add(Object); void getContents().remove(Object);
Accessing contents
EObject getEObject(String /*URIFragment*/); String getURIFromEObject(EObject);
- 19 -
Eclipse ECESIS Project
Model Handling with EMF
Model Persistence: ResourceSet Interface
ResourceSet = collection of Resources
Resources created or loaded together Resource allocation Automatic loading of cross-referenced Resources Empty Resource creation
ResourceSet interface (excerpt)
Resource createResource(URI);
Accessing contents
Resource getResource(URI, boolean /*loadOnDemand*/); EObject getEObject(URI, boolean /*loadOnDemand*/);
- 20 -
Eclipse ECESIS Project
Model Handling with EMF
Model Persistence: Example
Saving to persistent storage
Book myBook = new Book(); ResourceSet myResourceSet = new ResourceSetImpl(); Resource myResource = myResourceSet.createResource(URI.createURI(library.xml)); myResource.getContents().add(myBook); myResource.save(null);
Loading from persistent storage
ResourceSet myResourceSet = new ResourceSetImpl(); Resource myResource = myResourceSet.getResource(URI.createURI(library.xml), true);
- 21 -
Eclipse ECESIS Project
Model Handling with EMF
Model Edition & Navigation
EMF hierarchy
Ecore metamodel Ecore navigation EObject interface
EMF reflective API Dynamic EMF
Generic APIs
- 22 -
Eclipse ECESIS Project
Model Handling with EMF
Ecore Kernel
Ecore is
EClassifer +name : string EStrucuralFeature +name : string * +eSuperTypes EClass 1 EDataType
The model used to
represent EMF models
An EMF model Its own metamodel
+eOpposite
+eReferenceType 1 1 +eAttributeType 1
* EReference 0..1 +containment : bool * EAttribute
+eAttributes
A metametamodel
+eReferences
- 23 -
Eclipse ECESIS Project
Model Handling with EMF
Ecore Navigation (excerpt)
EClass a modeled class
EStructuralFeature getEStructuralFeature(String /*name*/); EList getEOperations(); boolean isInterface(); boolean isSuperTypeOf(EClass);
EStructuralFeature a class reference/attribute
Object getDefaultValue(); EClass getEContainingClass();
EReference an association end
EReference getEOpposite (); boolean isContainment();
- 24 -
Eclipse ECESIS Project
Model Handling with EMF
EObject Interface
EObject
Base interface of all modeled objects
EMF equivalent of java.lang.Object
EObject facilities (excerpt)
Model navigation
Model edition
Object eGet(EStructuralFeature); void set(EStructuralFeature, Object /*newValue*/); void eUnset(EStructuralFeature); EClass eClass(); EObject eContainer();
- 25 -
Eclipse ECESIS Project
Model Handling with EMF
EMF Reflective API
Generated programmatic facilities
Package and Factory classes generated for the model Model-specific classes
Package generic API
Generic access to models metadata
EClass getEClassifier(String /*name*/);
Factory generic API
Generic allocation of modeled objects
EObject create(EClass);
- 26 -
Eclipse ECESIS Project
Model Handling with EMF
EMF Reflective API: Example
Instantiating an object using the reflective API
EClass bookClass = libraryPackage.getEClassifier(Book); Book myBook = libraryFactory.create(bookClass); EAttribute bookTitle = bookClass.getEStructuralFeature(Title); bookClass.getEStructuralFeature(Title); myBook.eSet(bookTitle, myBook.eSet(bookTitle, KingLear); EAttribute bookPagesNb = bookClass.getEStructuralFeature(PagesNb); bookClass.getEStructuralFeature(PagesNb); myBook.eSet(bookPagesNb, new Integer(118)); EAttribute bookCategory = bookClass.getEStructuralFeature(Category); myBook.eSet(bookCategory, Theater);
Book
Title PagesNb Category King Lear 118 Theater
- 27 -
Eclipse ECESIS Project
Model Handling with EMF
Dynamic EMF
Generic programmatic facilities
Core Package/Factory classes
Handling different models in a generic way Runtime model generation
EcorePackage - access to Ecores metadata
EClass getEClass(); EAttribute getEAttribute(); EDataType getEString();
EcoreFactory - instantiate Ecore model objects
EPackage createEPackage(); EClass createEClass(); EAttribute createEAttribute();
- 28 -
Eclipse ECESIS Project
Model Handling with EMF
Dynamic EMF: Example (1/2)
Runtime model generation
Title: String
EcoreFactory ecoreFactory = EcoreFactory.eINSTANCE; EcorePackage ecorePackage = EcorePackage.eINSTANCE; EClass bookClass = ecoreFactory.createEClass(); bookClass.setName(Book); bookClass.setName(Book); EAttribute bookTitle = ecoreFactory.createEAttribute(); bookTitle.setName(Title); bookTitle.setName(Title); bookTitle.setEType(ecorePackage.getEString()); bookClass.getEAttributes().add(bookTitle); EPackage libraryPackage = ecoreFactory.createEPackage(); libraryPackage.setName(library); libraryPackage.getEClassifiers().add(bookClass);
Book
Title: String PagesNb: int Category:String
- 29 -
Eclipse ECESIS Project
Model Handling with EMF
Dynamic EMF: Example (2/2)
Using a dynamically generated model
Dynamic creation of instances
EFactory libraryFactory = libraryPackage.getEFactoryInstance(); EObject myBook = libraryFactory.create(bookClass); myBook.eSet(bookTitle, myBook.eSet(bookTitle, King Lear); myBook.eSet(bookPagesNb, new Integer(118)); myBook.eSet(bookCategory, Theater); Book
Title PagesNb Category King Lear 118 Theater
- 30 -
Eclipse ECESIS Project
Model Handling with EMF
EMF Reflective API vs. Dynamic EMF
Dynamic EMF
Type-unsafe Model-independent
Enable runtime model generation
Reflective API
Type-safe Better performance
Model-dependent
Use less memory Provide faster access to data Require generated classes to be maintained as models evolve
- 31 -
Eclipse ECESIS Project