Fakultät Informatik - Institut - und Multimediatechnik - Softwaretechnologie – Prof. Aßmann – Model-Driven Softwrae Development in Technical Spaces 30. Model Synchronisation, Code Generation and Round-Trip Engineering for the Consistency of Macromodels Code Generation as Apps for RAG

1) Single-source principle and macromodel Prof. Dr. U. Aßmann principle Technische Universität Dresden 2) Code generation techniques Institut für Software- und ● Template-based Code generation Multimediatechnik 3) Re- http://st.inf.tu-dresden.de/ teaching/most Version 20-1.1, 02.01.21

OO © Prof. U. Aßmann 2

Q10: The House of a Technical Space Technical of a The House Q10: Model-DrivenDevelopment Software in Technical(MOST) Spaces Model synchronisation Bridges Space Technical Bridges Space Technical Querying, Attribution, Analysis, Interpretation Querying,Attribution, Analysis, Metapyramid (Metahierarchy) Querying, Attribution, Analysis, Interpretation Querying,Attribution, Analysis, Metapyramid (Metahierarchy) Composition,Mapping, Transformation Tracing, Regeneration,Tracing, Synchronization Composition,Mapping, Transformation Tracing,Regeneration, Synchronization Mega- and Mega- Macromodels Mega-and Macromodels Technical Space ModelManagement Technical Space ModelManagement Tool Engineering Composition, Extension ModelAnalysis Tool Engineering Composition, Extension ModelAnalysis CodeGeneration modeling modeling Meta- Meta- © Prof. U. Aßmann 3 ► ► ► ► ► ► ► Literature Model-DrivenDevelopment Software in Technical(MOST) Spaces Optional Implementation, pages 156-165, Albuquerque, New Mexico, June 23-25, 1993. ACM SIGPLAN Conference '93 on Design and Daniel Weise and Roger Crew. Programmable syntax macros. In Proceedings of the ftp://www-users.cs.york.ac.uk/reports/2003/YCS/369/YCS-2003-369.pdf Computer Science, 2003 Chris Holmes, Andy Evans. review A of frame technology. University of York, Dept. of Paul Bassett. Frame-based software engineering. IEEE Software, 4(4):9-16, 1987. http://www.codegeneration.net/tiki-read_article.php?articleId=65 www.programtransformation.org http://www.codegeneration.net/ ■ ■ ■ Falk Hartmann. Safe Template Processing of XML Documents. PhD thesis, Völter, Stahl: Model-Driven Software Development, AWL 2005. http://doi.ieeecomputersociety.org/10.1109/MS.1987.231057 Technische Universität Dresden, Fakultät Informatik, July 2011. . http://nbn-resolving.de/urn:nbn:de:bsz:14-qucosa-75342 © Prof. U. Aßmann 4

Model-DrivenDevelopment Software in Technical(MOST) Spaces Fakultät Informatik - Institut Software- und Multimediatechnik - Softwaretechnologie – Prof. Aßmann – Model-Driven Softwrae Development in Technical Spaces

30.1 Model2Code Transformation (Code Generation)

