More Related Content
Similar to IBM Rhapsody Code Generation Customization (20)
IBM Rhapsody Code Generation Customization
- 1. IBM Software Group | Rational software
© 2008 IBM Corporation
© 2013 IBM Corporation
Rhapsody Code Generation
basics and customizations
Giulio Santoli
Client Technical Professional, IBM Rational
giulio_santoli@it.ibm.com
- 2. © 2013 IBM Corporation
Software and Systems Engineering | Rational
2
Model-Driven Development and the V-Process
Summary
1
6
Heavyweight Customizations4
Lightweight Customizations3
Model Paradigms and DMCA2
Full custom code generators with RulesComposer5
Agenda
- 3. © 2013 IBM Corporation
Software and Systems Engineering | Rational
3
Code Generation Customization, Why should I do that?
- 4. © 2013 IBM Corporation
Software and Systems Engineering | Rational
4
Model-Driven Development positioning in the V-Process
(Sub-)System
Integration & Test
System
Acceptance
Component
Integration &
Test
System
Design
Component
Analysis &
Design
Detailed
Component
Design
Requirements
Analysis
External
Requirements
Rhapsody Developer
Rhapsody Architect for Software
Rhapsody Designer for
Systems Engineers
Rhapsody Architect for
Systems Engineers
Rhapsody
TestConductor
Add On
MDD
MBT
MBSE
Rhapsody Design Manager
- 5. © 2013 IBM Corporation
Software and Systems Engineering | Rational
5
Generate new code from the model
Develop MISRA-C, MISRA-C++ and Ada applications
Maintain automated synchronization between model and code
Work simultaneously with architecture, software and target
All changes in one area reflected in the others
Visualize legacy C, C++ and Ada code
Visualize
existing
Generate
new code
Model Code
Synchronization
Model-Driven Development Approaches
- 6. © 2013 IBM Corporation
Software and Systems Engineering | Rational
6
Dynamic Model-Code Associativity (DMCA)
Change either the code or the model, the other changes automatically:
- 7. © 2013 IBM Corporation
Software and Systems Engineering | Rational
7
Rhapsody Code Generator can be customized in three different levels:
Lightweight customization
Setting code generation parameters (defaults)
Stereotyped based generation
Adding standard operations
Heavyweight customization (C and C++ only)
Simplifier helpers
Post simplification
Writer templates customization
Fully custom code generators
Creating a model-to-code transformation with RulesComposer
Customizing the Rhapsody Code Generation
- 8. © 2013 IBM Corporation
Software and Systems Engineering | Rational
8
Rhapsody provides a big set of properties that do affect the code generation:
The “GC properties” control code generation aspects that are common to all languages
Language-specific properties (“CPP_GC”, “C_CG”, “Java_CG”, “Ada_CG”) control code
generations aspects for the corresponding language
Each model element has a pre-defined set of transformation parameters
These properties can be redefined at various scopes (component, package, individual
element)
Lightweight Customization
- 9. © 2013 IBM Corporation
Software and Systems Engineering | Rational
9
Consider a class with one attribute (i.e. attribute1), by default Rhapsody generates
both accessor (getAttribute1) and mutator (setAttribute1) operations.
Common Properties Example – Accessors & Mutators (1/2)
- 10. © 2013 IBM Corporation
Software and Systems Engineering | Rational
10
Common Properties Example – Accessors & Mutators (2/2)
By changing some properties, it’s possible to obtain a different result, with no
additional operations nor attribute visibility change.
- 11. © 2013 IBM Corporation
Software and Systems Engineering | Rational
11
When a class is used as a read-only argument in an operation, by default
Rhapsody uses the reference rather than a pointer.
Lan-specific Properties Example – Class as Argument (1/2)
- 12. © 2013 IBM Corporation
Software and Systems Engineering | Rational
12
It’s possible to customize this behavior, either by specifying the default qualifier for
each class and/or by specifying the qualify to apply for a given argument.
Lang-specific Properties Example – Class as Argument (2/2)
- 13. © 2013 IBM Corporation
Software and Systems Engineering | Rational
13
Common Properties Customization Demo
- 14. © 2013 IBM Corporation
Software and Systems Engineering | Rational
14
Heavyweight customization with Simplifiers for C and C++
When generating C and C++ code, Rhapsody uses the following process:
The Rhapsody code generator silently converts the User Model to a temporary
Simplified Model, and then performs code generation on the Simplified Model
The Simplified Model is the User Model expanded to apply the code generation properties
and to include model elements that align more directly with the implementation language
The code generator does a literal translation of the Simplified Model to source code
User can define helpers to control how the Simplified Model is generated and how the
Simplified Model is translated to C/C++ code
Helpers
"Simplify" Properties
Simplifier
Code
Generator
Simplied Model
User Model
Properties
Application Code
- 15. © 2013 IBM Corporation
Software and Systems Engineering | Rational
15
A Better Picture of the Simplification Process
Source CodeSource Code
Code Model
(“Simplified” UML)
Code Model
(“Simplified” UML)
Transform
(Simplifier)
Transform
(Simplifier)
Generate
(Writer)
Generate
(Writer)
Implementation
Framework
Definition
Implementation
Framework
Definition
Generation
Parameters
(Properties)
Generation
Parameters
(Properties)
Structural Models Component ModelsBehavioral Models
Build
Files
Build
Files
Transformation
Logic
Transformation
Logic
Generation
Templates
Generation
Templates
- 16. © 2013 IBM Corporation
Software and Systems Engineering | Rational
16
Simplification Customization Properties
You can use the Simplify Properties to customize the Simplification process:
Custom Simplifiers can be added using pre-defined extension points (“hooks”)
Custom simplifiers manipulate the code model using Rhapsody APIs
Primary elements have pre-defined simplifier hooks
Class, Attribute, Operation, Generalization, Statechart, ….
For each element the simplify property can specify:
Default: follow out-of-the-box simplification
None: suppress the simplification
Copy: copy the application element to the
code model as is
User: replace out of the box one with a new
user defined one
User Post Default: invoke a custom simplifier
after the out of the box one
- 17. © 2013 IBM Corporation
Software and Systems Engineering | Rational
17
Simplified Model
Default Simplification Example
Let’s take the simplest example, with a class with just one public attribute.
The property “CPP_CG::Configuration::ShowCgSimplifiedModelPackage” makes
Rhapsody display the Simplified Model.
User Model
- 18. © 2013 IBM Corporation
Software and Systems Engineering | Rational
18
Simplified Model
“Copy” Simplify Example – Models
When you specify “Copy” in a simplify property for a model element, this is copied
into the Simplified Model as-is and no default simplification are applied.
User Model
- 19. © 2013 IBM Corporation
Software and Systems Engineering | Rational
19
Generate Code: “Default” Simplify v.s. “Copy” Simplify
“Default” Simplify “Copy” Simplify
- 20. © 2013 IBM Corporation
Software and Systems Engineering | Rational
20
Simplified Model
“None” Simplify Example – Models
When you specify “None” in a simplify property for a model element, this is ignored
by the simplify process and it’s not created in the Simplified Model at all.
User Model
- 21. © 2013 IBM Corporation
Software and Systems Engineering | Rational
21
Generate Code: “Default” Simplify v.s. “None” Simplify
“Default” Simplify “None” Simplify
- 22. © 2013 IBM Corporation
Software and Systems Engineering | Rational
22
Custom Simplifier Implementation
To create a custom Simplifier with Rhapsody APIs, you need to create a class that
extends RPCodeGenSimpifier and implement all the inherited abstract methods.
Two methods are involved during the Simplification process:
postSimplify, invoked when the Simplify property is “User Post Default”
simplify, invoked when the Simplify property is “User”
- 23. © 2013 IBM Corporation
Software and Systems Engineering | Rational
23
Simplify Method
The simplify() operation is invoked when a User Model element is going to be
simplified, and the plug-in code should create the corresponding element in the
Simplifed Model.
User Model Simplified Model
- 24. © 2013 IBM Corporation
Software and Systems Engineering | Rational
24
Post Simplify Method
User Model Simplified Model
The postSimplify() operation is invoked when a User Model element has been
already simplifed, and the plug-in code can make changes to the corresponding
element in the Simplifed Model.
- 25. © 2013 IBM Corporation
Software and Systems Engineering | Rational
25
Post Simplify Method Example
In this example we are adding a entry/exit trace call in the body of the operation.
There are things you need to consider when manipulating the Simplified Model:
always invoke notifySimplificationDone() at the end of simplify() and postSimpilify()
methods
if you enable code roundtrip, think about the possible impact of it for your solution
Invoke notifySimpolicationDone() of the
IRPCodeGenSimplifiersRegistry class
We are adding more code in the
operation body....
... what happens if roundtrip is
enabled?
- 26. © 2013 IBM Corporation
Software and Systems Engineering | Rational
26
How to handle Rountrip... Some Hints
To property handle the rountrip, you can adapt your implementation logic to
understand the best strategy:
you might check whether the model manipulation is needed
you might use special comments like makes Rhapsody ignore part of the generated code,
such as “//## auto_generated” or “//#[ ignore” and “//#]”
Add the entry/exit logs only if not
already there...
- 27. © 2013 IBM Corporation
Software and Systems Engineering | Rational
27
Simplifiers
- 28. © 2013 IBM Corporation
Software and Systems Engineering | Rational
28
Full Code Generator Replacement with RulesComposer
Rules Composer is an add-on which helps get complete control of the code
generated in Rhapsody
Model-to-Code Generation
C, C++, Java and ADA code generation
Model-to-Model Transformation
UML to Relational, UML 1.3 to UML 2.1
Model-to-Document Generation
Model to Word, Model to Open Document (ODF)
Rules composer additionally has a debugger that helps to set breakpoints and
stepping through rules templates to assist in debugging the rules surrounding
the development project
Custom Generator Plugin
- 29. © 2013 IBM Corporation
Software and Systems Engineering | Rational
29
Model-To-Code Transformations
Code generators are text-based templates:
a text template specifies the information to generate in a given file as well as the
name of the file.
text templates use the Text Generation Language (TGL) as their syntax, with
the Model Query Language (MQL) used to access the dynamic information
from the parameters.
The Text Generation Language (TGL) is a template-based language
used to specify the layout and contents of source code or other text
documents
The Model Query Language (MQL) is an intuitive language for
accessing and creating information in models. Constructs exist to easily
iterate over collections of elements, and to simply retrieve values in the
model without complex expressions or a difficult syntax
Java can be used for code generation too
- 30. © 2013 IBM Corporation
Software and Systems Engineering | Rational
30
Custom Code Generator Example
Here follows basic Java generator, with a Rulset and a Text Template
MyGeneratorMain.mql
MyGenerator.tgt
- 31. © 2013 IBM Corporation
Software and Systems Engineering | Rational
31
Exporting a Launch Configuration
Export a Launch Configuration to be used either in a Rhapsody Configuration or
as a Helper in the Rhapsody Tools menu
Three files are generated:
• MyGeneratorMain.myRule.classpath
• MyGeneratorMain.myRule.jar
• MyGeneratorMain.myRule.launch
- 32. © 2013 IBM Corporation
Software and Systems Engineering | Rational
32
Rhapsody Configuration Settings
You can customize a Rhapsody Configuration to use the external code generator,
by setting the following properties:
CPP_CG::Configuration::CodeGeneratorTool
CPP_CG::Configuration::GeneratorRulesSet
CPP_CG::Configuration::GeneratorScenarioName
- 33. © 2013 IBM Corporation
Software and Systems Engineering | Rational
33
Generating the Code in Rhapsody
At code generation time, Rhapsody invokes the RulesPlayer to run the generator
- 34. © 2013 IBM Corporation
Software and Systems Engineering | Rational
34
Custom Generator with Rules Composer
- 35. © 2013 IBM Corporation
Software and Systems Engineering | Rational
35
Rhapsody is very flexible and allows you to customize how the code is
generated
There are seral code generation customization strategies, from simple
settings to a complete code generator replacement
You can adopt one or more strategies at the same time to get the right
customizzation for your needs
Summary
- 36. © 2013 IBM Corporation
Software and Systems Engineering | Rational
36
www.ibm.com/software/rational
- 37. © 2013 IBM Corporation
Software and Systems Engineering | Rational
37
© Copyright IBM Corporation 2012. All rights reserved. The information contained in these materials is provided for informational purposes only, and is provided AS IS without warranty of any kind,
express or implied. IBM shall not be responsible for any damages arising out of the use of, or otherwise related to, these materials. Nothing contained in these materials is intended to, nor shall have
the effect of, creating any warranties or representations from IBM or its suppliers or licensors, or altering the terms and conditions of the applicable license agreement governing the use of IBM
software. References in these materials to IBM products, programs, or services do not imply that they will be available in all countries in which IBM operates. Product release dates and/or capabilities
referenced in these materials may change at any time at IBM’s sole discretion based on market opportunities or other factors, and are not intended to be a commitment to future product or feature
availability in any way. IBM, the IBM logo, Rational, the Rational logo, Telelogic, the Telelogic logo, and other IBM products and services are trademarks of the International Business Machines
Corporation, in the United States, other countries or both. Other company, product, or service names may be trademarks or service marks of others.
www.ibm.com/software/rational
- 38. © 2013 IBM Corporation
Software and Systems Engineering | Rational
38
© Copyright IBM Corporation 2012. All rights reserved.
– U.S. Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
IBM, the IBM logo, ibm.com, Rational, the Rational logo, Telelogic, the Telelogic logo, Green Hat, the Green Hat logo, and other IBM products and
services are trademarks or registered trademarks of International Business Machines Corporation in the United States, other countries, or both. If
these and other IBM trademarked terms are marked on their first occurrence in this information with a trademark symbol (® or ™), these symbols
indicate U.S. registered or common law trademarks owned by IBM at the time this information was published. Such trademarks may also be registered
or common law trademarks in other countries. A current list of IBM trademarks is available on the Web at “Copyright and trademark information” at
www.ibm.com/legal/copytrade.shtml
If you have mentioned trademarks that are not from IBM, please update and add the following lines:
[Insert any special third-party trademark names/attributions here]
Other company, product, or service names may be trademarks or service marks of others.
Availability: References in this presentation to IBM products, programs, or services do not imply that they will be available in all countries
in which IBM operates.
The workshops, sessions and materials have been prepared by IBM or the session speakers and reflect their own views. They are provided for
informational purposes only, and are neither intended to, nor shall have the effect of being, legal or other guidance or advice to any participant.
While efforts were made to verify the completeness and accuracy of the information contained in this presentation, it is provided AS-IS without
warranty of any kind, express or implied. IBM shall not be responsible for any damages arising out of the use of, or otherwise related to, this
presentation or any other materials. Nothing contained in this presentation is intended to, nor shall have the effect of, creating any warranties or
representations from IBM or its suppliers or licensors, or altering the terms and conditions of the applicable license agreement governing the use of
IBM software.
All customer examples described are presented as illustrations of how those customers have used IBM products and the results they may have
achieved. Actual environmental costs and performance characteristics may vary by customer. Nothing contained in these materials is intended to,
nor shall have the effect of, stating or implying that any activities undertaken by you will result in any specific sales, revenue growth or other results.
Acknowledgements and disclaimers
- 39. © 2013 IBM Corporation
Software and Systems Engineering | Rational
39
Some References