ATL The ATLAS Transformation Language ATL ATLAS Transformation

  • Slides: 50
Download presentation
ATL The ATLAS Transformation Language

ATL The ATLAS Transformation Language

ATL (ATLAS Transformation Language) • ATL transformation pattern • ATL metamodel • Helpers –

ATL (ATLAS Transformation Language) • ATL transformation pattern • ATL metamodel • Helpers – Operation helpers – Attribute helpers • Transformation rules – Matched rules (Declarative rules) – Imperative features • ATL tools – Execution engine – IDE 2

Model transformation • By definition, a model transformation is the automatic creation of target

Model transformation • By definition, a model transformation is the automatic creation of target models from source models. • Model transformation is not only about M 1 to M 1 transformations: – M 1 to M 2: promotion, – M 2 to M 1: demotion, – M 3 to M 1, M 3 to M 2, etc. 3

ATL transformation pattern MOF MMa is the source metamodel MMa Ma ATL MMa 2

ATL transformation pattern MOF MMa is the source metamodel MMa Ma ATL MMa 2 MMb. atl Ma is the source model MMb MMB is the target metamodel Mb Mb is the target model 4

ATL overview • • Unidirectional transformation Source models and target models are distinct. Source

ATL overview • • Unidirectional transformation Source models and target models are distinct. Source models cannot be modified, only navigated. Target models cannot be navigated. The language is a declarative-imperative hybrid: – There are imperative or called rules. – There are declarative or matched rules. An imperative/called rule is basically a procedure. A declarative/matched rule specifies: – a source pattern to be matched in the source models, – a target pattern to be created in the target model for each match. 5

ATL metamodel 6

ATL metamodel 6

ATL helpers • The term helper comes from the OCL specification • Can only

ATL helpers • The term helper comes from the OCL specification • Can only be specified on an OCL type or a source • • • type (coming from a source metamodel) Are specified in OCL Have a name, a context and a type Can be recursively defined 7

ATL helpers • Operation helpers – Purpose : to perform navigation over source models

ATL helpers • Operation helpers – Purpose : to perform navigation over source models – Can be used to specify operations in the context of a model – element or a module Can have input parameters • Attribute helpers – Can be considered as a means to decorate source models before – – – x– transformation execution Are used to associate read-only named values to source model elements. Cannot have input parameters, their values are specified by an OCL expression. Can be associated to a transformation and are not always attached to a metamodel Can be initialized in a pass performed before running the rest of the transformation but can also be lazily evaluated when the helper value is read for the first time 8

