简体   繁体   中英

Simple ATL transformation - create new classes as children of a certain class

I practice the use of ATL, so I'm trying to do very simple model transformation like below.

ATL:

module Form2NewForm;
create OUT : Form refining IN : Form;
-- @path Form=/Form2Form/Form.ecore

--parameter
helper def : subjectName : String = 'address';
helper def : newHeight : Integer = 500;
helper def : newWidth : Integer = 300;

helper context Form!ScreenItem def : isSbj() : Boolean = 
    if self.itemName = thisModule.subjectName
    then true else false endif;


rule NewScreem{
    from
        s : Form!Screen
    to
        t : Form!Screen(
            height <- thisModule.newHeight,
            width <- thisModule.newWidth
        )
}

--new three class are created below "address" class
rule Refinement{
    from
        s : Form!ScreenItem(s.isSbj())
    to
        t : Form!ScreenItem(
            --
            ),
        t1 : Form!SubScreenItem(
            itemName <- 'City',
            height <- s.height,
            width <- (s.width-20) div 3,
            x <- s.x - (s.width div 2) + (s.width-20) div 6,
            y <- s.y
            ),
        t2 : Form!SubScreenItem(
            itemName <- 'Prefecture',
            height <- s.height,
            width <- (s.width-20) div 3,
            x <- s.x,
            y <- s.y
            ),
        t3 : Form!SubScreenItem(
            itemName <- 'Number',
            height <- s.height,
            width <- (s.width-20) div 3,
            x <- s.x + (s.width div 2) - (s.width-20) div 6,
            y <- s.y
            )   
}

input model:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns="http://form/1.0">
  <Screen height="250" width="600">
    <screenItem itemName="lastName" width="200" height="30" x="150" y="65"/>
    <screenItem itemName="firstName" width="200" height="30" x="450" y="65"/>
    <screenItem itemName="address" width="500" height="30" x="300" y="145"/>
  </Screen>
</xmi:XMI>

I want to get this expected model from input model and ATL.

expected model:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:Form="http://form/1.0">
<Form:Screen height="500" width="300">
  <screenItem itemName="lastName" height="30" width="200" x="150" y="65"/>
  <screenItem itemName="firstName" height="30" width="200" x="450" y="65"/>
  <screenItem itemName="address" height="30" width="500" x="300" y="145">
    <children itemName="City" height="30" width="160" x="130" y="145"/>
    <children itemName="Prefecture" height="30" width="160" x="300" y="145"/>
    <children itemName="Number" height="30" width="160" x="470" y="145"/>
  </screenItem>
</Form:Screen>
</xmi:XMI>

But practically this below model was created.

output model:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:Form="http://form/1.0">
<Form:Screen height="500" width="300">
  <screenItem itemName="lastName" height="30" width="200" x="150" y="65"/>
  <screenItem itemName="firstName" height="30" width="200" x="450" y="65"/>
  <screenItem itemName="address" height="30" width="500" x="300" y="145"/>
</Form:Screen>
<Form:SubScreenItem itemName="City" height="30" width="160" x="130" y="145"/>
<Form:SubScreenItem itemName="Prefecture" height="30" width="160" x="300" y="145"/>
<Form:SubScreenItem itemName="Number" height="30" width="160" x="470" y="145"/>
</xmi:XMI>

In this transformation, I want to create new classes whose itemName are "City", "Prefecture" and "Number" as children of "address" class like expected model. But practically these three classes don't locate as children of "address" class.

I think the relation between "address" class and these three classes should be written in t : Form!ScreenItem( ... ) in rule Refinement, but I can't come up with how to write.

Please tell me how to write ATL code which can create expected model.

Form.ecore:

<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore">
  <ecore:EPackage name="Form" nsURI="http://form/1.0" nsPrefix="Form">
    <eClassifiers xsi:type="ecore:EClass" name="Screen">
      <eStructuralFeatures xsi:type="ecore:EAttribute" name="height" lowerBound="1"
      eType="#/1/Integer"/>
      <eStructuralFeatures xsi:type="ecore:EAttribute" name="width" lowerBound="1"
      eType="#/1/Integer"/>
      <eStructuralFeatures xsi:type="ecore:EReference" name="screenItem" upperBound="-1"
      eType="#/0/ScreenItem" containment="true" eOpposite="#/0/ScreenItem/screen"/>
    </eClassifiers>
    <eClassifiers xsi:type="ecore:EClass" name="ScreenItem">
      <eStructuralFeatures xsi:type="ecore:EAttribute" name="itemName" eType="#/1/String"/>
      <eStructuralFeatures xsi:type="ecore:EAttribute" name="height" eType="#/1/Integer"/>
      <eStructuralFeatures xsi:type="ecore:EAttribute" name="width" eType="#/1/Integer"/>
      <eStructuralFeatures xsi:type="ecore:EReference" name="screen" lowerBound="1"
      eType="#/0/Screen" changeable="false" eOpposite="#/0/Screen/screenItem"/>
      <eStructuralFeatures xsi:type="ecore:EAttribute" name="x" eType="#/1/Integer"/>
      <eStructuralFeatures xsi:type="ecore:EAttribute" name="y" eType="#/1/Integer"/>
      <eStructuralFeatures xsi:type="ecore:EReference" name="children" upperBound="-1"
      eType="#/0/SubScreenItem" containment="true"/>
    </eClassifiers>
    <eClassifiers xsi:type="ecore:EClass" name="SubScreenItem">
      <eStructuralFeatures xsi:type="ecore:EAttribute" name="itemName" eType="#/1/String"/>
      <eStructuralFeatures xsi:type="ecore:EAttribute" name="height" eType="#/1/Integer"/>
      <eStructuralFeatures xsi:type="ecore:EAttribute" name="width" eType="#/1/Integer"/>
      <eStructuralFeatures xsi:type="ecore:EAttribute" name="x" eType="#/1/Integer"/>
      <eStructuralFeatures xsi:type="ecore:EAttribute" name="y" eType="#/1/Integer"/>
    </eClassifiers>
  </ecore:EPackage>
  <ecore:EPackage name="PrimitiveTypes">
    <eClassifiers xsi:type="ecore:EDataType" name="String"/>
    <eClassifiers xsi:type="ecore:EDataType" name="Integer"/>
    <eClassifiers xsi:type="ecore:EDataType" name="Boolean"/>
  </ecore:EPackage>
</xmi:XMI>

You have to add:

t : Form!ScreenItem(

        children <- t1,
        children <- t2,
        children <- t3
        )

Regards,

Isabel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM