10.1 The UML to Relational Mapping

This section describes the transformation definition for transforming PIMs in UML to PSMs dependent on SQL. This is the formal definition of the same transformation that has been used in section 5.1 to transform Rosa's PIM into a relational model. In section 5.1, the transformation was described informally in plain English, which isn't likely to allow tool support. The definition in this section, on the other hand, is formal enough to enable tools to automatically perform the transformation.

As we have seen in section 9.2, we need the metamodels of both the source and target languages for a formal transformation definition. In the transformation definition, we refer to elements from those metamodels. We therefore need the UML metamodel and the SQL metamodel. The UML metamodel we use is a simplification of the UML 1.4 standard (OMG documents formal/2001-09-67), and is depicted in Figure 10-1. Note that the example UML metamodel in the previous chapter in Figure 9-4 is an even more simplified version of the UML metamodel. The metamodel in Figure 10-1 adds some generalizations like Classifier and Feature, and adds an AssociationClass.

Figure 10-1. Simplified UML metamodel

graphics/10fig01.gif

The SQL metamodel is shown in Figure 10-2. This metamodel is a simplified version of the SQL model from the CWM standard (OMG documents formal/2001-10-01 and formal/2001-10-27) and is consistent with the data definition part of SQL (ISO/IEC 9075:1992). Based on the metamodels for the source and target languages, we can now start to specify the transformation definition. In this case, the complete transformation definition has been broken down into two steps. First, we generate an incomplete relational model where the columns of the foreign keys are not fully specified. Next, we take care of the completion, that is, the generation of the columns of the foreign keys.

Figure 10-2. Simplified SQL metamodel

graphics/10fig02.gif

In the transformation rules, we use the additional operations attributes() , operations(), and assEnds() as defined for Classifier in section 9.3.3.

10.1.1 Transformation Rules for UML to Relational Model

This section describes the transformation rules in the transformation definition language defined in Chapter 9. We will see that the formalization of these transformation rules is not trivial.

  1. The first transformation rule defines the transformation from a Class in UML into a Table in SQL. It adds an explicit identifier to the Table, called id . As explained in Chapter 9, the mappings in the rule rely on other rules, in this case the rules that will transform attributes to columns and association ends to foreign keys.

      Transformation  ClassToTable (UML, SQL) {  params  tidName : String      = "ID"   ;      tidType : SQLDataType = INTEGER;  source  class   : UML::Class;  target  table   : SQL::Table ;      primary : SQL::Key   ;      tid     : SQL::Column;  target condition  table.primary = primary  and  tid.type      = tidType  and  tid.table     = table  and  tid.key       = primary  and  tid.nullable  = false;  unidirectional  ;  mapping  class.name + tidName    <~> tid.name;      class.name              <~> table.name;      class.attributes()      <~> table.column;      class.associationEnds() <~> table.foreign; } 
  2. This transformation rule defines the transformation of an association class in UML to a Table in SQL. The rule refers to other rules that will transform attributes to columns and association ends to foreign keys.

      Transformation  AssociationClassToTable (UML, SQL) {  source  assocClass : UML::AssociationClass;  target  table      : SQL::Table;      primary    : SQL::Key  ;  target condition  table.primary = primary;  unidirectional  ;  mapping  assocClass.name              <~> table.name;      assocClass.attributes()      <~> table.column;      assocClass.associationEnds() <~> table.foreign;      assocClass.end               <~> table.foreign; } 
  3. An association end in UML is transformed into a foreign key in SQL. The condition states that this rule only applies to simple associations, not to association classes.

      Transformation  AssociationEndToForeignKey (UML, SQL) {  source  assocEnd : UML::AssociationEnd;  target  foreign  : SQL::ForeignKey    ;  source condition  assocEnd.upper = 1  and  assocEnd.association.oclIsTypeOf(UML::Association)  unidirectional;   mapping  assocEnd.name <~> foreign.name;      assocEnd.type <~> foreign.referencedKey; } 
  4. An association end belonging to an association with an association class in UML is handled differently from a "normal" association-end.

      Transformation  AssociationClassEndToForeigKey (UML, SQL) {  source  assocEnd : UML::AssociationEnd;  target  foreign  : SQL::ForeignKey;  source condition  assocEnd.upper <> 1  and  assocEnd.association.oclIsTypeOf(UML::AssociationClass);  target condition  foreign.table.primary.foreign>includes(foreign);  unidirectional;   mapping  assocEnd.name <~> foreign.name;      assocEnd.type <~> foreign.referencedKey; } 
  5. An attribute in UML becomes a column in SQL.

      Transformation  AttributeToColumn (UML, SQL) {  source  attr : UML::Attribute;  target  column SQL::Column;  target condition  column.nullable = true;  unidirectional;   mapping  attr.name <~> column.name;      attr.type <~> column.type; } 

