Model Transformations Marcos Aurélio December, 2007 Extends: Fabrício Teles, Rodrigo Teixeira @...

Post on 21-Dec-2015

214 views 0 download

Tags:

Transcript of Model Transformations Marcos Aurélio December, 2007 Extends: Fabrício Teles, Rodrigo Teixeira @...

Model Transformations

Marcos AurélioDecember, 2007

Extends:

Fabrício Teles, Rodrigo Teixeira

@ 2006-1

Outline

• What is a model transformation?• Purposes in MDD• Taxonomy• Techniques• Comparison

What is a model transformation?

• Transformations takes models and produces models– PIM to PSM (OMG MDA)– Refining and refactoring models– Reverse engineering – Generating new views– Applying design patterns – Deriving products in a product line

Typical example (UML to RDMS)

Desirable properties of a Model Transformation Language

• be executable;• be implementable in an efficient way;• be fully expressive, yet unambiguous.• facilitate developer productivity with precise, concise and

clear descriptions:– source model selection rules rules for producing the target model;– the language should offer graphical constructs – the language should be declarative – Tools for development and testing

• provide a means to combine transformations to form composite ones [reuse]

• provide a means to define scheduling • is defined upon a Metamodel• has a formal semantics

Model transformations techniques

• General purpose programming languages (Java, FLORA)

• Generic transformation tools (XSLT)

• Case tools scripting languages (Rose)

• Dedicated model transformations tools (QVT, ATL)

General purpose programming languages

General purpose programming languages

• Available via APIs, – however rules are implemented from scratch

• There’s no clear concept of “rule”

– Example: JMI (MOF-compliant Java Interface)

– No overhead to learn a new language– The programming complexity problem

continues

JMI

Java Modeling Interface

Motivation• “Exchange of data” problem

–There is no standard between models

–Data extraction tools are not flexible enough

• Solution: –Use a standard to define metadata

Introduction

• Implements MOF specification in Java– Allows Java applications to specify, store,

access and interchange metadata.– Provides an integration and interoperability

framework for Java tools and applications– Integrates with OMG modeling and metadata

architecture

• Part of the J2EE platform

Example – Simple XML metamodel

Example –Generated Java Classes

Element---------------------------------------

+ getName() : String+ setName(s: String)

+ getContainer() : Node+ setContainer(n:Node)

AttributeClass-------------------------------------------+createAttribute() : Attribute

+ createAttribute(name:String,value: String)

ElementClass

Contains---------------------------------------+ exists(e:Element, c:Node) : Boolean+ getElements(c:Node):Element[*]+ getContainer(e:Element):Node+ add(e:Element, n:Node) : Boolean+ remove(e:Element, n:Node) : Boolean

XMLModelPackage--------------------------------------------------+ getNode() : NodeClass+ getAttribute() : AttributeClass+ getElement() : ElementClass+ getRootNode() : RootNodeClass+ getContains() : Contains

Example – Code

<test><node attr1="value1" attr2 ="value2">text</node></test>

Example – CodeRootNode root = service.getRootNode().createRootNode(<name>,

<documentName>);// create the root nodeRootNode root = service.getRootNode().createRootNode("test","test.xml");// create the first nodeNode node = service.getNode().createNode("node");// set root as its containernode.setContainer(root);// now create the two attributesAttribute attr1 = service.getAttribute().createAttribute("attr1","value1");Attribute attr2 = service.getAttribute().createAttribute("attr2","value2");

Example – Code

// (this is an alternative way of setting the container)

node.getElements().add(attr1);

node.getElements().add(attr2);

// create the text node

TextNode text = service.getTextNode().createTextNode("text");

// set node as its container

text.setContainer(node);

Flora

Flora (F-Logic)

• Frame Logic is an object oriented deductive database language

• Syntactical integration between Object Oriented Paradigm and Rules

• Combines expressivity and declarative semantics to object oriented concepts.

