43
Model-To-Text Transformation Language chapter 9 – J Cabot Model-to-Text Transformations Mr M.LotfiNia Email:[email protected] 1

Model-To-Text Transformation Language chapter 9 – J Cabot model driven engineering

  • Upload
    majidln

  • View
    212

  • Download
    3

Embed Size (px)

Citation preview

Model-To-Text Transformation Language chapter 9 – J Cabot

Model-to-Text TransformationsMr M.LotfiNiaEmail:[email protected]

1

Model-To-Text Transformation Language chapter 9 – J Cabot

Contents

• Basics of Model-Driven Code Generation• Code Generation Through Programming Languages• Code Generation Through M2T Transformation Languages

Template-Based Transformation Languages Benefits of M2T Transformation Languages Tools Acceleo

• Mastering Code Generation

2

Model-To-Text Transformation Language chapter 9 – J Cabot

Basic Defination

• Code generation has a long tradition in software engineering• Main goal of MDE: runing system out of the model• code-generation in compiler is the process of transforming

source code into machine code• M2T transformation in the area of MDE

3

Model-To-Text Transformation Language chapter 9 – J Cabot

MDE code-generation

• code-generation is the process of transforming models into source code

• MDE code-generation is built on top of existing compilers for programming languages

4

Model-To-Text Transformation Language chapter 9 – J Cabot

Questions

How much is generated?

How to generate?

What is generated?

5

Model-To-Text Transformation Language chapter 9 – J Cabot

How much is generated?

• which parts of the code can be automatically generated from models

• one layer of the application is completely generated while another layer may be completely manuallydeveloped.

• one layer is only partially generated and somemissing parts have to be manually completed

full or only a partial ?full or only a partial ?

6

Model-To-Text Transformation Language chapter 9 – J Cabot

Partial code-generation

• using only code generation for certain parts of a model • other parts are not touched by the code generator and have to

be implemented manually.

7

Model-To-Text Transformation Language chapter 9 – J Cabot

What is generated?

• kind of source code to generate?• concise as possible• Turing test for code generators• avoiding to reinvent the wheel• the less code to generate which is able to represent a system,

the better.

8

Model-To-Text Transformation Language chapter 9 – J Cabot

How to generate?

• When the requirements for the code-generation are specified, namely what has to be achieved by the code generator

• which parts are generated?• which target languages are used?• Ho w to implement these requirements?

9

Model-To-Text Transformation Language chapter 9 – J Cabot

Code-generation

• Code-generation may be described as the vertical transition from models on a higher-level of abstraction to lower-level artifacts

• code generators have to bridge this gap in the abstraction which may be achieved in differentways.

10

Model-To-Text Transformation Language chapter 9 – J Cabot

implementation of a code generator

• The implementation of a code generator can be based two approach:1. MDE principles approach2. Traditional programming approach

11

Model-To-Text Transformation Language chapter 9 – J Cabot

Code generation through programming languages

• A code generator may be implemented as a program• Using the model API to

Process the input models Generate out code Print out code statements to a file

12

Model-To-Text Transformation Language chapter 9 – J Cabot

Model API

• Model API automatically generated from the metamodel

13

Model-To-Text Transformation Language chapter 9 – J Cabot

Model API

• For each feature of the metaclasses, corresponding getter and setter methods are generated on the Java side

• This means, a model can be read, modified, and completely created from scratch by using Java code instead of using modeling editors.

14

Model-To-Text Transformation Language chapter 9 – J Cabot

GPL-based code generators

• we present how a GPL may be employed to develop a code generator

• By this we demonstrate:1) how models are processed using a model API generated from

the metamodel 2) highlight the features needed to realize a code generator

15

Model-To-Text Transformation Language chapter 9 – J Cabot

Phases for code generator

1. Load model Models have to be deserialized from their XMI representation

to a object graph loaded in-memory.1. Produce code

Collect the model information needed for generating the code by using the model API to process the models

1. Write code Saved code to the output file

16

Model-To-Text Transformation Language chapter 9 – J Cabot

Phases for code generator

17

Model-To-Text Transformation Language chapter 9 – J Cabot

Java-based Code Generation - Load Model

Model-To-Text Transformation Language chapter 9 – J Cabot

Java-based Code Generation - Produce Code

Model-To-Text Transformation Language chapter 9 – J Cabot

Code generator example

20

Model-To-Text Transformation Language chapter 9 – J Cabot

Advantage

• no additional programming skills are needed• It is sufficient to know :

programming languages used for developing the generator and used as target

work with the model API. • no additional tools are needed for the design time nor for the

runtime

21

Model-To-Text Transformation Language chapter 9 – J Cabot

disadvantage

• Intermingled static/dynamic code There is no separation of static co de and dynamic co de

Static co de : code that is generated in exactly the same way for every model element

package definition Imports

Dynamic code: which is derived from model information• Missing reusable base functionality

Code has to be developed for reading input models andpersisting output code again and again for each code generator

