Your SlideShare is downloading. ×
Code Generation with MDA and xUML
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Code Generation with MDA and xUML

1,257
views

Published on

Provides an overview of how to generate high quality code from Executable UML models.

Provides an overview of how to generate high quality code from Executable UML models.

Published in: Technology

2 Comments
1 Like
Statistics
Notes
  • Write a comment...
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • hi, Chris Raistrick, I'm a postgraduate from China, I read your book:Model Driven Architecture with Executable UML. It is really great....hope you can share more files :)
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
1,257
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
41
Comments
2
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Make a sign. There are a number of slides that contain what might be called tedious but relevant detail. These were included for reference. My reserves of time, and I suspect your reserves of alertness, preclude detailed examination of these slides.
  • Components are lovingly handcrafted Each is subjected to its own personal design and implementation process clearly nobody would build components like this. Look at all these tables…
  • Imagine these are software engineers – which is quite hard. What happens if we take the classic analyse-design-code process in the context of MDA? Hard to swim up a waterfall What happens when a requirement changes? What happens to the PIM and PSM?
  • Look how sunny it is on the right The more environmentally responsible among you will have noticed a problem with the right hand picture. It contains pollution. However, this is due to an inappropriately selected piece of tacky clip art. As we shall see, MDA embodies its own anti-pollution policy.
  • MDA is about formalisation of expertise…both application and technology expertise
  • We can formalise the rules and policies about how we organise our software… …in exactly the same way that we formalise rules about how we organise aircraft… by building executable UML models of those two domains Design is just a subject matter for analysis.
  • The primary deliverable in this approach to MDA is the PIM. It is here that all rules and policies are defined. It is here that requirement changes are reflected. The PSM and the PSI are merely derived products.
  • We build a PIM that is technology agnostic, but this can be accompanied by platform-specific configuration data to influence the code generation process. The xUML Runtime layer provides the xUML virtual machine. It may not be present in highly compact targets. The adaptation layer provides services to map onto the underlying platform – operating system, language and middleware.
  • This process is predicated on the need to build precise, executable models. This precludes use of highly complex, semantically ambiguous notations (such as UML 2.x) Executable UML is a precise proper subset of UML, composed of a small set of simple notations, organised in a predictable and simple way.
  • Much of the complexity of UML 2.x exists because of the need to build PSMs, in which we must concern ourselves with: different types of communication data structures code structures (e.g. friends) node boundaries sw engineers have a long track record of migrating to more abstract formalisms
  • The code generator is just a set of domains… …representing the components to be mapped… …with embedded mappings Let’s walk through the steps of this process
  • This is an executable model of our code generation rules. When we generate code from this model, we get a code generator!
  • So our PIM is now just data – even the code!
  • To generate code for a domain, we write a “generateCode” operation on the Domain class… …which iterates across all classes… etc
  • In the olden days, we used to pile all our data into a global area and systematically rape and plunder it at will. Not pretty, but very fast.
  • If we have a complex implementation architecture, we do what all modellers do with complex things… …we model them. Here we have a model of the PSM components.
  • All this extra complexity would typically end up in the model, but with this approach the model can be kept in its simnplest form.
  • Design is just another subject matter for analysis
  • What characteristics, what constraints? - select a paradigm.
  • Transcript

    • 1. Welcome A Practical Guide to Code Generation using Model Driven Architecture and Executable UML 25 Jun 2008 Chris Raistrick, Kennedy Carter [email_address] KC.COM
    • 2. Agenda
      • Evolution or (Industrial) Revolution?
      • Platform Independent Programming
      • The M Word and Code Generation
      • Optimised Architectures
      • Complex Architectures
      • Portable Architectures
      • Reuse of Mappings
      • Summary
    • 3. Evolution or (Industrial) Revolution?
    • 4. 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
    • 5. Elaboration Based Process The deliverables are up to 300% redundant… … and maintenance costs are correspondingly high Requirements (Text / Use Cases) This must be changed… or become obsolete This must be changed… or become obsolete This must be changed When this changes manually build a Platform Specific Model … PSM (UML) manually code a Platform Specific Implementation PSI (Ada/C++) manually build a Platform Independent Model … PIM (UML) Process definition Design policies Imp’n rules
    • 6. Elaboration vs. Translation PSM (UML) Process definition Design policies Coding rules manually code a Platform Specific Implementation PSI (Ada/C++) manually build a Platform Independent Model … PIM (UML) Elaborate Translate manually build a Platform Specific Model … PSI (Ada/C/C++) automatically generate a Platform Specific Implementation using PIM-PSI mappings PIM-PSI Mappings (xUML) manually specify mappings onto the platform … manually build a Platform Independent Model … PIM (xUML) Requirement Change impact Technology Change impact
    • 7. Translation Based Process Translate The knowledge in the heads of developers is: - Formalized - Accessible - Reusable - Refinable Process definition Design policies Coding rules PSI (Ada/C/C++) automatically generate a Platform Specific Implementation using PIM-PSI mappings PIM-PSI Mappings (xUML) manually specify mappings onto the platform … manually build a Platform Independent Model … PIM (xUML)
    • 8. Formalized Application and Software Design Expertise PIM-PSI Mappings (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 aircraft Process definition Design policies Coding rules PSI (Code) Rules and policies for organising software
    • 9. Platform Independent Modelling
    • 10. Platform Independent Modelling
      • A typical code generation process consists of:
      • Build and test a system model, using a precise modelling formalism
      • Build a code generator
      • Populate the code generator with the system model, and generate the system
      xUML Model (PIM) Platform-Specific Configuration System Model Populate xUML-Code Mappings iCCG Code Generator Code Generator Generate Generated Code xUML Runtime Layer Generated System Adaptation Layer
    • 11. The xUML Model Structure System Model Domains Classes Operations States xUML Model (PIM) Platform-Specific Configuration
    • 12. 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 stubs
        • Data distribution and replication, with consequential additional components to manage the distributed collection
        • Data structure decisions, with consequential code to access the structures
        • Shared resource locking
      • … and we can easily change our minds about:
        • Static or dynamic memory management
        • Threading strategy
        • Data persistence
        • Target language
      • … without changing the xUML model
    • 13. Metamodel Based Code Generation
    • 14. The Code Generator: Domains Populate Generate System Model Code Generator Generated Code xUML Runtime Layer Generated System Adaptation Layer The code generator itself is a set of domain models expressed using xUML. The domains represent the various components of an xUML system. (Part of) Code Generator Domain Chart xUML Model (PIM) Platform-Specific Configuration xUML-Code Mappings iCCG Code Generator
    • 15. The Code Generator: Classes and Methods (Part of) Configurable Code Generator Domain Chart iCCG Code Generator Code Generator xUML-Code Mappings (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_file typedef 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.
    • 16. Build a PIM
      • The domain experts build PIMs using xUML, such as this one:
      Platform Independent Model : Class Diagram Populate Generate xUML Model (PIM) Platform-Specific Configuration System Model xUML-Code Mappings iCCG Code Generator Code Generator Generated Code xUML Runtime Layer Generated System Adaptation Layer
    • 17. Instantiate the Formalism Metamodel Domain Instance Class Instances Attribute Instances Populated Executable UML Class Model When the Executable UML domain is populated with the PIM components, we see these instances… Populate xUML Model (PIM) Platform-Specific Configuration System Model xUML-Code Mappings iCCG Code Generator Code Generator
    • 18. 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. iCCG Code Generator Code Generator xUML-Code Mappings
    • 19. Generate the Code Platform Independent Model : Class Diagram Generated C Code Generate Generate xUML-Code Mappings iCCG Code Generator Code Generator Generated Code xUML Runtime Layer Generated System Adaptation Layer
    • 20. 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 polymorphic operation with different polymorphic methods to generate code for each different ASL statement
    • 21. Generate the Code Generator Project Code Generator Pre-Existing Code Generator Project Domain Models Project Code Code Generator PIM (part of) xUML Metamodel Domain Class Attribute xUML Metamodels xUML to Code Mappings in ASL
    • 22. Code Generation Overview Populate Generate xUML Model (PIM) Platform-Specific Configuration System Model xUML-Code Mappings iCCG Code Generator Code Generator Generated Code xUML Runtime Layer Generated System Adaptation Layer PLATFORM SPECIFIC CONFIGURATION FILE PROCESS "Process One" ONE 1 127.0.0.1 1000 1600 PROCESS "Process Two" TWO 1 127.0.0.1 1001 1601 CLASS-PROCESS WM TGT ONE CLASS-PROCESS WM WPN TWO (part of) xUML Metamodel Domain Class Attribute owning_domain = this -> R2 $FORMAT header_file typedef 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 -> R3 for the_attribute in {attributes_in_class} do [] = ATT1:generateCode [header_file] on the_attribute endfor $FORMAT header_file }; $ENDFORMAT Multi-node multi-process runtime Windows Vista adaptation layer
    • 23. Optimisation
    • 24. Structured or Object-Oriented?
      • Consider these two designs…
      Encapsulated data structure containing dynamically allocated injection demand records Application code modules Global data structure containing a static array of injection demand records add injection demand get injection time remove injection demand injection demand add injection demand get injection time remove injection demand injection demand create get time delete get volume
    • 25. How do they compare?
      • The non Object-Oriented Scheme (left):
      • Requires application module recoding if the data structure needs to change
      • Is faster because:
        • No dynamic memory management
        • No access module calls
      • Is smaller because:
        • No access module code
      Application code modules Encapsulated data structure containing dynamically allocated injection demand records Global data structure containing a static array of injection demand records add injection demand get injection time remove injection demand injection demand add injection demand get injection time remove injection demand injection demand create get time delete get volume
    • 26. With automatic code generation?
      • The non Object-Oriented Scheme (left):
      • Requires application module recoding if the data structure needs to change
      • Is faster because:
        • No dynamic memory management
        • No access module calls
      • Is smaller because:
        • No access module code
      Application code modules Encapsulated data structure containing dynamically allocated injection demand records Global data structure containing a static array of injection demand records add injection demand get injection time remove injection demand injection demand add injection demand get injection time remove injection demand injection demand create get time delete get volume
    • 27. 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...
      • The maximum number of objects in each class must be available at code generation time and may be
        • determined 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
      Customer custId custName custAddress custId array custName array custAddress array
    • 28. Optimized Instance Handles The first element is a status indicator: 0 means “undefined”, 1 means “defined”. 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 Customer custId custName custAddress
    • 29. Optimized Associations 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. Customer End Account End Customer 0 owns Account 2 Account 6 is owned by Customer 2 accountId dateOpened balance ownerId status Account owns is owned by 1..* 0..1 R1 Customer custId custName custAddress
    • 30. Generating Complex Architectures
    • 31. Formalism vs. Architecture Centric Metamodels UML Metamodel PIM Instantiate Define Mapping Apply Mapping PSI Metamodel PSI
      • 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 UML Metamodel PIM Instantiate PSM Metamodel Define Mapping PSM Apply Mapping PSI Metamodel Define Mapping PSI Apply Mapping More sophisticated architectures require a more sophisticated approach… …based upon building architecture centric metamodels Architecture Centric
    • 32. 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 Domain for theDomain in {allDomains} do {allClasses} = theDomain -> R2.Class for theClass in {allClasses} do [] = generateCode [] on theClass endfor endfor UML Metamodel PIM Instantiate Define Mapping Apply Mapping PSI Metamodel PSI
    • 33. Architecture Centric Metamodels {allNodes} = find-all Node for theNode in {allNodes} do {allProcesses} = theNode -> R2.Process for theProcess in {allProcesses} do [] = generateCode [] on theProcess endfor endfor UML Metamodel PIM Instantiate PSM Metamodel Define Mapping PSM Apply Mapping PSI Metamodel Define Mapping PSI Apply Mapping In architecture centric metamodels, the “generateCode” method iterates over the contents of the PSM (i.e. architecture) meta-models….
    • 34. 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 design
        • Each pass (Population and Code Generation) becomes simpler and therefore easier to test and debug
    • 35. Two Stage Transformation
      • When a platform model is used, model compilation takes place in two stages:
      • 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 code
      • In principle, other UML notations (component and deployment diagrams) could be used to visualise the instances in the platform model
      Action Language xUML Formalism MDA Process Build Sets Platform Code
    • 36. Platform Domain
      • The platform domain models the distribution of code and data across multiple processes.
      This metamodel holds the PIM-PSM mappings Data Distribution Code Distribution
    • 37. 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 Distribution Code Distribution Data Distribution across processes gives rise to the need for data protection, to guard against multiple concurrent accesses
    • 38. 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 B A calls remote B B Stub B Skeleton Communication Middleware 2. B Stub packs Input parameters and calls IPC send 5. B Skeleton packs output parameters and calls IPC send 6. B Stub unpacks output parameters 3. B Skeleton unpacks input parameters 1. A calls B stub with input parameters : later, B stub returns output parameters 4. B Skeleton calls B with input parameters, B returns output parameters
    • 39. Distributing PIM Data: Automatic Object Locking
      • The code generator can augment the generated code with calls to obtain and release read and write locks
      R1 xUML Runtime Model this.latitude = newLat this.longitude = newLong Target latitude longitude Weapon status myTarget = this -> R1.Target targetLat = myTarget.latitude targetLong = myTarget.longitude Automatically generated calls to xUML runtime obtain write semaphore (Target) here release write semaphore (Target) here obtain read semaphore (Target) here release read semaphore (Target) here
    • 40. 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.
    • 41. xUML Centric vs. Architecture Centric
      • Code generators can be:
        • formalism-centric , iterating over the UML metamodel
        • architecture-centric , iterating over the execution platform metamodel
      xUML centric Architecture centric {allDomains} = find-all Domain for theDomain in {allDomains} do {allClasses} = theDomain -> R2.Class for theClass in {allClasses} do [] = generateCode [] on theClass endfor endfor {allNodes} = find-all Node for theNode in {allNodes} do {allProcesses} = theNode -> R2.Process for theProcess in {allProcesses} do [] = generateCode [] on theProcess endfor endfor
    • 42. Portable Architectures
    • 43. 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 implementation platform-independent interface Generated Code xUML Runtime Layer Generated System Adaptation Layer
    • 44. Runtime Metamodel Generated System This metamodel holds the PSM-PSI mappings Generated Code xUML Runtime Layer Adaptation Layer
    • 45. Adaptation Layer Interfaces
      • The adaptation layer is implemented using inheritance.
      • An “adaptation” is an implementation of a set of derived classes.
      Run Time Specific Adaption Layer Generated Code xUML Runtime Layer Generated System Adaptation Layer
    • 46. Reuse of Mappings
    • 47. MDA Limits the Impact of Business Rule and Technology Changes UML Metamodel Platform & implementation environment changes impact only here PIM Instantiate Business rule & requirement changes impact only here Manual or Automated PSM Metamodel Define Mapping PSM Apply Mapping PSI Metamodel Define Mapping PSI Apply Mapping
    • 48. Reusable Mappings Are Defined at the Metamodel Level (part of) UML Metamodel Class Attribute Signal PSM Metamodel PSI Metamodel Define Mapping xUML Metamodel Define Mapping (part of) PSM Metamodel (object-based) Class PrivateDatum PublicMethod (part of) Ada 83 Metamodel Package BodyVariable SpecSubprogram
    • 49. Reusable Mappings Are Defined at the Metamodel Level (part of) UML Metamodel Class Attribute Signal PSM Metamodel PSI Metamodel Define Mapping xUML Metamodel Define Mapping (part of) PSM Metamodel (object-based) Class PrivateDatum PublicMethod (part of) Java Metamodel Class Attribute Operation
    • 50. 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
    • 51. 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
    • 52. The Code Structure Domain
    • 53. Summary
    • 54. 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.
    • 55. 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 maintainability 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
    • 56. The End A Practical Guide to Code Generation using Model Driven Architecture and Executable UML 25 Jun 2008 Chris Raistrick, Kennedy Carter [email_address] KC.COM