Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Model driven software engineering in practice book - Chapter 9 - Model to text transformations and code generation

2,466 views

Published on

Slides for the mdse-book.com chapter 9 - Model-to-text transformations.

Complete set of slides now available:
Chapter 1 - http://www.slideshare.net/mbrambil/modeldriven-software-engineering-in-practice-chapter-1-introduction
Chapter 2 - http://www.slideshare.net/mbrambil/modeldriven-software-engineering-in-practice-chapter-2-mdse-principles
Chapter 3 - http://www.slideshare.net/jcabot/model-driven-software-engineering-in-practice-chapter-3-mdse-use-cases
Chapter 4 - http://www.slideshare.net/jcabot/modeldriven-software-engineering-in-practice-chapter-4
Chapter 5 - http://www.slideshare.net/mbrambil/modeldriven-software-engineering-in-practice-chapter-5-integration-of-modeldriven-in-development-processes
Chapter 6 - http://www.slideshare.net/jcabot/mdse-bookslideschapter6
Chapter 7 - http://www.slideshare.net/mbrambil/model-driven-software-engineering-in-practice-book-chapter-7-developing-your-own-modeling-language
Chapter 8 - http://www.slideshare.net/jcabot/modeldriven-software-engineering-in-practice-chapter-8-modeltomodel-transformations
Chapter 9 - https://www.slideshare.net/mbrambil/model-driven-software-engineering-in-practice-book-chapter-9-model-to-text-transformations-and-code-generation
Chapter 10 - http://www.slideshare.net/jcabot/mdse-bookslideschapter10managingmodels

This book discusses how approaches based on modeling can improve the daily practice of software professionals. This is known as Model-Driven Software Engineering (MDSE) or, simply, Model-Driven Engineering (MDE).

MDSE practices have proved to increase efficiency and effectiveness in software development. MDSE adoption in the software industry is foreseen to grow exponentially in the near future, e.g., due to the convergence of software development and business analysis.

This book is an agile and flexible tool to introduce you to the MDE and MDSE world, thus allowing you to quickly understand its basic principles and techniques and to choose the right set of MDE instruments for your needs so that you can start to benefit from MDE right away.

The book is organized into two main parts.
The first part discusses the foundations of MDSE in terms of basic concepts (i.e., models and transformations), driving principles, application scenarios and current standards, like the wellknown MDA initiative proposed by OMG (Object Management Group) as well as the practices on how to integrate MDE in existing development processes.
The second part deals with the technical aspects of MDSE, spanning from the basics on when and how to build a domain-specific modeling language, to the description of Model-to-Text and Model-to-Model transformations, and the tools that support the management of MDE projects.
The book covers a wide set of introductory and technical topics, spanning MDE at large, definitions and orientation in the MD* world, metamodeling, domain specific languages, model transformations, reverse engineering, OMG's MDA, UML, OCL, A

Published in: Software
  • Be the first to comment

