ATHENA
MDI
 

Tutorials (exercises)

Tutorial #1: ATL - Book2Publish

This tutorial describes how to develop the Book2Publish example using Rational Software Modeler (RSM) and ATL.

Tutorial steps

Step 1: Create an ATL project Book2Publication

Step 2: Create the Book.emx metamodel (UML model)

Step 3: Export and import the Book.emx metamodel as Book.ecore

Steps 4 & 5: Create the Publication.emx metamodel (UML model) & Export and import the Publication.emx metamodel as Publication.ecore

Step 6: Create an ATL file Book2Publication.atl

Step 7: Write the ATL transformation

Step 8: Create a source model theBooks.ecore containing book instances

Step 9: Configure the ATL transformation

Step 10: Run the ATL transformation

Tutorial #2: ATL - PIM4SOA to XSD

0. Tutorial overview

In the scope of model-driven engineering, model transformation aims to provide a mean to specify the way to produce target models from a number of source models. In our example we choose to use a single input model.

PIM4SOA to XSD transformation include mapping the elements of the pim4soa meta-model to some elements of XSD meta-model. When the elements are mapped, artifacts from a model that conforms to the pim4soa metamodel( used as an input) are transformed to artifacts in another model that conforms to the XSD metamodel.

1. PIM4SOA information metamodel (source metamodel)

PIM4SOA meta-model describes the concepts needed to model information at the platform indipendent model. In our transformation we are going to use only a subpart of the elements from this meta-model. In here we describe the elements that are used in the mapping.

  • ItemTypes are the basic building block and they represend simple types like string, integer or boolean
  • An Association represent the association between two entities and is used to describe complex types. Container contained and cardinality are the attributes neccessary to related elements.
  • A Document represents an object with a specific structure and composed by entities.
  • An Entity represents a structure element of information

2. Simple XSD metamodel (target metamodel)

This is a conceptual and simplified XSD metamodel that is used only as an example. The elements of this meta-model that will be used in the mapping are the XSDSchema, which includes XSDComplexTypes that are refered as complex objects and XSDSimple types that are basic types. XSDElements and XSDAttributes are included in XSDComplexTypes.

3. Mapping

This figure shows the mapping:

  • Document from the PIM4SOA metamodel maps to XSDSchema.
  • Entity from the PIM4SOA metamodel maps to XSDComplexType.
  • Attribute from the PIM4SOA metamodel maps to XSDAttribute.
  • Association from the PIM4SOA metamodel maps to XSDElement.
  • ItemType from the PIM4SOA metamodel maps to XSDSimpleType.

4. The input model

This is a simple Purchase Order modeled in a PIM4SOA editor. The editor is generated with GMF.

In the model we can find a document order, three entities orderHeader, productInfo and productRecord, respectively with their attributes, three associations and two itemtypes. This artifacts will be transformed into XSDSchema artifacts as described in the mapping.

5. Create an ATL project

Before creating a new ATL project make sure to be in the ATL prospective.

Create the Atl project and than create two folders into it. The folders could be named for metamodels and models, because PIM4SOA and XSDSchema metamodels will be placed in the metamodels folder, while the input model and the generated one will be placed in the models folder.

6. Models and metamodels

Create:

  • Metamodel for PIM4SOA and XSDSchema using the ecore example diagram
  • the input Purchase Order model using the PIM4SOA_INFO diagram example from GMF

If already created import the input models and metamodels to the specified folders

7. Create an ATL file

In this part we create an Atl file that will contain the neccessary Atl code for transforming the Purchase Order from a PIM4SOA model into a XSDSchema model.

The model and metamodel specifications required from the ATL File wizard are only referances. A path to the actuall file must be given to these references before the transformation is run.

The ATL file created include has the specifications that we entered in the wizard as the header. The header section defines the name of the transformation module and the name of the variables corresponding to the source and target models.It also encodes the execution mode of the module.

8. ATL rules and helpers

In Atl there exist two kind of rules: the matched and called rules. In these example we are using only matched rules. Matched rules constitute the core of an ATL transformation since they make it possible to specify for which kind of source element targets elements must be generated and the way the generated target elements have to be initialized.

The called rules provide ATL developers with convenient imperative programming facilities. Called rules can be seen as a particular type of helpers: they have to be explicitly called to be executed and they can accept parameters. However, as opposed to helpers, called rules can generate target model elements as matched rules do. A called rule has to be called from an imperative code section, either from a match rule or another called rule.

ATL helpers can be viewed as the ATL equivalent to Java methods. They make it possible to define factorized ATL code that can be called from different points of an ATL transformation.

Document2Schema

The first rule shown transforms a Document element into a Schema element. In these cases the schema takes the document name and the targetNamespace is set to http://www.w3.org/2001/XMLSchema. We also have to make sure that the Entity elements from the PIM4SOA model is put into the collection xsd_complexType, and that the ItemType elements that are not Entity elements are put into the xsd_simpleType collection.