Transforming models into code (Programmüberführung) © Prof. U. Aßmann 6 MDSD-Code-Generators Model-DrivenSoftware Development inTechnical Spaces (MOST) (Code, Scripts, Templates) Scripts, (Code, specifications Textual Source artefacts Source artefacts UML-Diagrams Models Graph-based target language target the in Templates Specification of the transformation the of Specification RAG) grammar, String (RTG, language target the of Specification Generator Target program Target program Target Code Target with File © Prof. U. Aßmann (Code, Scripts) (Code, specifications Textual UML-Diagrams Models Tree-based Link 7 template language) template the in snippets from parsed partially or trees, syntax template (as language target the in Templates Source artefacts MDSD-Code-Generatorsas Attributors Syntaxof Trees Model-DrivenSoftware Development inTechnical Spaces (MOST) Source artefacts Syntax Tree Syntax Syntax Tree Tree target language target the of Specification RAG of the transformation the of Specification RAG codeattribute Higher-order synthesized RAGwith Target tree attribute Target tree attribute attribute higher-order in code target with Tree © Prof. U. Aßmann 8 ► ► ► DifferentKinds of Code Generators Model-DrivenSoftware Developmentin Technical Spaces (MOST) A code generators code Metaprogramming A ( ruleswith models    code selector code code scheduler code An An A after runs code scheduling Codeselection template expander template expander epae natpdadwlfre a CBSE) atemplates in andwellformedtyped way ( invasive fragment composer (invasive software composition) software composer (invasive fragment invasive is a transformation system (term,linksystem the transformation graphs) trees, ainput covering is orders instructions in an optimizedan in manner orders instructions code coverage code generates by filling code with generates templates ) transforming the the elements)transforming model once : → composes inset snippets inset © Prof. U. Aßmann 9 ► ► MDSD-Code-Generatorsas Template Expanders Model-DrivenSoftware Development inTechnical Spaces (MOST) UML-Diagrams Models Graph-based (OCL, Scripts) (OCL, specifications Textual Source artefacts A A Template engines are are enginesTemplate pretty-printingsimpleItunderinterface. afunctions provides template language) template the in snippets from parsed partially or trees, syntax template (as language target the in Templates Source artefacts ■ ■ template template engine pparse(): String in BaseLanguage templparse ) tigi epaeagae Tree (): String in TemplateLanguage hides the tree construction, tree hides construction, attribution code the with attributes, and apps ofhigher-order RAGs Parser of template language template of Parser Binding rules of templates of rules Binding Syntax Syntax Tree Tree Syntax Tree Attribution (templateexpander) Templateengine expanded code Tree with → Tree → Targetcode Targetcode code Target

Fakultät Informatik - Institut Software- und Multimediatechnik - Softwaretechnologie - Model-Driven Software Development in Technical Spaces (MOST)

30.1.2 Code Generation in RAGs

► With higher-order (tree-generation) attributes and special functions ■ partial parsing ■ template expansion

Model-Driven Software Development in Technical Spaces (MOST) © Prof. U. Aßmann

© Prof. U. Aßmann 11 ► ► Code Generationwith RAGs Model-DrivenDevelopment Software in Technical(MOST) Spaces Supposea Attribution functions may generate code syntax trees } eq } eq

if (AsBoolean())if

