+ All Categories
Home > Documents > Exam Model transformationsmvdbrand/courses/GLT/1112/slides/DSL... · 2012-01-15 · • OCL like...

Exam Model transformationsmvdbrand/courses/GLT/1112/slides/DSL... · 2012-01-15 · • OCL like...

Date post: 30-Jun-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
13
Exam 27 th of January 2012 9:00 - 12:00 Room: ??? Open book (use of laptop is allowed)! Material to be studied will be online later this week / Faculteit Wiskunde en Informatica PAGE 0 15-1-2012 Model transformations Xtend transformation language: Model transformation language OCL like expression language t f ti l fl strong functional flavour Can be used in constraint checks, model transformations and code generators Add “methods” to meta types (Java calls are possible if necessary) Tool support (syntax highlighting code completion Tool support (syntax highlighting, code completion, debugger) / Faculteit Wiskunde en Informatica PAGE 1 15-1-2012 Model transformations First we create an Xtend project Getting started Sl ti E li Select in Eclipse File/New/Other … Select then the X d/X d jt Xpand/Xpand project / Faculteit Wiskunde en Informatica PAGE 2 15-1-2012 Model transformations Enter project name Select then the Finish button A Xtend project is created A Xtend project is created Create/copy needed meta- models click on src and then select New/Other … Select Ecore Tools/Ecore Diagram / Faculteit Wiskunde en Informatica PAGE 3 15-1-2012
Transcript
Page 1: Exam Model transformationsmvdbrand/courses/GLT/1112/slides/DSL... · 2012-01-15 · • OCL like expression language −stftilfltrong functional flavour • Can be used in constraint

Exam

• 27th of January 2012y• 9:00 - 12:00• Room: ???• Open book (use of laptop is allowed)!• Material to be studied will be online later this week

/ Faculteit Wiskunde en Informatica PAGE 015-1-2012

Model transformations

• Xtend transformation language:g g• Model transformation language• OCL like expression language

t f ti l fl− strong functional flavour• Can be used in constraint checks, model

transformations and code generators• Add “methods” to meta types (Java calls are possible if

necessary)• Tool support (syntax highlighting code completion• Tool support (syntax highlighting, code completion,

debugger)

/ Faculteit Wiskunde en Informatica PAGE 115-1-2012

Model transformations

• First we create an Xtendproject

• Getting startedS l t i E li• Select in Eclipse− File/New/Other …

• Select then the X d/X d j tXpand/Xpand project

/ Faculteit Wiskunde en Informatica PAGE 215-1-2012

Model transformations

• Enter project namej• Select then the Finish

button• A Xtend project is createdA Xtend project is created• Create/copy needed meta-

models click on src and then select New/Other …/

• Select Ecore Tools/Ecore Diagram

/ Faculteit Wiskunde en Informatica PAGE 315-1-2012

Page 2: Exam Model transformationsmvdbrand/courses/GLT/1112/slides/DSL... · 2012-01-15 · • OCL like expression language −stftilfltrong functional flavour • Can be used in constraint

Model transformations

• Enter Directory• Enter Domain file name• Select Finish

/ Faculteit Wiskunde en Informatica PAGE 415-1-2012

Model transformations

• Create an Xtend file• Click on src • Select New/Other…• Select Xtend/Xtend FileSelect Xtend/Xtend File• Click Next

• Enter File name• Click Finish

/ Faculteit Wiskunde en Informatica PAGE 515-1-2012

Model transformations

• Xtend language concepts:g g• File extension must be *.ext• Example

import my::metamodel;extension other::ExtensionFile; /** * Documentation */* Documentation */

anExpressionExtension(String stringParam) : doingStuff(with(stringParam)) ;

/** * java extensions are just mappings */

String aJavaExtension(String param) : JAVA my.JavaClass.staticMethod(java.lang.String) ;

/ Faculteit Wiskunde en Informatica PAGE 615-1-2012

Model transformations

• Extend Import Statements• Using the import statement one can import name

spaces of different types• Syntax is:• Syntax is:import my::imported::namespace;

/ Faculteit Wiskunde en Informatica PAGE 715-1-2012

Page 3: Exam Model transformationsmvdbrand/courses/GLT/1112/slides/DSL... · 2012-01-15 · • OCL like expression language −stftilfltrong functional flavour • Can be used in constraint

Model transformations

• Extend Extension Import Statement• You can import another extend file using the extension

statement • The syntax is:The syntax is:extension fully::qualified::ExtensionFileName;

• Reexporting Extensions• To export extensions from another extension file together

with your local extensions, you add the keyword 'reexport'y , y y pextension fully::qualified::ExtensionFileName reexport;

/ Faculteit Wiskunde en Informatica PAGE 815-1-2012

Model transformations

• Extensions• The syntax of a simple expression extension is as follows:ReturnType extensionName(ParamType1 paramName1, ParamType2...): expression-using-params;ParamType2...): expression using params;

• Example:String getterName(NamedElement ele) : 'get'+ele name firstUpper()'get'+ele.name.firstUpper();

/ Faculteit Wiskunde en Informatica PAGE 915-1-2012

Model transformations

• Extension Invocation • There are two different ways of how to invoke an extension:− getterName(myNamedElement)− myNamedElement getterName()myNamedElement.getterName()

• Issues with extensions:• Type Inference • Recursion

/ Faculteit Wiskunde en Informatica PAGE 1015-1-2012

Model transformations

• Cached Extensions • If you call an extension without side effects very often, you

would like to cache the result for each set of parameters, in order improve the performance

• You can just add the keyword 'cached' to the extension in order to achieve this:cached String getterName(NamedElement ele) :cached String getterName(NamedElement ele) : 'get'+ele.name.firstUpper() ;

/ Faculteit Wiskunde en Informatica PAGE 1115-1-2012

Page 4: Exam Model transformationsmvdbrand/courses/GLT/1112/slides/DSL... · 2012-01-15 · • OCL like expression language −stftilfltrong functional flavour • Can be used in constraint

Model transformations

• Private Extensions • By default all extensions are public, i.e. they are visible from

outside the extension file.• If you want to hide extensions you can add the keywordIf you want to hide extensions you can add the keyword

'private' in front of them:private internalHelper(NamedElement ele) : // implementation ;// implementation.... ;

/ Faculteit Wiskunde en Informatica PAGE 1215-1-2012

Model transformations

• Create Extensions (Model Transformation)( )• The evaluation of extensions is innermost• Circular references may lead to non termination• “create” extensions enforces a non-innermost behaviour:create extensions enforces a non-innermost behaviour:

− Object is created before children are processed

/ Faculteit Wiskunde en Informatica PAGE 1315-1-2012

Model transformations

• Syntax of Xtend (Xpand) expressions:y ( )• There are two different forms of conditional expressions. • The first one is the so-called if expression. Syntax:condition ? thenExpression : elseExpressioncondition ? thenExpression : elseExpression

• The other one is called switch expression . Syntax:switch (expression) { (case expression : thenExpression)* default : catchAllExpression }thenExpression)* default : catchAllExpression }

