4.2 Mapping
4.2.1 The mapping DSL
Mapping
The documented mapping DSL grammar is given in Appendix A. To illustrate the func- tion of the mapping DSL, a short snippet of a demo mapping is discussed here. The result of each mapping model is a generated transformer Xtend class that transforms domain-specific elements to generic elements. The package declaration of the resulting transformer class and the required imports, such as domain-specific elements, can be defined in the mapping model and are copied to the resulting transformer class (Demo- Transformer).
Listing 4.1 gives an example mapping that maps multiplicative expressions and numbers to their generic counterpart, GenericBinaryExpression and GenericNumber respectively.
1 G r a m m a r D e m o 3 M a i n T y p e D e m o E x p r e s s i o n 5 T y p e B i n a r y E x p r e s s i o n 7 T y p e I n t C o n s t a n t 9 MAP B i n a r y E x p r e s s i o n B i n a r y E x p r e s s i o n w i t h l e f t C h i l d M e t h o d = l e f t o p e r a t o r M e t h o d op r e t u r n s B i n a r y O p e r a t o r . M U L T I P L Y 11 r i g h t C h i l d M e t h o d = r i g h t to t y p e M u l t i p l i c a t i o n ; 13 MAP L i t e r a l I n t C o n s t a n t to t y p e N U M B E R u s i n g o p e r a t i o n v a l u e ;
Listing 4.1: DemoMapping defining the mapping that generates the DemoTransformer Xtend class
Chapter 4. Common elements metamodel 44 The first line of the mapping defines the name of the grammar (Demo), which is used to name the resulting Xtend class.
In line 3 the MainType of the elements is defined, based on the assumption that a generalization is used to define the elements. This MainType is mapped to the Generi- cAbstractElement type.
After defining the main type, the different subtypes are defined in lines 5 and 7, using ‘Type’ plus the name of the subtype. These subtypes are used as the target subtypes provided during the definition of subsequent element mappings.
Line 9 till 12 describes an example binary mapping. After the keyword ‘MAP Binary Expression’ the name of the subtype should be given and, as stated, only defined sub- types are accepted. Since binary expressions have two child expressions and an operator, these elements have to be defined. The child expressions are defined by specifying the methods to get the children from the binary expression, while the operator is defined by specifying the method to get the operator and the resulting binary operation. In this case, op()is the method to retrieve the operator from the binary expression and Bina- ryOperator.MULTIPLY is the binary operation. The binary operations of the example DSL are defined in the DSL using an enumeration. Finally the user specifies one of the supported GenericBinaryOperators to be used as the operator in the GenericBinaryEx- pression. The supported GenericBinaryOperators are defined as an enumeration in the grammar.
Line 14 describes an example literal mapping. The name of the target type is again one of the subtypes defined in the mapping. The target type is a LiteralTarget, which is again defined in the grammar as an enumeration, and the operation should specify the method to extract the value from the DSL literal object.
This mapping is then used to generate a transformer Xtend class file, which transforms the domain-specific elements to the generic elements. To illustrate this functionality a DemoTransformer is discussed next.
Transformer
Listing 4.2 describes the DemoTransformer Xtend class, which transforms Demo ele- ments (DemoExpression objects) to generic elements (GenericAbstractElement objects), so these elements can be used in the framework to generate test cases.
1 p u b l i c c l a s s D e m o T r a n s f o r m e r { 3 def G e n e r i c A b s t r a c t E l e m e n t t r a n s f o r m ( D e m o E x p r e s s i o n e l e m e n t ) { if( e l e m e n t i n s t a n c e o f B i n a r y E x p r e s s i o n ) { 5 t r a n s f o r m ( e l e m e n t as B i n a r y E x p r e s s i o n ) } 7 e l s e if( e l e m e n t i n s t a n c e o f I n t C o n s t a n t ) { t r a n s f o r m ( e l e m e n t as I n t C o n s t a n t ) 9 } e l s e { 11 t h r o w n e w E x c e p t i o n (" T r a n s f o r m e r e n c o u n t e r e d u n s u p p o r t e d E x p r e s s i o n t y p e : " + e x p r e s s i o n ) ; } 13 } 15 def G e n e r i c B i n a r y E x p r e s s i o n t r a n s f o r m ( B i n a r y E x p r e s s i o n e x p r e s s i o n ) { if( e x p r e s s i o n . op . e q u a l s ( B i n a r y O p e r a t o r . M U L T I P L Y ) ) { 17 r e t u r n n e w G e n e r i c B i n a r y E x p r e s s i o n ( t r a n s f o r m ( e x p r e s s i o n . l e f t ) , t r a n s f o r m ( e x p r e s s i o n . r i g h t ) , G e n e r i c B i n a r y O p e r a t o r . M U L T I P L I C A T I O N ) } 19 e l s e { t h r o w n e w E x c e p t i o n (" T r a n s f o r m e r e n c o u n t e r e d u n s u p p o r t e d B i n a r y O p e r a t o r t y p e : " + e x p r e s s i o n . o p e r a t o r ) 21 } } 23 def G e n e r i c N u m b e r t r a n s f o r m ( I n t C o n s t a n t e x p r e s s i o n ) { 25 r e t u r n n e w G e n e r i c N u m b e r ( e x p r e s s i o n . v a l u e ) ; } 27 }
Listing 4.2: Generated DemoTransformer Xtend class which transforms DemoExpression objects to GenericAbstractElement objects
The class starts with a method (line 3 till 13) to transform the different types of demo expressions (e.g., binary expressions) by exploiting polymorphism, based on the types defined in the mapping file. When an unsupported type is encountered, an exception is thrown.
Since the demo mapping specifies one binary expression operation (multiplication), the method to transform binary expressions (line 15 till 22) only has one if-clause and no else-if clauses. The method’s function is to transform the BinaryExpression to a Gener- icBinaryExpression while also transforming the child expressions. When an unsupported binary operator is encountered, an exception is thrown.
IntConstants are transformed (line 24 till 27) by creating a GenericNumber with the value of the expression.
Chapter 4. Common elements metamodel 46