• Implements Structural and Behavioural Inheritance with support for Single-source Multiple Inheritance

• Flora is an implementation for F-logic over XSB Prolog

Flora - MetaModel

criminal(X) :- american(X), sellsWeaponsTo(X,cuba).american(X) :- bornIn(X,usa).bornIn(west, usa).

Program

Example:

Flora - MetaModel

criminal(X) :- american(X), sellsWeaponsTo(X,cuba).american(X) :- bornIn(X,usa).bornIn(west, usa).

ClauseExample:

Flora - MetaModel

criminal(X) :- american(X), sellsWeaponsTo(X,cuba).american(X) :- bornIn(X,usa).bornIn(west, usa).

LogicalPredicativeAtomExample:

Flora - MetaModel

criminal(X) :- american(X), sellsWeaponsTo(X,cuba).american(X) :- bornIn(X,usa).bornIn(west, usa).

QueryExample:

Flora - MetaModel

criminal(X) :- american(X), sellsWeaponsTo(X,cuba).american(X) :- bornIn(X,usa).bornIn(west, usa).

OperatorExample:

F-Atoms

• Class Hierarchy–cachorro :: mamifero, jacare :: reptil –mamifero :: animal, reptil :: animal

• Class Instances–cao1 : cachorro, jacare1 : jacare

• Attributes and Methods definition–cao1[qtdPatas -> 4], joao[salario(2005)->1200]

–cachorro[qtdPatas *-> 4], empregado[salario(2005)*->1200]

Book To Publication Example

Flora

• Example:

book[title => string, chapters =>> chapter[nbPages => integer,

title => string,

author => string]].

publication[title => string,

nbPages => integer,

authors =>> string].

P:publication[title -> T, nbPages -> N, authors -> As] :-

B:book[title -> T, chapters ->> C],

N = sum{M|C[nbPages -> M]},

As = collectset{A|C[author -> A]}.

F-logic file

XSLT

eXtensible Stylesheet Language Transformation

eXtensible Stylesheet Language Transformation (XSLT)

• XML-based language used for the transformation of XML documents

• Part of a W3C family of languages (XSL)– Describes how to format

and transform XML files– XSLT, XSL-FO, XPath

• Used to model transformation– Where models are in XMI

encoding format

eXtensible Stylesheet Language Transformation (XSLT)

• XSLT Characteristics– Declarative rather than imperative– Consist of a template rules collection– Each rule specifies what to add to a target fragment

• Based on a source fragment and a fixed algorithm

– XSLT processor scan the source tree, apply rules and generate de target tree

– Syntactical & ineffic for model transformations• XML (XMI) is verbose• Operates on tree structures• More batch than interactive• Parameters passed by value

eXtensible Stylesheet Language Transformation (XSLT)

XMLDocument 1

XMLSDocument 1

Valid

XSLTDocument

XSLTEngine

SourceSchema

FragmentMatchingPatterns

XMLDocument 2

XMLSDocument 2

Valid

TargetSchema

FragmentInsertion Actions

MatchedSource Fragments

• Transformation process:

– Source document matching patterns

– XSLT document specifying actions that copy matched elements and attributes from source document and assemble them in a novel way

– Target document matching transformations

eXtensible Stylesheet Language Transformation (XSLT)

• Example:

<model xmlns:uml="http://www.eclipse.org/uml2/1.0.0/UML" xmlns:xmi="http://www.omg.org/XMI">

<package id="pck1" name=“library">

<class id="c1" name="Book">

<association aggregationType="composite" id="c1_ass1" name="chapters" targetClass="c2"/>

<attribute cardinality="1..1" id="c1_atr1" name="title" type="dt1"/>

</class>

<class id="c2" name="Chapter">

<attribute cardinality="1..1" id="c2_atr1" name="nbPages" type="dt2"/>

<attribute cardinality="1..1" id="c2_atr2" name="title" type="dt2"/>

<attribute cardinality="1..1" id="c2_atr3" name="author" type="dt1"/>