rule Document2Schema{
    from
        doc : PIM4SOA ! Document
    to
        schema : XSD!XSDSchema(
            document <- doc.name,
            targetNameSpace <- 'http://www.w3.org/2001/XMLSchema',
            xsd_complexType <- Set{PIM4SOA!Entity.allInstances()},
            xsd_simpleType <- Set{PIM4SOA!ItemType.allInstances() -> select(a|a.oclIsKindOf(PIM4SOA!Entity) = false)})
}

Entity2ComplexType

The entity to complex type rule states that an entity is transformed into a complex type that will take the name of the entity. Every attribute of the entity is transformed into an xsd_attribute and the associations from this entity to other entities are transformed into xsd elements. In these cases we can se the use of a helper context that is called from a rule to retrieve all the association coming from this entity. The helper context retrieves all instances of an association and returns those that have the source entity as contained reference.

helper context PIM4SOA ! Entity def : getAssociations():
    PIM4SOA ! Entity = PIM4SOA ! Association.allInstances()
    -> select(assoc | assoc.contained = self);

rule Entity2ComplexType{
    from
        ent : PIM4SOA!Entity
    to
        ct : XSD ! XSDComplexType(
            name <- ent.name,
            xsd_attribute <- Sequence{ent.attribute},
            xsd_element <- ent.getAssociations())
}

Association2Element

This rule maps associations between entities, to elements that are contained by the complexType that is referenced by the container reference of the association. The associations that are not between two entities are not mapped, as we do not need any links between the XSDSchema and the elements that comprises it. These relationships are already specified by the xsd_element aggregation of the metamodel.

rule Association2Element{
    from
        assoc : PIM4SOA ! Association(assoc.contained.oclIsKindOf(PIM4SOA!Entity))
    to
        el : XSD ! XSDElement(
            name <- assoc.name,
            type <- assoc.container)
}

Attribute2Attribute

This rule maps the attributes of the source model to attributes in the target model.

rule Attribute2Attribute{
    from
        att : PIM4SOA!Attribute
    to
        el : XSD!XSDAttribute(
            name <- att.name,
            type <- att.type )
}

ItemType2SimpleType

Since an ItemType is a generalization of an Entity in these rules we needed some constrains in order to transform only the ItemTypes and not the Entities. Atl makes use of OCL and provides some operations useful in the constraining process. In this case a oclIsKindOf operation is used and it returns a boolean value stating whether it is either an instance of an Entity or of one of its subtypes.

rule ItemType2SimpleType{
from it : PIM4SOA!ItemType(
-- transform only ItemTypes and not Entities
it.oclIsKindOf(PIM4SOA!Entity)= false
)

to st : XSD!XSDSimpleType(
name <- it.name)
}

9. Run the file

On the run configuration window create a new Atl Transformation(dubble click in ATL Transformation) and set the project name to be the atl project you are working on and the Atl file is the Atl file included in this project.

On the model choice specify the source and target models and metamodels. You need to set a path for these specifications and remember:

  • IN -> input model(*.pim4soa)
  • PIM4SOA -> source metamodel(PIM4SOA Info.ecore)
  • OUT -> output model(choose your file name)
  • XSDSchema -> target metamodel(XSD metamodel.ecore)

10. The result

This is the result of running the model transformation with the input model.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsdMetamodel:XSDSchema xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsdMetamodel="http:///XSDMetamodel/model" document="Order" targetNameSpace="http://www.w3.org/2001/XMLSchema">
    <xsd_simpleType name="String"/>
    <xsd_simpleType name="Integer"/>
    <xsd_complexType name="OrderHeader">
        <xsd_attribute name="orderID" type="//@xsd_simpleType.0"/>
        <xsd_attribute name="issueDate" type="//@xsd_simpleType.0"/>
    </xsd_complexType>
    <xsd_complexType name="ProductInformation">
        <xsd_element name="record" type="//@xsd_complexType.2"/>
        <xsd_attribute name="name" type="//@xsd_simpleType.0"/>
        <xsd_attribute name="code" type="//@xsd_simpleType.0"/>
    </xsd_complexType>
    <xsd_complexType name="ProductRecord">
        <xsd_attribute name="supplierProductCode" type="//@xsd_simpleType.0"/>
        <xsd_attribute name="buyerProductCode" type="//@xsd_simpleType.0"/>
        <xsd_attribute name="quantity" type="//@xsd_simpleType.1"/>
        <xsd_attribute name="description" type="//@xsd_simpleType.0"/>
        <xsd_attribute name="model" type="//@xsd_simpleType.0"/>
        <xsd_attribute name="productPrice" type="//@xsd_simpleType.1"/>
        <xsd_attribute name="comments" type="//@xsd_simpleType.0"/>
    </xsd_complexType>
</xsdMetamodel:XSDSchema>