27.06.2013 Views

FROM UML TO ANSI-C - An Eclipse-Based Code Generation ...

FROM UML TO ANSI-C - An Eclipse-Based Code Generation ...

FROM UML TO ANSI-C - An Eclipse-Based Code Generation ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Background<br />

Workflow & Design<br />

<strong>ANSI</strong>-C Meta-Model<br />

Implementation of the Framework<br />

Experience, Summary<br />

<strong>FROM</strong> <strong>UML</strong> <strong>TO</strong> <strong>ANSI</strong>-C<br />

<strong>An</strong> <strong>Eclipse</strong>-<strong>Based</strong><br />

<strong>Code</strong> <strong>Generation</strong> Framework<br />

Horst Lichter<br />

Matthias Funk<br />

Alexander Nyßen<br />

Research Group<br />

Software Construction<br />

horst.lichter@rwth-aachen.de<br />

www.swc.rwth-aachen.de


Background & Motivation<br />

Joined research project with ABB CRC<br />

Context<br />

Very similar products<br />

Software is most important part<br />

Challenges<br />

European-wide distributed development<br />

Hard memory and computation time<br />

Programming language C


Components of the Solution<br />

MeDUSA<br />

Concepts<br />

(MBD, <strong>UML</strong>)<br />

Languages<br />

<strong>ANSI</strong>-C<br />

ViPER


MeDUSA<br />

Method for <strong>UML</strong>2-based Construction of Embedded & Real-Time Software<br />

Requirements<br />

Modeling<br />

Use Case Modeling<br />

Use Case Details<br />

Modeling<br />

<strong>An</strong>alysis Modeling<br />

Context Modeling<br />

Information<br />

Modeling<br />

Intra- & Inter-Object<br />

Modeling<br />

Architectural Design<br />

Modeling<br />

Subsystem<br />

Identification &<br />

Consolidation<br />

Structural & Behavioral<br />

Architecture Design<br />

Modeling<br />

Detailed Design<br />

Modeling<br />

Detailed Structural<br />

Design<br />

Detailed Behavioral<br />

Design<br />

Implementation<br />

<strong>Code</strong> <strong>Generation</strong><br />

Implementing &<br />

Integration<br />

C


ViPER<br />

Visual Tooling Platform for Model-<strong>Based</strong> Engineering


<strong>Code</strong> <strong>Generation</strong> – Requirements<br />

Adaptability<br />

Different application domains require different <strong>UML</strong> elements<br />

Different target platforms require library specific code generation<br />

Separation of<br />

conceptual transformation from <strong>UML</strong> to C<br />

• application domain specific<br />

code generation<br />

Modular architecture<br />

defined extension/modification points<br />

shall be adaptable<br />

may be reused or<br />

adapted


2-Step <strong>Code</strong> <strong>Generation</strong> Strategy<br />

Split the generation process in 2 modular steps!<br />

Apply a model-to-model and a model-to-text transformation<br />

<strong>UML</strong><br />

Model<br />

Advantages<br />

<strong>ANSI</strong>-C<br />

Model<br />

<strong>ANSI</strong>-C<br />

<strong>Code</strong><br />

Each step can be developed and tested in isolation<br />

Increased testability, reduced complexity<br />

Easy adoption of the <strong>UML</strong> - <strong>ANSI</strong>-C m2m transformation<br />

Stable code generation based on the <strong>ANSI</strong>-C model


2-Step <strong>Code</strong> <strong>Generation</strong><br />

<strong>Eclipse</strong> based technologies should be applied as far as possible<br />

<strong>UML</strong> Modeling<br />

Tool<br />

<strong>UML</strong><br />

Meta Model<br />

<strong>UML</strong><br />

Model<br />

<strong>UML</strong> –<strong>ANSI</strong>-C<br />

Transformator<br />

<strong>ANSI</strong>-C<br />

Meta Model<br />

Transformation <strong>Generation</strong><br />

<strong>ANSI</strong>-C<br />

Model<br />

<strong>ANSI</strong>-C<br />

<strong>Code</strong><br />

Generator<br />

<strong>ANSI</strong>-C<br />

<strong>Code</strong><br />

A B A produces B B A A consumes B A B B is instance of A


Core of <strong>ANSI</strong>-C Meta-Model<br />

<strong>Based</strong> on C/C++ meta-model provided by <strong>Eclipse</strong> CDT project<br />

