Post on 04-Feb-2018
7/21/2019 Emf Tutorial
1/32
What every Eclipse Developer shouldknow about EMF
by Jonas Helming and Maximilian Koegel
Eclipseource Munich
Contact Details:Email: munich@eclipsesource.comWebsite: http://eclipsesource.com/munichPhone: +49 89 21 !"1
!his tutorial is also available as a more comprehensive EMF
training"
Abstract
#his tutorial is an intro$uction to E%& an$ e'plains the basics o( E%&. We start b) sho*in
)ou ho* to buil$ a ,er) simple $ata-centric application inclu$in the 0 base$ on E%&. We
e'plain ho* to $e(ine a mo$el in E%& an$ enerate co$e (rom it. We e'plore the P0 o( the
enerate$ co$e that is ho* to create na,iate an$ mo$i() mo$el instances.
e't *e $emonstrate ho* to buil$ a 0 base$ on this mo$el usin $atabin$in. &or oure'ample *e buil$ an application to manae a bo*lin leaue inclu$in matches an$
pla)ers. 3ater on in the tutorial *e e'plore the a$,antaes o( usin $apter&actories an$
brie(l) loo at $ata manaement in E%&. We also inclu$e a (e* pointers on the most
important a$$-on technoloies (or E%&. 0( )ou are intereste$ in ettin (ast results buil$in
an application base$ on E%& ma)be E%& Client Plat(ormis also a oo$ startin point (or
)ou see this tutorial.
#nstallation $e%uirements: #o *or throuh the e'amples )ou5ll nee$ to $o*nloa$ an$
install a (resh ,ersion o( the Eclipse Modeling !ools(rom the Eclipse Do*nloa$ Pae.
Pae 1 - What E,er) Eclipse De,eloper 6houl$ 7no* about E%& Cop)riht 2"11-2"14 Eclipse6ource
http://eclipsesource.com/munichhttp://eclipsesource.com/en/services/training/training-details/eclipse-modeling/http://eclipsesource.com/en/services/training/training-details/eclipse-modeling/http://eclipse.org/emfclient/http://eclipsesource.com/blogs/tutorials/getting-started-with-the-emf-client-platform/http://www.eclipse.org/downloads/http://eclipsesource.com/en/services/training/training-details/eclipse-modeling/http://eclipsesource.com/en/services/training/training-details/eclipse-modeling/http://eclipse.org/emfclient/http://eclipsesource.com/blogs/tutorials/getting-started-with-the-emf-client-platform/http://www.eclipse.org/downloads/http://eclipsesource.com/munich7/21/2019 Emf Tutorial
2/32
Introduction
#o ans*er the uestion What is E%&; *e5ll borro* the $escription (rom the E%& *ebsite:
#he E%& pro
7/21/2019 Emf Tutorial
3/32
Pae ! - What E,er) Eclipse De,eloper 6houl$ 7no* about E%& Cop)riht 2"11-2"14 Eclipse6ource
7/21/2019 Emf Tutorial
4/32
Example Model0n this tutorial *e *ill create an e'ample mo$el (or manain a bo*lin leaue an$ its
tournaments. 3eaue contains an arbitrar) number o( Pla)ers. #ournament consists o(
an arbitrar) number o( %atchups. Each %atchup al*a)s contains t*o Aames. Aame is a
list o( (rames Bthe score an$ is assine$ to a certain Pla)er. &inall) a #ournament has an
Enumeration that $etermines the t)pe o( #ournament.
0n the ne't section *e *ill sho* ho* to create an$ enerate co$e (rom this mo$el.
Pae 4 - What E,er) Eclipse De,eloper 6houl$ 7no* about E%& Cop)riht 2"11-2"14 Eclipse6ource
7/21/2019 Emf Tutorial
5/32
ModelingWe *ill create our e'ample mo$el in E%& to enerate the entit) classes (or our application.
#he (irst step is to create an empt) mo$elin pro
7/21/2019 Emf Tutorial
6/32
#he essential part o( the mo$elin pro
7/21/2019 Emf Tutorial
7/32
Clic &inish; to create the mo$el. 0t *ill open in the $e(ault Ecore e$itor *hich allo*s the
$e(inition o( Ecore mo$els in a tree-base$ ,ie*. #here are se,eral a$$itional options (or
$e(inin Ecore mo$els inclu$in raphical mo$elin te'tual mo$elin ?a,a annotations an$
importin (rom %3 tools. We *ill stic to the $e(ault e$itor in this tutorial an$ later brie(l)
$emonstrate the raphical e$itor (or Ecore.
0n the Ecore e$itor tree )ou can create an$ $elete mo$el elements as *ell as mo$i() the
structure o( )our mo$el ,ia $ra an$ $rop. Properties o( mo$el elements can be mo$i(ie$ in
a secon$ ,ie* *hich opens up on $ouble-clic or riht-clic 6ho* Properties Gie*;.
Hou5ll nee$ to i,e thepacae o( )our ne* mo$el a name an$ an 0. #his *ill be $one in
the properties ,ie*. #he 0 is use$ to i$enti() the mo$el later on. ame the pacae
bo*lin; set the s Pre(i' to or.eclipse.e'ample.bo*lin; an$ the s 0 to
;http://or/eclipse/e'ample/bo*lin;.
o* *e can $e(ine our mo$el elements as chil$ren o( the root pacae. Create a ne*
EClass b) riht clicin on the bo*lin pacae e* Chil$; EClass; an$ set the
name to Pla)er in the properties ,ie* o( the ne*l) create$ EClass.
Pae I - What E,er) Eclipse De,eloper 6houl$ 7no* about E%& Cop)riht 2"11-2"14 Eclipse6ource
7/21/2019 Emf Tutorial
8/32
7/21/2019 Emf Tutorial
9/32
E%& mo$els usuall) buil$ up a structure$ hierarch) that is mo$el element instances. &or
e'ample a Pla)er is containe$ in a speci(ic container ob
7/21/2019 Emf Tutorial
10/32
We can alrea$) enerate co$e (rom this (irst mo$el iteration *hich *ill be sho*n in the ne't
section. E%& can also enerate an e'ample e$itor. With this e$itor )ou can create instances
o( the enerate$ mo$el in our case instances o( 3eaues an$ Pla)ers. #his allo*s us to $o
initial testin on the mo$el b) creatin instances o( it. #hen *e can (urther re(ine an$ a$$
more Ettributes an$ Ee(erences in a secon$ iteration that *ill complete the mo$el.
Code Generation0n this step *e *ill enerate the entities (rom the Ecore (ile *e ha,e create$. ote that i( )ou
nee$ to chane )our mo$el )ou *ill be able to reenerate the entities aain. E%& can $eal
*ith simple chanes lie a$$in mo$el elements or Ettributes. 0( )ou ha,e comple'
chanes lie mo,in an attribute to another class )ou *ill ha,e to mirate e'istin
instances o( the mo$el. #his is supporte$ b) the EDP# (rame*or. Bsee
http://***.eclipse.or/e$apt/
#o enerate entities *e (irst ha,e to create a enerator mo$el. #his allo*s )ou to con(iure
properties (or the co$e eneration that are not part o( the mo$el itsel(. &or e'ample source
co$e is enerate$ (or the pluin an$ sub(ol$er as *ell.
Pae 1" - What E,er) Eclipse De,eloper 6houl$ 7no* about E%& Cop)riht 2"11-2"14 Eclipse6ource
http://www.eclipse.org/edapt/http://www.eclipse.org/edapt/7/21/2019 Emf Tutorial
11/32
iht clic the mo$el (ol$er in the pro
7/21/2019 Emf Tutorial
12/32
Pae 12 - What E,er) Eclipse De,eloper 6houl$ 7no* about E%& Cop)riht 2"11-2"14 Eclipse6ource
7/21/2019 Emf Tutorial
13/32
0n the root no$e o( the enerator mo$el )ou can set the properties (or eneratin co$e. 0n
the tree o( the enerator mo$el *e can set properties (or e,er) enerate$ entit). &or the (irst
co$e eneration *e5ll use the $e(ault settins. Jase$ on the enerator mo$el *e can no*
enerate the source co$e. E%& allo*s )ou to enerate a ma'imum o( (our $i((erent pluins
(or a $e(ine$ mo$el:
Model: #he mo$el contains all entities pacaes an$ (actories to create instances o(
the mo$el.
Edit: #he e$it pluin contains pro,i$ers to $ispla) a mo$el in a 0. &or e'ample the
pro,i$ers o((er a label (or e,er) mo$el element *hich can be use$ to $ispla) an
entit) sho*in an icon an$ a name.
Editor: #he e$itor pluin is a enerate$ e'ample e$itor to create an$ mo$i()
instances o( a mo$el.
!est: #he test pluin contains templates to *rite tests (or a mo$el.
#o enerate the pluins riht-clic on the root no$e o( the enerator mo$el an$ select the
pluin. &or our tutorial please select enerate all;.
Je(ore *e loo at the enerate$ co$e let5s start the application an$ create an entit) o( our
mo$el. iht clic on the pluin containin the ecore (ile an$ select Debu as Eclipse
pplication;. #his *ill start a ne* runtime Eclipse.
#hen in the runtime create a ne* empt) pro
7/21/2019 Emf Tutorial
14/32
iht clic the create$ pro
7/21/2019 Emf Tutorial
15/32
6elect 3eaue as the mo$el ob
7/21/2019 Emf Tutorial
16/32
#he enerate$ e$itor (or mo$el instances *ors similarl) to the Ecore e$itor. %o$el element
instances can be create$ ,ia a riht-clic an$ Ettributes can be mo$i(ie$ in the properties
,ie*. Please i,e the 3eaue a name an$ create t*o Pla)ers. n sa,e all create$ instances
are serialiKe$ in the =%0 (ile leaue.bo*lin;.
Model Refinement3et5s s*itch bac to our 0DE Eclipse en,ironment complete the mo$el an$ reenerate the
source co$e. 0n this secon$ mo$el iteration *e *ill a$$ $i((erent t)pe o( Ee(erences as *ell
as EEnums an$ %ulti-Ettributes. &irst a$$ the (ollo*in classes to the bo*lin mo$el:
#ournament
%atchup
Aame
#hese classes mo$el the results o( bo*lin tournaments an$ buil$ up a secon$ tree in our
mo$el. #here(ore *e a$$ containment Ee(erences (rom #ournament to %atchup an$ (rom
%atchup to Aame. &ollo*in the bo*lin rules a %atchup consists o( t*o Aames Beach
(rom one Pla)er. We mo$el this b) settin the upper boun$ an$ lo*er boun$ o( theEe(erence ames; o( the EClass %atchup to 2;.
Pae 1F - What E,er) Eclipse De,eloper 6houl$ 7no* about E%& Cop)riht 2"11-2"14 Eclipse6ource
7/21/2019 Emf Tutorial
17/32
We $e(ine$ the Ee(erence bet*een %atchup an$ Aame as bi-$irectional. #his means that
the re(erence can be na,iate$ (rom both en$s. #here(ore *e ha,e to create a secon$
Ee(erence (rom Aame to %atchup an$ bin$ both Ee(erences. E%& *ill tae care o( the
bi-$irectional s)nchroniKation. 0n other *or$s a$$in a %atchup to a Aame *ill automaticall)
a$$ the Aame to the %atchup.
Please a$$ an Ee(erence to Aame calle$ matchup; *ith the E#)pe %atchup;. J) settin
the Epposite to the Ee(erence ames; both Ee(erences are couple$ bi-$irectionall).
ote that the propert) Container; *ill automaticall) be set to #rue.
Pae 1I - What E,er) Eclipse De,eloper 6houl$ 7no* about E%& Cop)riht 2"11-2"14 Eclipse6ource
7/21/2019 Emf Tutorial
18/32
#he ne't step is to a$$ a cross-Ee(erence. 0n contrast to containment Ee(erences cross-
re(erence$ mo$el elements $o not contain each other. 0n our mo$el *e a$$ a cross-
re(erencin Ee(erence (rom Aame to Pla)er name$ pla)er;. 6et both container an$
containment properties to (alse;. n arbitrar) number o( ames can be assine$ to a Pla)er
no* an$ the Pla)er is still containe$ in a 3eaue.
Pae 18 - What E,er) Eclipse De,eloper 6houl$ 7no* about E%& Cop)riht 2"11-2"14 Eclipse6ource
7/21/2019 Emf Tutorial
19/32
s a (inal man$ator) step *e *ill create an EEnumeration (or the t)pe o( #ournament.
#ournament can be o( t)pe Pro; an$ mateur; in our mo$el. Please create an EEnum b)
riht-clicin on the root bo*lin mo$el pacae in the same *a) *e create$ a class. $$
t*o EEnum 3iterals to this EEnum.
#hen a$$ an Ettribute to the EClass #ournament name it t)pe; an$ set the E#)pe to
#ournament#)pe;.
#he e'ten$e$ e'ample mo$el contains more Ettributes an$ Ee(erences to be a$$e$
inclu$in all basic t)pes an$ some special cases as the %ulti-0nteer Ettribute in
#ournament. 0( )ou5$ lie )ou can also mo$el the (ollo*in (eatures:
Pla)er
heiht: EDouble
isPro(essional: EJoolean
Aame
(rames: E0nt pperJoun$ L 1"
(ter appl)in comple' chanes to the mo$el it is al*a)s a oo$ i$ea to ,ali$ate it *ith a
riht-clic on the mo$el root in the Ecore e$itor. 3et5s $o somethin *ron in the mo$el an$set the lo*er boun$ o( the Ettribute ames; Bin %atchup to !. s the upper boun$ is 2
this mo$el $oesn5t mae too much sense. #his *ill be $etecte$ b) the mo$el ,ali$ation -
somethin that is impossible in plain ?a,a co$e.
Pae 19 - What E,er) Eclipse De,eloper 6houl$ 7no* about E%& Cop)riht 2"11-2"14 Eclipse6ource
7/21/2019 Emf Tutorial
20/32
(ter this mo$el re(inement *e *ill re-enerate the co$e to re(lect our chanes. 6tart the
runtime pplication aain an$ create a secon$ mo$el tournament;. $$ a %atchup an$ t*o
Aames. #o assin the Aames to Pla)ers )ou *ill ha,e to loa$ the leaue; mo$el create$
earlier. 6elect 3oa$ esource; (rom the menu Jo*lin E$itor; an$ select the (irst mo$el
(ile. o* lin the Aames to the Pla)ers in the properties ,ie*.
Why is !his (etter than Writing )*J*s+Hou miht as Wh) shoul$ 0 use E%& instea$ o( creatin the mo$el b) *ritin plain
P?s; Without consi$erin bene(its lie the enerate$ e$itor (or rapi$ testin an$ all the
a$$itional (rame*ors a,ailable (or E%& let5s loo at t*o ,er) simple an$ e'emplar)
bene(its.
Je(ore *e loo at the enerate$ co$e B*e *ill $o that in a minute let5s consi$er the amount
o( co$e *e ha,e
7/21/2019 Emf Tutorial
21/32
EMF API0n this part o( the tutorial *e *ill e'plore E%&5s P0 inclu$in the enerate$ co$e as *ell
as E%&5s utilit) classes. 3et5s ha,e a loo at the enerate$ co$e (irst.
0n the mo$el pluin (rom our tutorial or.eclipse.e'ample.bo*lin )ou *ill (in$ inter(aces an$
implementations (or all o( the mo$el5s entities. loo at the outline o( an entit)5s inter(ace
re,eals that it contains etters an$ setters (or the attributes *e ha,e $e(ine$ in the mo$el as
*ell as etters (or the re(erences. ll entities o( the enerate$ E%& mo$el are subclasses o(
Eb
7/21/2019 Emf Tutorial
22/32
Pae 22 - What E,er) Eclipse De,eloper 6houl$ 7no* about E%& Cop)riht 2"11-2"14 Eclipse6ource
7/21/2019 Emf Tutorial
23/32
public voidtestMatchupGameRef() { Matchup matchup = BowlingFactory.eINSTANCE.createMatchup(); Game game = BowlingFactory.eINSTANCE.createGame();
matchup.getGames().add(game); assert!uals(game.e$ontainer()" matchup);#
Eb
7/21/2019 Emf Tutorial
24/32
Import Intermediate Sample SolutionJe(ore *e continue *ith the tutorial please import the interme$iate sample solution *hich
can be $o*nloa$e$here.
6*itch to an empt) *orspace B&ile 6*itch Worspace an$ select 0mport; Aeneral;
E'istin Pro
7/21/2019 Emf Tutorial
25/32
AdapterFactories&or the ne't sections o( the tutorial it is important to un$erstan$ the concept o(
$apter&actories. We *ill i,e a basic intro$uction. %ore a$,ance$ concepts are also
$escribe$ here.
#he basic (unction o( $apter&actories is to pro,i$e )ou *ith the inter(ace )ou nee$ (or a
certain purpose such as an 03abelPro,i$er nee$e$ in the 0. E%& enerates a lot o( these
classes (or )ou. #o retrie,e the riht class )ou can use an $apter&actor) implementation o(
the inter(ace )ou nee$ e.. an $apter&actor)3abelPro,i$er.
#he $apter&actor)3abelPro,i$er *ill retrie,e the enerate$ 3abelPro,i$ers (or all Eb
7/21/2019 Emf Tutorial
26/32
EMF Data Management0n the pre,ious sections *e ha,e sho*n ho* to enerate a structure$ $ata mo$el *ith E%&.
0n a t)pical application these $ata mo$els ha,e to be store$ an$ liel) also ,ersione$ an$
$istribute$. #here are a couple o( (rame*ors that support $i((erent use cases.
J) $e(ault E%& pro,i$es the abilit) to serialiKe Eb
7/21/2019 Emf Tutorial
27/32
&irst *e create an e$itin $omain. n e$itin $omain manaes a set o( interrelate$ mo$els
an$ the comman$s that are run to mo$i() them. &or e'ample it contains the stac o( all
(ormer comman$s. n e$itin $omain can create a resource *hich is a container (or storin
Eb
7/21/2019 Emf Tutorial
28/32
#he ne't step is to implement un$o. #o un$o the last comman$ all )ou5ll nee$ to $o is to call
un$o on the comman$ stac o( the e$itin $omain.
8%/errideprotected voidundo() { :: 7ndo the last change 3dapterFactoryditing6omain .getditing6omainFor(get2ournament()) .get$ommandtac*().undo();#
o* start the bo*lin application an$ create an =%0 (ile *ith the e'ample e$itor. 0t shoul$
contain a #ournament an$ se,eral %atchups an$ Aames. iht clic on the (ile an$ select
#utorial; ;pen E'ample #ournament Gie*;. 0n this ,ie* )ou can a$$ ne* #ournamentsun$o this operation an$ sa,e b) clicin on 7;. Hou can ,ali$ate the result b) openin the
(ile in the Ecore e$itor. Please note aain that the 0 o( the Gie* *ill not be up$ate$ )et but
*e *ill initialiKe the 0 in the ne't step o( the tutorial.
,dditional )ersistence Frameworks#here are se,eral (rame*ors (or storin an$ ,ersionin E%& mo$el instances. ere are
three that *e can recommen$:
E%&6toreB%o$el epositor)
CDB%o$el epositor)
Teneo(Database Back-end)
E%&6tore%ere Dialo
Pae 28 - What E,er) Eclipse De,eloper 6houl$ 7no* about E%& Cop)riht 2"11-2"14 Eclipse6ource
https://www.eclipse.org/emfstore/https://www.eclipse.org/cdo/https://wiki.eclipse.org/Teneohttp://eclipse.org/emfstorehttps://www.eclipse.org/emfstore/https://www.eclipse.org/cdo/https://wiki.eclipse.org/Teneohttp://eclipse.org/emfstore7/21/2019 Emf Tutorial
29/32
EMF -#0n this section *e *ill (ill the e'ample ,ie* *ith t*o basic 0 elements. &irst *e *ill bin$ the3abel on top sho*in the number o( %atchups in the opene$ #ournament to the mo$el. We
*ill use the noti(ication mechanism to up$ate the 3abel *hene,er the number o( %atchups
chanes. 6econ$ *e *ill (ill the #reeGie*er *ith a list o( the %atchups an$ $ispla) their
Aames as chil$ren. #o up$ate the 3abel *e *ill reister a listener on the #ournament
Eb
7/21/2019 Emf Tutorial
30/32
element. 0n our case the input is the #ournament.
ContentPro,i$er an$ especiall) 3abelPro,i$er usuall) $epen$ on a certain EClass. E%&
enerates pro,i$ers (or se,eral purposes inclu$in Content- an$ 3abelPro,i$er. We *ill use
the $apter&actor) concept e'plaine$ pre,iousl) to retrie,e the riht pro,i$er (or e,er)
element. &inall) *e set the 0nput to the #ournament that is currentl) open.
8%/errideprotected voidinitialie2ree/iewer(2ree5iewer tree5iewer) { :: initialie a 2ree5iewer to show the Matchups :: and Games of the opened 2ournament 3dapterFactory1a&elro/ider la&elro/ider =new 3dapterFactory1a&elro/ider(get3dapterFactory()); 3dapterFactory$ontentro/ider contentro/ider =new 3dapterFactory$ontentro/ider(get3dapterFactory());
tree5iewer.set1a&elro/ider(la&elro/ider); tree5iewer.set$ontentro/ider(contentro/ider); tree5iewer.set0nput(get2ournament());#
#o test the 0 (eatures
7/21/2019 Emf Tutorial
31/32
8%/erridepublic tring get2e9t(%&'ect o&'ect) { if (o&'ect instanceof Matchup) {
1istGame@ games = ((Matchup) o&'ect).getGames();
if (games >= null) { return -Matchup" GamesA - games.sie(); # #
return gettring(-+70+Matchup+type-);#
0n the runnin application the ne* 3abelPro,i$er is $ispla)e$ in the #ournament E'ample
,ie* as *ell as in the Ecore E$itor:
s a last step )ou shoul$ remo,e all listeners *hen closin the ,ie*. ote that
3abelPro,i$er an$ ContentPro,i$er are reistere$ listeners on the mo$el so )ou shoul$
$elete them as *ell.
0( )ou *ant to ha,e a loo at the (inal sample solution please import the pro
7/21/2019 Emf Tutorial
32/32
E%& Client Plat(orm a,iator an$ E$itor
$$itional (rame*ors that are *orth*hile ha,in a loo at (or creatin 0s are:
AraphitiBAraphical E$itor
Araphical %o$elin &rame*orBAraphical E$itor
E'ten$e$ E$itin &rame*orB$,ance$ Propert) Gie*
,dditional EMF'based !echnologies0n this last section o( the tutorial *e5$ lie to i,e )ou our short list o( a$$itional E%&-base$
technoloies (or )ou to e'plore:
E%& Compare(or comparin mo$els
EDP#(or miratin mo$els
E%& 0ncQuer)(or uer)in mo$els
=#e't(or te'tual mo$elin BD63s
.onclusionWe hope )ou (oun$ this tutorial help(ul. 0( )ou ha,e (ee$bac or uestions please (eel (ree tocontact us at mo$elin@eclipsesource.com.p$ates o( this tutorial *ill become a,ailable
here.
https://www.eclipse.org/graphiti/http://www.slideshare.net/mchv/gmf-create-your-graphical-dsl-eclipsecon-11http://www.slideshare.net/mchv/eef-sexy-properties-wizards-and-views-eclipsecon-11http://www.slideshare.net/mchv/eef-sexy-properties-wizards-and-views-eclipsecon-11https://www.eclipse.org/emf/compare/#comparehttps://projects.eclipse.org/projects/modeling.emft.edapthttps://projects.eclipse.org/projects/modeling.incqueryhttp://www.eclipse.org/Xtext/mailto:modeling@eclipsesource.commailto:modeling@eclipsesource.comhttp://eclipsesource.com/en/info/emf-tutorial/https://www.eclipse.org/graphiti/http://www.slideshare.net/mchv/gmf-create-your-graphical-dsl-eclipsecon-11http://www.slideshare.net/mchv/eef-sexy-properties-wizards-and-views-eclipsecon-11https://www.eclipse.org/emf/compare/#comparehttps://projects.eclipse.org/projects/modeling.emft.edapthttps://projects.eclipse.org/projects/modeling.incqueryhttp://www.eclipse.org/Xtext/mailto:modeling@eclipsesource.comhttp://eclipsesource.com/en/info/emf-tutorial/