ecore模型分析
目前生成ecore模型主要由四种途径,如图:
这里我们采用从UML Model产生ecore模型,首先用Rose设计包emf,然后在包中新建如下类图:
生成的ecore模型如下
<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage 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" name="emf"
nsURI="http:///emf.ecore" nsPrefix="emf">
<eClassifiers xsi:type="ecore:EClass" name="Customer" eSuperTypes="#//Element">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="orders" upperBound="-1"
eType="#//Order" containment="true"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Order" eSuperTypes="#//Element">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="id" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//ELong"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="price" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EFloat"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Element" abstract="true">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="bool" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EBoolean"/>
</eClassifiers>
</ecore:EPackage>
下面我们就来分析一下这个文件:
EPackage
eocre模型的顶层元素是EPackage,它和UML Model中的包(Package)匹配;
EPackage的nsURI和nsPrefix属性不能在UML Model中直接表示出来,这些属性的缺省值都是自动根据Package的名称产生;
EPackage的name属性和UML Model中Package的名称是一样的;
<ecore:EPackage 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" name="emf"
nsURI="http:///emf.ecore" nsPrefix="emf">
...
</ecore:EPackage>
EClass,EEnum,EDataType
UML Model中类(Class)和可以匹配EClass,EEnum,EDataType,具体匹配哪个,取决于类的版型(stereotype)。
l 如果UML中的类的版型为空或者为Interface,则匹配EClass;
l 如果UML中的类的版型为enumeration,则匹配EEnum;
l 如果UML中的类的版型为datatype,则匹配EDataType;
在我们的UML Model中,类Customer的版型为空,所以匹配EClass。
<eClassifiers xsi:type="ecore:EClass" name="Customer">
...
</eClassifiers>
类的名称就是name属性,
<eClassifiers xsi:type="ecore:EClass" name="Customer">
...
</eClassifiers>
如果这个类有父类,可以在属性eSuperTypes指定:
<eClassifiers xsi:type="ecore:EClass" name="Customer" eSuperTypes="#//Element">
...
</eClassifiers>
如果是抽象类,abstract属性为true,默认值为false
<eClassifiers xsi:type="ecore:EClass" name="Element" abstract="true">
...
</eClassifiers>
如果类的版型是interface,interface属性为true,默认值为false
EAttribute 和EReference
UML 类中的每个属性和EAttribute匹配
<eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
UML中属性的name和EAttribute的name一样。
<eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
EAttribute 的eType属性来源于UML中属性的类型,这个类型必须是一个基本的 Java 类型,或者是在UML Model中定义的 EEnum 或者 EDataType。l.
<eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
UML类中每个有箭头的关联(navigable association)和Ereference匹配
<eStructuralFeatures xsi:type="ecore:EReference" name="orders" upperBound="-1"
eType="#//Order" containment="true"/>
EReference的lowerBound 和 upperBound属性值来自于UML 关联的阶元(multiplicty)。例如, 假如你指定阶元是 0..1,则属性lowerBound 是0 ,而 upperBound 是 1,假如阶元是 0..n, 则属性lowerBound 是 0 而upperBound 是 -1 (unbounded)。
如果UML 关联是聚合并且目标类的containment 是"by value",则EReference 的containment 属性为true ,缺省是false。
eOperations
在UML类图中每个操作和eOperations匹配,例如如果在Customer增加name的get和set方法,如图:
ecore模型的代码如下:
<eOperations name="getName" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
name属性对应着操作名,eType属性对应着操作返回值类型。
<eOperations name="setName">
<eParameters name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
</eOperations>
eParameters子元素对应着操作的参数,它的name属性对应参数的名字,eType属性对应着参数类型。