Download ATL User Manual - first version
Transcript
ATL Documentations
ATL User Manual
Date 21/03/2006
4.5.2.2 Local variables section
The optional using section makes it possible to locally declare a number of local variables. The
variables declared in this section can be used in the using section itself (provided that the variable is
not invoked before its declaration), as well as in the to and the do sections. Each declared variable is
identified by its name (vari) and its type (var_typei), and must be initialized using an OCL expression.
The following code excerpt illustrates the use of the using section:
from
c : GeometricElement!Circle
using {
pi : Real = 3.14;
area : Real = pi * c.radius.square();
}
4.5.2.3 Simple target pattern element
The to section corresponds to the target pattern of the rule. It contains a number of target pattern
elements. This section is mandatory and must contain at least one target pattern element. When
several target pattern elements are specified, they must be separated by comas (“,”). Note that the first
target pattern element corresponds to the default pattern element of the rule. This means that the
target model element associated with this rule’s default target pattern can be viewed as the default
counterpart of the source model element matched by the rule.
In ATL, there exist two different kinds of target pattern elements: the simple and the iterative ones.
Each target pattern element, whatever its type, corresponds to a variable declaration characterized by
a name (out_vari) and a type (out_typei). A simple target pattern is specified as a set of bindings that
define the way the features (either attributes or references) of the generated element must be
initialized. Each binding has to conform to the following syntax:
feature_name <- exp
The name of the initialized feature (feature_name) has to refer to a feature of the variable associated
with the target pattern element, as defined in its metamodel. The specified expression (exp) is an OCL
expression. When a target pattern element contains more than one binding, the successive bindings
have to be separated by comas. Note that it is not required to explicitly initialize all the features of a
generated model element. The default value of the features that are not initialized by means of an
explicit binding may change according to the model handler used to access the model element. As a
consequence, ATL developers are strongly encouraged not to produce code that depends on these
default values.
As an example, it is possible to consider the following ATL rule, which is defined in the context of the
Biblio metamodel described in Appendix C:
rule Journal2Book {
from
j : Biblio!Journal
to
b : Biblio!Book (
title <- j.title + '_(' + j.vol + '):' + j.num,
authors <- j.articles
->collect(e | e.authors)->flatten()->asSet()
chapters <- j.articles,
pagesNb <- j.articles->collect(e | e.pagesNb)->sum()
)
}
This rule aims to produce a Book model element from a Journal model element. It initializes the title,
authors, chapters and pagesNb features of the generated Book:
Page 38