Model-Driven Software Engineering Code...

33
IBM Research – Zurich Model-Driven Software Engineering Code Generation Dr. Jochen Küster ([email protected])

Transcript of Model-Driven Software Engineering Code...

Page 1: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

IBM Research – Zurich

Model-Driven Software Engineering

Code Generation

Dr. Jochen Küster ([email protected])

Page 2: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

2

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Contents

� Code Generation in Model-Driven Software Engineering

� Concept and Styles of Code Generators

� A Closer Look at Xpand

� Summary and References

Page 3: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

3

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Code Generation in Model-Driven Software Engineering

Page 4: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

4

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Motivation for Code Generators

� Model-Driven Software Development makes models key artifacts in the software engineering process

� When working with models, automation of recurring tasks can often be achieved by code generation

� The MDA approach favors code generation from models

� Architecture-centric Model Driven Software Development generates code from models

PSM

Code

Page 5: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

5

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Recap: Purpose of Code Generation in AC-MDSE

� Generate generic code for the platform instead of writing it

� Generate schematic code using transformations based on an application model

� Write individual code that is application specific

ApplicationModel

Transformation

SchematicRepetitive

Code

Individual

code

Schematic

code

Generic

codeIndividual

Code

Platform

Page 6: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

6

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Purpose of Code Generation

� Separation of application logic from implementatio n platform detailsin order to ease transition to other platforms

– Example: SOA solutions where a process model is implemented

� Improved productivity by using code generation– Example: EMF generation of code from domain models

� Improved quality of the application due to standardized implementation (patterns and best practices)

– Example: EMF code generation

� Increased performance of the application using generators that produce efficient code

– Example: Code generation from statecharts in embedded applications

Page 7: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

7

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Concept and Styles of Code Generators

Page 8: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

8

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Code Generation as a form of Model Transformation

� Source models include models defined in various modeling languages and even program code

� The target is code which has to conform to the syntax of the target language

– Java, C#, C++

SourceModel

Code/Program

Code GenerationDefinition

SourceMeta-Model

TargetMeta-Model

CodeGenerator

<<instance of>><<instance of>>

reads

refers to

writes

refers to

Page 9: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

9

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Terminology of Code Generation

� Target is also referred to as Program

� Code Generator and Code Generation Definition also called Meta Program

� Meta Program and Program can be mixed or separated from each other

SourceModel

Code/Program

Code GenerationDefinition

SourceMeta-Model

TargetMeta-Model

CodeGenerator

<<instance of>><<instance of>>

reads

refers to

writes

refers to

Page 10: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

10

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Generation Patterns

� There are different ways how to design and implement a code generator

� We discuss several well-known generation patterns:–Templates and filtering–Templates and metamodel–API-based generators–Inline code generation

� Different generation patterns have different advantages and disadvantages

Page 11: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

11

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Templates and Filtering

� Source model is in textual XMI/XML form

� Source model is filtered to obtain a subset of the source model

� Templates are instantiated using values of the filtered source model

� Result yields the code/program

SourceModel

Subset ofSourceModel

Templates

Code/ProgramFilter

Apply to

Code GenerationDefinition

Page 12: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

12

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

XSLT Example (1)

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

<JavaBean name="Customer">

<Package>com.developer</Package> <Imports> <Import>java.util.Collection</Import> </Imports>

<Superclass name="Object"/>

<Properties>

<Property name="name" type="String"/> <Property name="addrln1" type="String"/>

<Property name="addrln2" type="String"/>

<Property name="city" type="String"/> <Property name="state" type="String"/>

<Property name="zip" type="String"/>

<Property name="contacts" type="Collection"/>

</Properties>

</JavaBean>

Source: Jeff Ryan: Code Generation with XSL, developer.com

� Code generation of a Java Bean class

� Input is an XML description of the required information

Page 13: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

13

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

XSLT Example (2)<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/

1999/XSL/Transform">

<xsl:output method="text"/>

<xsl:template match="/">

package <xsl:value-of select="//Package"/>;

<xsl:apply-templates select="//Import"/>