22

Model-To-Text Transformation Language chapter 9 – J Cabot

disadvantage

• None graspable output structure The structure of the output is not easily graspable in the code

generator specification The problem is that the produced code is embedded into the

producing code• Missing declarative query language

No declarative query language for accessing modelinformation is available

thus many iterators, loops, and conditions as well as type casts unnecessarily lead to a huge amount of code.

23

Model-To-Text Transformation Language chapter 9 – J Cabot

Eliminate disadvantages

• DSLs have been developed for generating text frommodels.

• This has also lead to an OMG standard called MOF Mo de l to Te xt Transfo rmatio n Languag e (MOFM2T)

24

Model-To-Text Transformation Language chapter 9 – J Cabot

Code generation through M2T TL

• M2T transformation languages aim at improving thecode generator development by tackling the afore stated drawbacks of GPL-based code generators

25

Model-To-Text Transformation Language chapter 9 – J Cabot

Template Base M2T TL

26

Model-To-Text Transformation Language chapter 9 – J Cabot

Template Base M2T TL

• me ta-marke t: simple text fragments for the static part placeholders and have to be interpreted by a template e ng ine

which processes the templates and queries additional data sources to produce the dynamic parts.

27

Model-To-Text Transformation Language chapter 9 – J Cabot

Benefit M2T Transformation Language

• Separated static/dynamic code static text elements shared by all artifacts as well as dynamic

parts which have to be filled with information specific to each particular case

• Explicit out put structure

28

Model-To-Text Transformation Language chapter 9 – J Cabot

Benefit M2T Transformation Language

• Declarative query language code is used to access the information stored in the models current M2T transformation languages also allow to use OCL

• Reusable base functionality allow to directly read in models and to serialize text into files

by just defining configuration files

29

Model-To-Text Transformation Language chapter 9 – J Cabot

Overview of telmplate-base TL

• Different template-based languages exist which may be employed to generate text from models.

• XSLT: XMI serializations of the models may be processed with XSLT directly operating on the model level

30

Model-To-Text Transformation Language chapter 9 – J Cabot

Overview of telmplate-base TL

• JET: Java Emitter Template first approaches for developing code-generation for EMF-based

models JET is not limited to EMF-based models In JET every Java-based object is transformable to text JET templates are transformed to pure Java code for execution

purposes

31

Model-To-Text Transformation Language chapter 9 – J Cabot

Overview of telmplate-base TL

• Xpand: This M2T transformation language emerged in the

openArchitectureWare project migrated to Eclipse hosted as an Eclipse modeling project language for querying models which is a mixture of Java and

OCL

32

Model-To-Text Transformation Language chapter 9 – J Cabot

Overview of telmplate-base TL

• MOFScript: providing similar features such as Xpand OMG standardization effort providing a standardized language

for M2T transformations available as an Eclipse plug-in and supports EMF-based

models

33

Model-To-Text Transformation Language chapter 9 – J Cabot

Overview of telmplate-base TL

• Acceleo provide a pragmatic version of the M2T transformation

standard of the OMG for EMF-based models provides full OCL support for querying models mature tool support which has proven useful in industry

34

Model-To-Text Transformation Language chapter 9 – J Cabot

Acceleo

• Acceleo is a protagonist to demonstrate M2T transformation languages

• practical relevance and mature tool support• offers a template-based language for defining code-generation

templates.

35

Model-To-Text Transformation Language chapter 9 – J Cabot

Acceleo

• comes with a powerful API supporting OCL • advanced functions for manipulating strings• powerful tooling such as:

editor with syntax highlighting error detection code completion refactoring, debugger, profiler

36

Model-To-Text Transformation Language chapter 9 – J Cabot

Acceleo tag

• Acceleo template language offers several meta-markers which are called tags

• Files files have to be opened, filled, and closed The path and the file name are both defined by an attribute of

the tag

37

Model-To-Text Transformation Language chapter 9 – J Cabot

Acceleo tag

• Control structures: tags for defining control structures such as:

loops (for tag) conditional branches (if tag)

• Queries OCL queries can be defined (query tag) used to factor out recurring code

38

Model-To-Text Transformation Language chapter 9 – J Cabot

Acceleo tag

• Expressions generated text to produce the dynamic parts of the output text call other templates to include the code generated Calling other templates can be compared to method

calls in Java

39

Model-To-Text Transformation Language chapter 9 – J Cabot

Acceleo tag

• Protected areas support projects where only partial code-generation partial code-generation supported by protected tag Protected areas are used to mark sections in the generated code

that shall not be overridden again by subsequent generator runs.

These sections typically contain manually written code

40

Model-To-Text Transformation Language chapter 9 – J Cabot

Acceleo code generation example

• Listing 9.2 in the book, page 135

41

Model-To-Text Transformation Language chapter 9 – J Cabot42

Model-To-Text Transformation Language chapter 9 – J Cabot43