Mapping Models To Code
Mapping Models To Code
Mapping Models To Code
M.Shafi Tokhi
TUBerlin 2008
[email protected]
Software Engineering project
Table of Contents
● An overview of mapping
● Mapping concept
● Optimizing the object model design
● Collapsing objects into attributes
● Mapping associations to collections
● Mapping object models to a relational database.
● Primary key, candidate key, foreign key
● Buried association
● Realizing Inheritance
● Comparing separate columns vs duplicate columns
● Summary
An Overview of Mapping
● Transformation is usually localized, affects a small
number of classes, attributes, and operations, and is
executed in a series of small steps.
● The transformations occur during numerous object
design and implementation activities.
Mapping Concept
● Model transformation: operate on object models, e.g: a
class with street, address,zip code and etc..
● Refactoring: improve a single aspect of a system,
manipulate the source code.
● Forward Engineering: produces a source code template
that corresponds to an object model.
● Reverse Engineering: produce a model that corresponds
to source code.
Mapping Concept
Forward engineering
Refactoring
Model
transformation
Reverse engineering
Model space Source code space
Model Transformation
● Applies to an object model and results in another
object model [Blaha & Permerlani, 1998].
Object design model before transformation
LeagueOwner Advertiser Player
+email:Address +email:Address +email:Address
Object design model after transformation
User
+email:Address
Before refactoring After refactoring
public class User {
public class Player { private String email;
private String email; }
//... public class Player extends User {
} //...
public class LeagueOwner { }
private String eMail; public class LeagueOwner extends
//... User {
} //...
public class Advertiser { }
private String email_address; public class Advertiser extends
User {
//...
//...
}
}
Forward Engineering
Object design model before transformation
User LeagueOwner
+email:String +maxNumLeagues:int
+notify(msg:String)
Source code after transformation
public class LeagueOwner extends User {
public class User { private int maxNumLeagues;
private String email; public int getMaxNumLeagues() {
public String getEmail() { return maxNumLeagues;
return email;
}
}
public void setMaxNumLeagues
public void setEmail(String value){
(int value) {
email = value;
} maxNumLeagues = value;
public void notify(String msg) { }
// .... /* Other methods omitted */
} }
/* Other methods omitted */
}
Mapping Activities
● Optimization
● Realizing associations
● Mapping contracts to exceptions
● Mapping class Models to a strong schema
Optimizing the Object Model Design
● Adding associations to optimize access paths.
– prevent bottleneck performance.
– Reduce many associations to one.
– Remove excessive modeling (misplaced attributes).
● Collapsing objects into attributes (Figure in next slide).
● Delaying expensive computations.
● Caching the results of expensive computations.
Collapsing Objects into attributes
Object design model before transformation
Person SocialSecurity
number:String
Object design model after transformation
Person
SSN:String
Mapping Associations to Collections
● Associations are UML concepts that denote collection
of bidirectional links between two or more objects.
● OOP language do not support associations, they do
support references instead.
● References are unidirectional, take place between two
objects.
Mapping Associations
● Unidirectional onetoone associations:
Object design model before transformation
1 1
Advertiser Account
Source code after transformation
Source code after transformation
public Account(owner:Advertiser) {
public Advertiser() {
this.owner = owner;
account = new Account(this); }
} public Advertiser getOwner() {
public Account getAccount() { return owner;
return account; }
} }
}
Realization of bidirectional onetoone association
Mapping Associations
● Onetomany associations:
Object design model before transformation
1 *
Advertiser Account
Source code after transformation
Realization of bidirectional onetomany association
Mapping Associations
● Manytomany associations:
Object design model before transformation
* {ordered} *
Tournament Player
Source code after transformation
Object design model before transformation
* *
League Player
Object design model before forward engineering
* 0..1
League nickName Player
Mapping Associations
● Qualified association ( continued )
Realization of bidirectional qualified association
Transformation of an association class
Object design model before transformation
Statistics
+getAverageStat(name)
+ getTotalStat(name)
+ updateStats(match)
Tournament Player
* *
Object design model after transformation
Statistics
+getAverageStat(name)
+ getTotalStat(name)
+ updateStats(match)
1 1
Tournament Player
* *
Exception handling in Java
● Many object oriented languages including Java do not
builtin support for contracts.
● we can use exceptions mechanism for building blocks for
signaling and handling contract violations.
● In java we use trythrowcatch mechanism.
● Example:
– Assume the acceptPlayer() operation of TournamentControl
is invoked with a player who is already part of the
Tournament.
– In this case acceptPlayer() should throw an exception of
type KnownPlayer. ( source code on next slide )
Mapping Contracts to Exceptions
public class TournamentControl {
private Tournament tournament;
public void addPlayer(Player p) throws KnownPlayerException {
if (tournament.isPlayerAccepted(p)) {
throw new KnownPlayerException(p);
}
//... Normal addPlayer behavior
}
}
public class TournamentForm {
private TournamentControl control;
private ArrayList players;
public void processPlayerApplications() {
// Go through all the players who applied for this tournament
for (Iteration i = players.iterator(); i.hasNext();) {
try {
// Delegate to the control object.
control.acceptPlayer((Player)i.next());
} catch (KnownPlayerException e) {
// If an exception was caught, log it to the console, and
// proceed to the next player.
ErrorConsole.log(e.getMessage());
}
}
}
}
Implementing a contract
For each operation in the contract do the following steps:
● Checking preconditions: check preconditions at the
beginning of the method and raise an exception if the
precondition is fails.
● Checking postconditions: check postconditions at the end
of the method and raise an exception if the contract is
violated. ( corresponds to a boolean expression ).
● Check invariants: invariants are checked at the same time
as postconditions.
● Dealing with inheritance: encapsulate the checking code
for preconditions and postconditions into separate
methods that can be called from subclasses.
Implementing a contract
«invariant»
getMaxNumPlayers() > 0
Tournament
-maxNumPlayers: int
«precondition»
+getNumPlayers():int
!isPlayerAccepted(p) +getMaxNumPlayers():int
+isPlayerAccepted(p:Player):boolean
+addPlayer(p:Player)
«precondition»
getNumPlayers() < «postcondition»
getMaxNumPlayers() isPlayerAccepted(p)
A complete implementation of the Tournament.addPlayer() contract
download Tournament.java src !
Mapping object model to a relational
database
● UML constructs must be mapped to a single relational
database construct – the table.
● Each UML class is mapped to a table and each
attribute is mapped to a column.
● Instance of a class represent a row in the table.
● A manytomany association is mapped to it's own
table.
● A onetomany association implemented as buried
foreign key.
Mapping the User class to a database
User
+firstName:String
+login:String
+email:String
User table
id:long firstName:text[25] login:text[8] email:text[32]
Primary key, Candidate key, Foreign key
Primary key
User table
FirstName login email
“alice” “am384” “[email protected]”
“john” “js289” “[email protected]”
“bob” “bd” “[email protected]”
Candidate key Candidate key
League table
name login
“tictactoeNovice” “am384”
“tictactoeExpert” “am384”
“chessNovice” “js289”
Foreign key referencing User table
Buried association
● For one tomany association we put the foreign key on
the class representing the many side.
● Association with multiplicity “one” can be implemented
using a foreign key. Because the association vanishes
in the table, we call this a buried association.
LeagueOwner 1 * League
LeagueOwner table League table
id:long ... id:long ... owner:long
Realizing Inheritance
● 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 the subclass
holds it's own and superclass attributes.
Realizing Inheritance with a sperate table
User
name
LeagueOwner Player
maxNumLeagues credits
User table
id name ... role
56 zoe LeagueOwner
79 john Player
LeagueOwner table Player table
id maxNumLeagues ... id credits ...
56 12 79 126
Realizing Inheritance by duplicating
columns
User
name
LeagueOwner Player
maxNumLeagues credits
LeagueOwner table Player table
id name maxNumLeagues ... id name credits ...
● Implementing a contract using ( checking precondition, checking postcondition, invariants ).
● Mapping classes to relational database tables ( map UML class to a table and attributes in
columns).
● Realizing inheritance ( vertical mapping, horizontal mapping )
● Comparing realizing inheritance methods.
● Separate columns vs duplicate columns , separate column need join operation, duplicate
columns error pron but faster.