<association aggregationType="none" id="c2_ass1" name="book"

targetClass="c1"/>

</class>

</package>

</model>

Source file: book.xmi

eXtensible Stylesheet Language Transformation (XSLT)

• Example:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<xsl:output method="xml" />

<xsl:template match="library.Book" >

<xsl:variable name="title" select="@title" />

<xsl:variable name="authors" select="concat(@chapters.author, ',')"/>

<xsl:variable name="nbPages" select="sum(@chapters.nbPages)"/>

<xsl:call-template name="createPublication">

<xsl:with-param name="title" select="$title"/>

<xsl:with-param name="authors" select="$authors"/>

< xsl:with-param name="nbPages" select="$nbPages"/>

</xsl:call-template>

</xsl:template>

</xsl:transform>

XSLT file: book2Publicartion.xslt fragment

eXtensible Stylesheet Language Transformation (XSLT)

• Example:

<model xmlns:uml="http://www.eclipse.org/uml2/1.0.0/UML" xmlns:xmi="http://www.omg.org/XMI">

<package id="pck1" name=“library">

<class id="c1" name="Publication">

<attribute cardinality="1..1" id="c1_atr1" name="title" type="dt1"/>

<attribute cardinality="1..1" id="c1_atr2" name="nbPages" type="dt2"/>

<attribute cardinality="1..1" id="c1_atr3" name="authors" type="dt1"/>

</class>

<datatype id="dt1" name="String"/>

<datatype id="dt2" name="Integer"/>

</package>

</model>

Target file: publication.xmi

Case tools scripting languages

CASE tool scripting language

• Ex.: Rose, OptimalJ, Arcstyler, Fujaba• Pro

– Good level of maturity– Excellent integration with their CASE tool

• Drawback– Proprietary language – Often developed as a “second-class” feature, non

central role– Many limitations when model transformations get

complex • Structuration, modularity, reuse and configuration management

problems

QVT

Query View Transformation

Query-View-Transformation (QVT)

• OMG standard language for expressing queries, views, and transformations on MOF models

• DSL for transformation aimed for– Simple development and maintenance of

model transformations– With high expressiveness– And enhanced structuring capabilities

• Composition of rules and Interoperability

OCL-based hybrid procedural and declarative transformation language

QVT [hybrid] Architecture

DeclarativeImperative

Overview of the Architecture

• Declarative Relations transformation language– Specification of relations over model elements

• Declarative Core transformation language– Simple transformation language– Reference point for defining the semantics of the

Relations language

• Operational Mappings– Imperative transformation language– Extends Relations language with imperative constructs

Model Transformation expressed inOperational Mappings Language

Overall structure of a transformation program:

metamodel 'http://www.borland.com/together/uml20';

transformation BookToPublication (in InputMetamodel): OutputMetamodel

…………………………………………………………………

main () {…………………………………………………………………

}

…………………………………………………………………

…helpers……………………………………………

…mapping operations………………

…………………………………………………

Signature:Declares the identifier of the transformation

Entry point:The execution of the transformation starts here by executing the operations in the body of main.It also declares the source and target metamodels. in keywords indicate source and target model variables. The target is represented by the return of main.

Transformation elements:Transformation consists of mapping operations and helpers. They form the transformation logic.

BookToPublication Example in the OMG QVT Specification [05-11-01.pdf]

metamodel BOOK { class Book {title: String; composite chapters: Chapter;} class Chapter {title : String; nbPages : Integer;} }metamodel PUB { class Publication {title : String; nbPages : Integer;} }

transformation Book2Publication(in bookModel:BOOK,out pubModel:PUB)

main() { bookModel->objectOfType(Book)->map book_to_publication(); }mapping Class::book_to_publication () : Publication { title := self.title; nbPages := self.chapters->nbPages->sum();}

Book To Publication considering the UML 2.0 metamodel

Publication

