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.

CG2010 Tailored Code Generators


Published on

DSL and tool support for building Tailored Code Generators

Talk at CG2010, Cambridge UK. June 18th.

Presentation of the Essential tool.

Published in: Technology
  • Be the first to comment

CG2010 Tailored Code Generators

  1. 1. DSLs and tool support for building Tailored Code Generators Pedro J. Molina, PhD. Software Engineer Capgemini Spain | Valencia [email_address] http://
  2. 2. Contents <ul><li>Context </li></ul><ul><li>Some problems </li></ul><ul><li>How to address it? </li></ul><ul><li>A solution proposal </li></ul><ul><li>Demo time </li></ul><ul><li>Scenarios Usages </li></ul><ul><li>Conclusions </li></ul>©MMX Pedro J.Molina, PhD.
  3. 3. Initial Context <ul><li>Custom Software Development </li></ul><ul><li>Specific developments for custom needs </li></ul><ul><li>Staff specialised with diverse and partial degree of knowledge: </li></ul><ul><ul><li>People with knowledge in the sector, in specific customers needs </li></ul></ul><ul><ul><ul><li>enterprise architects, analysts </li></ul></ul></ul><ul><ul><li>People with knowledge in specific stacks of technology </li></ul></ul><ul><ul><ul><li>SW architects, developers </li></ul></ul></ul>©MMX Pedro J.Molina, PhD.
  4. 4. Main goal <ul><li>Understand business needs </li></ul><ul><li>Recommend bests technologies </li></ul><ul><li>Align IT with Business needs </li></ul>©MMX Pedro J.Molina, PhD.
  5. 5. Problem #1 : IT Diversity <ul><li>Customers have diverse IT standards </li></ul><ul><ul><li>Preferred technology </li></ul></ul><ul><ul><li>Frameworks & libraries </li></ul></ul><ul><ul><li>Coding conventions and style guides </li></ul></ul><ul><ul><li>Regulations to fulfil </li></ul></ul><ul><li>How we can target as much as possible and still have quality, reuse and be able to apply best practices? </li></ul>©MMX Pedro J.Molina, PhD.
  6. 6. Problem #2: Technology changes <ul><li>Companies needs to follow adoption paths soon or later </li></ul><ul><ul><li>Lack of support </li></ul></ul><ul><ul><li>Obsolete technology </li></ul></ul><ul><ul><li>Lack of qualified professionals </li></ul></ul><ul><ul><li>Cost of maintenance (TCO) </li></ul></ul><ul><li>How we can reduce our dependency on technology? </li></ul><ul><ul><li>Make business know-how independent of the technology (as much as you can) </li></ul></ul>©MMX Pedro J.Molina, PhD.
  7. 7. Problem #3: Time to Market and Costs <ul><li>Frequently customers avoid new developments based on about previous experiences: </li></ul><ul><ul><li>It will not be on time </li></ul></ul><ul><ul><li>It will cost too much </li></ul></ul><ul><li>MDSD can turn projects feasible that were not viable with a traditional development </li></ul>©MMX Pedro J.Molina, PhD.
  8. 8. How to address it? <ul><li>Enforce a strong separation between </li></ul><ul><ul><li>Business Know-How & </li></ul></ul><ul><ul><li>Technological Know-How </li></ul></ul><ul><ul><li>Promote job specialization </li></ul></ul><ul><li>Automate as much as possible </li></ul><ul><ul><li>Better Quality and Time to Market </li></ul></ul>©MMX Pedro J.Molina, PhD.
  9. 9. A solution proposal <ul><li>Use MDSD: </li></ul><ul><ul><li>Model your Business Know-How </li></ul></ul><ul><ul><li>Standardize your Technological substrate to be used </li></ul></ul><ul><ul><li>Generate your apps from models to your selected architecture </li></ul></ul><ul><ul><li>Refine and iterate whenever </li></ul></ul><ul><ul><ul><li>Business changes or </li></ul></ul></ul><ul><ul><ul><li>Technology changes </li></ul></ul></ul>©MMX Pedro J.Molina, PhD.
  10. 10. Approaches to create code generators <ul><li>From 1998 till 2010 I played with lots of approaches: </li></ul><ul><ul><li>C++ with string concatenations </li></ul></ul><ul><ul><li>XML & XSL-T ( horrible !) </li></ul></ul><ul><ul><li>C++ with custom text template engine (2 engines) </li></ul></ul><ul><ul><li>JSP or ASP languages </li></ul></ul><ul><ul><li>C# with Velocity </li></ul></ul><ul><ul><li>C# with T4 </li></ul></ul><ul><ul><li>PHP and Smarty </li></ul></ul><ul><ul><li>C# or Java with StringTemplate </li></ul></ul><ul><ul><li>Frameworks for building code generators </li></ul></ul><ul><ul><li>Declarative Code Generation... </li></ul></ul>©MMX Pedro J.Molina, PhD.
  11. 11. MVC <ul><li>Model-View-Controller </li></ul><ul><ul><li>Useful pattern for Presentation Layers </li></ul></ul><ul><ul><li>Crucial for maintainability in Code Generators </li></ul></ul><ul><ul><li>StringTemplate enforces MVC </li></ul></ul>Model View Controller ©MMX Pedro J.Molina, PhD.
  12. 12. Conceptual Map for Code Generation Metamodel Model Templates Code Transformations Higher Abstraction Level Lower Instances Types Code Generator ©MMX Pedro J.Molina, PhD.
  13. 13. Conceptual Map for CG Specification Transformation Algorithms Programs Models Classes / Types Instances ©MMX Pedro J.Molina, PhD. Source Code Template Meta-model Reverse Engineering Abstraction Synthesis Mappings Instanciation Abstraction
  14. 14. DSLs <ul><li>Domain Specific Languages to define: </li></ul><ul><ul><li>Metamodels </li></ul></ul><ul><ul><li>Models </li></ul></ul><ul><ul><li>Templates </li></ul></ul><ul><ul><li>Model to Text Transformations (M2T) </li></ul></ul><ul><ul><li>Model to Model Transformations (M2M) </li></ul></ul>©MMX Pedro J.Molina, PhD.
  15. 15. Essential <ul><li>Essential is an </li></ul><ul><ul><li>IDE and a runtime for creating code generators </li></ul></ul><ul><ul><li>Declarative, Clean </li></ul></ul><ul><ul><li>Fast </li></ul></ul><ul><ul><li>Promotes reuse </li></ul></ul><ul><ul><li>Forward engineering approach </li></ul></ul>©MMX Pedro J.Molina, PhD.
  16. 16. Motivations for a new tool <ul><li>Easy to use MDD tool </li></ul><ul><ul><li>Learning curve: as lower as possible </li></ul></ul><ul><li>Explore ways to create better and faster code generation </li></ul><ul><li>Declaratively describe the bricks of MDD </li></ul><ul><li>Fast Code Generator prototyper </li></ul><ul><li>Explore the concept of Fowler Language Workbench </li></ul><ul><ul><li>IDE for creating code generators </li></ul></ul><ul><li>Apply SoC to MDD </li></ul><ul><li>Teach the ropes of Code Generation </li></ul><ul><li>Searching for the build bricks of MDD </li></ul><ul><li>And enjoy in the Quest! </li></ul>©MMX Pedro J.Molina, PhD.
  17. 17. It should be easier ©MMX Pedro J.Molina, PhD.
  18. 18. Metamodel <ul><li>Sample model </li></ul><ul><ul><li>Describes email </li></ul></ul><ul><ul><li>Sender </li></ul></ul><ul><ul><li>And recipients </li></ul></ul><ul><ul><li>namespace Meta </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>class MailMessage </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li> Person From; </li></ul></ul><ul><ul><li> List<Person> To; </li></ul></ul><ul><ul><li> string Subject; </li></ul></ul><ul><ul><li> string Message; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>class Person </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>string FullName; </li></ul></ul><ul><ul><li> string Email; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>©MMX Pedro J.Molina, PhD.
  19. 19. Models <ul><li>Instantiation of Metamodels </li></ul><ul><ul><li>Object </li></ul></ul><ul><ul><li>Properties </li></ul></ul><ul><ul><li>References </li></ul></ul><ul><ul><li>Namespaces </li></ul></ul><ul><li>Sample </li></ul><ul><ul><li>Create mail and persons </li></ul></ul><ul><ul><li>using Meta; </li></ul></ul><ul><ul><li>namespace Model </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>MailMessage FirstMessage </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li> From = Peter ; </li></ul></ul><ul><ul><li> To = [ Alicia , Peter ]; </li></ul></ul><ul><ul><li> Subject = &quot;Welcomed&quot;; </li></ul></ul><ul><ul><li> Message = &quot;Hello world!&quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>Person Peter </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li> FullName = &quot;Peter Mills&quot;; </li></ul></ul><ul><ul><li> Email = &quot;;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>Person Alicia </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li> FullName = &quot;Alicia Lopez&quot;; </li></ul></ul><ul><ul><li> Email = &quot;;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>©MMX Pedro J.Molina, PhD.
  20. 20. Templates <ul><li>StringTemplate </li></ul><ul><ul><li>Clean and reusable template </li></ul></ul><ul><ul><li>No control inside </li></ul></ul><ul><ul><li>Support for: </li></ul></ul><ul><ul><ul><li>Subtemplates </li></ul></ul></ul><ul><ul><ul><li>Rendering </li></ul></ul></ul><ul><ul><ul><li>Clean iteration </li></ul></ul></ul><ul><ul><ul><li>Control language agnostic </li></ul></ul></ul><ul><ul><li>group Templates ; </li></ul></ul><ul><ul><li>genMail( msg ) ::= << </li></ul></ul><ul><ul><li>From: $ msg.From : genRecipient()$ </li></ul></ul><ul><ul><li>To: $ msg.To :genRecipient() ; separator=&quot;, &quot;$ </li></ul></ul><ul><ul><li>Subject: $ msg.Subject $ </li></ul></ul><ul><ul><li>MESSAGE: </li></ul></ul><ul><ul><li>------------------------------ </li></ul></ul><ul><ul><li>$ msg.Message $ </li></ul></ul><ul><ul><li>------------------------------ </li></ul></ul><ul><ul><li>>> </li></ul></ul><ul><ul><li>genRecipient ()::= << </li></ul></ul><ul><ul><li>$ it.FullName $ <$ it.Email $> </li></ul></ul><ul><ul><li>>> </li></ul></ul>©MMX Pedro J.Molina, PhD.
  21. 21. Control and Transformations <ul><li>Custom Control Language called Trx to: </li></ul><ul><ul><li>Select Model parts (Model Query) </li></ul></ul><ul><ul><li>Apply templates </li></ul></ul><ul><ul><li>Organize generation output files / models </li></ul></ul><ul><ul><li>using Meta; </li></ul></ul><ul><ul><li>using Model; </li></ul></ul><ul><ul><li>namespace Gen </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>transformation Main() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li> MailMessage msg = Model.FirstMessage ; </li></ul></ul><ul><ul><li> Templates.genMail (msg) > &quot;message.txt&quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>©MMX Pedro J.Molina, PhD.
  22. 22. Tool support <ul><li>Grammars tools </li></ul><ul><li>Parsers construction </li></ul><ul><li>Editor </li></ul><ul><ul><li>Syntax colorization </li></ul></ul><ul><ul><li>Code completion </li></ul></ul><ul><li>Model Checker </li></ul>©MMX Pedro J.Molina, PhD.
  23. 23. Why another tool? <ul><li>Fast changes to metamodel </li></ul><ul><ul><li>UML tools: fixed metamodel! </li></ul></ul><ul><ul><li>EMF/GMF/MS DSL Tools </li></ul></ul><ul><li>MVC approach to Templates </li></ul><ul><ul><li>Avoiding JSP approaches to code gen </li></ul></ul><ul><li>Test a full declarative code generator in the way I like it </li></ul>©MMX Pedro J.Molina, PhD.
  24. 24. Composable Models <ul><li>Partial model definition </li></ul><ul><li>Extensible </li></ul><ul><li>Link them before transformation </li></ul><ul><li>Promotes </li></ul><ul><ul><li>Maintainability </li></ul></ul><ul><ul><li>Separation of Concerns </li></ul></ul>©MMX Pedro J.Molina, PhD.
  25. 25. Model transformations <ul><li>M2T </li></ul><ul><ul><li>Model to Template (StringTemplate) </li></ul></ul><ul><li>M2M </li></ul><ul><ul><li>Model 2 Model transformations </li></ul></ul><ul><ul><li>Based in rules </li></ul></ul>©MMX Pedro J.Molina, PhD.
  26. 26. Essential Demo <ul><li>Hello world </li></ul><ul><li>Mail Sample </li></ul><ul><li>Class Model to DB Schema Model </li></ul><ul><li>DB Schema Model to SQL </li></ul><ul><li>Application Code Generation </li></ul>©MMX Pedro J.Molina, PhD.
  27. 27. Generation Settings <ul><li>Just another Model! </li></ul><ul><li>Describes the design choices and configuration options selected for the final solution </li></ul>©MMX Pedro J.Molina, PhD.
  28. 28. Usage Scenarios I <ul><li>Direct Generation </li></ul><ul><li>Benefits </li></ul><ul><ul><li>Forward engineering </li></ul></ul><ul><ul><li>Automation </li></ul></ul><ul><ul><li>Quality assurance </li></ul></ul>Model Generation Settings Code Generator Code ©MMX Pedro J.Molina, PhD.
  29. 29. Usage Scenarios II <ul><li>Addressing Multi-target code </li></ul><ul><ul><li>C#, Java, Ruby, PHP, C/C++, HTML + Javascript, </li></ul></ul><ul><ul><li>SQL dialects, etc. </li></ul></ul><ul><ul><li>Based in generation settings selections </li></ul></ul>Model Generation Settings Code Generator Code B Code A ©MMX Pedro J.Molina, PhD.
  30. 30. Usage Scenarios III <ul><li>Derive a Design Model (PIM  PSM) </li></ul><ul><li>Benefits </li></ul><ul><ul><li>Technology agnostic / Vendor independence </li></ul></ul><ul><ul><li>Detailed design </li></ul></ul><ul><ul><li>Opportunity to tune the design if needed </li></ul></ul>Abstract Model (PIM) Model Transformation Design Model (PSM) ... ©MMX Pedro J.Molina, PhD.
  31. 31. Usage Scenarios IV <ul><li>(Semi)? Automatic Reverse Engineering </li></ul><ul><li>Benefits </li></ul><ul><ul><li>Legacy apps migration </li></ul></ul><ul><ul><li>Pragmatic Cost/Benefit approach </li></ul></ul>Model Extraction Tool Legacy Asset ©MMX Pedro J.Molina, PhD.
  32. 32. Usage Scenarios V <ul><li>Model Migration </li></ul><ul><li>Benefits </li></ul><ul><ul><li>Automatic conversion of models when metamodel changes </li></ul></ul>Model A M2M Converter Model A’ Metamodel (v1) Metamodel (v2) Conforms with Conforms with Metamodel Evolution ©MMX Pedro J.Molina, PhD.
  33. 33. Usage Scenarios VI <ul><li>Transformation Chaining </li></ul><ul><li>Benefits </li></ul><ul><ul><li>Componentization </li></ul></ul><ul><ul><ul><li>Modular transformations </li></ul></ul></ul><ul><ul><ul><li>Testability </li></ul></ul></ul><ul><ul><ul><li>Reuse </li></ul></ul></ul>Model M2M Design Model M2T M2T Persistence Code Logic Code M2T Documentation ©MMX Pedro J.Molina, PhD.
  34. 34. Usage Scenarios VII <ul><li>Selecting best-suitable technologies </li></ul><ul><li>Benefits </li></ul><ul><ul><li>Architecture evolution </li></ul></ul><ul><ul><li>Selection of best architecture based in the nature of your expected (or real) workload </li></ul></ul><ul><ul><li>Measure performance </li></ul></ul>Model M2T A M2T B A Implementation B Implementation Load Test Perfor-mance Measure ©MMX Pedro J.Molina, PhD.
  35. 35. Usage Scenarios VIII <ul><li>Enforcing Naming Rules </li></ul><ul><li>Benefits </li></ul><ul><ul><li>Enforce company naming rules </li></ul></ul><ul><ul><li>Assure consistency </li></ul></ul><ul><ul><li>Central point of change for naming rules </li></ul></ul>Model Naming Code M2T Named Design Model Naming Policy Central point of change ©MMX Pedro J.Molina, PhD.
  36. 36. Usage Scenarios IX <ul><li>Assuring Compliance </li></ul><ul><ul><li>Accessibility, Auditing, Privacy Regulations, Security </li></ul></ul><ul><li>Benefits </li></ul><ul><ul><li>Enforce compliance </li></ul></ul><ul><ul><li>Proved by design </li></ul></ul><ul><ul><li>Avoid manual errors </li></ul></ul><ul><ul><li>Certify that all code derived from this method satisfies the required compliance level </li></ul></ul>Model Compliant Code Generator Compliant Code ©MMX Pedro J.Molina, PhD.
  37. 37. Usage Scenarios X <ul><li>Technology Forward Escape </li></ul><ul><li>Benefits </li></ul><ul><ul><li>Language / Architecture Evolution </li></ul></ul><ul><ul><li>Leverage Legacy Code </li></ul></ul><ul><ul><li>Consolidation in enterprise defined standards platforms </li></ul></ul><ul><ul><li>Reduce the investment in outdated technology ASAP (Reduce TCO) </li></ul></ul><ul><ul><li>Early adoption: </li></ul></ul><ul><ul><ul><li>Faster Time to Market </li></ul></ul></ul><ul><ul><ul><li>Opportunity to be the first one (Pioneer)! </li></ul></ul></ul>Model Old Code Generator New Code Generator Obsolete Unsupported Technology Code Latest Super cool Technology Code Extraction Tool Legacy Code ©MMX Pedro J.Molina, PhD.
  38. 38. Road Map for Essential <ul><li>Release an early Community Edition </li></ul><ul><li>Get feedback from the CG Community </li></ul><ul><li>Command line support (on going) </li></ul><ul><li>Improve M2M transformations </li></ul><ul><li>Include derivations (calculated model properties) </li></ul><ul><li>Add model query DSL </li></ul><ul><li>Translate transformations to C# or Java code </li></ul><ul><li>Pack code generators for specific needs </li></ul><ul><li>Plugin and extensibility model for the IDE </li></ul><ul><li>Support it all! </li></ul><ul><li>Projectional editor </li></ul>©MMX Pedro J.Molina, PhD.
  39. 40. <ul><li>Thank you! </li></ul><ul><li>More info about Essential on: </li></ul>The Metalevel http:// ©MMX Pedro J.Molina, PhD.