Upload
chris-raistrick
View
1.288
Download
1
Embed Size (px)
DESCRIPTION
Provides an overview of how to generate high quality code from Executable UML models.
Citation preview
WelcomeA Practical Guide to Code Generationusing Model Driven Architecture and Executable UML
25 Jun 2008 Chris Raistrick, Kennedy Carter [email protected]
KC.COM
K E N N E D Y C A R T E R2
Agenda
Evolution or (Industrial) Revolution?Platform Independent ProgrammingThe M Word and Code GenerationOptimised ArchitecturesComplex ArchitecturesPortable ArchitecturesReuse of MappingsSummary
Adopting MDA
K E N N E D Y C A R T E R
The information contained in this document is the property of Kennedy Carter Limited.No part of it may be reproduced or used except by written permission.
The copyright and foregoing restrictions on reproduction and use extend to all the media in which the information may be embodied.
www.kc.com
Evolution or(Industrial) Revolution?
K E N N E D Y C A R T E R4
The Industrial Revolution
Before 1800
• Hand crafting by skilled practitioners
• Idiosyncratic design strategies• Every item different• Premium is on the practitioner
After 1800
• Automated production lines• Consistent design strategies• Every item identical• Premium is on the process
K E N N E D Y C A R T E R5
Elaboration Based Process
manually build a Platform Specific
Model…
PSM
(UML)
manually code a Platform Specific Implementation
PSI
(Ada/C++)
The deliverables are up to 300%
redundant…… and
maintenance costs are
correspondingly high
Requirements(Text /
Use Cases)
manually build a Platform
Independent Model…
PIM
(UML)
This must be changed…or become obsolete
This must be changed…or become obsolete
This must be changed
When this changes
Processdefinition
Designpolicies
Imp’nrules
K E N N E D Y C A R T E R6
Elaboration vs. Translation
PSM
(UML)
Processdefinition
Designpolicies
Codingrules
manually build a Platform Specific
Model…
manually code a Platform Specific Implementation
PSI
(Ada/C++)
manually build a Platform
Independent Model…
PIM
(UML)
PSI
(Ada/C/C++)
automatically generate a
Platform Specific Implementation using PIM-PSI
mappings
PIM-PSIMappings
(xUML)
manually specify mappings onto the
platform…
manually build a Platform
Independent Model…
PIM
(xUML)
Requirement Change impact
Technology Change impact
Elaborate Translate
K E N N E D Y C A R T E R7
Translation Based Process
PSI
(Ada/C/C++)
automatically generate a
Platform Specific Implementation using PIM-PSI
mappings
PIM-PSIMappings
(xUML)
manually specify mappings onto the
platform…
manually build a Platform
Independent Model…
PIM
(xUML)
TranslateThe knowledge in the heads of developers is:
- Formalized
- Accessible
- Reusable
- Refinable
Processdefinition
Designpolicies
Codingrules
K E N N E D Y C A R T E R8
Processdefinition
Designpolicies
Codingrules
Formalized Application and Software Design Expertise
PSI
(Code)
PIM-PSIMappings
(xUML)
automatically generate a
Platform Specific Implementation using PIM-PSI
mappings
manually specify mappings onto the
platform…
manually build a Platform
Independent Model…
PIM
(xUML)
Rules and policies for organising
aircraftRules and
policies for organising software
Adopting MDA
K E N N E D Y C A R T E R
The information contained in this document is the property of Kennedy Carter Limited.No part of it may be reproduced or used except by written permission.
The copyright and foregoing restrictions on reproduction and use extend to all the media in which the information may be embodied.
www.kc.com
Platform Independent Modelling
K E N N E D Y C A R T E R10
Platform Independent Modelling
xUML Model(PIM)
Platform-SpecificConfiguration
System Model
Populate
xUML-CodeMappings
iCCGCode Generator
Code Generator
Generate
GeneratedCode
xUML RuntimeLayer
Generated System
AdaptationLayer
A typical code generation process consists of:Build and test a system model, using a precise modelling formalismBuild a code generatorPopulate the code generator with the system model, and generate the system
K E N N E D Y C A R T E R11
DomainsClasses
Operations
States
The xUML Model Structure
xUML Model(PIM)
Platform-SpecificConfiguration
System Model
K E N N E D Y C A R T E R12
Isn’t xUML with ASL Just Coding in Another Language?
Yes…but…
…as xUML “programmers”, we do not need to clutter our minds or our models with:
Code distribution decisions, with consequential additional components such as inter-node communication skeletons and stubsData distribution and replication, with consequential additional components to manage the distributed collectionData structure decisions, with consequential code to access the structuresShared resource locking
…and we can easily change our minds about:Static or dynamic memory managementThreading strategyData persistenceTarget language
…without changing the xUML model
Adopting MDA
K E N N E D Y C A R T E R
The information contained in this document is the property of Kennedy Carter Limited.No part of it may be reproduced or used except by written permission.
The copyright and foregoing restrictions on reproduction and use extend to all the media in which the information may be embodied.
www.kc.com
Metamodel Based Code Generation
K E N N E D Y C A R T E R14
The Code Generator: Domains
(Part of) Code Generator Domain Chart
Populate Generate
xUML Model(PIM)
Platform-SpecificConfiguration
System Model Code Generator
GeneratedCode
xUML RuntimeLayer
Generated System
AdaptationLayer
xUML-CodeMappings
iCCGCode
Generator
The code generator itself is a set of domain models expressed using
xUML.
The domains represent the various components of an xUML system.
K E N N E D Y C A R T E R15
(Part of) Configurable Code Generator
Domain Chart
The Code Generator: Classes and Methods
iCCGCode
Generator
Code Generator
xUML-CodeMappings
(Part of)Executable UML
Class Model
the classes in each domain represent the elements that make up those components.
Method to Generate Java
Method to Generate Ada
Method to Generate C++
Method to Generate C…$FORMAT header_filetypedef struct C[I:this.class_ID]_struct { /* "[T:this.class_name]" Class Header */ struct s_object *next_instance;$ENDFORMAT…
Each element contains operations which specify how to map that xUML element onto a specific
target language.
K E N N E D Y C A R T E R16
Platform Independent Model : Class Diagram
Build a PIM
The domain experts build PIMs using xUML, such as this one:
Populate Generate
xUML Model(PIM)
Platform-SpecificConfiguration
System Model
xUML-CodeMappings
iCCGCode
Generator
Code Generator
GeneratedCode
xUML RuntimeLayer
Generated System
AdaptationLayer
K E N N E D Y C A R T E R17
Domain Instance
Class Instances
Attribute Instances
Instantiate the Formalism Metamodel
Populated Executable
UMLClass Model
Populate
xUML Model(PIM)
Platform-SpecificConfiguration
System Model
xUML-CodeMappings
iCCGCode
Generator
Code Generator
When the Executable
UML domain is populated with
the PIM components, we see these instances…
K E N N E D Y C A R T E R18
The Metamodels Embody the Code Generation Rules
Domain.generateCode
Class.generateCode
Attribute.generateCode
The task of translation involves iterating
through these instances and
generating suitable code from them.
iCCGCode
Generator
Code Generator
xUML-CodeMappings
K E N N E D Y C A R T E R19
Platform Independent Model : Class Diagram
Generated C Code
Generate
Generate the Code
Generate
xUML-CodeMappings
iCCGCode
Generator
Code Generator
GeneratedCode
xUML RuntimeLayer
Generated System
AdaptationLayer
K E N N E D Y C A R T E R20
polymorphic operation with different
polymorphic methods to generate code for each different ASL
statement
We have illustrated generation of data structures from the class model. The
process of generating code from the action
language is the same, and based upon populating and
translating instances in the “Action Language”
domain…
Generating Code from Action Language
Note that the “generateCode” operation of the “ASL Statement” class is polymorphic, allowing
us to implement rival versions of this method for each type of ASL statement, represented as the
subclasses “Create Statement”, “Delete Statement” and so on
K E N N E D Y C A R T E R21
Generate the Code Generator
Project Code
Generator
Code
Generator
PIM
(part of) xUML Metamodel
Domain
Class
Attribute
xUML Metamodels xUML to Code Mappings in ASL
Pre-Existing
Code Generator
Project Domain Models
Project Code
K E N N E D Y C A R T E R22
Code Generation Overview
Populate Generate
xUML Model(PIM)
Platform-SpecificConfiguration
System Model
xUML-CodeMappings
iCCGCode Generator
Code Generator
GeneratedCode
xUML RuntimeLayer
Generated System
AdaptationLayer
PLATFORM SPECIFIC CONFIGURATION FILE
PROCESS "Process One" ONE 1 127.0.0.1 1000 1600PROCESS "Process Two" TWO 1 127.0.0.1 1001 1601
CLASS-PROCESS WM TGT ONECLASS-PROCESS WM WPN TWO
(part of) xUML Metamodel
Domain
Class
Attribute
owning_domain = this -> R2
$FORMAT header_filetypedef struct D[I:owning_domain.domain_ID]_C[I:this.class_ID]_struct { /* "[T:this.class_name]" Class Header */ struct s_object *next_instance; /* Linked list of */ struct s_object *prev_instance; /* object instances */ struct s_object *rel_ptr; /* list of rel'ns */ struct s_object *cpr_ptr; /* list of cp rel'ns */$ENDFORMAT
{attributes_in_class} = this -> R3for the_attribute in {attributes_in_class} do [] = ATT1:generateCode [header_file] on the_attributeendfor
$FORMAT header_file};
$ENDFORMAT
Multi-node multi-process runtime
Windows Vista adaptation layer
Adopting MDA
K E N N E D Y C A R T E R
The information contained in this document is the property of Kennedy Carter Limited.No part of it may be reproduced or used except by written permission.
The copyright and foregoing restrictions on reproduction and use extend to all the media in which the information may be embodied.
www.kc.com
Optimisation
K E N N E D Y C A R T E R24
Structured or Object-Oriented?
addinjectiondemand
getinjection
time
removeinjectiondemand
injectiondemand
addinjectiondemand
getinjection
time
removeinjectiondemand
injectiondemand
create get time deleteget volume
Encapsulated data structure containing dynamically
allocated injection demand records
Application code modules
Consider these two designs…
Global data structure containing a static array of injection
demand records
K E N N E D Y C A R T E R25
How do they compare?
The non Object-Oriented Scheme (left):Requires application module recoding if the data structure needs to changeIs faster because:
No dynamic memory managementNo access module calls
Is smaller because:No access module code
addinjectiondemand
getinjection
time
removeinjectiondemand
injectiondemand
addinjectiondemand
getinjection
time
removeinjectiondemand
injectiondemand
create get time deleteget volume
Application code modules
Encapsulated data structure containing dynamically
allocated injection demand records
Global data structure containing a static array of injection
demand records
K E N N E D Y C A R T E R26
With automatic code generation?
The non Object-Oriented Scheme (left):Requires application module recoding if the data structure needs to changeIs faster because:
No dynamic memory managementNo access module calls
Is smaller because:No access module code
addinjectiondemand
getinjection
time
removeinjectiondemand
injectiondemand
addinjectiondemand
getinjection
time
removeinjectiondemand
injectiondemand
create get time deleteget volume
Application code modules
Encapsulated data structure containing dynamically
allocated injection demand records
Global data structure containing a static array of injection
demand records
K E N N E D Y C A R T E R27
Avoiding Memory Management
Attributes of a class can be realized as fixed-length arrays, with a flag for each index to indicate whether that instance exists...
Customer
custIdcustNamecustAddress
The maximum number of objects in each class must be available at code generation time and may bedetermined automatically (static populations only),specified using a Max_Number_of_Instances tag.
Read-only attributes can be declared const and may be located in ROM.
An object handle is implemented as a simple array index……which will use less space than a pointer
custId array
custName array
custAddress array
K E N N E D Y C A R T E R28
Optimized Instance Handles
The first element is a status indicator: 0
means “undefined”, 1
means “defined”.
Customer
custIdcustNamecustAddress
The second element (which is defined only if the first element is 1) is an index into the attribute arrays of the object it references, i.e. an object “pointer”.
An instance handle can be realised as a 2-element array of
integers.
Instance attributes can be realised as fixed-length arrays, with a flag for each index to indicate whether that instance exists
K E N N E D Y C A R T E R29
Optimized Associations
accountIddateOpenedbalanceownerIdstatus
Account
owns
is owned by
1..*
0..1
R1
Customer
custIdcustNamecustAddress
note that creating and deleting objects and
links does not involve any dynamic memory
management…
…even though there are ‘create’ and ‘delete’
statements in the PIM
A binary association is implemented as two 2-
dimensional arrays. Each array implements the association in
one direction only.
CustomerEnd
AccountEnd
Customer 0 owns Account 2
Account 6 is owned by Customer 2
Adopting MDA
K E N N E D Y C A R T E R
The information contained in this document is the property of Kennedy Carter Limited.No part of it may be reproduced or used except by written permission.
The copyright and foregoing restrictions on reproduction and use extend to all the media in which the information may be embodied.
www.kc.com
Generating Complex Architectures
K E N N E D Y C A R T E R31
Formalism vs. Architecture Centric Metamodels
UMLMetamodel
UMLMetamodel
PIMPIM
Instantiate
DefineMapping
ApplyMapping
PSIMetamodel
PSIMetamodel
PSIPSI
So far, we have looked at a code generation approach based upon formalism centric metamodels. These work well for targets in which:
There is a single task;
The code is homogeneous:
Uniform memory management strategy;
All calls are local;
All signals are either synchronous or asynchronous
Formalism Centric
UMLMetamodel
UMLMetamodel
PIMPIM
Instantiate
PSMMetamodel
PSMMetamodel
DefineMapping
PSMPSMApplyMapping
PSIMetamodel
PSIMetamodel
DefineMapping
PSIPSIApplyMapping
More sophisticated architectures require a more sophisticated approach……based upon building architecture centric metamodels
Architecture Centric
K E N N E D Y C A R T E R32
Formalism Centric Metamodels
In formalism centric metamodels, the
“generateCode” method iterates over the contents of
the xUML (i.e. formalism) meta-models….
{allDomains} = find-all Domainfor theDomain in {allDomains} do {allClasses} = theDomain -> R2.Class for theClass in {allClasses} do [] = generateCode [] on theClass endforendfor
UMLMetamodel
UMLMetamodel
PIMPIM
Instantiate
DefineMapping
ApplyMapping
PSIMetamodel
PSIMetamodel
PSIPSI
K E N N E D Y C A R T E R33
Architecture Centric Metamodels
{allNodes} = find-all Nodefor theNode in {allNodes} do {allProcesses} = theNode -> R2.Process for theProcess in {allProcesses} do [] = generateCode [] on theProcess endforendfor
UMLMetamodel
UMLMetamodel
PIMPIM
Instantiate
PSMMetamodel
PSMMetamodel
DefineMapping
PSMPSMApplyMapping
PSIMetamodel
PSIMetamodel
DefineMapping
PSIPSIApplyMapping
In architecture centric metamodels, the
“generateCode” method iterates over the contents of the PSM (i.e. architecture)
meta-models….
K E N N E D Y C A R T E R34
The Platform Metamodel
The Platform (meta)model is added to the standard set of metamodels……and provides some key benefits:
xUML model of Abstract Platform Architecture provides a clear basis for understanding the system designEach pass (Population and Code Generation) becomes simpler and therefore easier to test and debug
K E N N E D Y C A R T E R35
Two Stage Transformation
When a platform model is used, model compilation takes place in two stages:
Action Language
xUML Formalism
MDA Process
Build Sets
Platform
Code
The first stage takes the instances in the meta-models (the user PIM) and creates instances in the platform model(s)The second stage takes the instances in the platform model(s) and creates codeIn principle, other UML notations (component and deployment diagrams) could be used to visualise the instances in the platform model
K E N N E D Y C A R T E R36
Platform Domain
The platform domain models the distribution of code and data across multiple processes.
Data DistributionCode Distribution
This metamodel holds the PIM-PSM mappings
K E N N E D Y C A R T E R37
Code and Data Distribution: Consequences
Code Distribution across processes gives rise to the
need for inter-process signals and operation calls,
and optimisation of inter-process communications
Data DistributionCode Distribution
Data Distribution across processes gives rise to the need for data protection, to guard against multiple concurrent accesses
K E N N E D Y C A R T E R38
Distributing PIM Code: Inter-Process Operation Calls
Remote procedures are implemented using the classic client-server model.Stubs and skeletons are automatically generated.
Client Process Server Process
A BA calls remote B
B Stub B Skeleton
Communication Middleware
2. B Stub packsInput parameters and
calls IPC send
5. B Skeleton packsoutput parameters andcalls IPC send
6. B Stub unpacksoutput parameters
3. B Skeletonunpacks input parameters
1. A calls B stub with inputparameters : later, B stub returns
output parameters
4. B Skeleton calls B withinput parameters, B returnsoutput parameters
K E N N E D Y C A R T E R39
Distributing PIM Data: Automatic Object Locking
The code generator can augment the generated code
with calls to obtain and release read and write locks
this.latitude = newLat
this.longitude = newLong
Target
latitudelongitude
Weapon
statusR1
myTarget = this -> R1.TargettargetLat = myTarget.latitudetargetLong = myTarget.longitude
xUML Runtime Model
Automatically generatedcalls to xUML runtimeobtain
write semaphore (Target) hererelease write semaphore (Target) here
obtain read
semaphore (Target)
hererelease read
semaphore (Target)
here
K E N N E D Y C A R T E R40
Complexity Simplified
Even when the system architecture is complex, the model can be kept relatively simple by omission of rules and policies governing:
Memory management
Code and data optimization strategies
Process and data distribution
Shared resource protection
All of which can be automatically incorporated into the target system by the code generator.
K E N N E D Y C A R T E R41
xUML centric Architecture centric
xUML Centric vs. Architecture Centric
Code generators can be:
formalism-centric, iterating over the UML metamodel
architecture-centric, iterating over the execution platform metamodel
{allDomains} = find-all Domainfor theDomain in {allDomains} do {allClasses} = theDomain -> R2.Class for theClass in {allClasses} do [] = generateCode [] on theClass endforendfor
{allNodes} = find-all Nodefor theNode in {allNodes} do {allProcesses} = theNode -> R2.Process for theProcess in {allProcesses} do [] = generateCode [] on theProcess endforendfor
Adopting MDA
K E N N E D Y C A R T E R
The information contained in this document is the property of Kennedy Carter Limited.No part of it may be reproduced or used except by written permission.
The copyright and foregoing restrictions on reproduction and use extend to all the media in which the information may be embodied.
www.kc.com
Portable Architectures
K E N N E D Y C A R T E R43
Adaptation Layer on the Domain Model
The runtime layer uses operating system and middleware resources to distribute xUML models across multiple threads in multiple processes.The runtime accesses these resources via a low-level, platform-independent interface, to enable it to support as wide a range of operating systems and middleware products as possible.A platform-specific implementation of this interface must be developed for each operating system and middleware product that the runtime is required to support.platform-
specific implementati
on
platform-independent
interface
GeneratedCode
xUML RuntimeLayer
Generated System
AdaptationLayer
K E N N E D Y C A R T E R44
Generated System
Runtime Metamodel
This metamodel holds the PSM-PSI mappings
GeneratedCode
xUML RuntimeLayer
AdaptationLayer
K E N N E D Y C A R T E R45
Adaptation Layer Interfaces
Run Time
Specific Adaption Layer
The adaptation layer is implemented using inheritance.An “adaptation” is an implementation of a set of derived classes.
GeneratedCode
xUML RuntimeLayer
Generated System
AdaptationLayer
Adopting MDA
K E N N E D Y C A R T E R
The information contained in this document is the property of Kennedy Carter Limited.No part of it may be reproduced or used except by written permission.
The copyright and foregoing restrictions on reproduction and use extend to all the media in which the information may be embodied.
www.kc.com
Reuse of Mappings
K E N N E D Y C A R T E R47
MDA Limits the Impact of Business Rule and Technology Changes
UMLMetamodel
UMLMetamodel
Platform & implementation environmentPlatform & implementation environmentchanges impact only herechanges impact only here
PIMPIM
Instantiate
Business rule & requirementBusiness rule & requirementchanges impact only herechanges impact only here
Manual or Automated
PSMMetamodel
PSMMetamodel
DefineMapping
PSMPSMApplyMapping
PSIMetamodel
PSIMetamodel
DefineMapping
PSIPSIApplyMapping
K E N N E D Y C A R T E R48
Reusable Mappings Are Defined at the Metamodel Level
(part of)UML Metamodel
Class
Attribute
Signal
PSMMetamodel
PSMMetamodel
PSIMetamodel
PSIMetamodel
DefineMappingxUML
Metamodel
xUMLMetamodel
DefineMapping
(part of)PSM Metamodel(object-based)
Class
PrivateDatum
PublicMethod
(part of) Ada 83 Metamodel
Package
BodyVariable
SpecSubprogram
PSMMetamodel
PSMMetamodel
PSIMetamodel
PSIMetamodel
DefineMapping
PSIPSIPSMPSMPIMPIM
Instantiate
PIMMetamodel
PIMMetamodel
ApplyMapping
DefineMapping
ApplyMapping
K E N N E D Y C A R T E R49
Reusable Mappings Are Defined at the Metamodel Level
(part of)UML Metamodel
Class
Attribute
Signal
PSMMetamodel
PSMMetamodel
PSIMetamodel
PSIMetamodel
DefineMappingxUML
Metamodel
xUMLMetamodel
DefineMapping
(part of)PSM Metamodel(object-based)
Class
PrivateDatum
PublicMethod
(part of) Java Metamodel
Class
Attribute
Operation
PSMMetamodel
PSMMetamodel
PSIMetamodel
PSIMetamodel
DefineMapping
PSIPSIPSMPSMPIMPIM
Instantiate
PIMMetamodel
PIMMetamodel
ApplyMapping
DefineMapping
ApplyMapping
K E N N E D Y C A R T E R50
The Code Structure Domain
The code generator can be further augmented with a “Code Structure” domain, which embodies the detailed language structure and syntax…
…allowing the Platform Model to be reused with different language-specific code structure domains
K E N N E D Y C A R T E R51
The Code Structure Domain
The code generator can be further augmented with a “Code Structure” domain, which embodies the detailed language structure and syntax…
…allowing the Platform Model to be reused with different language-specific code structure domains
K E N N E D Y C A R T E R52
The Code Structure Domain
Adopting MDA
K E N N E D Y C A R T E R
The information contained in this document is the property of Kennedy Carter Limited.No part of it may be reproduced or used except by written permission.
The copyright and foregoing restrictions on reproduction and use extend to all the media in which the information may be embodied.
www.kc.com
Summary
K E N N E D Y C A R T E R54
Summary
The subject matter of “design” is now seen as just another subject matter for analysis……because the design and implementation rules are specified using UML
The Platform-Specific Implementation can be optimized without sacrificing maintainability and reuse ……because only the PIMs are maintained and reused.
K E N N E D Y C A R T E R55
Maintainability vs. Executability
PSM
(UML)
manually build a Platform Specific
Model…
manually code a Platform Specific Implementation
PSI
(Code)
manually build a Platform
Independent Model…
PIM
(UML)
Elaborate
Compromise between
maintainability and
executability
In classic approaches, the PSI (code) must be built to be maintainable, typically by incorporating layering and encapsulation……which have a detrimental effect on speed and size of the executing system
PSI
(Code)
automatically generate a
Platform Specific Implementation using PIM-PSI
mappings
manually build a Platform
Independent Model…
PIM
(xUML)
Translate
Built for executability
Built for maintainabilit
y
In translation-based approaches, the maintained entity (the PIM) is built for maintainability with
layering and encapsulation…
…while the executable entity (the PSI) is optimized for
execution efficiency
The EndA Practical Guide to Code Generationusing Model Driven Architecture and Executable UML
25 Jun 2008 Chris Raistrick, Kennedy Carter [email protected]
KC.COM