TranslationUnit<br />

aggregates all elements<br />

language elements<br />

preprocessor directives<br />

comments (annotations)<br />

Preprocessor directives<br />

and annotations<br />

stored as an ordered list<br />

each has at most one<br />

reference to its language<br />

element<br />

reused from<br />

<strong>Eclipse</strong> CDT metamodel<br />

. . . . . .


<strong>ANSI</strong>-C Meta-Model - <strong>An</strong>notations<br />

Comments contain information<br />

expressed „non-structurally“ in<br />

<strong>UML</strong><br />

Protected regions<br />

mark sections in the C code where<br />

custom code can be inserted<br />

function body in .c files<br />

Are save against re-generation<br />

Prerequisite for practical usability<br />

of the code generation framework


<strong>ANSI</strong>-C Meta-Model - Container<br />

Containers<br />

are needed to structurally organize C-<strong>Code</strong><br />

contain one or more translation units<br />

may contain recursively containers<br />

root of each<br />

<strong>ANSI</strong>-C model


Generic <strong>UML</strong> - <strong>ANSI</strong>-C Transformation<br />

Transformation strategy<br />

<strong>UML</strong> packages are transformed into containers<br />

All <strong>UML</strong> structural classifiers and types (of the design model) are<br />

transformed generically into <strong>ANSI</strong>-C types<br />

• struct and a set of functions<br />

Constraint<br />

• Declarations and implementations are generated into a<br />

header and source file<br />

• Since struct-declaration may need to refer recursively to<br />

itself, an additional forward declaration header file is also<br />

generated<br />

Generalization is not supported<br />

<br />

.h<br />

_fdef.h<br />

includes<br />

.c


Implementing the Transformation with oAW<br />

<strong>UML</strong> and <strong>ANSI</strong>-C model are EMF<br />

models<br />

openArchitectureWare<br />

Framework for implementing<br />

• M2M transformations<br />

• M2T transformations (code<br />

generation)<br />

Check language<br />

• Used to check source and<br />

target models<br />

Transformation language xTend<br />

• scripts transform source model<br />

elements to target model<br />

elements<br />

<strong>UML</strong><br />

Meta Model<br />

<strong>UML</strong><br />

Model<br />

<strong>UML</strong> –<strong>ANSI</strong>-C<br />

Transformator<br />

<strong>ANSI</strong>-C<br />

Meta Model<br />

<strong>ANSI</strong>-<br />

C<br />

Model


M2M-Transformation with xTend<br />

/* starting point of transformation */<br />

create ansic::Model this createModel<br />

(uml::Model m,<br />

uml::EncapsulatedClassifier topLevelComponent) :<br />

// set name of model<br />

setName(m.Name() == null ? "" : m.Name()) -><br />

// transform packages<br />

subContainer.addAll(m.nestedPackage.transformPackage(m)) -><br />

// transform top-level classifier<br />

subContainer.addAll(m.getGeneratedClassifiers().transformClassifier(m)) -><br />

// create a default main translation unit<br />

m.getComponents().size > 0<br />

? cm.translationUnit.add(m.createApplication(topLevelComponent))<br />

: true<br />

;


M2M-Transformation with xTend<br />

ansic::Container transformPackage(uml::Package p, uml::Model m) :<br />

let c = new ansic::Container :<br />

c.setName(p.Name()) -><br />

;<br />

/* process nested packages recursively; add their container<br />

representations to subcontainer list */<br />

c.subContainer.addAll(p.nestedPackage.transformPackage(m)) -><br />

/* classifiers of the package are transformed into subcontainers */<br />

c.subContainer.addAll<br />

(p.getGeneratedClassifiers().transformClassifier(m)) -> c<br />

ansic::Container transformClassifier(uml::Classifier c, uml::Model m) :<br />

let con = new ansic::Container :<br />

con.setName(c.Name()) -><br />

con.translationUnit.add(c.createSourceFile(c.getFullyQualifiedName())) -><br />

con.translationUnit.add(c.createHeaderFile(c.getFullyQualifiedName())) -><br />

con.translationUnit.add(c.createForwardDecl(c.getFullyQualifiedName())) -><br />

con<br />

;


<strong>Code</strong> <strong>Generation</strong><br />

xPand language of oAW<br />

template based language<br />

code generation is done by a<br />

sequence of template expansions<br />

used to generate C-<strong>Code</strong> from the<br />

