model driven engineering : basic concepts -...

38
Model Driven Engineering : Basic Concepts Lesson 3 Model to Text Transformations : ACCELEO Guglielmo De Angelis CNR - IASI / ISTI [email protected]

Upload: hoangtram

Post on 16-Apr-2018

231 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

Model Driven Engineering : Basic Concepts

Lesson 3Model to Text Transformations : ACCELEO

Guglielmo De AngelisCNR - IASI / ISTI

[email protected]

Page 2: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

model transformationMMM

Page 3: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

model transformation

M2MMMM

Page 4: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

model transformation

M2T

M2MMMM

Page 5: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

what is ACCELEO● it is a framework supporting automatic model

transformations– programming language

– execution framework

– originally developed by Obeo, now in the Eclipse Modeling Framework

● ACCELEO has been designed following MDE principles– enabling the automatic generation of text form a given metamodel (e.g.

UML, MOF, EMF)

● available documentation :

– Acceleo 3 Web Site● http://www.eclipse.org/acceleo/

– The Official Documentation● http://www.eclipse.org/acceleo/documentation/

– The Offical Acceleo Wiki● http://wiki.eclipse.org/Acceleo

Page 6: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

about ACCELEO● ACCELEO is a framework supporting the implementation,

and the execution of MODEL 2 TEXT transformations

Page 7: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

about ACCELEO● Acceleo is a framework supporting the implementation,

and the execution of MODEL 2 TEXT transformations

AcceleoTransformator

(i.e. the program)

Acceleo Execution Engine

Page 8: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

about templates – 1

● a template can be seen as a document which it is partially specified– common structure for all the instances of the

document

– “empty fields” containing istance-specific information

● examples of documents are :– textual documents

– models

– source codes

Page 9: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

about templates – 2

Page 10: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

about templates – 3C

om

mon

Str

uct

ure

Gen

era

ted

Part

s

Page 11: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

definition

● the type of the elements which can be elaborated in each “empty field” of a template is referred context– all the possible contexts (so all the admissible type of

information) depend from the referred metamodel

Page 12: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

about templates – 4 co

nte

sto =

auth

ors

conte

xt =

title

con

test

o =

em

ail

Page 13: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

definition

● the type of the elements which can be elaborated in each “empty field” of a template is referred context– all the possible contexts (so all the admissible type of

information) depend from the referred metamodel

● example : – if we consider the metamodel of UML2, the

example of context are:● class, attribute, package, behavior, action

Page 14: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

syntax – 1● the “empty fields” represents the variable part of

a template● they are “filled” by script areas which can emit

text, according to both the context they are executed, and the given input model

● any script area is marked by means of specific tags– [<example_of_tag>] ...

[/<example_of_tag>]

– [<another_example_of_tag> … /]

Page 15: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

syntax – 2● the acceleo programming language forseen also

“comment area” :[comment ... /]

● la direttiva module, definisce il modulo Acceleo che si sta implementando ed il tipo di linguaggio dei modelli in ingresso– il metamodello può essere considerato la grammatica

sulla quale sono basati i modelli oggetto della trasformazione

[module <moduleName>(<MetaModelURI>)/]

Page 16: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

example● refer in an ACCELEO program the UML 2.0

metamodel– URI: http://www.eclipse.org/uml2/3.0.0/UML

[module   myGenerate('http://www.eclipse.org/uml2/3.0.0/UML')

/]

Page 17: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

syntax – template● the statement template declares a trasformator and it defines the elements

that will match

[template public myGenerator(instance : myType)] 

    …

[/template]

● the declaration of a template can include:

– overriding properties

– pre-conditions : ?(<condition>)

– post-treatments : post(<funzione/template>)

– inizialization : { <varName>:<type> = <val>; }● myType is the type of the parsed instance that matching will activate the script

(Package, Class, Properties, …)

● templates can be called each-other

– please always think to templates as “functions”, and design them with modularity

Page 18: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

syntax – file● the statement file declares on which file the emitted text

will be redirected

[template public myGenerator(instance : myType)/] [file(fileName, appendMode, charEncoding)] 

    …

  [/file] 

[/template]

● fileName is the file that will be created during the execution ( the ext. must be given)

● appendMode and charEncoding are optional parameters

Page 19: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

template && file – example

[template public myGenerator(uc : UseCase)]

[file (uc.name.concat('.txt'), false)]

These are all the instructions for the script “generate”.

[/file]

[/template]

Page 20: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

template && file – example

[template public myGenerator(uc : UseCase)]

[file (uc.name.concat('.txt'), false)]

These are all the instructions for the script “generate”.

[/file]

[/template]

● the attributes of an element are accessed like in most of the object-oriented notations

● but, some conditional expression will use the syntaxt “->” (see the following example on the IF statement)

– the reason is that such expression are not attributes, but functions in the OCL constraint language

Page 21: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

context[template public generate(uc : UseCase)]

  * Explicit Reference @ Name : [uc.name/]

  ­ Implicit Reference @ Name : [name/]

[/template]

● the “context” is given by the element of the metamodel (i.e. a type) that is considered for that specifc portion of code

● the “context” subsumes the visibility of the functionalities and the properties that are accessible

● it is possible to refer properties both explicitely or implicetely

Page 22: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

syntax – for● the for statement defines loops on the matching modelied

elements ● usage:

[for (<iterableExpr>) ]

This text will be generated for each element

[/for]

[for (<iterationVar> : <iteratorType> | <iterableExpr>)]

This text will be generated for each element

[/for] 

● it defines an implicit context-switch

Page 23: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

for – example● ... for any use case in the input model generate a list (e.g. HTML list

item tag) with the names of all the contained extension point

[template public generate(uc : UseCase)]

