20081202 md workbench tool

38
MDWorkbench Ander Zubizarreta

Upload: azubi

Post on 26-May-2015

683 views

Category:

News & Politics


0 download

TRANSCRIPT

Page 1: 20081202 Md Workbench Tool

MDWorkbench

Ander Zubizarreta

Page 2: 20081202 Md Workbench Tool

MDWorkbench

• MDWorkbench is a model-driven engineering development environment to build:– Source code and text generators– Word documentation– Model transformers

• Eclipse-based IDE– Java code may be used in MDWorkbench projects

• Developed by Sodius SAS

Page 3: 20081202 Md Workbench Tool

MDWorkbench

Page 4: 20081202 Md Workbench Tool

MDWorkbenchVersions

• MDWorkbench Free– Limitation: UML models only

• MDWorkbench Pro– Any metamodel

• MDWorkbench Academic– Any metamodel

Page 5: 20081202 Md Workbench Tool

MDWorkbenchCode generation

• Code generation is based on text templates, rulesets and scripts

• Text templates are written in TGL (Template Generation Language)

• Rulesets are written in MQL (Model Query Language)

• Scripts can be written in TGL, MQL or Java

Page 6: 20081202 Md Workbench Tool

Code generationExample model

Page 7: 20081202 Md Workbench Tool

Code generationText template