• The default part is mandatory, because switch is an expression, therefore it needs to evaluate to something in any caseany case

/ Faculteit Wiskunde en Informatica PAGE 1415-1-2012

Model transformations

• Syntax of Xtend (Xpand) expressions:y ( )• There is an abbreviation for Boolean expressions:switch{ case booleanExpression : thenExpressiondefault : catchAllExpression }

• Chain expression: expressions and functional languages should be free of side effects as far as possible. But sometimes there you need invocations that do have sideBut sometimes there you need invocations that do have side effects. If you need to call such operations, you can use the chain expression. Syntax:Sy taanExpr -> anotherExpr -> lastExpr

• Each expression is evaluated in sequence, but only the result of the last expression is returnedof the last expression is returned

/ Faculteit Wiskunde en Informatica PAGE 1515-1-2012

Page 5: Exam Model transformationsmvdbrand/courses/GLT/1112/slides/DSL... · 2012-01-15 · • OCL like expression language −stftilfltrong functional flavour • Can be used in constraint

Model transformations

• Syntax of Xtend (Xpand) expressions:y ( )• The create expression is used to instantiate new objects of a

given type:new TypeName yplet expression

• The let expression lets you define local variables. Syntax is as follows:as follows:let v = expression : expression-with-v

• This is especially useful together with a chain- and a create iexpressions

