Create Table columns

We now want to add a Relational Column for each attribute of the UML classes. The created columns must be attached to the Table corresponding to the attribute's class.

Change the UML2Relational ruleset code to:

package tutorial.uml2relational;

public ruleset UML2Relational(in source : uml21, out target : relational) {

    public rule main() {
        // create a Table for each Class
        foreach (class : uml21.Class in source.getInstances("Class")) {
            @createTable(class, target.create("Table"));
        }
    }

    private rule createTable(class : uml21.Class,
                             table : relational.Table) {
        // set the name
        table.name = class.name;
        
        // create a column for each attribute
        foreach (attribute : uml21.Property in class.attribute) {
            @createColumn(attribute, target.create("Column"));
        }
    }

    private rule createTable::createColumn(attribute : uml21.Property,
                                           column : relational.Column) {
        // set the name
        column.name = attribute.name;
        
        // set the owner of the column
        column.owner = table;
    }

}

The rule createColumn is a subrule of createTable. This means the rule createColumn:

If you relaunch the transformation and browse the Relational output model, you see that the Tables now have associated Columns.

From the type Table From the type Column
         

A Column is linked to its Table using the reference owner. The opposite reference of owner is columns, defined on Table. This means that when we set the owner reference, the framework automatically updates the columns reference:

myColumn.owner = myTable;

is strictly equivalent to:

myTable.columns.add(myColumn);

Only one of the above statements is required to link a Column to its Table.

Related tasks
Relaunching a program

Related reference
subrule