[#package tutorial.java] [#template public JavaSource(class : uml20.Class)] [#file]generated/${class.name}.java[/#file] public [#if class.isAbstract]abstract [/#if]class ${class.name} { [#-- Attributes declaration --] [#foreach attr : uml20.Property in class.attribute] private ${attr.javaType} ${attr.name}; [/#foreach] [#-- Operations declaration --] [#foreach operation : uml20.Operation in class.ownedOperation] ${operation.declaration}[#trim] [/#foreach] } [/#template]

Page 8: 20081202 Md Workbench Tool

Code generationRuleset

package tutorial.java; public ruleset JavaGeneration(in model : uml20) { public rule main() { foreach (class : uml20.Class in model.getInstances("Class")) { $JavaSource(class); } } }

Page 9: 20081202 Md Workbench Tool

Code generationScripts

• Script to handle multiplicity:package tutorial.java; metatype uml20.Property; public script javaType() : String { if (self.isMultivalued()) { return "java.util.Collection"; } else { // if the type is not set, returns "Object" return self.type.name ? "Object"; } }

Page 10: 20081202 Md Workbench Tool

Code generationScripts

• Scripts to add parameters declaration in operations– Script to filter UML parameters:

package tutorial.java; import com.sodius.mdw.metamodel.uml20.ParameterDirectionKind; metatype uml20.Operation; public script returnType() { return self.ownedParameter.detect("direction", com.sodius.mdw.metamodel.uml20.ParameterDirectionKind.RETURN_LITERAL).type; } public script inParameters() { return self.ownedParameter.reject("direction", com.sodius.mdw.metamodel.uml20.ParameterDirectionKind.RETURN_LITERAL); }

Page 11: 20081202 Md Workbench Tool

Code generationScripts

– Script to generate UML parameters:

[#package tutorial.java] [#metatype uml20.Operation] [#script public declaration] [#set parameters = self.inParameters.concat("declaration", ", ")] public ${self.returnType.name} ${self.name}(${parameters}) { return null; // TODO insert your code here } [/#script]

Page 12: 20081202 Md Workbench Tool

Code generationRunning generation

• Running configuration:

Page 13: 20081202 Md Workbench Tool

Code generationRunning generation

• Output files:

Page 14: 20081202 Md Workbench Tool

Code generationRunning generation

• Generated code example:/* * Generated by Administrador * on Fri Nov 28 09:01:31 CET 2008 */ public class Order { private String date; private java.util.Collection items; private Customer customer; public Product findLineItem(String productName) { // Start of user code of findLineItem() throw new RuntimeException("No such product: " + productName); // End of user code } }

Page 15: 20081202 Md Workbench Tool

Documentation generation

• Documentation generators are Microsoft Word® based templates

• MQL is used to access the dynamic information

• Doc Template XML schema must be added in Word

Page 16: 20081202 Md Workbench Tool

Documentation generationDoc template

• Doc template uses XML tags to delimit dynamic sections in the document

in model : uml20

result.xml

Introduction This document is generated using MDWorkbench.

This is a comment, not part of the output

Page 17: 20081202 Md Workbench Tool

Documentation generationDoc template

• Microsoft Word® validates the doc template using the referenced XML schema

Page 18: 20081202 Md Workbench Tool

Documentation generationDoc template

• Calling a script from a template:package tutorial.doc; metatype uml20.Property; public script typeName() : String { var name : String = self.type.name; if (self.isMultivalued()) { return "Collection of " + name; } else { return name; } }

... Attributes

Name Type

attr in

class.attribute attr.name

attr.typeName

Page 19: 20081202 Md Workbench Tool

Documentation generationDoc template

• Example Doc template:

Page 20: 20081202 Md Workbench Tool

Documentation generationRunning configuration

Page 21: 20081202 Md Workbench Tool

Documentation generationOutput example

Page 22: 20081202 Md Workbench Tool

Model transformation

• Example: – Transformation from a UML model to a Relational

model– Generation of SQL from the Relational model

• Transformation is done using a Ruleset

Page 23: 20081202 Md Workbench Tool

Model transformationUML2Relational: Ruleset

package tutorial.uml2relational; public ruleset UML2Relational(in source : uml20, out target : relational) { public rule main() { @createTypes(); // create a Table for each Class foreach (class : uml20.Class in source.getInstances("Class")) { @createTable(class, target.create("Table")); } } private rule createTypes() { // create a Relational Type for each UML DataType foreach (dataType : uml20.DataType in source.getInstances("DataType")) { var type : relational.Type = target.create("Type"); if (dataType.name.equalsIgnoreCase("int")) type.name = "INT"; else if (dataType.name.equalsIgnoreCase("string")) type.name = "VARCHAR(255)"; else type.name = dataType.name; dataType#coref.add(type); } } ...

Page 24: 20081202 Md Workbench Tool

Model transformationRunning transformation

- The output is a XMI file which defines the relational model

Page 25: 20081202 Md Workbench Tool

Model transformationSQL generation

• SQL code is generated from the Relational model using a text template:

[#package tutorial.uml2relational] [#template public GenerateSQL(in model : relational)] [#file]generated/generatedTables.sql[/#file] [#foreach table : relational.Table in model.getInstances("Table")] -- -- TABLE ${table.name} -- DROP TABLE IF EXISTS `${table.name}`; CREATE TABLE `${table.name}` ( [#foreach column : relational.Column in table.columns] [#set sqlName = column.name.toUpperCase() sqlType = column.type.name ? "VARCHAR(255)" hasNext = column != table.columns.last()] `${sqlName}` ${sqlType} [#if hasNext],[/#if] [/#foreach] ) ENGINE=MyISAM DEFAULT CHARSET=latin1; [/#foreach] [/#template]

Page 26: 20081202 Md Workbench Tool

Model transformationSQL generation

• We can chain the model transformation and the code generation in a Ruleset:

package tutorial.uml2relational; public ruleset TransformAndGenerate(in source : uml20) { public rule main() { // create an empty Relational model var target = context.createModel("relational"); // Transform input UML model to Relational @UML2Relational(source, target).main(); // Generate SQL code $GenerateSQL(target); } }

Page 27: 20081202 Md Workbench Tool

Model transformationOutput result

--

-- TABLE IdentifiedElement

--

DROP TABLE IF EXISTS `IdentifiedElement`;

CREATE TABLE `IdentifiedElement` (

`ID` INT

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--

-- TABLE NamedElement

--

DROP TABLE IF EXISTS `NamedElement`;

CREATE TABLE `NamedElement` (

`NAME` VARCHAR(255)

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--

-- TABLE Customer

--

DROP TABLE IF EXISTS `Customer`;

CREATE TABLE `Customer` (

`ORDERS` VARCHAR(255)

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

• The result is a SQL file:

Page 28: 20081202 Md Workbench Tool

MDWorkbenchTGL

• TGL is the language used by MDWorkbench to create text templates

• is a simple imperative language using markups (tags) to distinguish static text from dynamic code

Page 29: 20081202 Md Workbench Tool

MDWorkbenchTGL

• TGL template structure:

• TGL script structure:

[#package packageName] ([#import importName])* [#template visibility templateName(templateParameters)] (body statement)* [/#template]

[#package packageName] ([#import importName])* [#metatype metatypeName] ([#script visibility scriptName(scriptParameters)] (body statement)* [/#script])*

Page 30: 20081202 Md Workbench Tool

MDWorkbenchTGL

• A TGL body statement may be:– Static text: This is a static text – Dynamic text: ${MQL expression} – Comment: [#-- This is a comment --] – Body directive

Page 31: 20081202 Md Workbench Tool

MDWorkbenchTGL directives

Most commonly used directives

guard Conditionally skips the evaluation of a text template or of a script body.

file

Defines the name of the file where the text generated by a text template will be written.

foreach, break Processes a section of the template for each element contained within a collection.

if, else, elseif Conditionally skips a section of the template.

import

Makes the referenced element (text template, ruleset or Java type) directly available in the template.

include

Evaluates a referenced text template and inserts the resulting text at the current position.

metatype Specifies the metatype for which the contained scripts apply.

package Defines the namespace of text templates and of scripts.

protectedStartTag protectedEndTag

Specifies sections in the generated text where any user manual modification must be preserved.

script Defines the signature of a script, as well as the contents to be generated.

set Declares and initializes variables for use within the template.

template

Defines the signature of a text template, as well as the contents to be generated.

Page 32: 20081202 Md Workbench Tool

MDWorkbenchTGL directives

Advanced directives

attempt, recover Provides alternate processing in case an error occurs.

compress

Removes superfluous white-space for white-space insensitive formats (e.g. HTML or XML).

library Defines the name of the library, as well as the macros it contains.

macro Defines the signature of a macro, as well as the contents to be generated

noparse Causes the contained text to be skipped by the parser and output directly

stop Aborts the evaluation process.

tab Shifts right using a tab character a section of the template.

trim, ltrim, rtrim, notrim

Instructs the engine to ignore certain whitespace characters in the line of the directive.

using

Makes the macros defined in the referenced library directly available in the template.

Page 33: 20081202 Md Workbench Tool

MDWorkbenchMQL

• MQL is the language used to write Rulesets• Rulesets are used to define model transformations

and to launch text generations• It is also used as an expression language for text

templates statements

Page 34: 20081202 Md Workbench Tool

MDWorkbenchMQL

• MQL ruleset structure:

• MQL rule structure:

package packageName; (import importName;)* visibility ruleset ruleSetName(ruleSetParameters) { (rule declaration)* }

visibility [parentRuleName::]rule ruleName(ruleParameters) : returnType { (body statements)* }

Page 35: 20081202 Md Workbench Tool

MDWorkbenchMQL

• MQL script structure:

package packageName; (import importName;)* metatype metaTypeName; (visibility script scriptName(parameters) : returnType { (body statements)* })*

Page 36: 20081202 Md Workbench Tool

MDWorkbenchMQL statements

Most commonly used statements

assignment Changes variables value or model element properties.

foreach, break Processes some statements for each element contained within a collection.

if, else Conditionally skip some statements.

import

Makes the referenced element (text template, ruleset or Java type) directly available in the ruleset.

metatype Specifies the metatype for which the contained scripts apply.

method call Calls a method or a script.

package Defines the namespace of rulesets and of scripts.

return Exits a script or a rule, with an optional value.

rule Defines the signature of a rule, as well as the contents to be evaluated.

rule call Evaluates a rule, either of the same ruleset or of an external ruleset.

ruleset Defines the signature of a ruleset, as well as the contained rules.

script Defines the signature of a script, as well as the contents to be evaluated

text template call

Generates a file based on a referenced text template.

var Creates a new variable and initializes its value.

Advanced statements

attempt, recover Provides alternate processing in case an error occurs.

stop Aborts the evaluation process.

Page 37: 20081202 Md Workbench Tool

MDWorkbenchMQL expressions

Literals

Strings: "Hello World" Booleans: true or false Numbers: 3, 0.08 Lists: ["winter", "spring", "summer", "autumn"] Maps: {"winter" = "cold", "summer" = "warm"} No value: null

Variables

myClass

Dotted notation myClass.isAbstract(), myClass.namespace.name

Concatenation

"Hello" + "World"

Arithmetical operations a + b, a * b

Comparison operations name == "Account", size > 2

Logical operations ! isAbstract, a || b

Type checking

myClassifier instanceof uml20.Actor

Range access myString[0], myList[1..4]

Default value expression

myProperty.type.name ? "String"

Transient links

myClass#targets.iterator()

Static field or method access

System.getProperty("myProperty")

Page 38: 20081202 Md Workbench Tool

MDWorkbenchUsing ATL in MDWorkbench

• MDWorkbench provides a set of imperative tools for model transformation and text generation

• ATL transformation style is mainly declarative• MDWorkbench brings the best of the two worlds by

integrating ATL• Declarative ATL modules can be easily mixed with imperative

rulesets or Java code into MDWorkbench projects