Map Objects Before Links

This is a modularisation pattern.

Summary

To define a correct and efficient mapping transformation specification when there are cycles of entity type dependencies in the source model.

Application conditions

Applicable whenever the source model contains self-associations on entities or longer cycles of entity type dependencies, which need to be mapped to similar target structures by the transformation. A strictly hierarchical phased construction form is not suitable in this case.

Solution

Split the transformation specification into two phases, the first maps source entity types and their attributes and any other data which is not involved in dependency cycles. The second phase links together the target objects based on the source model links, using traces or identity attributes to look up the target objects that should be linked.

Benefits

The specification is decomposed into phases, and the complexity of individual rules should be reduced. Bounded iteration can be used to implement rules instead of fixed-point iteration.

Disadvantages

The specification of local postconditions LPost (setting target instance attribute features) and global postconditions GPost (setting association end features on the same instance) becomes split into separate rules, an example of scattering of specification text. In addition, more iterations are potentially required over the source language entity types.

For transformations where contextual information needs to be passed from the processing of composite objects to the processing of their components, the Recursive Descent pattern is more appropriate.

Applications and examples

The pattern is applicable to the same categories of transformations as Phased Construction.

An example of this situation is the GMF migration transformation of (Lano et al, 2011). A variation on the pattern is where new instances in the target model need to be created for each link in the source (as opposed to simply linking target instances for each source link). The Tree to Graph transformation in ETL is an example of this variation. In the source language there is a *-to-0..1 self-association parent from Tree to itself. The original ETL specification uses trace lookup and implicit rule invocation:

rule Tree2Node
transform t : Tree!Tree
to n : Graph!Node
{ n.label := t.label;
if (t.parent.isDefined())
{ var edge := new Graph!Edge;
edge.source := n;
edge.target := t.parent.equivalent();
}
}

The obj.equivalent() expression looks up in the transformation trace to check if obj has already been mapped to a target element tobj, if so, it returns such an element, otherwise it invokes any applicable rules (in this case, Tree2Node itself) to map obj to a target element, which is then returned. This solution therefore uses a form of the Recursive Descent pattern to process the source language self-association parent (processing of a tree t may lead to processing of t.parent, then t.parent.parent, etc.).

Applying the Map Objects Before Links pattern, a first rule creates a node for each tree:

for t : Tree
create n : Node satisfying n.label = t.label

A second rule then creates edges for each link between parent and child trees:

for t : Tree; p : Tree satisfying p : t.parent
create e : Edge satisfying
e.source = Node[t.label] and e.target = Node[p.label]

This solution avoids implicit invocation, and has a stronger control over the ordering of instance creation. Bounded iterations can be used to implement each rule so that its time complexity and termination are simpler to establish.

The default execution mode of ATL uses this pattern: target objects are created from source objects in an initial phase, then inter-related by a second phase (ATLManual).

Related patterns

The pattern is a special case of Phased Construction which deals with self-associations and other circular data relations in the source model. It is described as the separation of generate and refinement transformation phases in (Cuadrado, 2009). The pattern uses Entity Merging because target objects are typically created in an initial phase and updated with links in a second phase.

(Cuadrado et al., 2009) J. Cuadrado, J. Molina, Modularisation of model transformations through a phasing mechanism, Software Systems Modelling, Vol. 8, No. 3, pp. 325--345, 2009.

(Lano et al, 2011) K. Lano, S. Kolahdouz-Rahimi, Solving the TTC 2011 migration case study with UML-RSDS, TTC 2011, EPTCS vol. 74, pp. 36--41, 2011.