Constant.Code() } else { if (AsBoolean()) Constant.Code() } else { partial parse function parse partial else return EmptyTree; else return (AsValue() ==else if 0) 1)(AsValue() == if else EmptyTree; return else 0) (AsValue() ==if if 1)(AsValue() == return pparse(“ return pparse(“ else else else if 0) (AsValue() == if 1)(AsValue() == else else (AsValue() ==else if 0) if 1) (AsValue() == return pparse(„ return pparse(„ return pparse(„ return return pparse(„ return pparse(„ return pparse(„ return return pparse(“ return pparse(“ { {

pparse(): String-> pparse(): (boolean)1 (boolean)1 new Integer(“+AsValue()+„)“new Integer(0)new Integer(1)new new Integer(“+AsValue()+„)“new Integer(0)new Integer(1)new (boolean)0 (boolean)0 ”); ”); Link ”); ”); Tree “); “); “); “); ); ); © Prof. U. Aßmann 12 } eq ► ► ► Template-Based Code Generationwith RAGs Model-DrivenDevelopment Software in Technical(MOST) Spaces } eq , List(pparse(„ templparse( return ); return templparse( return ); List(pparse(„ , GenericClassInstantiation.Code() templparse() attribution functions, e.g., templparse(): String, List(ID)-> Done with the Attribution functions may expand code templates to code trees GenericClassInstantiation.Code() “ “ }“ GenClass class public }“ GenClass class public public int getId() { return myId; } myId; return { getId() int public } GenClass public int myId; private public int getId() { return myId; } myId; return { int getId() public } GenClass public int myId; private

template processing function processing template is calledis a Person Person “)) template processor “)) $TypeParameterName$ TypeParameterName→ TypeParameterName() $TypeParameterName$ $TypeParameterName$ $TypeParameterName$ Link Tree { {

, , String that expands variable names into () { // constructor // {() () { // constructor // { () is is of extends Object { Object extends extends Object { Object extends a template language © Prof. U. Aßmann 13 } } “ eq ► ► Template-Based Code Generationwith RAGs Model-DrivenDevelopment Software in Technical(MOST) Spaces } } “ eq return templparseGeneric( return RTGAceleo; RTGrammar CSAceleo; CSGrammar ); , return templparseGeneric( return RTGAceleo; RTGrammar CSAceleo; CSGrammar ); ); , GenericClassInstantiation.Code() templparse() TypeParameterName() variable names into attribution functions, e.g., templparseGeneric The GenericClassInstantiation.Code() List( List( public class GenClass class public } public class GenClass class public } “ template processing function processing template “ pparse(„ public int getId() { return myId; } myId; return { getId() int public } GenClass public int myId; private pparse(„ public int getId() { return myId; } myId; return { int getId() public } GenClass public int myId; private is calledis a Person Person (): CSGrammar, RTG, String RTG, CSGrammar, (): “)) “)) template processor $TypeParameterName$ $TypeParameterName$ $TypeParameterName$ $TypeParameterName$ CSAceleo CSAceleo

canbegeneric made interms grammars: of , { , { RTGAceleo RTGAceleo , String TypeParameterName → () { // constructor // {() () { // constructor // { () , extends Object { Object extends List(ID)->Link of is extends Object { Object extends , , a templatelanguage Tree that expands Fakultät Informatik - Institut Software- und Multimediatechnik - Softwaretechnologie

30.1.3 Single-Source Principle and Macromodels

Model-Driven Software Development in Technical Spaces (MOST) © Prof. U. Aßmann © Prof. U. Aßmann 15 TreeWare Major Source Major

Model-DrivenSoftware Developmentin Technical Spaces (MOST) Round-Trip Engineering Single-Source-Principle, Major-Source,Code Addition, and Documen- tation Program/Code- Hand-written <> Generated generation Document Generation) Code Source Models code „patching“), code composition Manualaddition (codeaddition, Major Source Major Optimizedand Parsing Code- Target code extended GrammarWare ModelWare © Prof. U. Aßmann 16 TreeWare Major Source Major

Model-DrivenSoftware Developmentin Technical Spaces (MOST) Macromodel Principle andRound-Trip Engineering Documen- tation Program/Code- Hand-written Source code Generated generation Document Generation) Code SourceModel A SourceModel Optimizedand Parsing Code- Target code B extended SourceModel C Consistent Multiple

Sources GrammarWare ModelWare © Prof. U. Aßmann 17 ► ► ► MacromodelPrinciple Single-Source Principle,Major-Source Principle and Model-DrivenSoftware Development inTechnical Spaces (MOST) A A In a macromodel, there are always always macromodel, there a In are multi-model)ofamodels (all documentation andALLmodels, (major-source) code, between consistency tests, and synchronisation A consistency code, tests (derived which models) derived,are ■ ■ ■ Single-Source-Technology Macromodel Technology Macromodel Generatedanddata suites test Generated (Chapter documentation) documentation on Generated(this chapter) code is a single-source technology with automatic technologywith single-source a is uses a uses derivedmodels single source source single ( with automatic synchronisation and synchronisation automatic with one ofone set major-source from models), © Prof. U. Aßmann 18 ► Synchronization is Used in Round-Trip Engineering Model-DrivenDevelopment Software in Technical(MOST) Spaces Trip-Engineering Technically, the Single-Source-Principle and the Macromodel principle needs   Model-to-code synchronisation Model-to-model synchronization      (RTE) between ModelWare and GrammarWare, to achieve Codegeneration Modek, SUM) View based transformations TGG) Bidirectional transformations Code reparsing templates Template-based codegeneration of the changed source code into models into severalprogramming languages with (later) with (with (e.g., with Triple-Graph-Grammars, inserts code snippets itno code e.g., with Single-Underlying Round- Example: Round-Trip Engineering in Together (P. Coad, Borland)

19 Model-Driven Software Development in Technical Spaces (MOST)

► In 1997, the CASE tool Together was the first to provide a Single-Source-Technology with automatic synchronisation and consistency between UML model, code and documentation ► Supported Programming Languages: Java, Visual Basic, VisualBasic.Net, CORBA IDL, C++, C#  Synchronisation by reparsing of generated, modified and extended code ► Round-trip Engineering:  Changes of class diagrams will be transformed to code  Changes of code reparsed to class diagrams  Reverse Engineering of entire projects n n a m ß A

. U

.

f http://www.borland.com/downloads/download_together.aspx o r P

http://www.borland.com/de-DE/Products/Requirements-Management/Together/Testimoni © als https://en.wikipedia.org/wiki/Borland_Together

1 © Prof. U. Aßmann 20

Model-DrivenSoftware Development inTechnical Spaces (MOST) Together Screenshot © Prof. U. Aßmann 21

Model-DrivenSoftware Development inTechnical Spaces (MOST) Code Generation in Different Languagesin Together    Code template based code generation code based template Code configured be can views Appropriate and Developer Designer, Modeler, Business roles: Supports Fakultät Informatik - Institut Software- und Multimediatechnik - Softwaretechnologie

30.2 Technologies for Model-2-Code Generation and Synchronization

Model-Driven Software Development in Technical Spaces (MOST) © Prof. U. Aßmann ©<> Prof. U. Aßmann <> <> Generated Subclass Generated Delegator Generated Generated Decorator Generated 23 ► ► ► Composition of Separated Hand-Writtenand Generated Code Model-DrivenDevelopment Software in Technical(MOST) Spaces Synchronization is easy:do touchnot generatees TemplateMethod, Composite, Decorator, etc Use class composition like delegation, pattern [Völter/Stahl] In separate files: <> <> <> Coupling by implementation Generated Generated Decoratee Superclass Generated Delegatee Generated <> <> <> <> <> <> ► ► hedged hedged areas Synchronizationshould outstay of (Trennmarkierung) Inone file: Generated Wrapper Generated /*** Generated code ***/ /***Generated code hedge ***/ Couplingwith /*** Handcode hedge ***/ … Hand-written Code .... Code .... hegdes hegdes © Prof. U. Aßmann 24 } eq } eq } „ “ eq ► Composition of Generated and Hand Written Code in an RAG Model-DrivenDevelopment Software in Technical(MOST) Spaces } eq } eq } „ “ eq GeneratedBody.Code() { Head.Code() { Procedure return Body.Code(); return pparse(“publicreturn „+Head.name+“()”); Head.Code()+ return Fine-grained glueing possible GeneratedBody.Code() { Head.Code() { Procedure return pparse(“public„+Head.name+“()”); return Head.Code()+ return Body.Code(); GeneratedBody.Code()+ GeneratedBody.Code()+ /*** */“;/*** HEDGE2Gen END /*** HEDGE2Gen BEGIN */“+ /*** HEDGE2Gen END */“;/*** HEDGE2Gen END /*** HEDGE2Gen BEGIN */“+ .Code() .Code() { { © Prof. U. Aßmann 25 ► ► ► ► ► ► andTemplate Programmingwith RAG Model-DrivenSoftware Developmentin Technical Spaces (MOST) A → (templateswith hooks) with genericand extensible fragments Invasiveprogramming with genericfragments (templates). Genericprogramming extended hooks (extensionpoints) A fragment be parameters,variation points) isa fragment with A describedby metaclassa nonterminalof thegrammar, or sentence ofa language, derived from a extensible fragment genericfragment (template, frame) form, fragment(snippet)

CBSEcourse bound (filled, expanded) to a fragment to a result fragment slots(holes, code

isa incomplete

isa fragmentwith is programming

isprogramming , whichcan be with an , whichcan inset

Hedge Generic Snippet Template- expander Inset Inset Slot Inset Fragment Result Fakultät Informatik - Institut Software- und Multimediatechnik - Softwaretechnologie

30.2.1 Template-based Code Generation (Schablonenbasierte Programmüberführung)

Model-Driven Software Development in Technical Spaces (MOST) © Prof. U. Aßmann © Prof. U. Aßmann 27 Coupling stringexpansion Coupling by

Template Expansion byComposition of Insets and Hedging Model-DrivenDevelopment Software in Technical(MOST) Spaces Coupling by string expansion Couplingby string String-Template- expander Inset Slot (Fill-in) Inset Coupling program composition Coupling by Coupling by program composition by Coupling Composition program Inset Slot (Fill-in) Inset © Prof. U. Aßmann 28 ► ► ►

Slots are Marked by Hedges Model-DrivenDevelopment Software in Technical(MOST) Spaces metaprogramming language to the code language Inset hedges to the slot language Slot hedges Hedges are delimiters that do not occur the in base nor in the slot language are template2slot hedges marking the transition from the code language are metaprogramming2code hedges marking the transition from the } { t:TYPE) (superclass:CLASS, template >> << hedges slot code // } { t:TYPE) (superclass:CLASS, template >> << hedges slot code // class Worker extends <> { <> extends Worker class class Worker extends <> { <> extends Worker class } } } } void setAttr(<>); void void setAttr(<>); void <> getAttr(); <> <>(); new = attr <> <> getAttr(); <> <>(); new = attr <> © Prof. U. Aßmann 29 ► ► ► ► Tools for Untyped Template Expansion Model-DrivenDevelopment Software in Technical(MOST) Spaces String template engines processor XML template processing processing technology Frame processing – – – – – – – – The original frame processor used $ hedge a as symbol for slots (slot variables) Frame processing the is main technology for web engineering today: it organizes Acceleo Jenerator for Java Parr's template engine StringTemplate Apache Velocity http://velocity.apache.org/ http://sourceforge.net/projects/fxvcl/files/XVCL%20Specification/Version Because only slot variables hold insets, macro parameters correspond to slot reuse of page templates %202.10/ variables , universal for textual all languages [Holmes/Evans] https://www.eclipse.org/acceleo/ was invented in [P. Bassett] as an is not is much different usein today http://www.voelter.de/data/pub/jeneratorPaper.pdf engine XVCL [Jarzabek] is XML-controlled an frame

untyped string template expansion © Prof. U. Aßmann 30 ► ► ► < (from XML) is the inset hedge {#, $} are slot hedges frame processing language with Velocity Template Language (VTL) a is

Velocity String Template Language Model-DrivenDevelopment Software in Technical(MOST) Spaces ■ Hello ) "Velocity" $foo = #set( metaprograms in slots, written in a Hello ) "Velocity" $foo = #set( slot language $foo $foo World! World! http://velocity.apache.org/engine/releases/velocity-1.7 (blue) #end ) $customer.hasPurchased($mud) ( $mudsOnSpecial) in #foreach($mud

Hello
#end ) $customer.hasPurchased($mud) ( $mudsOnSpecial) in #foreach($mud Hello #end #if #end #if $customer.Name $customer.Name
$flogger.getPromo( $mud ) $mud $flogger.getPromo( $flogger.getPromo( $mud ) $flogger.getPromo( ! ! © Prof. U. Aßmann 31 ► ► ► Similar: Acceleo (in exercises) foreach), assignments (set), and macros It controlhas structures (if, switch, TCL simple in the spirit of Velocity Template Language (VTL) a is

Velocity Template Language Model-DrivenDevelopment Software in Technical(MOST) Spaces (error-prone) may be syntacticallynot correct, nor well-formed, target language Problem: the result of string template expansion (error-prone) may not be syntactically correct, nor well-formed, target language Problem: resultthe of string template expansion http://velocity.apache.org/engine/releases/velocity-1.7 #outer( "#inner($bar)" ) "#inner($bar)" #outer( 'calltimelala') = #set($bar #end $foo ) outer #macro( #end $foo ) inner #macro( #outer( "#inner($bar)" ) "#inner($bar)" #outer( 'calltimelala') = #set($bar #end ) $foo outer #macro( #end ) $foo inner #macro( inner : $foo : inner inner : $foo : inner outer : $foo outer "outerlala") = #set($bar outer : $foo outer "outerlala") = #set($bar © Prof. U. Aßmann 32 ► ► Typed Template Expansion Model-DrivenDevelopment Software in Technical(MOST) Spaces expansion (see CBSE course) Invasive Software Composition provides fully typed and wellformed template Metamodel-controlled template engines ■ ■ ■ ■ ■ – – – https://bitbucket.org/svenkarol/skat/wiki/Home http://www.reuseware.org http://www.the-compost-system.org Can be instantiated for arbitrary languages Typed template expansion Acceleo code generating system (see exercises) XML slot EMF: Xtend and Xpand scripting languages and -extension (obsolete now) and weaving

© Prof. U. Aßmann 33 ► ► Semantic Macros Model-DrivenDevelopment Software in Technical(MOST) Spaces Examples: results. Semantic Macros       A A semantic macro compiled is to fragmenta tree http://docs.scala-lang.org/overviews/macros/overview.html Scala Scheme In an higher-order RAG, a semantic macro can be instantiated in a higher- order They allow for type-safe static metaprogramming. attribute http://scalamacros.org/  are metaprogramming procedures which are typed parameters and metamodel, and copied to the instantiation spot which is instantiated by fragment parameters, type-checked on the

© Prof. U. Aßmann Attribute 34 Code- ► Xdoclet(xdoclet.sf.net) forMetadata-Based Code Generation Model-DrivenDevelopment Software in Technical(MOST) Spaces Xdoclet transforms attributes (metadata) into helper code (aka )   Metadata attributes With template-based code generation Code-Rules Attribut- Parser QDox XDoclet Plugin Syntax trigger Tree the filling of templates, usedfrom a library Templates Templates- Code- Expander Syntax Tree Syntax Tree Target CodeTarget © Prof. U. Aßmann 35 ► ► Slot MarkupLanguages Model-DrivenDevelopment Software in Technical(MOST) Spaces The slot language represented is an as XML dialect itself(XSD schema) [Hartmann] A ■ ■ ■ slot markup language XML tools are usable Uniform syntax for templates Filling templates is . . checked and wellformed, because OCL constraints be can definedthat are type-safe is is a special template language for any XML dialect © Prof. U. Aßmann 36

TheEnd ► ► ► ► ► ► ► Model-DrivenDevelopment Software in Technical(MOST) Spaces Think about GOTO statements machinein code, or in C programs. Why are template engines apps for RAGs? RAG? What theis difference of a metadata attribute (annotation), and attributean in an Why does code generation most often use synthesized attributes? GeneratedDelegator, GeneratedSuperClass, GeneratedSubclass! Explain the difference of the code generation patterns GeneratedDelegatee, How would you generate code with Xcerpt? Why code is generation gooda application for RAG? ■ ■ Why are AG not really appropriate for representing GOTOs? How would you represent them anin RAG? Fakultät Informatik - Institut Software- und Multimediatechnik - Softwaretechnologie

30.A.1. Code Modification and Reparsing (Codemodifikation und -rückführung)

Model-Driven Software Development in Technical Spaces (MOST) © Prof. U. Aßmann © Prof. U. Aßmann 38 ► rs01_t03_ManuelBork_EMCDA2008_slides.pdf http://www.fokus.fraunhofer.de/en/fokus_events/motion/ecmda2008/_docs/ ► Example Codeof Reparsing Technique Model-DrivenDevelopment Software in Technical(MOST) Spaces indeterministic situations of re-parsing Parallel Parsing of Template and Generated Code, with comparison to resolve Code-Reparsing in Fujaba: