Advance Software Engineering CEN5011 Mapping Models to Code

  • Slides: 46
Download presentation
Advance Software Engineering (CEN-5011) Mapping Models to Code Fall 2004 Instructor: Masoud Sadjadi http:

Advance Software Engineering (CEN-5011) Mapping Models to Code Fall 2004 Instructor: Masoud Sadjadi http: //www. cs. fiu. edu/~sadjadi/Classes/CEN-5011/ CEN 5011 Seventh Lecture (2 nd part) Oct 27, 2004

Acknowledgements Overview: Overview Dr. Bernd Bruegge Dr. Allen Dutoit Optimization Associations Contracts Tables Summary

Acknowledgements Overview: Overview Dr. Bernd Bruegge Dr. Allen Dutoit Optimization Associations Contracts Tables Summary CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 2

Agenda Overview: Overview Optimizing the Object Design Model Mapping Associations Mapping Contracts to Exceptions

Agenda Overview: Overview Optimizing the Object Design Model Mapping Associations Mapping Contracts to Exceptions Mapping Object Models to Tables Optimization Associations Contracts Tables Summary CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 3

Overview: Overview Object design is situated between system design and implementation. Object design is

Overview: Overview Object design is situated between system design and implementation. Object design is not very well understood and if not well done, leads to a bad system implementation. Optimization Associations Contracts Tables Summary CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 4

Overview: Overview Optimization Associations Contracts Tables Summary In this lecture, we describe a selection

Overview: Overview Optimization Associations Contracts Tables Summary In this lecture, we describe a selection of transformations to illustrate a disciplined approach to implementation to avoid system degradation. 1. Operations on the object model: Optimizations to address performance requirements 2. Implementation of class model components: Realization of associations Realization of operation contracts 3. Realizing entity objects based on selected storage strategy Mapping the class model to a storage schema CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 5

Object Design Activities Overview: Overview Optimization Associations Contracts Tables Summary Developers perform transformations to

Object Design Activities Overview: Overview Optimization Associations Contracts Tables Summary Developers perform transformations to the object model to improve its modularity and performance. Developers transform the associations of the object model into collections of object references, because programming languages do not support the concept of association. If the programming language does not support contracts, the developer needs to write code for detecting and handling contract violations. Developers often revise the interface specification to accommodate new requirements from the client. All these activities are intellectually not challenging – However, they have a repetitive and mechanical flavor that makes them error prone. CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 6

Model-based Software Engineering Overview: Overview – During object design we would like to implement

Model-based Software Engineering Overview: Overview – During object design we would like to implement a system that realizes the use cases specified during requirements elicitation and system design. Optimization Associations Contracts Tables Summary The Vision The Reality – Different developers usually handle contract violations differently. – Undocumented parameters are often added to the API to address a requirement change. – Additional attributes are usually added to the object model, but are not handled by the persistent data management system, possibly because of a miscommunication. – Many improvised code changes and workarounds that eventually yield to the degradation of the system. CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 7

Model transformations Overview: Overview Optimization Associations Contracts Tables Forward engineering Summary Refactoring Model transformati

Model transformations Overview: Overview Optimization Associations Contracts Tables Forward engineering Summary Refactoring Model transformati on Reverse engineering Model space CEN 5011: Advanced Software Engineering Source code space Seventh Lecture (2 nd part) on Oct. 27, 2004 8

Model Transformation Example Overview: Overview Optimization Associations Contracts Tables Object design model before transformation

Model Transformation Example Overview: Overview Optimization Associations Contracts Tables Object design model before transformation League. Owner +email: Address Advertiser +email: Address Player +email: Address Summary Object design model after transformation: User +email: Address League. Owner Advertiser CEN 5011: Advanced Software Engineering Player Seventh Lecture (2 nd part) on Oct. 27, 2004 9

Refactoring Example: Pull Up Field Overview: Overview Optimization Associations Contracts Tables Summary public class

