uml2code Extension for Enterprise Architect

Extending EA’s code generation capabilities with the uml2code plugin form LieberLieber. It provides you 100% control of the generated code and an easy to adapt interface.

When you generate code, probably you know how to write code. Hence, the approach of the uml2code generator is an implemented code generator in C#. It consists of 2 steps and 4 packages.

This article gives you an overview of the general architecture of the uml2code generator. The mentioned packages actually represent VS projects and provide the following functionality:

uml2code Package Dependencies

 

MDE.Apps.enar.uml2code: This package represents the EA-PlugIn. The purpose of the project is to start the code generation from within EA. However, the code generation can also be triggered from outside.

MDE.AnyModel2UML.SourceCode.Uml2Code: This packageis responsible for reading EA’s UML model, parses it and generates an abstract representation of  the UML model in programming language semantics, the abstract code tree (ACT). Hence, it uses the package MDE.DataModel.AbstractCode.

MDE.DataModel.AbstractCode: This package contains interfaces which represent an Abstract Code Tree (ACT). It is used by Uml2Code, which implements the interfaces of it and by Uml2Code.Uml2C, which writes the source code based on the ACT.

MDE.AnyModel2UML.SourceCode.Uml2Code.Uml2C: This package uses the interfaces from AbstractCode and generates the source code. This is the main place where the source code is written and influenced.

The two steps of code generation are the following:

  1. Parse the UML model and create an Abstract Code Tree (ACT).
  2. Use the ACT to write the source code.

How to Influence the Code Generation

Each of the above steps can be influenced. To influence the code which is written, you can adapt the content of the package Uml2C.
The Uml2Code plugin also provides implementations for C#, C++ (Uml2CSharp, Uml2Cpp, respectively).

The generator generates ANSI C89 code which can be used out of the box. However, if you have another idea how the model should be translated into code, you simply select the method which generates the code you want to influence.

public void GetCode(IMethodCall<IMyNamedElement> methodCall){…}

public void GetCode(ILoop<IMyNamedElement> loop){…}

public void GetCode(IIfThenElse<IMyNamedElement> ifThenElse){…}

The method GetCode(…) provides access to the part of the ACT which is currently translated into concrete source code. If the ACT doesn’t contain the information you are looking for to generate source code (e.g. tags, stereotypes, status, phase, version, etc.), you can simply access the corresponding EA model element, get the information and use it to write the code you would like to see. For instance, if you would like to generate different code for methods with stereotype “ComponentX” , query the corresponding model element and get its stereotype.

if(methodCall.ModelElement != null

&& methodCall.ModelElement.Stereotype == “ComponentX”)

The ACT tells you if the model contains a method, attribute, loop, etc. How the method, attribute, loop is modeled in UML is transparent within the package Uml2C. The package Uml2C only relies on ACT element. If necessary, you can dig deeper and get the corresponding model element for the ACT, as explained above. However, depending on the corresponding model, it may be that there is no single model element, e.g. a loop modeled with Decision and Merge in UML::Activities. Hence, you have to check for null first.

If your are not happy with the way your model is mapped into the ACT, go to the place where the ACT is generated (the package SourceCode.Uml2Code) and influence it.

Use a DSL Model to Generate Code

Because the package SourceCode.Uml2Code is responsible for the mapping between UML and code and provides an out of the box mapping of UML semantics and programming language concepts, this is the place to influence the generation of the Abstract Code Tree (ACT) if you are not happy how the UML mode is mapped into code.

This is for instance necessary if you have created your own DSL. If this is the case, you merely have to adapt uml2code and create the ACT you would like to see. Uml2C will take the new ACT and create the code without any further necessary changes.

 

 

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>