/ Faculteit Wiskunde en Informatica PAGE 1615-1-2012

Model transformations

• Recall the Xtext specification for Booleans:

Model : OrBool ;

OrBool : lhs=AndBool ('|' rhs=OrBool)? ;

AndBool : lhs=NotBool ('&' rhs=AndBool)? ;

NotBool : (not?='~’)? arg=BracketBool ;

BracketBool : '(' orArg=OrBool ')' | conArg=BoolCon ;

BoolCon : {TrueNode} 'true' | {FalseNode} 'false' ;

/ Faculteit Wiskunde en Informatica PAGE 1715-1-2012

Model transformations

• Recall meta-model (of syntax tree) for Booleans:

/ Faculteit Wiskunde en Informatica PAGE 1815-1-2012

Model transformations

• First alternative for Boolean meta-model

/ Faculteit Wiskunde en Informatica PAGE 1915-1-2012

Page 6: Exam Model transformationsmvdbrand/courses/GLT/1112/slides/DSL... · 2012-01-15 · • OCL like expression language −stftilfltrong functional flavour • Can be used in constraint

Model transformations

• From concrete to abstract syntax meta-modelimport mbool;import ABool;

extension org::eclipse::xtend::util::stdlib::io;

mbool::Bool convert(aBool::OrBool model) :model.convertOrBool();

mbool::Bool convertOrBool(aBool::OrBool orBool) :if (orBool.rhs != null) then (orBool makeOrBool()orBool.makeOrBool()

)else (orBool.lhs.convertAndBool()

);

create mbool::OrBool makeOrBool(aBool::OrBool orBool) :this.setLhs(orBool.lhs.convertAndBool()

)-> this.setRhs(

/ Faculteit Wiskunde en Informatica PAGE 2015-1-2012

orBool.rhs.convertOrBool());

Model transformations

• From concrete to abstract syntax meta-modelmbool::Bool convertAndBool(aBool::AndBool andBool) :if (andBool.rhs != null) then (andBool.makeAndBool()

)else (andBool.lhs.convertNotBool()

);

create mbool::AndBool makeAndBool(aBool::AndBool andBool):this.setLhs(andBool.lhs.convertNotBool())->->this.setRhs(andBool.rhs.convertAndBool());

mbool::Bool convertNotBool(aBool::NotBool notBool) :if (notBool.not == true) then (makeNotBool(notBool.arg)

)else (convertBracketBool(notBool.arg)

);

/ Faculteit Wiskunde en Informatica PAGE 2115-1-2012

Model transformations

• From concrete to abstract syntax meta-modelcreate mbool::NotBool makeNotBool(aBool::BracketBool bracketBool) :this.setArg(convertBracketBool(bracketBool));

mbool::Bool convertBracketBool(aBool::BracketBool bracketBool) :if (bracketBool.orArg != null) then (bracketBool.orArg.convertOrBool()

)else (bracketBool.conArg.convertBoolCon()

);

mbool::BoolCon convertBoolCon(aBool::BoolCon boolCon) :syserr("Unknown boolCon encounterd: " + boolCon.toString());

create mbool::BoolCon convertBoolCon(aBool::TrueNode trueNode) :this.setValue(mbool::BoolLiteral::True);

create mbool::BoolCon convertBoolCon(aBool::FalseNode trueNode) :this.setValue(mbool::BoolLiteral::False);

/ Faculteit Wiskunde en Informatica PAGE 2215-1-2012

Model transformations

• December 2011: Xtend 2.2 has been released• More information:

http://www.eclipse.org/Xtext/xtend/

/ Faculteit Wiskunde en Informatica PAGE 2315-1-2012

Page 7: Exam Model transformationsmvdbrand/courses/GLT/1112/slides/DSL... · 2012-01-15 · • OCL like expression language −stftilfltrong functional flavour • Can be used in constraint

Model transformations

• QVT offers:• Queries take a model as input and select specific elements

from that model• Views are models that are derived from other modelsViews are models that are derived from other models• Transformations take a model as input and update it or create

a new model

/ Faculteit Wiskunde en Informatica PAGE 2415-1-2012

Model transformations

• QVT provides a model transformation language to implement model to model transformationsmodel-to-model transformations

• QVT fully supports OCL 2.0 expressions

• Query libraries• Reusable libraries of QVT mappingsT bilit• Traceability• Automated traceability when executing mapping

• ExtensibleQVT can call custom Java methods• QVT can call custom Java methods

/ Faculteit Wiskunde en Informatica PAGE 2515-1-2012

Model transformations

• Example:• Transforming a Use Case Model to a Class Model• This transformation automates the construction of a set of

use case realization classesuse case realization classes• It is rather simple: a 1-to-1 mapping from Actor to Class. Use

Cases owned by the Actor are created as Operations within the Classthe Class

/ Faculteit Wiskunde en Informatica PAGE 2615-1-2012

Model transformations

• The QVT adds scaffolding code (e.g., getters and g ( g gsetters)

/ Faculteit Wiskunde en Informatica PAGE 2715-1-2012

Page 8: Exam Model transformationsmvdbrand/courses/GLT/1112/slides/DSL... · 2012-01-15 · • OCL like expression language −stftilfltrong functional flavour • Can be used in constraint

Model transformations

• Three classes and a few attributes…

• QVT: AddScaffoldingCode• QVT: AddScaffoldingCode• Simply add getter and setter methods

/ Faculteit Wiskunde en Informatica PAGE 2815-1-2012

Model transformations

• AddScaffoldingCode.qvtg

/ Faculteit Wiskunde en Informatica PAGE 2915-1-2012

Model transformations

• Control Flow in AddScaffoldingCode.qvtg• main (not surprisingly) starts it off• mapping methods do most of the work

/ Faculteit Wiskunde en Informatica PAGE 3015-1-2012

Model transformations

• Transformation result – note added getter/setter gmethods

/ Faculteit Wiskunde en Informatica PAGE 3115-1-2012

Page 9: Exam Model transformationsmvdbrand/courses/GLT/1112/slides/DSL... · 2012-01-15 · • OCL like expression language −stftilfltrong functional flavour • Can be used in constraint

Model transformations

• See for tooling:g• http://sourceforge.net/projects/smartqvt/

− No documentation!− Status unclearStatus unclear

• Borland Together contains an implementation of QVT Operational and is now developed as the Eclipse M2M Operational QVT projectp Q p j

• Documentation:• http://www.omg.org/spec/QVT/1.1/

/ Faculteit Wiskunde en Informatica PAGE 3215-1-2012

Overview of DSLE

• Model driven software engineering in generalg g g• Grammars and meta-models• DSL Design• Model transformations• Code generation

/ Faculteit Wiskunde en Informatica PAGE 3315-1-2012

Code generation

• Automatic transformation of domain specific models into software models

• Automatic translation from software models into executable code

• Ingredients:• syntax and semantics of modeling formalisms should be described• correctness preserving transformation steps should be definedcorrectness preserving transformation steps should be defined• code generators should be developed

/ Faculteit Wiskunde en Informatica PAGE 3415-1-2012

Code generation

• Xpand:• Designed specifically for code (actually, text) generation• OO template engine supporting template polymorphism

F h t ti k h t• French quotation marks as escape characters• Embedded Expressions based on Xtend (OCL‐like)• Support for RecursionSuppo t o ecu s o• Metamodel‐aware Editor with Code Completion and

Syntax Highlighting

/ Faculteit Wiskunde en Informatica PAGE 3515-1-2012

Page 10: Exam Model transformationsmvdbrand/courses/GLT/1112/slides/DSL... · 2012-01-15 · • OCL like expression language −stftilfltrong functional flavour • Can be used in constraint

Code generation

• Xpand statements:• Import: make meta models visible to template file• Define: define a new template

Fil fil i t hi h t t i itt• File: open a file into which output is written• Expand: Call another template for one or several elements• Foreach: iterate over coll., generate code for each elemento eac te ate o e co , ge e ate code o eac e e e t• If: conditional template code• Extension: import an extension file for use in the

t l ttemplates• Error: report an error• Let: define a temporary variablep y

/ Faculteit Wiskunde en Informatica PAGE 3615-1-2012

Code generation

• Import statement• Xpand Editor knows your metamodel (code completion,

validation)• You can work on different types of meta metamodels• You can work on different types of meta‐metamodels− EMF, UML2, Java, …

• When writing a template you have to specify the metamodel you are using− This is done using the import syntax

/ Faculteit Wiskunde en Informatica PAGE 3715-1-2012

Code generation

• Define statement• A Template file contains any number of templates• A template is specified using the DEFINE keyword

A t l t h d i d fi d f t i t• A template has a name and is defined for a certain type (from your meta model)

• It is possible to have several templates with the same name but different types (i.e. Polymorphism if those are subtypes)

• Optionally, a template may have additional parameters.Optionally, a template may have additional parameters.

/ Faculteit Wiskunde en Informatica PAGE 3815-1-2012

Code generation

• Example of Xpand codeDefine

/ Faculteit Wiskunde en Informatica PAGE 3915-1-2012

Page 11: Exam Model transformationsmvdbrand/courses/GLT/1112/slides/DSL... · 2012-01-15 · • OCL like expression language −stftilfltrong functional flavour • Can be used in constraint

Code generation

• File statement• The FILE statement is used to specify the output file

where the contents of a template and its called templates goes totemplates goes to

• The filename might be an expression

/ Faculteit Wiskunde en Informatica PAGE 4015-1-2012

Code generation

• Example of Xpand codeFile

/ Faculteit Wiskunde en Informatica PAGE 4115-1-2012

Code generation

• Expand statement• EXPAND is used to invoke another template• You have to specify the target object (if called for this,

the FOR part can be omitted)the FOR part can be omitted)• It is also possible to call a template FOREACH element

in a collection (support for SEPARATOR)

/ Faculteit Wiskunde en Informatica PAGE 4215-1-2012

Code generation

• Example of Xpand codeExpand

/ Faculteit Wiskunde en Informatica PAGE 4315-1-2012

Page 12: Exam Model transformationsmvdbrand/courses/GLT/1112/slides/DSL... · 2012-01-15 · • OCL like expression language −stftilfltrong functional flavour • Can be used in constraint

Code generation

• Foreach statement• FOREACH is used to iterate over a collection• The current element from the list is assigned to a local

variable (AS part)variable (AS‐part)• Optionally you may specify a SEPARATOR and

ITERATOR)