Model driven software engineering in practice book - Chapter 9 - Model to text transformations and code generation

  1. 1. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Teaching material for the book Model-Driven Software Engineering in Practice by Marco Brambilla, Jordi Cabot, Manuel Wimmer. Morgan & Claypool, USA, 2012. Copyright © 2012 Brambilla, Cabot, Wimmer. www.mdse-book.com MODEL-TO-TEXT TRANSFORMATIONS CHAPTER 9
  2. 2. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Content § Introduction § Programming Languages based Code Generation § M2T Transformation based Code Generation § Mastering Code Generation
  3. 3. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. www.mdse-book.com INTRODUCTION
  4. 4. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Introduction Terminology §  Code generation §  Wikipedia: „Code generation is the process by which a compiler‘s code generator converts a syntactically-correct program into a series of instructions that can be executed by a machine.“ §  Code Generation in Action (Herrington 2003): „Code generation is the technique of using or writing programs that write source code.“ §  Code generation (http://en.wikipedia.org) §  Compiler Engineering: component of the synthesis phase §  Software Engineering: program to generate source code §  Résumé: Term Code Generation is overloaded!
  5. 5. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Introduction Code Generation - Basic Questions §  How much is generated? §  Which parts can be automatically generated from models? §  Full or partial code generation? §  What is generated? §  Which kind of source code to generate? §  The less code to generate, the better! §  How to generate? §  Which languages and tools to use for developing code generators? §  GPLs vs. DSLs
  6. 6. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Introduction Code Generation in MDA (just an example) Requirements Analysis Design Implementation Test&Deployment CIM PIM PSM Code Feedback CIM PIM Web Model DAO Model SQL Model JSP Code DAO Code SQL Code Manual Development + Model2Model Transformation Model2Model Transformation Model2Text Transformation Rational Unified Process MDA Artifacts
  7. 7. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Introduction What kind of code is generated? §  Model-to-Text, whereas text may be distinguished in §  Program code §  Documentation §  Test cases §  Model serialization (XMI) §  Direct translation to machine code possible, but inconvenient, error- prone and hard to optimize §  Reuse existing code generators §  Using existing functionality (frameworks, APIs, components) §  Motto: The less code to generate, the better!
  8. 8. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Introduction Example: Platform for Web application development §  Example: developing a code generator for Web applications §  What options exist for the to be generated code? §  Dimensions of Web applications: Content, Hypertext, Presentation §  Programming languages: Java, C#, Ruby, PHP, … §  Architectures: 2-layer, 3-layer, MVC, ActiveRecords, … §  Frameworks: JSF, Spring, Struts, Hibernate, Ruby on Rails, ASP, … §  Products: MySQL, Tomcat, WebLogic, … §  Which combinations are appropriate? §  Experience gained in earlier projects §  What has proven useful? §  Reference architectures
  9. 9. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Introduction What kind of code is generated? Production code Test code Class diagrams State charts OCL Object diagrams Sequence diagrams A B C 1..* 1 1 * 1 0..1 z1 z2 z3 z4 e e‘ e‘ a:A b:B c:C m1 m2 m3 b1:B c1:C c2:C b2:B Picture based on Berhard Rumpe: Agile Modellierung mit UML. Springer, 2012.
  10. 10. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Introduction Overview of generation techniques Model Source Code Object Code or Byte Code Running Program Model Transformation Source Code Generation Code Transformation Byte Code Rewriting Reflection C++ Templates Java, . Net Java, .Net AspectJ ATL QVT Code Generation in MDE Acceleo XPand MofScript Compilation Java, . Net Based on Markus Völter. A catalog of patterns for program generation. In Proceedings of the 8th European Conference on Pattern Languages of Programs (EuroPLoP’03), pages 285–320, 2003.
  11. 11. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Introduction Why code generation? §  Code generation enables §  Separation of application modeling and technical code §  Increasing maintainability, extensibility, portability to new hardware, operating systems, and platforms §  Rapid prototyping §  Early and fast feedback due to demonstrations and test runs §  Code generation enables to combine redundant code fragments in one source §  Example: DDL, Hibernate, and Java Beans à may be specified in one UML Class Diagram
  12. 12. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Introduction Why code generation? – in contradiction to MDE? (1/2) §  Often no “real” model simulation possible §  UML environments mostly do not provide simulation features §  However, they provide transparent transformation to C, C#, Java, … §  UML Virtual Machines §  Interpreter approach – spare code generation for certain platforms §  Gets a new twist with fUML! §  Semantics of modeling languages, especially DSMLs, often defined by code generation class Car { public String color; public int door; public int engine; } Car Color : String Door : Integer Engine : Integer DSL Code Generator Java
  13. 13. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Introduction Why code generation? – in contradiction to MDE? (2/2) §  Runtime environments are designed for programming languages §  Established frameworks available (Struts, Spring, Hibernate, …) §  Systems depend on existing software (Web Services, DB) §  Extensions for code level often required (Logging) §  Disadvantage: using models and code in parallel §  No single source of information – OUCH! §  Having the same information in two places may lead to inconsistences, e.g., consider maintainability of systems
  14. 14. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Introduction Example: MiniUML_2_MiniJava MiniUML Metamodel MiniUML Model class  Student  extends  Person{        private  String  regNo;        public  void  setRegNo(…){…}        public  String  getRegNo(){…}     }   MiniJava Code MiniJava Grammar Model2Text NamedElement type : Type type 0..1 Relationship 0..1 superClass minCard : Integer maxCard : Integer * * –  String –  Boolean –  … «enumeration» Type ClassDec  :=  Modifier  “class”  Identifier  [“extends”                            Identifier]  ClassBody;     AttributeDec  :=  Modifier  Type  Identifier“;”;     MethodDec  :=  Modifier  ReturnType  Identifier  “(”                                ParamList  “)”  “{”  MethodBody  “}”;     Identifier  :=  {“a”-­‐”z”  |  “A”-­‐”Z”  |  “0”-­‐”9”};   Class Attribute name: String Person Student regNo : String
  15. 15. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. www.mdse-book.com PROGRAMMING LANGUAGES BASED CODE GENERATION
  16. 16. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Programming languages Introduction – Code generation with Java based on EMF §  Code generation may be realized using a traditional general purpose programming language, e.g., Java, C#, … §  Models are de-serialized to an in-memory object graph §  Pre-defined XMI de-serialzer provided by meta-modeling frameworks §  Out-of-the-box support in EMF §  Model API eases processing of models §  Generated automatically from metamodels §  In EMF: .ecore -> .genmodel -> Java code §  If metamodel not available, you may use reflection
  17. 17. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Programming languages Model APIs for processing models §  Example: Ecore-based metamodel and automatically generated Java code (shown as UML Class Diagram) Attribute atts0..* Class name : String name : String type : Type Operation ops0..* name : String returnType : Type Ecore2Java Attribute Class Operation getName() : String setName(String) : void getAtts() : EList<Attribute> getOps() : EList<Operation> getName() : String setName(String) : void … getName() : String setName(String) : void … MM in Ecore Model API
  18. 18. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Programming languages Code generation with Java: phases of code generation 1.  Load models §  Load XMI file into memory 2.  Process models and produce code §  Process models by traversing the model structure §  Use model information to produce code §  Save code into String variable 3.  Write code §  Persist String variable to a file using streams
  19. 19. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Programming languages Code generation with Java: Process and Architecture Model Code Generator XMI Parser FileStream API Code String code; Model API e.g., UML e.g., JavaJava Object Graph M2T (1) Load (2) Produce (3) Write Process Architecture EMF
  20. 20. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Programming languages Running Example solved in Java ResourceSet  resourceSet  =  new  ResourceSetImpl();   Resource  resource  =  resourceSet.getResource(URI.create("model.miniUML"));   TreeIterator  treeIter  =  resource.getAllContents();     while  (treeIter.hasNext())  {        Object  object  =  treeIt.next();        if  (!object  instanceof  Class)  continue;          Class  cl  =  (Class)  object;        String  code  =  "class  „  +  cl.getName()  +  "{";        //  generate  Constructor:  code  +=  …        //  generate  Attributs:  code  +=  …        //  generate  Methods:  code  +=  …        code  +=  "}";              try  {          FileOutputStream  fos  =  new  FileOutputStream(cl.getName()  +".java“);          fos.write(code.getBytes());          fos.close();      }  catch  (Exception  e)  {…}   }   Query values via model API Create a file for each class Get all model elements (1) Load (2) Produce (3) Write
  21. 21. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Programming languages Summary §  Advantages §  No new languages have to be learned §  No additional tool dependencies §  Disadvantages §  Intermingled static/dynamic code §  Non-graspable output structure §  Lack of declarative query language §  Lack of reusable base functionality
  22. 22. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. www.mdse-book.com M2T TRANSFORMATION BASED CODE GENERATION
  23. 23. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. M2T Transformation Languages… …are Template based §  Templates are a well-established technique in software engineering §  Application domains: Text processing, Web engineering, … §  Example: §  Components of a template-based approach §  Templates §  Text fragments and embedded meta-markers §  Meta-markers query an additional data source §  Have to be interpreted and evaluated in contrast to text fragments §  Declarative model query: query languages (OCL, XPath, SQL) §  Imperative model query: programming languages (Java, C#) §  Template engine §  Replaces meta-markers with data at runtime and produces output files Dear Homer Simpson, Congratulations! You have won … E-Mail Text Dear «firstName» «lastName», Congratulations! You have won … Template Text
  24. 24. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. M2T Transformation Languages Core Architecture §  Template-based approach at a glance «context class» public class «name» { String id, … } Template Engine Query Result Input public class Person { String id, …} Template Text fragment Meta-marker Output1 Output2 …public class Customer { String id, …} Person Customer … Source Model Produced Text
  25. 25. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. M2T Transformation Languages Benefits §  Separated static/dynamic code §  Templates separate static code, i.e., normal text, from dynamic code that is described by meta-markers §  Explicit output structure §  Primary structure of the template is the output structure §  Computation logic is embedded in this structure §  Declarative query language §  OCL is employed to query the input models §  Reusable base functionality §  Support for reading in models, serialize text to files, …
  26. 26. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. M2T Transformation Languages Approaches § A bunch of template languages for M2T transformation available § JET, JET2 § Xpand, Xtend § MOFScript § Acceleo § XSLT § …
  27. 27. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Acceleo Introduction §  Acceleo is a mature implementation of the OMG M2T transformation standard §  Acceleo website: http://www.eclipse.org/acceleo/ §  M2T Transformation standard: http://www.omg.org/spec/MOFM2T §  Template-based language §  Several meta-markers for useful for code generation available §  Powerful API supporting §  OCL §  String manipulation functions §  … §  Powerful tooling supporting §  Editor, debugger, profiler, traceability between model and code, …
  28. 28. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Acceleo Language Concepts § Module concept is provided § Imports the metamodels for the input models § Act as container for templates § A template is always defined for a particular meta-class § Plus an optional pre-condition to filter instances § Templates may call each other § Templates may extended each other § Templates contain text and provided meta-markers
  29. 29. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Acceleo Language Concepts §  Several meta-markers (called tags) are supported §  File Tag: To open and close files in which code is generated §  For/If Tag: Control constructs for defining loops and conditions §  Query Tag: Reusable helper functions §  Expression Tag: Compute values that are embedded in the output §  Protected Tag: Define areas that are not overridden by future generation runs
  30. 30. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Acceleo Example [module generateJavaClass('http://smvcml/1.0')] [query public getter(att : Attribute) : String = 'get'+att.name.toUpperFirst() /] [query public returnStatement(type: String) : String = if type = 'Boolean' then 'return true;' else '...' endif /] [template public javaClass(aClass : Class)] [file (aClass.name.toUpperFirst()+'.java', false, 'UTF-8')] package entities; import java.io.Serializable; public class [aClass.name/] implements Serializable { [for (att : Attribute | aClass.atts) separator ('n')] [javaAttribute(att)/] [/for] [for (op : Operation | aClass.ops) separator ('n')] [javaMethod(op)/] [/for] } [/file] [/template] … … [template public javaAttribute(att : Attribute)] private [att.type/] [att.name/]; public [att.type/] [att.getter()/]() { return [att.name/]; } ... [/template] [template public javaMethod(op : Operation)] public [op.type/] [op.name/]() { // [protected (op.name)] // Fill in the operation implementation here! [returnStatement(op.type)/] // [/protected] } [/template] Import metamodel (root package) Template definition Open output file Close output file Expression Meta class Loop Static Text Protected Area Query Template Call
  31. 31. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Acceleo Protected Areas §  Protected areas are not overriden by the next generator run §  They are marked by comments §  Their content is merged with the newly produced code §  If the right place cannot be found, warning is given! §  Example public boolean checkAvailability(){ // Start of user code checkAvailability // Fill in the operation implementation here! return true; // End of user code }
  32. 32. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. www.mdse-book.com MASTERING CODE GENERATION
  33. 33. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Abstracting Templates §  To ensure that generated code is accepted by developers (cf. Turing test for code-generation), familiar code should be generated §  Especially when only a partial code generation is possible! §  Abstract code generation templates from reference code to have known structure and coding guidelines considered §  Acceleo supports dedicated refactorings to transform code into templates §  E.g., substitute String with Expression Tag
  34. 34. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Generating step-by-step §  Divide code generation process into several steps §  Same applies as for M2M transformations! §  Transformation chains may use a mixture of M2M and M2T transformations §  To keep the gap between the models and the code short §  If code generators exists, try to produce their required input format with simpler M2M or M2T transformations §  E.g., code generator for flat state machines, transform composite state machines to flat ones and run existing code generator
  35. 35. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Separating transformation logic from text §  Separete complex transformation logic from text fragements §  Use queries or libraries that are imported to the M2T transformation §  By this, templates get more readable and maintainable §  Queries may be reused
  36. 36. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Mastering code layout §  Code layout is determined by the template layout §  Challenging to produce code layout when several control structures such as loops and conditionals are used in the template §  Special escape characters for line breaks used for enhancing the reabability of the template are provided §  Alternative §  Use code beautifiers in a post-processing step §  Supported by Xpand for Java/XML out-of-the-box
  37. 37. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Model/code synchronization issues §  Protected areas help saving manually added code in succeeding generator runs §  Code contained in protected areas is not always automatically integrated in the newly generated code §  Assume a method is renamed on model level §  Where to place the code of the method implementation? §  Which identifier to use for identifying a protected area? §  Natural or artificial identifiers? §  Model refactorings may be replayed on the code level before the next generator run is started §  Code in protected areas may also reflect the refactorings!
  38. 38. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Code Generation = M2M + TCS? §  Code Generation achievable through applying a M2M transformations to a programming language metamodel §  If a TCS is available for the programming language metamodel, the resulting model may be directly serialized into text §  Only recommended when §  programming language metamodel + TCS are already available §  full code generation is possible Model Metamodel (ModLang) Model Metamodel (ProgLang) M2M Spec M2M Exec TCS Spec (ProgLang) TCS Serialization «conformsTo» «conformsTo» «conformsTo» visualizes symbolizes
  39. 39. Marco Brambilla, Jordi Cabot, Manuel Wimmer. Model-Driven Software Engineering In Practice. Morgan & Claypool 2012. Teaching material for the book Model-Driven Software Engineering in Practice by Marco Brambilla, Jordi Cabot, Manuel Wimmer. Morgan & Claypool, USA, 2012. Copyright © 2012 Brambilla, Cabot, Wimmer. www.mdse-book.com MODEL-DRIVEN SOFTWARE ENGINEERING IN PRACTICE Marco Brambilla, Jordi Cabot, Manuel Wimmer. Morgan & Claypool, USA, 2012. www.mdse-book.com www.morganclaypool.com

×