ATL operation helper - example helper context XML!Element def: get. Children(type : Ocl. Type,

ATL operation helper - example helper context XML!Element def: get. Children(type : Ocl. Type, name : String) : Sequence(XML!Node) = type can be: XML!Element XML!Attribute XML!Text self. children->select(e | e. ocl. Is. Kind. Of(type) )->select(e | e. name = name ); <module name="My module 1"> <interface name="My interface 1"/> <module name="My module 2"/> </module> Calling this operation with get. Children(XML!Element, ’interface’) gives a sequence containing interface elements 9 A simple XML metamodel

ATL attribute helper - example all. Attributes is recursively called helper context Simple. Class!Class

ATL attribute helper - example all. Attributes is recursively called helper context Simple. Class!Class def : all. Attributes : Sequence(Simple. Class!Attribute) = self. attrs->union( if not self. parent. ocl. Is. Undefined() then self. parent. all. Attributes->select(attr | not self. attrs->exists(at | at. name = attr. name) ) else Sequence {} endif )->flatten(); 10 Simple. Class metamodel

Matched rules (declarative rules) Source pattern • The source pattern is composed of: –

Matched rules (declarative rules) Source pattern • The source pattern is composed of: – Source types coming from the source metamodels, – A guard (Boolean expression in OCL) used to filter matches. • A source pattern is evaluated to a set of matches in source models • A match corresponds to a set of elements coming from the source models that: – Are of the types specified in the source pattern (one element for – each type), Verify the guard. 11

Matched rules Target pattern • The target pattern is composed of: – A set

Matched rules Target pattern • The target pattern is composed of: – A set of target types coming from the target metamodels – For each element of this set, a set of bindings (identified by the – symbol <-). A binding specifies the initialization of a feature of a target element using an expression whose value is used to initialize the feature. • For each match, the target pattern is applied: – Elements are created in the target models (one for each type of – the target pattern), Target elements are initialized by executing the bindings: • First evaluating their value, • Then assigning this value to the corresponding property. 12

Types of matched rules • There exist three types of matched rules : –

Types of matched rules • There exist three types of matched rules : – Standard rules are applied once for every match that can be found in source models – Lazy rules are triggered by other rules. They are applied on a match as many times as it is referred to by other rules. A lazy rule may be applied multiple times on a single match, each time producing a new set of target elements – Unique lazy rules are also triggered by other rules. They are applied only once for a given match. If a unique lazy rule is triggered later on the same match the already created target elements are used 13

Matched standard rules - example Source pattern Target pattern rule Persistent. Class 2 Table{

Matched standard rules - example Source pattern Target pattern rule Persistent. Class 2 Table{ from c : Simple. Class!Class ( c. is_persistent and c. parent. ocl. Is. Undefined() ) to t : Simple. RDBMS!Table ( name <- c. name ) } 14

Matched lazy rules - example lazy rule R 1 { from s : Element

Matched lazy rules - example lazy rule R 1 { from s : Element to t : Element ( value <- [R 2. t]s ) } 15

Matched uniquely lazy rules - example unique lazy rule Feature 2 Column { from

Matched uniquely lazy rules - example unique lazy rule Feature 2 Column { from trace : Sequence(Ocl. Any) to col : Simple. RDBMS!Column ( name <- trace->iterate(e; acc : String = '' | acc + if acc = '' then '' else '_' endif + f. name), type <- trace->last(). type ) } 16

Execution order • The order in which rules are matched and applied is not

Execution order • The order in which rules are matched and applied is not specified • The order in which bindings are applied is not specified • The execution of declarative rules can however be kept deterministic: – The execution of a rule cannot change source models: it cannot – change the set of matches Target elements are not navigable: the execution of a binding cannot change the value of another 17

Execution order • Lazy rules may lead to executions that do not terminate –

Execution order • Lazy rules may lead to executions that do not terminate – Transformations run in a cycle – Recursive references are present lazy rule R 1 { from s : Element to t : Element ( value <- [R 2. t]s ) } lazy rule R 2 { from s : Element to t : Element ( value <- [R 1. t]s ) } 18

Called rules (imperative rules) • Are basically a procedure • Are invoked by its

Called rules (imperative rules) • Are basically a procedure • Are invoked by its name and may take arguments. • Its implementation can be native or specified in ATL (e. g. as a • • target pattern without source pattern since no match is needed). Can lead to executions that do not terminate due to their imperative nature Called rules and action block helper def: id : Integer = 0; rule get. Id() { -- : Integer do { this. Module. id <- this. Module. id + 1; -- increment id this. Module. id; -- returns id } } • Hybrid rules (matched rule with action block) rule Test { from s : S!Test to t : T!Test do { t. id <- this. Module. get. Id(); } } 19

ATL classification 20

ATL classification 20

ATL summary 21

ATL summary 21

Implementation of ATL • The ATL execution engine is based on a Virtual •

Implementation of ATL • The ATL execution engine is based on a Virtual • • Machine (VM). The VM executes bytecode. There is a compiler from ATL code to bytecode. • The Virtual Machine can handle models that are: – Stored in Eclipse EMF, – Or stored in Netbeans MDR. 22

ATL engine algorithm - executing rules -- START OF ALGORITHM: execute called rule marked

ATL engine algorithm - executing rules -- START OF ALGORITHM: execute called rule marked as entrypoint -- This results in a traditional imperative control flow. -- Match standard matched rules: For. Each standard rule R { For. Each candidate pattern C of R { -- a candidate pattern is a set of elements matching the -- types of the source pattern of a rule evaluate the guard of R on C If guard is true Then create target elements in target pattern of R create Trace. Link for R, C, and target elements Else discard C End. If } } -- Apply standard matched rules: For. Each Trace. Link T { R = the rule associated to T C = the matched source pattern of T P = the created target pattern of T -- Initialize elements in the target pattern: For. Each target element E of P { -- Initialize each feature of E: For. Each binding B declared for E { This algorithm does not suppose an order in § Rule matching § Creation of target elements for a match § Initialization of target elements for a Trace. Link § Initialization of target element’s features. expression = initialization expression of B value = evaluate expression in the context of C feature. Value = resolve value set feature. Value to corresponding feature of B } } execute action block of R in the context of C and T -- imperative blocks can perform any navigation in C or T and -- any action on T -- it is the programmer's responsibility to perform valid -- operations } execute called rule marked as endpoint -- We have again an imperative control flow. -- END OF ALGORITHM 23 Action block (if present) must, however, be executed after having applied the declarative part of the rule. The imperative parts of the algorithm are not completely supported yet.

The ATL Virtual Machine Architecture ATL Compiler ATL programs ATL Virtual Machine EMF MDR

The ATL Virtual Machine Architecture ATL Compiler ATL programs ATL Virtual Machine EMF MDR (Eclipse Modeling Framework) (Netbeans Meta. Data Repository) 24 Etc.

ATL Development Tools: perspective, editor and outline 25

ATL Development Tools: perspective, editor and outline 25

ATL Development Tools: launch configuration 26

ATL Development Tools: launch configuration 26

ATL Development Tools: source-level debugger 27

ATL Development Tools: source-level debugger 27

Book to Publication transformation

Book to Publication transformation

Book to Publication transformation • Metamodel Book – Contains an ordered set of Chapters.

Book to Publication transformation • Metamodel Book – Contains an ordered set of Chapters. – Chapters hold the information of the number of pages of Chapters, the authors. • The metamodel Publication – Contains a class Publication with a title and the total number of pages. • Transformation requirements – All chapters of a Book have to be visited to calculate the • sum of all pages • authors of all chapters 29

Book to Publication transformation CD of a Book metamodel CD of a Publication metamodel

Book to Publication transformation CD of a Book metamodel CD of a Publication metamodel Book Publication +title : String +nb. Pages : Integer +author : String +book *+chapters {ordered} Chapter +title : String +nb. Pages : Integer +author : String CD = Class Diagram 30

A Book metamodel in KM 3 The Book. km 3 file package Book {

A Book metamodel in KM 3 The Book. km 3 file package Book { class Book { attribute title : String; reference chapters[*] ordered container : Chapter opposite. Of book } class Chapter { attribute title : String; attribute nb. Pages : Integer; attribute author : String; reference book : Book opposite. Of chapters; } } package Primitive. Types { datatype Integer; datatype String; datatype Boolean; } 31

A Publication metamodel in KM 3 The Publication. km 3 file package Publication {

A Publication metamodel in KM 3 The Publication. km 3 file package Publication { class Publication { attribute title: String; attribute authors: String; attribute nb. Pages: Integer; } } package Primitive. Types { datatype Integer; datatype String; datatype Boolean; } To obtain both metamodels in XMI format (. ecore extension in the ATL environment) they must be injected using the injector - KM 3 file to KM 3 Ecore metamodel 32

A Book 2 Publication transformation The Book 2 Publication. atl file 1/3 module Book

A Book 2 Publication transformation The Book 2 Publication. atl file 1/3 module Book 2 Publication; create OUT : Publication from IN : Book; rule Book 2 Publication { from b : Book!Book ( b. get. Sum. Pages() > 2 -- only Books with more than 2 pages are publications ) Book Publication to out : Publication!Publication ( +title : String title <- b. title, +nb. Pages : Integer +author : String authors <- b. get. Authors(), +book nb. Pages <- b. get. Sum. Pages() ) } *+chapters {ordered} Chapter +title : String +nb. Pages : Integer +author : String 33

A Book 2 Publication transformation The Book 2 Publication. atl file 2/3 -- get.

A Book 2 Publication transformation The Book 2 Publication. atl file 2/3 -- get. Sum. Pages does the same as get. Nb. Pages, -- but it uses the OCL sum operation helper context Book!Book def : get. Sum. Pages() : Integer = self. chapters->collect(f | f. nb. Pages). sum() ; -- get. Nb. Pages collects all nb. Pages of all chapters -- and calculates the sum helper context Book!Book def : get. Nb. Pages() : Integer = self. chapters->collect(f | f. nb. Pages)-> iterate(pages; acc : Integer = 0 | acc + pages) ; Book +title : String +book *+chapters {ordered} Chapter +title : String +nb. Pages : Integer +author : String 34

A Book 2 Publication transformation -- get. Authors collects all Autors of a Book

A Book 2 Publication transformation -- get. Authors collects all Autors of a Book -- the as. Set operation removes all duplicates The Book 2 Publication. atl file 3/3 helper context Book!Book def : get. Authors() : String = self. chapters->collect(e | e. author)->as. Set()-> iterate(author. Name; acc : String = '' | acc + if acc = '' then author. Name Book else ' and ' + author. Name endif) +title : String ; +book *+chapters {ordered} Chapter +title : String +nb. Pages : Integer +author : String 35

Global view • Until here, we have : – The Book and Publication metamodels

Global view • Until here, we have : – The Book and Publication metamodels – The Book 2 Publication transformation 36

Global view M 3 EBNF ME/EMF ANTLR Ecore M 2 KM 3 grammar ATL

Global view M 3 EBNF ME/EMF ANTLR Ecore M 2 KM 3 grammar ATL grammar M 1 Book Public. km 3 Book 2 Publ. atl Book Public metamdel metamodel ATL metamodel Book 2 Publ model ATL file to ATL model A KM 3 file to KM 3 Ecore metamodel 37

Global view • Now, we need to wonder how to define the source model.

Global view • Now, we need to wonder how to define the source model. For this there exist several solutions, for instance 1. To write an XMI document. ATL transformations use as input XMI documents and generate as output XMI documents. That means that models in ATL are materialized as XMI documents. 2. To write an XML document, inject it using the XML file to XML model (Ecore) and to make a very simple transformation XML to Book. The second solution is used frequently in the ATL world. We adopt this solution in the rest of this example. 38

Global view – XML as input and XML 2 Book transformation XML ME/EMF EBNF

Global view – XML as input and XML 2 Book transformation XML ME/EMF EBNF M 3 XML schema Ecore ANTLR M 2 An XML schema M 1 input. Mod. xml XML metamodel ATL grammar input. Mod. XML. ecore XML 2 Book model XML 2 Book. atl XML file to XML model (Ecore) ATL file to ATL model It is generated automatically when an. atl file is saved 39

The input example in XML The input. Model. XML. xml file <books> <book title="livre">

The input example in XML The input. Model. XML. xml file <books> <book title="livre"> <chapter title="chapter 1" nb. Pages="13" author="toto"/> <chapter title="chapter 2" nb. Pages="17" author="toto"/> <chapter title="chapter 3" nb. Pages="20" author="titi"/> </book> <book title="article"> <chapter title="chapter 1" nb. Pages="13" author="Michel"/> <chapter title="chapter 2" nb. Pages="1" author="David"/> </books> Book +title : String +book Book metamodel *+chapters {ordered} Chapter +title : String +nb. Pages : Integer +author : String XML metamodel 40

A XML metamodel in KM 3 package XML { abstract class Node { attribute

A XML metamodel in KM 3 package XML { abstract class Node { attribute start. Line[0 -1] : Integer; attribute start. Column[0 -1] : Integer; attribute end. Line[0 -1] : Integer; attribute end. Column[0 -1] : Integer; attribute name : String; attribute value : String; reference parent[0 -1] : Element opposite. Of children; } class Attribute extends Node {} class Text extends Node {} class Element extends Node { reference children[*] ordered container : Node opposite. Of parent; } class Root extends Element {} } package Primitive. Types { datatype Boolean; datatype Integer; datatype String; } 41

A XML 2 Book transformation module XML 2 Book; create OUT : Book from

A XML 2 Book transformation module XML 2 Book; create OUT : Book from IN : XML; helper context XML!Element def: get. Attr. Val(name : String) : String = self. children->select(c | c. ocl. Is. Kind. Of(XML!Attribute ) and c. name = name)->first(). value; rule Book { from e : XML!Element ( e. name = 'book' ) to b : Book!Book ( title <- e. get. Attr. Val('title'), chapters <- e. children->select(c | c. ocl. Is. Kind. Of(XML!Element ))->as. Sequence() ) Book } +title : String rule Chapter { from e : XML!Element ( e. name = 'chapter' ) to c : Book!Chapter ( title <- e. get. Attr. Val('title'), nb. Pages <- e. get. Attr. Val('nb. Pages'). to. Integer(), author <- e. get. Attr. Val('author') )} 42 +book *+chapters {ordered} Chapter +title : String +nb. Pages : Integer +author : String

Launch configuration – transformation file 43

Launch configuration – transformation file 43

Launch configuration – model and metamodel files 44

Launch configuration – model and metamodel files 44

The generated file – a Book model 45

The generated file – a Book model 45

Global view As a last step, we have to execute our Book 2 Publication

Global view As a last step, we have to execute our Book 2 Publication transformation! ME/EMF M 3 Ecore XML ATL Book M 2 metamodel M 1 input. Mod. XML. ecore XML 2 Book model input. Mod. Book. ecore 46 Public metamodel Book 2 Publ model output. Mod. Public. ecore

Launch configuration – transformation file 47

Launch configuration – transformation file 47

Launch configuration – model and metamodel files 48

Launch configuration – model and metamodel files 48

The generated file – a Publication model 49

The generated file – a Publication model 49

Bibliography • GMT Home page. http: //eclipse. org/gmt/ • ATL at GMT Home Page.

Bibliography • GMT Home page. http: //eclipse. org/gmt/ • ATL at GMT Home Page. http: //www. eclipse. org/m 2 m/atl/ – – – ATL presentation sheet: Short presentation of ATL project. ATL Starter's Guide: Working draft of the ATL Starter's Guide. ATL User Manual: Working draft of the ATL User Manual. Specification of the ATL Virtual Machine: Working draft of the specification of the ATL Virtual Machine. ATL Transformation Description Template: Working draft of the transformation description template. • ATL home page. http: //www. sciences. univ-nantes. fr/lina/atldemo/ 50