/ Faculteit Wiskunde en Informatica PAGE 4415-1-2012

Code generation

• If statement• IF is used to add conditions to your template• You may have any number of ELSE IF statements

A d ELSE• And one ELSE

/ Faculteit Wiskunde en Informatica PAGE 4515-1-2012

Code generation

• If statement• IF is used to add conditions to your template• You may have any number of ELSE IF statements

A d ELSE• And one ELSE

• Let statementLet statement• LET is used to define a local variable (assign once)• Not used very often – better use an extension

/ Faculteit Wiskunde en Informatica PAGE 4615-1-2012

Code generation

• Example of Xpand code Creation of filesCreation of files

Iteration

/ Faculteit Wiskunde en Informatica PAGE 4715-1-2012

Page 13: Exam Model transformationsmvdbrand/courses/GLT/1112/slides/DSL... · 2012-01-15 · • OCL like expression language −stftilfltrong functional flavour • Can be used in constraint

Code generation

• Example of Xpand code

/ Faculteit Wiskunde en Informatica PAGE 4815-1-2012

Code generation

• More information on Xpand:• http://www.openarchitectureware.org/pub/documentatio

n/4.3.1/html/contents/core_reference.html#xpand_reference introductionnce_introduction

/ Faculteit Wiskunde en Informatica PAGE 4915-1-2012


Recommended