Attributes+title:String+authors:String+nbPages:Integer

Library

Operations+get

PublicationMyBook:PublicationSlots

title=Eng. Softwareauthors=Rodrigo and FabricionbPages=25

1

1

has

<<instantiate>>

Book

Attributes+title:String

Chapter

Attributes+nbPages:Integer+title:String+authors:String

Library

Operations+get:Book

Cha2:ChapterSlots

nbPages=15title="Implementacao"authors=Fabricio

Cha1:ChapterSlots

nbPages=10title="Modelagem"authors=Rodrigo

MyBook:BookSlots

title=Eng. Software

chapters

has

<<instantiate>>

<<instantiate>>

<<instantiate>>

BookToPublication - Together (imperative QVT implementation)

mapping main(in model: uml::together::Model): ... Model { ownedMembers += replaceClasses (model); ownedMembers += replaceInstances (model); }

query getClasses (in root: ...Model): OrderedSet(...Class){ root.ownedMembers->...collect(c | class2class(c.oclAsType(...Class)))-> asOrderedSet();

}

query publicationClass () : ...Class

object uml20::classes::Class {

name := 'Publication';

ownedAttributes += object uml20::kernel::Property { name := 'title'; type := string};

...

BookToPublication – Together (cont.)

mapping publicationInstance (in bookInstance: InstanceSpecification) :...when { bookInst.instantiates->...exists(c | c.name = 'Book')} var publicationClass := bookInst.instantiates->select( ... 'Publication‘...

name := 'Publication'+bookInst.name; instantiates += bookInst.instantiates->resolve(‘class2class’).... slots += object uml20::kernel::instances::Slot { definingFeature := publicationClass >select...-> any(... | feature.name = 'authors');

values += select (bookInst.dependencies ... -> select( ... 'chapters')->

....select( ...definingFeature.name = 'authors')-> .... iterate(authorName; acc : String = ‘ ' | acc + (if acc = ‘ ' then authorName else ' and ' + authorName endif)); ...

ATL

Atlas Transformation Language

ATL

• Source models and target models are distinct:– Source models are read-only (they can only be

navigated, not modified),– Target models are write-only (they cannot be navigated).

• The language is a declarative-imperative hybrid:– Declarative part:

• Matched rules with automatic traceability support,• Side-effect free navigation (and query) language: OCL 2.0

– Imperative part:• Called rules,• Action blocks.

• Recommended programming style: declarative

ATL x QVT

• ATL and QVT have common features but their problem domains are different:– QVT is mainly for software development;– ATL aims at solving data engineering transformation problems

• Major ATL requirement: ability to deal with various models expressed in different languages and technologies

• Architectural alignment between ATL and QVT shows that language interoperability is feasible via model transformations

• ATL tools may be called QVT conformant

Comparing the Languages

Comparation TableJava FLORA XSLT Case Tools QVT ATL

Concise X X Xwell-know languages

X X

Executable X X X X ? XExpressive X X X XMetamodel X X X X XFormal semantics X Standards X X XAll OO/MOF concepts

X X X X

Tools X X X X X X

Conclusions

• Model Transformations are assets– guided by sound Soft. Engineering principles

• Analysis, Design, Implementation, V & V (Test), and Configuration Management (evolution)

• Transformations should also be models (principles of MDE)– In order to tackle complexity

Links and Papers

• JSR-040• http://modelware.inria.fr• http://www.modelware-ist.org (Main Repository Page \ Training)• http://www.eclipse.org/gmt/atl• http://wwwcs.uni-paderborn.de/cs/kindler/Forschung/

ComponentTools/ct-seminar/Java%20Metadata%20Interface%20-%20Ausarbeitung.pdf

• Czarnecki and Helsen: Classification of Model Transformation Approaches.

• Shane Sendall and Wojtek Kozaczynski: Model Transformation – the Heart and Soul of Model-Driven Software Development

• Anna Gerber at al: Transformation: The Missing Link of MDA