<strong>ANSI</strong>-C model<br />

<br />

For each element of the <strong>ANSI</strong>-C<br />

meta model a xPand template is<br />

provided<br />

The generation process starts at the<br />

top-level Model element<br />

<strong>ANSI</strong>-C<br />

Meta<br />

Model<br />

<strong>ANSI</strong>-<br />

C<br />

Model<br />

<strong>ANSI</strong>-C<br />

<strong>Code</strong><br />

Generator<br />

<strong>ANSI</strong>-<br />

C<br />

<strong>Code</strong>


xPand - Examples<br />

«DEFINE E FOR ansic::Model»<br />

«EXPAND Container::E FOREACH (List[ansic::Container])subContainer»<br />

«EXPAND TranslationUnit::E FOREACH<br />

(List[ansic::TranslationUnit])translationUnit»<br />

«ENDDEFINE»<br />

«DEFINE E FOR ansic::TranslationUnit»<br />

«FILE container.getFullFilePath() + "/" + name»/<br />

*************************************************************<br />

* File: «name»<br />

* Generated by ViPER.<strong>Code</strong>gen<br />

*************************************************************/<br />

«EXPAND PreprocessorElement::E FOREACH getOrphanPreprocessorStatements()»<br />

«EXPAND ASTElement::E FOREACH astElement»<br />

«ENDFILE»<br />

«ENDDEFINE»


Architecture of the Framework<br />

default generic<br />

transformation<br />

reference code<br />

generation


GUI<br />

Integration in <strong>Eclipse</strong> Workbench<br />

Transformation and generation<br />

wizzards<br />

Wizzards guide the user through<br />

the process<br />

Called from special context menus


<strong>UML</strong> Structural Design Example<br />

Subsystem CurrentOutput Component<br />

Parts<br />

Ports <strong>UML</strong> structural<br />

classifiers


Generated <strong>Code</strong>


Framework Application<br />

Development of a platform-dependent code generator<br />

Generates C code for the Renesas M16C target platform<br />

new <strong>UML</strong> meta model<br />

elements<br />

Steps<br />

Enhance the <strong>UML</strong> meta-model by domain specific classifiers<br />

• using <strong>UML</strong> profile mechanism<br />

Adapt the default transformation<br />

• create/modify xTend scripts


Adapted Transformation<br />

create ansic::Model this createModel(uml::Model m,<br />

uml::EncapsulatedClassifier topLevelComponent) :<br />

// set name of model<br />

setName(m.Name() == null ? "" : m.Name()) -><br />

subContainer.addAll(m.nestedPackage.transformPackage(m)) -><br />

triggerObjectsCount(getPeriodicTriggerObjects(m)) > 0<br />

? subContainer.addAll(getPeriodicTriggerObjects(m).<br />

transformPeriodicTriggerObjectClassifier(m))<br />

: true<br />

-><br />

triggerObjectsCount(getAperiodicTriggerObjects(m)) > 0<br />

? subContainer.addAll(getAperiodicTriggerObjects(m).<br />

transformAperiodicTriggerObjectClassifier(m))<br />

: true<br />

...<br />

xTend scripts are modified<br />

new scripts are implemented for a new classifiers


Experience<br />

Only the transformation unit has to be customized<br />

Only local changes/extension were necessary<br />

The reference generation unit was not subject to change<br />

The overall implementation effort could be significantly<br />

reduced<br />

Technical details related to the generation process could be<br />

delegated to the reference generation unit<br />

Detailed know-how of the applied technology is necessary


Summary<br />

<strong>Code</strong> generation is mandatory in a MDD approach<br />

To reduce complexity a 2-step generation strategy was<br />

chosen<br />

M2M transformation <strong>UML</strong> meta-model -> <strong>ANSI</strong>-C meta-model<br />

M2T transformation <strong>ANSI</strong>-C model -> <strong>Code</strong><br />

<strong>ANSI</strong>-C meta model contains all element to represent<br />

language elements<br />

annotations and preprocessor directives<br />

structural organization of C-<strong>Code</strong><br />

oAW framework was used to implement<br />

default generic transformation of <strong>UML</strong> structural classifiers<br />

reference code generation based on the <strong>ANSI</strong>-C model<br />

First experience is very promising


More information is available from the ViPER home page<br />

www.viper.sc<br />

Alexander Nyßen any@swc.rwth-aachen.de

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!