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.
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).
(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.