2. Model-To-Text Transformation Language chapter 9 – J Cabot
Contents
• Basicsof Model-Driven CodeGeneration
• CodeGeneration Through Programming Languages
• CodeGeneration Through M2T Transformation Languages
Template-Based Transformation Languages
Benefitsof M2T Transformation Languages
Tools
Acceleo
• Mastering CodeGeneration
2
3. Model-To-Text Transformation Language chapter 9 – J Cabot
Basic Defination
• Codegeneration hasalong tradition in softwareengineering
• Main goal of MDE: runing system out of themodel
• code-generation in compiler istheprocessof transforming
sourcecodeinto machinecode
• M2T transformation in theareaof MDE
3
4. Model-To-Text Transformation Language chapter 9 – J Cabot
MDE code-generation
• code-generation istheprocessof transforming modelsinto
sourcecode
• MDE code-generation isbuilt on top of existing compilersfor
programming languages
4
6. Model-To-Text Transformation Language chapter 9 – J Cabot
How much isgenerated?
• which partsof thecodecan beautomatically generated from
models
• onelayer of theapplication iscompletely generated while
another layer may becompletely manually
developed.
• onelayer isonly partially generated and some
missing partshaveto bemanually completed
full or only a partial ?full or only a partial ?
6
7. Model-To-Text Transformation Language chapter 9 – J Cabot
Partial code-generation
• using only codegeneration for certain partsof amodel
• other partsarenot touched by thecodegenerator and haveto
beimplemented manually.
7
8. Model-To-Text Transformation Language chapter 9 – J Cabot
What isgenerated?
• kind of sourcecodeto generate?
• conciseaspossible
• Turing test for codegenerators
• avoiding to reinvent thewheel
• thelesscodeto generatewhich isableto represent asystem,
thebetter.
8
9. Model-To-Text Transformation Language chapter 9 – J Cabot
How to generate?
• When therequirementsfor thecode-generation arespecified,
namely what hasto beachieved by thecodegenerator
• which partsaregenerated?
• which target languagesareused?
• Ho w to implement theserequirements?
9
10. Model-To-Text Transformation Language chapter 9 – J Cabot
Code-generation
• Code-generation may bedescribed asthevertical transition
from modelson ahigher-level of abstraction to lower-level
artifacts
• codegeneratorshaveto bridgethisgap in theabstraction
which may beachieved in different
ways.
10
11. Model-To-Text Transformation Language chapter 9 – J Cabot
implementation of acodegenerator
• Theimplementation of acodegenerator can be
based two approach:
1. MDE principlesapproach
2. Traditional programming approach
11
12. Model-To-Text Transformation Language chapter 9 – J Cabot
Codegeneration through programming
languages
• A codegenerator may beimplemented asaprogram
• Using themodel API to
Processtheinput models
Generateout code
Print out codestatementsto afile
12
14. Model-To-Text Transformation Language chapter 9 – J Cabot
Model API
• For each featureof themetaclasses, corresponding getter and
setter methodsaregenerated on theJavaside
• Thismeans, amodel can beread, modified, and completely
created from scratch by using Javacodeinstead of using
modeling editors.
14
15. Model-To-Text Transformation Language chapter 9 – J Cabot
GPL-based codegenerators
• wepresent how aGPL may beemployed to develop acode
generator
• By thiswedemonstrate:
1) how modelsareprocessed using amodel API generated from
themetamodel
2) highlight thefeaturesneeded to realizeacodegenerator
15
16. Model-To-Text Transformation Language chapter 9 – J Cabot
Phasesfor codegenerator
1. Load model
Modelshaveto bedeserialized from their XMI representation
to aobject graph loaded in-memory.
1. Producecode
Collect themodel information needed for generating thecode
by using themodel API to processthemodels
1. Writecode
Saved codeto theoutput file
16
21. Model-To-Text Transformation Language chapter 9 – J Cabot
Advantage
• no additional programming skillsareneeded
• It issufficient to know :
programming languagesused for developing thegenerator and
used astarget
work with themodel API.
• no additional toolsareneeded for thedesign timenor for the
runtime
21
22. Model-To-Text Transformation Language chapter 9 – J Cabot
disadvantage
• Intermingled static/dynamic code
Thereisno separation of static co de and dynamic co de
Static co de: codethat isgenerated in exactly thesameway for
every model element
packagedefinition
Imports
Dynamic code: which isderived from model information
• Missing reusablebasefunctionality
Codehasto bedeveloped for reading input modelsand
persisting output codeagain and again for each codegenerator
22
23. Model-To-Text Transformation Language chapter 9 – J Cabot
disadvantage
• Nonegraspableoutput structure
Thestructureof theoutput isnot easily graspablein thecode
generator specification
Theproblem isthat theproduced codeisembedded into the
producing code
• Missing declarativequery language
No declarativequery languagefor accessing model
information isavailable
thusmany iterators, loops, and conditionsaswell astypecasts
unnecessarily lead to ahugeamount of code.
23
24. Model-To-Text Transformation Language chapter 9 – J Cabot
Eliminatedisadvantages
• DSLshavebeen developed for generating text from
models.
• Thishasalso lead to an OMG standard called MOFMo delto
Text Transfo rmatio n Language (MOFM2T)
24
25. Model-To-Text Transformation Language chapter 9 – J Cabot
Codegeneration through M2T TL
• M2T transformation languagesaim at improving the
codegenerator development by tackling theaforestated
drawbacksof GPL-based codegenerators
25
27. Model-To-Text Transformation Language chapter 9 – J Cabot
TemplateBaseM2T TL
• meta-market:
simpletext fragmentsfor thestatic part
placeholdersand haveto beinterpreted by atemplate engine
which processesthetemplatesand queriesadditional data
sourcesto producethedynamic parts.
27
28. Model-To-Text Transformation Language chapter 9 – J Cabot
Benefit M2T Transformation Language
• Separated static/dynamic code
static text elementsshared by all artifactsaswell asdynamic
partswhich haveto befilled with information specific to each
particular case
• Explicitoutputstructure
28
29. Model-To-Text Transformation Language chapter 9 – J Cabot
Benefit M2T Transformation Language
• Declarativequery language
codeisused to accesstheinformation stored in themodels
current M2T transformation languagesalso allow to useOCL
• Reusablebasefunctionality
allow to directly read in modelsand to serializetext into files
by just defining configuration files
29
30. Model-To-Text Transformation Language chapter 9 – J Cabot
Overview of telmplate-baseTL
• Different template-based languagesexist which may be
employed to generatetext from models.
• XSLT:
XMI serializationsof themodelsmay beprocessed with XSLT
directly operating on themodel level
30
31. Model-To-Text Transformation Language chapter 9 – J Cabot
Overview of telmplate-baseTL
• JET:
JavaEmitter Template
first approachesfor developing code-generation for EMF-based
models
JET isnot limited to EMF-based models
In JET every Java-based object istransformableto text
JET templatesaretransformed to pureJavacodefor execution
purposes
31
32. Model-To-Text Transformation Language chapter 9 – J Cabot
Overview of telmplate-baseTL
• Xpand:
ThisM2T transformation languageemerged in the
openArchitectureWareproject
migrated to Eclipse
hosted asan Eclipsemodeling project
languagefor querying modelswhich isamixtureof Javaand
OCL
32
33. Model-To-Text Transformation Language chapter 9 – J Cabot
Overview of telmplate-baseTL
• MOFScript:
providing similar featuressuch asXpand
OMG standardization effort providing astandardized language
for M2T transformations
availableasan Eclipseplug-in and supportsEMF-based
models
33
34. Model-To-Text Transformation Language chapter 9 – J Cabot
Overview of telmplate-baseTL
• Acceleo
provideapragmatic version of theM2T transformation
standard of theOMG for EMF-based models
providesfull OCL support for querying models
maturetool support which hasproven useful in industry
34
35. Model-To-Text Transformation Language chapter 9 – J Cabot
Acceleo
• Acceleo isaprotagonist to demonstrateM2T transformation
languages
• practical relevanceand maturetool support
• offersatemplate-based languagefor defining code-generation
templates.
35
37. Model-To-Text Transformation Language chapter 9 – J Cabot
Acceleo tag
• Acceleo templatelanguageoffersseveral meta-markerswhich
arecalled tags
• Files
fileshaveto beopened, filled, and closed
Thepath and thefilenameareboth defined by an attributeof
thetag
37
38. Model-To-Text Transformation Language chapter 9 – J Cabot
Acceleo tag
• Control structures:
tagsfor defining control structuressuch as:
loops(for tag)
conditional branches(if tag)
• Queries
OCL queriescan bedefined (query tag)
used to factor out recurring code
38
39. Model-To-Text Transformation Language chapter 9 – J Cabot
Acceleo tag
• Expressions
generated text to producethedynamic partsof theoutput text
call other templatesto includethecodegenerated
Calling other templatescan becompared to method
callsin Java
39
40. Model-To-Text Transformation Language chapter 9 – J Cabot
Acceleo tag
• Protected areas
support projectswhereonly partial code-generation
partial code-generation supported by protected tag
Protected areasareused to mark sectionsin thegenerated code
that shall not beoverridden again by subsequent generator
runs.
Thesesectionstypically contain manually written code
40