public class <xsl:value-of select="JavaBean/@name"/> extends <xsl:value-of select="//Superclass/@name"/> {

<xsl:apply-templates select="//Property" mode="instanceVariable"/>

public <xsl:value-of select="JavaBean/@name"/>()

{ }

<xsl:apply-templates select="//Property" mode="accessor"/>

<xsl:apply-templates select="//Property" mode="mutator"/> }

</xsl:template>

<xsl:template match="Import">

import

<xsl:value-of select="." />

;

</xsl:template>

<xsl:template match="Property" mode="accessor">

Source: Jeff Ryan: Code Generation with XSL, developer.com

generate import of Java class

generate properties

generate getters/setters

Page 14: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

14

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Template and Filtering Drawbacks

� Templates become very complex for larger examples

� Approach tightly couples the generation definition (templates) to the concrete syntax of the model

� This yields low maintainability if source modeling language evolves

Page 15: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

15

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Templates and Meta Model

� Source model is parsed in order to create instance of source model meta model

� Templates are defined in terms of source model meta model terms

� Templates are instantiated using values of the instance of the source model meta model

� Result yields the code/program

SourceModel

Instance of source modelmeta model

Templates

Code/Program

Parse

Apply to

Code GenerationDefinition

Page 16: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

16

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Templates and Meta Model Example: Xpand

� Xpand is a model-to-text transformation language developed in the context of openArchitectureWare

� Recently added to be part of the Eclipse M2T project

� Suited for transformation of models to text for code generation

� Direct reference of metamodelelements in transformation rules

«IMPORT metamodel»

«DEFINE main FOR Book»

«FILE name+".java"»

public class «this.name» {

}

«ENDFILE»

«ENDDEFINE»

Page 17: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

17

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

API –based Generators

� Client program uses API which is based on a Grammar

ClientProgram

API

Grammar

Code/Program

uses

based on

Code GenerationDefinition

Page 18: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

18

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

API-based Generators Example

� C# API for generating code CodeNamespace n = ...

CodeTypeDeclaration c = new

