简单的 ATL 转换 - 创建新的 classes 作为某个 class 的 children

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

我练习 ATL 的使用,所以我尝试做如下非常简单的模型转换。

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
            )   
}

输入模型:

<?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>

我想从输入模型和 ATL 中得到这个预期的模型。

预期型号:

<?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>

但实际上创建了以下模型。

输出模型:

<?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>

在此转换中,我想创建新的 classes,其 itemName 为 "City"、"Prefecture" 和 "Number" 作为 children of [=42] =] class 像预期的模型。 但实际上这三个 classes 并不定位为 children of "address" class.

我觉得"address" class和这三个class的关系应该写在t : Form!ScreenItem( ... ) in rule Refinement, 但是我可以'想出怎么写。

请告诉我如何编写可以创建预期模型的 ATL 代码。

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>

您必须添加:

t : 表单!ScreenItem(

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

此致,

伊莎贝尔