atl the atlas transformation language. 2 atl (atlas transformation language) atl transformation...
TRANSCRIPT
ATLATLThe ATLAS Transformation
Language
2
ATL (ATLAS Transformation Language)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
3
Model transformationModel transformation
• By definition, a model transformation is the automatic creation of target models from source models.
• Model transformation is not only about M1 to M1 transformations:– M1 to M2: promotion,
– M2 to M1: demotion,
– M3 to M1, M3 to M2, etc.
4
ATL transformation patternATL transformation pattern
MOF
MMa MMb
Ma MbMMa2MMb.atl
ATL
MMa is thesourcemetamodel
Ma is the source model Mb is the target model
MMB is thetargetmetamodel
5
ATL overviewATL 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.
6
+location : String+commentsBefore : String+commentsAfter : String
Element
+name : String
Unit
Library Query
+isRefining : Boolean
Modul
ModuleElement
+name : String
Rule
+isAbstract : String+isRefining : String
MatchedRule
+varName : String+id : String
VariableDeclaration
RuleVariableDeclaration PatternElement
InPatternElement
OutPatternElement
SimpleInPatternElement IterateInPatternElement
SimpleOutPatternElement
ForEachOutPatternElement
+name : String
LibraryRef
+propertyName : String
Binding
InPattern
OutPattern
Helper
+name : String
OclMode
+unit
1
+libraries
*
+module
1
+elements *
+library0..1
+helpers*
+rule1
+inPattern
0..1
+rule
1
+outPattern0..1
+rule
1
+variables*
+outPattern
1
+elements
1..*
1
+inModels 1..*
1
+outModels 1..*
+metamodel
1
+models
*
+query0..1
+helpers
*
+inPattern
1
+elements
1..*
OclExpression
+outPattern1
+bindings*
0..1
+collection
1
Iterator
0..1
+iterator 1
0..1
+body
1
0..1
+value 1
0..1
+collection 1
OclFeatureDefinition
0..1
+definition 1
+name : String
OclFeature
Attribute
Operation
Parameter
OclContextDefinition
+operation1
+parameters
*
+definition0..1
+feature
1
+definition
1 +context_
0..1
+operation
0..1
+body
1
+name : String
OclType
+operation0..1
+returnType1
+attribute0..1
+type
1
+attribute
0..1
+initExpression
1
OclModelElement
+model1
+elements *
+definitions0..1
+context_
1
ATL metamodelATL metamodel
7
ATL helpersATL 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
8
ATL helpersATL 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
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
x
9
ATL operation helper - exampleATL operation helper - example
helper context XML!Element def:
getChildren(type : OclType, name : String) : Sequence(XML!Node) =
self.children->select(e |
e.oclIsKindOf(type)
)->select(e |
e.name = name
);
A simple XML metamodel
type can be: XML!Element XML!Attribute XML!Text
<module name="My module 1"><interface name="My interface
1"/><interface name="My interface
1"/><module name="My module 2"/>
</module>
Calling this operation with getChildren(XML!Element,’interface’)gives a sequence containing interface elements
10
ATL attribute helper - exampleATL attribute helper - example
helper context SimpleClass!Class def :
allAttributes : Sequence(SimpleClass!Attribute) =
self.attrs->union(
if not self.parent.oclIsUndefined() then
self.parent.allAttributes->select(attr |
not self.attrs->exists(at | at.name = attr.name)
)
else Sequence {}
endif
)->flatten();
SimpleClass metamodel
allAttributes is recursively called
11
Matched rules (declarative rules)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.
12
Matched rulesMatched 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.
13
Types of matched rulesTypes 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
14
Matched standard rules - exampleMatched standard rules - example
rule PersistentClass2Table{from
c : SimpleClass!Class ( c.is_persistent and
c.parent.oclIsUndefined() )
to t : SimpleRDBMS!Table ( name <- c.name ) }
Sourcepattern
Targetpattern
15
Matched lazy rules - exampleMatched lazy rules - example
lazy rule R1 {
from
s : Element
to
t : Element (
value <- [R2.t]s
)
}
16
Matched uniquely lazy rules - exampleMatched uniquely lazy rules - example
unique lazy rule Feature2Column {
from
trace : Sequence(OclAny)
to
col : SimpleRDBMS!Column (
name <- trace->iterate(e; acc : String = '' |
acc + if acc = ''
then ''
else '_' endif + f.name),
type <- trace->last().type
)
}
17
Execution orderExecution 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
18
Execution orderExecution order
• Lazy rules may lead to executions that do not terminate– Transformations run in a cycle– Recursive references are present
lazy rule R1 {from
s : Elementto
t : Element ( value <- [R2.t]s)
}
lazy rule R2 {from
s : Elementto
t : Element ( value <- [R1.t]s)
}
19
Called rules (imperative rules)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 getId() { -- : Integer do { thisModule.id <- thisModule.id + 1; -- increment id
thisModule.id; -- returns id } }
• Hybrid rules (matched rule with action block)rule Test { from s : S!Test to t : T!Test do { t.id <- thisModule.getId(); }}
20
ATL classificationATL classification
21
ATL summaryATL summary
22
Implementation of ATLImplementation 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.
23
ATL engine algorithm - executing rules 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:ForEach standard rule R {
ForEach 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 CIf guard is true Then create target elements in target pattern of R create TraceLink for R, C, and target elementsElse discard CEndIf
}}-- Apply standard matched rules:ForEach TraceLink T {
R = the rule associated to TC = the matched source pattern of TP = the created target pattern of T
-- Initialize elements in the target pattern:ForEach target element E of P {
-- Initialize each feature of E:ForEach binding B declared for E {
expression = initialization expression of Bvalue = evaluate expression in the context of
CfeatureValue = resolve valueset featureValue 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
This algorithm does not suppose an order in Rule matching Creation of target elements for a match Initialization of target elements for a TraceLink Initialization of target element’s features.
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.
24
The ATL Virtual Machine ArchitectureThe ATL Virtual Machine Architecture
ATL Virtual Machine
EMF
(Eclipse Modeling Framework)
MDR
(Netbeans
MetaData Repository)
Etc.
ATL Compiler ATL programs
25
ATL Development Tools: perspective, ATL Development Tools: perspective, editor and outlineeditor and outline
26
ATL Development Tools: launch ATL Development Tools: launch configurationconfiguration
27
ATL Development Tools: source-level ATL Development Tools: source-level debuggerdebugger
Book to PublicationBook to Publicationtransformationtransformation
29
Book to Publication transformationBook 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
30
Book to Publication transformationBook to Publication transformation
CD of a Book metamodel CD of a Publication metamodel
CD = Class Diagram
+title : String+nbPages : Integer+author : String
Chapter
+title : String
Book
*+chapters{ordered}
+book
+title : String+nbPages : Integer+author : String
Publication
31
A Book metamodel in KM3A Book metamodel in KM3
package Book {
class Book { attribute title : String;
reference chapters[*] ordered container : Chapter oppositeOf book}
class Chapter {attribute title : String;attribute nbPages : Integer;attribute author : String;reference book : Book oppositeOf chapters;
}}
package PrimitiveTypes {datatype Integer;datatype String;datatype Boolean;
}
The Book.km3 file
32
A Publication metamodel in KM3A Publication metamodel in KM3
package Publication {
class Publication { attribute title: String;
attribute authors: String;attribute nbPages: Integer;
}}
package PrimitiveTypes {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- KM3 file to KM3 Ecore metamodel
The Publication.km3 file
33
A Book2Publication transformationA Book2Publication transformation
module Book2Publication;create OUT : Publication from IN : Book;
rule Book2Publication {from
b : Book!Book (b.getSumPages() > 2-- only Books with more than 2 pages are publications
) to
out : Publication!Publication ( title <- b.title,
authors <- b.getAuthors(), nbPages <- b.getSumPages())
}
+title : String+nbPages : Integer+author : String
Chapter
+title : String
Book
*+chapters{ordered}
+book
+title : String+nbPages : Integer+author : String
Publication
The Book2Publication.atl file 1/3
34
A Book2Publication transformationA Book2Publication transformation
-- getSumPages does the same as getNbPages,-- but it uses the OCL sum operation
helper context Book!Book def : getSumPages() : Integer =self.chapters->collect(f | f.nbPages).sum()
;
-- getNbPages collects all nbPages of all chapters-- and calculates the sum
helper context Book!Book def : getNbPages() : Integer =self.chapters->collect(f | f.nbPages)->
iterate(pages; acc : Integer = 0 | acc + pages)
;
+title : String+nbPages : Integer+author : String
Chapter
+title : String
Book
*+chapters{ordered}
+book
The Book2Publication.atl file 2/3
35
A Book2Publication transformationA Book2Publication transformation
-- getAuthors collects all Autors of a Book-- the asSet operation removes all duplicates
helper context Book!Book def : getAuthors() : String = self.chapters->collect(e | e.author)->asSet()->
iterate(authorName; acc : String = '' | acc + if acc = '' then authorName else ' and ' + authorName endif)
;
+title : String+nbPages : Integer+author : String
Chapter
+title : String
Book
*+chapters{ordered}
+book
The Book2Publication.atl file 3/3
36
Global viewGlobal view
• Until here, we have :– The Book and Publication metamodels
– The Book2Publication transformation
37
Global viewGlobal view
M1
M2
M3
KM3grammar
EBNF ME/EMF
ANTLR Ecore
ATLgrammar
ATLmetamodel
Book2Publ.atl
Book2Publmodel
ATL file to ATL model
Public.km3
Bookmetamdel
Book.km3
A KM3 file to KM3 Ecore metamodel
Publicmetamodel
38
Global viewGlobal view
• Now, we need to wonder how to define the source model. For this there exist several solutions, for instance1. 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.
39
Global view – XML as input and XML2Book Global view – XML as input and XML2Book transformationtransformation
M1
M2
M3 XMLschema
An XMLschema
XMLmetamodel
inputMod.xml
inputMod-XML.ecore
XML file to XML model (Ecore)
XML ME/EMF
ATLmetamodel
XML2Bookmodel
Ecore ANTLR
ATLgrammar
XML2Book.atl
EBNF
ATL file to ATL model
It is generated automatically when an .atl file is saved
40
The input example in XMLThe input example in XML
<books>
<book title="livre"><chapter title="chapter 1" nbPages="13" author="toto"/><chapter title="chapter 2" nbPages="17" author="toto"/><chapter title="chapter 3" nbPages="20" author="titi"/>
</book>
<book title="article"><chapter title="chapter 1" nbPages="13" author="Michel"/><chapter title="chapter 2" nbPages="1" author="David"/>
</book>
</books>
+title : String+nbPages : Integer+author : String
Chapter
+title : String
Book
*+chapters{ordered}
+book
Book metamodel
XML metamodel
The inputModelXML.xml file
41
A XML metamodel in KM3A XML metamodel in KM3
package XML {abstract class Node {
attribute startLine[0-1] : Integer;attribute startColumn[0-1] : Integer;attribute endLine[0-1] : Integer;attribute endColumn[0-1] : Integer;attribute name : String;attribute value : String;reference parent[0-1] : Element oppositeOf children;
}class Attribute extends Node {}class Text extends Node {}class Element extends Node {
reference children[*] ordered container : Node oppositeOf parent;}
class Root extends Element {}}
package PrimitiveTypes {datatype Boolean;datatype Integer;datatype String;
}
42
A XML2Book transformationA XML2Book transformation
module XML2Book;create OUT : Book from IN : XML;
helper context XML!Element def: getAttrVal(name : String) : String =self.children->select(c | c.oclIsKindOf(XML!Attribute) and c.name = name)->first().value;
rule Book {from
e : XML!Element ( e.name = 'book')
tob : Book!Book ( title <- e.getAttrVal('title'), chapters <- e.children->select(c | c.oclIsKindOf(XML!Element))->asSequence())
}
rule Chapter {from
e : XML!Element ( e.name = 'chapter')
toc : Book!Chapter ( title <- e.getAttrVal('title'), nbPages <- e.getAttrVal('nbPages').toInteger(), author <- e.getAttrVal('author'))}
+title : String+nbPages : Integer+author : String
Chapter
+title : String
Book
*+chapters{ordered}
+book
43
Launch configuration – transformation Launch configuration – transformation filefile
44
Launch configuration – model and Launch configuration – model and metamodel filesmetamodel files
45
The generated file – a Book modelThe generated file – a Book model
46
Global viewGlobal view
M1
M2
M3
XMLmetamodel
inputMod-XML.ecore
ME/EMF
ATLmetamodel
XML2Bookmodel
Ecore
As a last step, we have to execute our Book2Publication transformation!
Bookmetamodel
Publicmetamodel
Book2Publmodel
inputMod-Book.ecore
outputMod-Public.ecore
47
Launch configuration – transformation Launch configuration – transformation filefile
48
Launch configuration – model and Launch configuration – model and metamodel filesmetamodel files
49
The generated file – a Publication The generated file – a Publication modelmodel
50
BibliographyBibliography
• GMT Home page. http://eclipse.org/gmt/
• ATL at GMT Home Page. http://www.eclipse.org/m2m/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/atl/atldemo/