[file (uc.name.concat('.html'), false)]

   …

   <ul>

   [for (uc.extensionPoint)]

   <li>[name/]</li>

   [/for]

   </ul>

   …

[/file]

[/template]

Page 24: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

for – example● another implementation :

[template public generate(uc : UseCase)]

[file (uc.name.concat('.html'), false)]

   …

   <ul>

   [for ( e : ExtensionPoint | uc.extensionPoint ) ]

   <li>[name/]</li>

   <li>[e.name/]</li>

   [/for]

   </ul>

   …

[/file]

[/template]

Page 25: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

syntax – if ● the statement if enables conditional generations:[if (<condition>) ]

        <statements>

[else]

        <statements>

[/if]

● where condition is a boolean

● admissible operators are :– = , <> , < , <= , > , >= , or , and , not

● operands: values, objects, call to scripts, call to services, links to metamodels

Page 26: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

if – example[template public generate(uc : UseCase)]

[file (uc.name.concat('.html'), false)]

   …

  [if (uc.extensionPoint­>isEmpty())]

    <p>No Extension Points</p>

  [/fi]

   <ul>

   [for ( e : ExtensionPoint | extensionPoint ) ]

   <li>[e.name/]</li>

   [/for]

   </ul>

   …

[/file]

[/template]

Page 27: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

syntax – let● ACCELEO in mostly declarative!!! Nevertheless ...

Page 28: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

syntax – let● the statement let stores the result of an

expression within a local variable● a local variable is accessible only within the scope

of its declaration block● a local variable CANNOT have modified its value

after the initialization

[let <varName>:<VarType>=<Expression>  ]

...

[/let]

Page 29: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

ACCELEO services : advanced features

● the ACCELEO services are primitives which extend the minimal (default) language– typically they implements functionalities which are

difficult to be directly coded within a “.mtl”file

● two type of services:– native services offered by the ACCELEO platform

● among the others services for the OCL support

– services developed by the user of the platform● coded directly in Java● basically, a service is a(ny) public method offered by a Java

class ● the Java classes providing services must be

– imported within the templates– integrated with the generator

Page 30: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

ACCELEO services – example● the service indexOf :

– it can be applied to a sting R ( i.e. the input )

– it takes a string S as parameter

– it returns the position of the first occurrence of S in R

● its signature in a Java class would be :

public int indexOf (String R, String S)

● defining a service by means of a Java class:– the first parameter of (public) method is the receiver of the service

● the receiver defines the type of the objects where that service can be applied

– the other parameters of the (public) method are the (ordered) parameters of the service

– the return type of the (public) method matches with the type returned by the service

Page 31: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

ACCELEO services : how to● how to define/import services in ACCELEO?

– this part is well explained in several tutorials:

– http://wiki.eclipse.org/Acceleo/Getting_Started– receipt “I want see what happens under the hood”:

● implement a Java class● implement the public method offering the service

– ATTENTION!!: remember the convention about the receiver● link the service in Java to an ACCELEO query

● import the query, if defined in some other template

[query public <query_name>(<receiver_formale> : <type> [, <par_formale> : <type>]) : <ret_type> = invoke('<percorso_alla_classe>','<segantura_metodo>', Sequence{<receiver_formale>[, <lista_par_formali>]}) /]

[import <template_da_importare> /]

Page 32: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

ACCELEO services – example 1● write an ACCELEO service which sets to capital

the first caracther of a string

Page 33: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

ACCELEO services – example 1.1public class MyService {

   public String firstUpper(String s){        if (s.length() > 0)

        s = Character.toUpperCase(s.charAt(0)) + s.substring(1);

        return s;

     }

   …

  }

Page 34: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

ACCELEO services – example 1.2public class MyService {

   public String firstUpper(String s){        if (s.length() > 0)

        s = Character.toUpperCase(s.charAt(0)) + s.substring(1);

        return s;

     }

   …

  }

[query 

 public acceleoFirstUpper (rec : String) : String = invoke('MyService', 'firstUpper(java.lang.String)', Sequence{'rec'}) /]

Page 35: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

ACCELEO services – example 2● write an ACCELEO service which counts the

occurrences of a given char (as integer in ASCII) in an input string.

Page 36: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

ACCELEO services – example 2.1public class MyService {

  public int count(String str, int asciiCodeCar){

  int counter=0;

  char c = (char) asciiCodeCar;

  for (int i=0; i < str.length(); i++)  if (str.charAt(i) == c)  counter++;

  return counter;

}

}

Page 37: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

ACCELEO services – example 2.2public class MyService {

  public int count(String str, int asciiCodeCar){

  int counter=0;

  char c = (char) asciiCodeCar;

  for (int i=0; i < str.length(); i++)  if (str.charAt(i) == c)  counter++;

  return counter;

}

}[ query 

 public acceleoCount (rec:String,code:Integer): Integer = invoke('MyService','count(java.lang.String,int)', Sequence{'rec','code'}) 

/]

Page 38: Model Driven Engineering : Basic Concepts - CNRlabsewiki.isti.cnr.it/_media/projects/gulyx/gssi/mde20142015/lect3.pdf · Model Driven Engineering : Basic Concepts ... Model-Driven

basic biblio● Marco Brambilla, Jordi Cabot, Manuel Wimmer : Model-Driven Software

Engineering in Practice. Synthesis Lectures on Software Engineering, Morgan & Claypool Publishers (2012)

● J. Arlow, I. Neustadt: “UML2 e Unified Process - analisi e progettazione Object-Oriented”, 2a Ed., McgrawHill. 2007

● Acceleo

– The Official Documentation

● http://www.eclipse.org/acceleo/documentation/

– The Offical Acceleo Wiki

● http://wiki.eclipse.org/Acceleo

– Acceleo 3 Web Site

● http://www.eclipse.org/acceleo/