At the lowest level, we need to transform UML data types to SQL data types. These transformation rules are not defined here, but there should be a number of (predefined) transformations at the lowest level that handle the UML data types to SQL data types. The definition below simply states that there must be such a transformation.

  Transformation  UMLDataTypeToSQLDataType (UML, SQL) {  source  umlDataType : UML::DataType;  target  sqlDataType : SQL::SQLDataType;  unidirectional;  } 

10.1.2 Completion of the Relational Model

When the above transformation rules have been applied to a UML model, the result is an incomplete relational model. The following transformation rules take care of the completion, that is, the generation of the columns of the foreign keys. These transformation rules are transforming a relational model into an extended relational model.

The complete transformation definition for transforming a UML model into a complete relational model is defined as a combination of the specification in the previous section and the specification defined by the following rules. The completion rules take the resulting relational model of the rules of the previous section as input and produce a more complete relational model as output.

  1. This transformation rule defines the generation of the columns for foreign keys that are not part of the primary key. For each reference from a foreign key to one column in the referenced key, one column is generated that is part of this foreign key. Note that the mapping " column.table <~> foreign.table" defines that the table of the source and target of the rule is the same table.

      Transformation  CompleteForeignColumns (SQL, SQL) {  source  foreign          : SQL::ForeignKey;      referencedColumn : SQL::Column;      key              : SQL::Key;  target  column : SQL::Column;  source condition  key.table = foreign.table  and  foreign.referencedKey.column->includes(referencedColumn)  and   not  key.foreign->includes(foreign)  target condition  column.nullable = false  unidirectional;   mapping  column.name             <~> referencedColumn.name;      column.table            <~> foreign.table;      column.type             <~> referencedColumn.type;      column.foreign->first() <~> foreign; } 
  2. The last transformation is for generating the columns for foreign keys that are part of the primary key. The tables involved are originally generated based on association classes in the PIM. This rule is the same as above with the exception of a part of the source condition "key.foreign.contains(foreign)" and mapping "column.key <~> key".

      Transformation  CompleteKeyColumns (SQL, SQL) {  source  foreign : SQL::ForeignKey,      referencedColumn SQL::Column,      key SQL::Key  target  column : SQL::Column  source condition  key.table = foreign.table  and  foreign.referencedKey.column->includes(referencedColumn)  and  key.foreign->includes(foreign);  target condition  column.nullable = false;  unidirectional;   mapping  column.name             <~> referencedColumn.name;      column.table            <~> foreign.table;      column.type             <~> referencedColumn.type;      column.key              <~> key;      column.foreign->first() <~> foreign; } 

Note that in the full transformation definition, rules should be present that map all other elements in the source model, that is, elements that are not foreign keys, keys, and columns that match the given source patterns, to exactly the same elements in the target model. These rules are very simple to formalize, and therefore not included in this section.



MDA Explained. The Model Driven Architecture(c) Practice and Promise 2003
Project Leadership (The Project Management Essential Library)
ISBN: N/A
EAN: 2147483647
Year: 2004
Pages: 118

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net