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> 下面我們就來分析一下這個檔案:
EPackageeocre模型的頂層元素是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,EDataTypeUML 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屬性對應著參數類型。