620 likes | 635 Views
Model Transformations. Fabrício Teles, Rodrigo Teixeira {fst,rtr}@cin.ufpe.br. Outline. What is a model transformation? Purposes in MDD Taxonomy Techniques General purpose programming languages Generic transformation tools (XSLT and Viatra) Dedicated transformation language (QVT and ATL)
E N D
Model Transformations Fabrício Teles, Rodrigo Teixeira {fst,rtr}@cin.ufpe.br
Outline • What is a model transformation? • Purposes in MDD • Taxonomy • Techniques • General purpose programming languages • Generic transformation tools (XSLT and Viatra) • Dedicated transformation language (QVT and ATL) • Comparison
What is a model transformation? • Transformations takes model and produces model • PIM to PSM a la OMG MDA • Refining and refactoring models • Reverse engineering • Generate new views • Applying design patterns • Deriving products in a product line ...Any model engineering activity that can be automated...
Taxonomy • Declarative • What is mapped • Imperative • How it is mapped • Hybrid
Operational Context MOF conformsTo conformsTo Define Transformation Meta-model A Meta-model B Transformation based on based on conformsTo conformsTo output input Model A Model B Apply Transformation using Engine
Model transformationstechniques • General purpose programming languages • Imperative object-oriented programming (Java) • Declarative object-oriented logic programming (Flora) • Generic transformation tools • Graph transformations (VIATRA) • XSLT • Case tools scripting languages (Rose) • Dedicated model transformations tools • OMG QVT style (QVT, ATL) • Meta-modeling tools (Kermeta)
General purpose programming languages • Java, VB, C# (take your favourite lang.) • Available via APIs, • however rules are implemented from scratch • Example: JMI (MOF-compliant Java Interface) • No overhead to learn a new language • The programming complexity problem, which MDD wish mastering, continues
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 Sequential Transaction Frame Logic Programming Sequential Transaction Logic Programming Frame Logic Programming General Logic Programming Hilog Logic Programming Definit Logic Programming
Flora - MetaModel DLP Package
Flora - MetaModel DLP::Core
Program Flora - MetaModel Exemple: criminal(X) :- american(X), sellsWeaponsTo(X,cuba). american(X) :- bornIn(X,usa). bornIn(west, usa).
Clause Flora - MetaModel Exemple: criminal(X) :- american(X), sellsWeaponsTo(X,cuba). american(X) :- bornIn(X,usa). bornIn(west, usa).
LogicalPredicativeAtom Flora - MetaModel Exemple: criminal(X) :- american(X), sellsWeaponsTo(X,cuba). american(X) :- bornIn(X,usa). bornIn(west, usa).
Query Flora - MetaModel Exemple: criminal(X) :- american(X), sellsWeaponsTo(X,cuba). american(X) :- bornIn(X,usa). bornIn(west, usa).
Operator Flora - MetaModel Exemple: criminal(X) :- american(X), sellsWeaponsTo(X,cuba). american(X) :- bornIn(X,usa). bornIn(west, usa).
a :- b , c. Flora - MetaModel DLP::Operations
a :- b ; c. Flora - MetaModel DLP::Operations
f f(a,b) g(A,2) Flora - MetaModel DLP::Atom
2 x 2.78 “teste” f(“teste”) Flora - MetaModel DLP::Terms
Flora - MetaModel solteiro(P) :- homem(P), not casado(P). GLP::Operators
Flora - MetaModel fecho(R)(X,Y) :- R(X,Y). fecho(R)(X,Y) :- R(X,Z), fecho(R)(Z,Y). HLP::Atom
Flora - MetaModel STLP::Atom
LogicalBacktrackableUpdateAtom Flora - MetaModel newRule :- insert{p(X, Y) :- q(Y,Z)}, delete{p(2)}. STLP::Operators
Flora - MetaModel FLP::PathExpression
Flora - MetaModel FLP::Atom
F-Logic X::Y X:Y C[M->V] C[M->>V] Taxonomic F-Atom C[M=>V] C[M=>>V] Non-Inheritable C[M*->V] C[M*->>V] Multiplicity: single Multiplicity: multi Signature Specification Value Specification C[M*=>V] Inheritable C[M*=>>V] F-Logic OO Concepts Flora - MetaModel
F-Logic OO Concepts X:aposentado :- X[idade->I], I>65. mary[spouse->john]. mary[spouse->joe]. john[son*->>frank].
Flora - MetaModel Tipos de F-Atoms • Definição de hierarquia de classes (SubclassFAtom) • cachorro :: mamifero, jacare :: reptil • mamifero :: animal, reptil :: animal • Definição de relações entre objetos/classes (InstanceFAtom): • cao1 : cachorro, jacare1 : jacare • Definição de valores de atributos e métodos (AttributeValueSpecification / MethodValueSpecification): • cao1[qtdPatas -> 4], joao[salario(2005)->1200] • Definição dos valores e tipos herdáveis para atributos e métodos de classes (AttributeValueSpecification / MethodValueSpecification – inheritable = true): • cachorro[qtdPatas *-> 4], empregado[salario(2005)*->1200] • Definição de valores de atributos e métodos (AttributeValueSpecification / MethodValueSpecification): • cao1[qtdPatas -> 4], joao[salario(2005)->1200]
Flora • Exemple: 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]}. book2publication :- B:book[title -> T, chapters ->> C], N = sum{M|C[nbPages -> M]}, As = collectset{A|C[author -> A], btinsert{publication[title -> T, nbPages -> N, authors -> As]}. F-logic file
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
Target Schema Fragment Insertion Actions Matched Source Fragments XSLT Document XMLS Document 2 XMLS Document 1 Source Schema Fragment Matching Patterns Valid Valid XML Document 2 XML Document 1 XSLT Engine eXtensible Stylesheet Language Transformation (XSLT) • 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) • Exemple: <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) • Exemple: <?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) • Exemple: <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 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 thought, not central • Many limitations when model transformations get complex • Structuration, modularity, reuse and configuration management problems
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 higher expression power • And enhanced structuration • Composition of rules and Interoperability OCL-based hybrid procedural and declarative transformation language
QVT [hybrid] Architecture Imperative Declarative
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(); }
BookToPublication - Together (imperative QVT implementation) mappingmain(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 • 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