CodeTypeDeclaration („Vehicle");

c.IsClass = true;

c.BaseTypes.Add (typeof (System.Object) );

c.TypeAttributes = TypeAttributes.Public;

n.Types.Add(c);

public class Vehicle : object {

}

Page 19: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

19

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Inline Code Generation

� Code generation is done implicitly by means of a precompiler

� The precompiler modifies the program which is then compiled or interpreted

� Examples are common in the programming language domain (C++ precompiler)

Source Codewith variants

partially resolved

Source Codewith variants

Code/Program

Compiler with Preprocessor

Source: [Voelter et al.]

Page 20: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

20

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Comparison of Approaches - Criteria

not very goodnot very goodsimpleInline code generation

not very gooddepends on APIdepends on APIAPI-based

very goodvery goodhighTemplate + Metamodel

goodnot very goodsimpleTemplate + Filtering

Suitability to model-to-code transformations

Suitability for complex uses

Learning complexity

[Source: adapted from Voelter]

Page 21: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

21

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

A Closer Look at XPand

Page 22: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

22

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Introduction to XPand

� XPand is a template language for model to text transformations

� Originally developed as part of openArchitectureWare

� Now part of the Eclipse M2T project

� XPand project contains Template files

Page 23: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

23

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Overview of XPand Templates

«IMPORT meta::model»

«EXTENSION my::ExtensionFile»

«DEFINE javaClass FOR Entity»

«FILE fileName()»

package «javaPackage()»;

public class «name» {

// implementation

}

«ENDFILE»

«ENDDEFINE»

import meta models

specify meta model extensions

defines template for transformation

defines file for output

generated text

Page 24: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

24

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Import and Extension of Meta Models

� Import meta models into templates

� Describe extensions for meta models such as queries or target platform specific names

� Can be done using meta model extensions using the Xtend language

«IMPORT meta::model»

«EXTENSION my::ExtensionFile»

Page 25: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

25

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Defining Templates for Transformation

� The template is executed for each instance of the MetaClass

� Support of polymorphism: –If there are two templates with the same name that are defined for two

meta classes which inherit from the same super class, the corresponding subclass template is used in case the template is called for the super class. Vice versa the super class’s template would be used in case a subclass template is not available.

«DEFINE templateName(formalParameterList) FOR MetaClass»

a sequence of statements

«ENDDEFINE»

Page 26: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

26

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Defining Subroutine Templates using EXPAND

� EXPAND defines another template inside a given template

� «EXPAND myDef FOR this» evaluates myDef for the model element in the context of which EXPAND is situated

� «EXPAND myDef FOR entity» evaluates myDef for entity.

� «EXPAND myDef FOREACH entity.allAttributes»

� If FOREACH is specified, the target expression must evaluate to a collection type. In this case, the specified definition is executed for each element of that collection

«EXPAND definitionName [(parameterList)]

[FOR expression | FOREACH expression [ SEPARATOR expression] ]»

Page 27: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

27

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Defining Files for Output

� FILE allows to redirect output into a file

� expression specifies the file name of the file

� The file is saved relative to the directory where the generator executes

� outletName allows to specify an identifier for reuse

«FILE expression [outletName]»

a sequence of statements

«ENDFILE»

«FILE name+".java"»

public class «this.name» {

«EXPAND generateChapters FOREACH this.chapters»

}

«ENDFILE»

Page 28: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

28

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Expressions in XPand

� Expressions provide access to instantiated meta model values

� Expressions are based on an expression language that has similarities to Java and OCL

� The expression language is based on a type system supporting built-in types such as Boolean, Integer, String and Real and Collection types

public class «this.name» { .. }

Page 29: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

29

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Example

«IMPORT metamodel»

«DEFINE main FOR Book»

«FILE name+".java"»

public class «this.name» {

«EXPAND generateChapters FOREACHthis.chapters»

}

«ENDFILE»

«ENDDEFINE»

«DEFINE generateChapters FOR Chapter»

public void print«this.title»() {}

«ENDDEFINE»

Page 30: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

30

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Overview of XPand project in Eclipse

Page 31: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

31

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Workflow for Generation

<component class="oaw.xpand2.Generator2">

<fileEncoding value="ISO-8859-1"/>

<metaModel class="oaw.type.emf.EmfMetaModel">

<metaModelPackage value="org.eclipse.emf.ecore.Ecore Package"/>

</metaModel>

<expand value="example::Java::all FOR myModel"/>

<!-- aop configuration -->

<aspects value='example::Aspects1, example::Aspects 2'/>

<!-- output configuration -->

<outlet path='main/src-gen'/>

<outlet name='TO_SRC' path='main/src' overwrite='fa lse'/>

<beautifier class="oaw.xpand2.output.JavaBeautifier "/>

<beautifier class="oaw.xpand2.output.XmlBeautifier" />

<!-- protected regions configuration -->

<prSrcPathes value="main/src"/>

<prDefaultExcludes value="false"/>

<prExcludes value="*.xml"/>

</component>

Defines generator

Defines meta model to be used for applying templates

Defines how to evaluate

Defines outlets for output

Specifies beautifier for output

Page 32: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

32

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Example with Polymorphism

«DEFINE generateBook FOR Book»

«FILE name+".java"»

public class «this.name» {

«EXPAND generateChapters FOREACH this.chapters»

}

«ENDFILE»

«ENDDEFINE»

«DEFINE generateBook FOR ScientificBook»

«FILE name+".java"»

public class «this.name» {

«EXPAND generateScientificArea»

«EXPAND generateChapters FOREACH this.chapters»

}

«ENDFILE»

«ENDDEFINE»

Page 33: Model-Driven Software Engineering Code Generationresearcher.watson.ibm.com/researcher/files/zurich-jku/mdse-08.pdfSummary of Lecture and References Code generation is an important

33

IBM Research – Zurich

Dr. Jochen Küster | MDSE 2011

Summary of Lecture and References

� Code generation is an important aspect in model-driven software engineering

� Different forms of code generation from an architecture point of view

� XPand Eclipse code generation as an example for Template and Meta Model

References:

� Voelter et al. Model-Driven Software Development, Chapter on Code Generation.

� S. Efftinge and C. Kadura. OpenArchitectureWare 4.1 Xpand Language Reference.

� J. Oldevik. MOFScript User Guide, Version 0.8. Available online.