Refactoring Example: Pull Up Field Overview: Overview Optimization Associations Contracts Tables Summary public class Player { private String email; //. . . } public class League. Owner { private String e. Mail; //. . . } public class Advertiser { private String email_address; //. . . } CEN 5011: Advanced Software Engineering public class User { private String email; } public class Player extends User { //. . . } public class League. Owner extends User { //. . . } public class Advertiser extends User { //. . . } Seventh Lecture (2 nd part) on Oct. 27, 2004 10

Example: Pull Up Constructor Body Overview: Overview Optimization public class User { private String

Example: Pull Up Constructor Body Overview: Overview Optimization public class User { private String email; } Associations Contracts Tables Summary public class Player extends User { public Player(String email) { this. email = email; } } public class League. Owner extends User{ public League. Owner(String email) { this. email = email; } } public class Advertiser extends. User{ public Advertiser(String email) { this. email = email; } }CEN 5011: Advanced Software Engineering public class User { public User(String email) { this. email = email; } } public class Player extends User { public Player(String email) { super(email); } } public class League. Owner extends User { public League. Owner(String email) { super(email); } } public class Advertiser extends User { public Advertiser(String email) { super(email); } } Seventh Lecture (2 nd part) on Oct. 27, 2004 11

Forward Engineering Example Overview: Overview Optimization Associations Object design model before transformation League. Owner

Forward Engineering Example Overview: Overview Optimization Associations Object design model before transformation League. Owner +max. Num. Leagues: int User +email: String +notify(msg: String) Contracts Tables Summary Source code after transformation public class User { public class League. Owner extends private String email; User { public String get. Email() { private int max. Num. Leagues; return email; public int get. Max. Num. Leagues() { } return max. Num. Leagues; public void set. Email(String value){ } email = value; public void set. Max. Num. Leagues } (int value) { public void notify(String msg) { max. Num. Leagues = value; //. . } } /* Other methods omitted */ } } CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 12

Agenda Overview: Overview Optimizing the Object Design Model Mapping Associations Mapping Contracts to Exceptions

Agenda Overview: Overview Optimizing the Object Design Model Mapping Associations Mapping Contracts to Exceptions Mapping Object Models to Tables Optimization Associations Contracts Tables Summary CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 13

Collapsing Class Diagrams Overview: Collapsing an object without interesting behavior Overview Optimization Object design

Collapsing Class Diagrams Overview: Collapsing an object without interesting behavior Overview Optimization Object design model before transformation Associations Contracts Tables Summary Person Social. Security number: String Object design model after transformation Person SSN: String CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 14

Delaying expensive computations Overview: Overview Object design model before transformation Optimization Image Associations filename:

Delaying expensive computations Overview: Overview Object design model before transformation Optimization Image Associations filename: String data: byte[] paint() Contracts Tables Summary Object design model after transformation Image filename: String paint() Image. Proxy filename: String paint() image 1 CEN 5011: Advanced Software Engineering 0. . 1 Real. Image data: byte[] paint() Seventh Lecture (2 nd part) on Oct. 27, 2004 15

Agenda Overview: Overview Optimizing the Object Design Model Mapping Associations Mapping Contracts to Exceptions

Agenda Overview: Overview Optimizing the Object Design Model Mapping Associations Mapping Contracts to Exceptions Mapping Object Models to Tables Optimization Associations Contracts Tables Summary CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 16

Mapping a one-to-one association Overview: Overview Object design model before transformation Optimization Associations Advertiser

Mapping a one-to-one association Overview: Overview Object design model before transformation Optimization Associations Advertiser 1 1 Account Contracts Tables Summary Source code after transformation public class Advertiser { private Account account; public Advertiser() { account = new Account(); } public Account get. Account() { return account; } } CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 17

Bidirectional one-to-one association Overview: Overview Object design model before transformation Optimization Advertiser Associations 1

Bidirectional one-to-one association Overview: Overview Object design model before transformation Optimization Advertiser Associations 1 Account Source code after transformation Contracts Tables Summary 1 public class Advertiser { /* The account field is initialized * in the constructor and never * modified. */ private Account account; public Advertiser() { account = new Account(this); } public Account get. Account() { return account; } CEN 5011: Advanced Software Engineering } public class Account { /* The owner field is initialized * during the constructor and * never modified. */ private Advertiser owner; public Account(owner: Advertiser) { this. owner = owner; } public Advertiser get. Owner() { return owner; } } Seventh Lecture (2 nd part) on Oct. 27, 2004 18

Bidirectional, one-to-many association Overview: Object design model before transformation Overview Optimization Associations Contracts Tables

Bidirectional, one-to-many association Overview: Object design model before transformation Overview Optimization Associations Contracts Tables Summary Advertiser 1 * Account Source code after transformation public class Advertiser { private Set accounts; public Advertiser() { accounts = new Hash. Set(); } public void add. Account(Account a) { accounts. add(a); a. set. Owner(this); } public void remove. Account(Account a) { accounts. remove(a); a. set. Owner(null); } } CEN 5011: Advanced Software Engineering public class Account { private Advertiser owner; public void set. Owner(Advertiser new. Own { if (owner != new. Owner) { Advertiser old = owner; owner = new. Owner; if (new. Owner != null) new. Owner. add. Account(this); if (old. Owner != null) old. remove. Account(this); } } } Seventh Lecture (2 nd part) on Oct. 27, 2004 19

Bidirectional, many-to-many association Overview: Object design model before transformation Overview Optimization Tournament * {ordered}

Bidirectional, many-to-many association Overview: Object design model before transformation Overview Optimization Tournament * {ordered} * Player Associations Contracts Tables Summary Source code after transformation public class Player { public class Tournament { private List tournaments; private List players; public Player() { public Tournament() { tournaments = new players = new Array. List(); } } public void add. Player(Player public void add. Tournament(Tournament t) { p) { if (!tournaments. contains(t)) { if (!players. contains(p)) { tournaments. add(t); players. add(p); t. add. Player(this); p. add. Tournament(this); } } } 20 CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 }

Bidirectional qualified association Overview: Overview Object design model before transformation Optimization Associations Contracts *

Bidirectional qualified association Overview: Overview Object design model before transformation Optimization Associations Contracts * League * Tables Player nick. Name Summary Object design model before forward engineering League 0. . 1 nick. Name * Player Source code after forward engineering CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 21

Bidirectional qualified association Overview: Source code after forward engineering Overview Optimization Associations Contracts Tables

Bidirectional qualified association Overview: Source code after forward engineering Overview Optimization Associations Contracts Tables public class League { private Map players; Summary public class Player { private Map leagues; public void add. Player public void add. League (String nick. Name, Player (String nick. Name, League l) { p) { if if (!leagues. contains. Key(l)) { (!players. contains. Key(nick. Name)) leagues. put(l, { nick. Name); players. put(nick. Name, p); l. add. Player(nick. Name, this); } p. add. League(nick. Name, this); } } } CEN 5011: Advanced Software Engineering } } Seventh Lecture (2 nd part) on Oct. 27, 2004 22

Transformation of an association class Overview: Overview Object design model before transformation Optimization Statistics

Transformation of an association class Overview: Overview Object design model before transformation Optimization Statistics Associations +get. Average. Stat(name) +get. Total. Stat(name) +update. Stats(match) Contracts Tables Summary Tournament * Object design model after transformation: 1 class and two binary associations * Player Statistics +get. Average. Stat(name) +get. Total. Stat(name) +update. Stats(match) 1 1 Tournament * CEN 5011: Advanced Software Engineering * Player Seventh Lecture (2 nd part) on Oct. 27, 2004 23

Agenda Overview: Overview Optimizing the Object Design Model Mapping Associations Mapping Contracts to Exceptions

Agenda Overview: Overview Optimizing the Object Design Model Mapping Associations Mapping Contracts to Exceptions Mapping Object Models to Tables Optimization Associations Contracts Tables Summary CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 24

Exceptions and contract violations Overview: Overview Optimization Associations Contracts Tables Summary Many object-oriented languages,

Exceptions and contract violations Overview: Overview Optimization Associations Contracts Tables Summary Many object-oriented languages, including Java do not include built-in support for contracts. However, we can use their exception mechanisms as building blocks for signaling and handling contract violations In Java we use the try-throw-catch mechanism Example: – Let us assume the accept. Player() operation of Tournament. Control is invoked with a player who is already part of the Tournament. – In this case accept. Player() should throw an exception of type Known. Player. – See source code on next slide CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 25

The try-throw-catch Mechanism in Java Overview: Overview Optimization Associations Contracts Tables Summary public class

The try-throw-catch Mechanism in Java Overview: Overview Optimization Associations Contracts Tables Summary public class Tournament. Control { private Tournament tournament; public void add. Player(Player p) throws Known. Player. Exception { if (tournament. is. Player. Accepted(p)) { throw new Known. Player. Exception(p); } //. . . Normal add. Player behavior } } public class Tournament. Form { private Tournament. Control control; private Array. List players; public void process. Player. Applications() { // Go through all the playe for (Iteration i = players. iterator(); i. has. Next(); ) { try { // Delegate to the control object. control. accept. Player((Player)i. next()); } catch (Known. Player. Exception e) { // If an exception was caught, log it to the co Error. Console. log(e. get. Message()); } } CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 26

Implementing a contract Overview: Overview Optimization For each operation in the contract, do the

Implementing a contract Overview: Overview Optimization For each operation in the contract, do the following Check precondition: – Check the precondition before the beginning of the method with a test that raises an exception if the precondition is false. Associations Contracts Tables Summary Check postcondition: – Check the postcondition at the end of the method and raise an exception if the contract is violoated. If more than one postcondition is not satisfied, raise an exception only for the first violation. Check invariant: – Check invariants at the same time as postconditions. Deal with inheritance: – Encapsulate the checking code for preconditions and postconditions into separate methods that can be called from subclasses. CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 27

Heuristics: Contracts and Exceptions Overview: Be pragmatic, if you don’t have enough time. Overview

Heuristics: Contracts and Exceptions Overview: Be pragmatic, if you don’t have enough time. Overview Optimization Associations Omit checking code for postconditions and invariants. Omit the checking code for private and protected methods. Focus on components with the longest life Reuse constraint checking code. Contracts Tables Summary CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 28

Agenda Overview: Overview Optimizing the Object Design Model Mapping Associations Mapping Contracts to Exceptions

Agenda Overview: Overview Optimizing the Object Design Model Mapping Associations Mapping Contracts to Exceptions Mapping Object Models to Tables Summary Optimization Associations Contracts Tables Summary CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 29

Object model and relational dbase Overview: Overview Optimization – Some degradation occurs because all

Object model and relational dbase Overview: Overview Optimization – Some degradation occurs because all UML constructs must be mapped to a single relational database construct - the table. Associations Contracts Tables Summary UML object models can be mapped to relational databases: UML mappings – Each class is mapped to a table – Each class attribute is mapped onto a column in the table – An instance of a class represents a row in the table – A many-to-many association is mapped into its own table – A one-to-many association is implemented as buried foreign key Methods are not mapped CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 30

Example Overview: Overview Mapping the User class to a database table Optimization Associations Contracts

Example Overview: Overview Mapping the User class to a database table Optimization Associations Contracts User Tables +first. Name: String +login: String +email: String Summary User table id: long first. Name: text[25] login: text[8] email: text[32] CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 31

Primary and Foreign Keys Overview: Overview Optimization Associations Contracts Tables Summary Any set of

Primary and Foreign Keys Overview: Overview Optimization Associations Contracts Tables Summary Any set of attributes that could be used to uniquely identify any data record in a relational table is called a candidate key. The actual candidate key that is used in the application to identify the records is called the primary key. – The primary key of a table is a set of attributes whose values uniquely identify the data records in the table. A foreign key is an attribute (or a set of attributes) that references the primary key of another table. CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 32

Example for Primary and Foreign Keys Overview: Primary key User table Overview Optimization Associations

Example for Primary and Foreign Keys Overview: Primary key User table Overview Optimization Associations Contracts Tables Summary first. Name login email “alice” “am 384@mail. org” “john” “js 289” “john@mail. de” “bob” “bd” “bobd@mail. ch” League table Candidate key name Candidate key login “tictactoe. Novice” “am 384” “tictactoe. Expert” “am 384” “chess. Novice” “js 289” Foreign key referencing User table CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 33

Buried Association Overview: Overview Associations with multiplicity one can be implemented using a foreign

Buried Association Overview: Overview Associations with multiplicity one can be implemented using a foreign key. For one-to-many associations we add a foreign key to the table representing the class on the “many” end. For all other associations we can select either class at the end of the association. Optimization Associations Contracts Tables Summary CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 34

Buried Association Overview: Overview Associations with multiplicity “one” can be implemented using a foreign

Buried Association Overview: Overview Associations with multiplicity “one” can be implemented using a foreign key. Because the association vanishes in the table, we call this a buried association. For one-to-many associations we add the foreign key to the table representing the class on the “many” end. For all other associations we can select either class at the end of the association. Optimization Associations Contracts Tables Summary CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 35

Example Overview: Overview Optimization Associations Contracts Tables Transaction transaction. ID Summary Portfolio * Foreign

Example Overview: Overview Optimization Associations Contracts Tables Transaction transaction. ID Summary Portfolio * Foreign Key Transaction Table transaction. ID portfolio. ID CEN 5011: Advanced Software Engineering portfolio. ID. . . Portfolio Table portfolio. ID . . . Seventh Lecture (2 nd part) on Oct. 27, 2004 36

Mapping Many-To-Many Associations Overview: Overview In this case we need a separate table for

Mapping Many-To-Many Associations Overview: Overview In this case we need a separate table for the association Optimization Associations Contracts City Tables Summary * Serves * city. Name City Table city. Name Houston Albany Munich Hamburg Airport Table airport. Code IAH HOU ALB MUC HAM Serves Table airport. Name Intercontinental Hobby Albany County Munich Airport Hamburg Airport CEN 5011: Advanced Software Engineering Airport airport. Code airport. Name city. Name airport. Code IAH Houston HOU Houston ALB Albany MUC Munich HAM Hamburg Seventh Lecture (2 nd part) on Oct. 27, 2004 37

Example Overview: Overview Mapping the Tournament/Player association as a separate table Optimization Associations Contracts

Example Overview: Overview Mapping the Tournament/Player association as a separate table Optimization Associations Contracts Tournament * Tables * Player Summary Tournament table id name 23 novice 24 expert . . . Player table Tournament. Player. Association table tournament player 23 56 23 79 CEN 5011: Advanced Software Engineering id name 56 alice 79 john Seventh Lecture (2 nd part) on Oct. 27, 2004 . . . 38

Realizing Inheritance Overview: Overview Optimization Associations Contracts Tables Summary Relational databases do not support

Realizing Inheritance Overview: Overview Optimization Associations Contracts Tables Summary Relational databases do not support inheritance Two possibilities to map UML inheritance relationships to a database schema – With a separate table (vertical mapping) The attributes of the superclass and the subclasses are mapped to different tables – By duplicating columns (horizontal mapping) There is no table for the superclass Each subclass is mapped to a table containing the attributes of the subclass and the attributes of the superclass CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 39

Realizing inheritance with a separate table Overview: Overview User Optimization name Associations Contracts Tables

Realizing inheritance with a separate table Overview: Overview User Optimization name Associations Contracts Tables Player credits League. Owner max. Num. Leagues Summary User table id name 56 zoe 79 john . . . role League. Owner Player League. Owner table id 56 max. Num. Leagues Player table. . . 12 CEN 5011: Advanced Software Engineering id credits 79 126 Seventh Lecture (2 nd part) on Oct. 27, 2004 . . . 40

duplicating columns Overview: Overview Optimization Associations User Contracts name Tables Summary League. Owner max.

duplicating columns Overview: Overview Optimization Associations User Contracts name Tables Summary League. Owner max. Num. Leagues Player credits League. Owner table id name 56 zoe Player table max. Num. Leagues. . . id name credits 12 79 john 126 CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 . . . 41

Comparison Overview: Overview Optimization – How likely is a change of the superclass? –

Comparison Overview: Overview Optimization – How likely is a change of the superclass? – What are the performance requirements for queries? Associations Contracts Tables Summary The trade-off is between modifiability and response time Separate table mapping J We can add attributes to the superclass easily by adding a column to the superclass table L Searching for the attributes of an object requires a join operation. Duplicated columns L Modifying the database schema is more complex and error-prone J Individual objects are not fragmented across a number of tables, resulting in faster queries CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 42

Heuristics for Transformations Overview: Overview For a given transformation use the same tool Keep

Heuristics for Transformations Overview: Overview For a given transformation use the same tool Keep the contracts in the source code, not in the object design model Use the same names for the same objects Have a style guide for transformations Optimization Associations Contracts Tables Summary CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 43

Agenda Overview: Overview Optimizing the Object Design Model Mapping Associations Mapping Contracts to Exceptions

Agenda Overview: Overview Optimizing the Object Design Model Mapping Associations Mapping Contracts to Exceptions Mapping Object Models to Tables Summary Optimization Associations Contracts Tables Summary CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 44

Summary (1) Overview: Overview Undisciplined changes => degradation of the system model Four mapping

Summary (1) Overview: Overview Undisciplined changes => degradation of the system model Four mapping concepts were introduced Optimization Associations Contracts Tables Summary – Model transformation improves the compliance of the object design model with a design goal – Forward engineering improves the consistency of the code with respect to the object design model – Refactoring improves the readability or modifiability of the code – Reverse engineering attempts to discover the design from the code. CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 45

Summary (2) Overview: Overview Optimization Associations Contracts Tables Summary We reviewed model transformation and

Summary (2) Overview: Overview Optimization Associations Contracts Tables Summary We reviewed model transformation and forward engineering techniques: – – Optiziming the class model Mapping associations to collections Mapping contracts to exceptions Mapping class model to storage schemas CEN 5011: Advanced Software Engineering Seventh Lecture (2 nd part) on Oct. 27, 2004 46