圖1.Ecore類層次結構:這個圖像顯示出Ecore元數據完整的類層次。
EcoreFactory ecoreFactory = EcoreFactory.eINSTANCE;
EcorePackage ecorePackage = EcorePackage.eINSTANCE;
//創建一Company類
EClass companyClass = ecoreFactory.createEClass();
companyClass.setName("Company");
//創建公司名
EAttribute companyName = ecoreFactory.createEAttribute();
companyName.setName("name");
companyName.setEType(ecorePackage.getEString());
companyClass.getEStructuralFeatures().add(companyName);
//創建一Employee類
EClass employeeClass = ecoreFactory.createEClass();
employeeClass.setName("Employee");
//在Employee類上添加一個名字屬性
EAttribute employeeName = ecoreFactory.createEAttribute();
employeeName.setName("name");
employeeName.setEType(ecorePackage.getEString());
employeeClass.getEStructuralFeatures().add(employeeName);
//創建一Department類
EClass departmentClass = ecoreFactory.createEClass();
departmentClass.setName("Department");
//添加department標志數字
EAttribute departmentNumber = ecoreFactory.createEAttribute();
departmentNumber.setName("number");
departmentNumber.setEType(ecorePackage.getEInt());
departmentClass.getEStructuralFeatures().add(departmentNumber);
//department類能夠包含到一個或多個employee的參考
EReference departmentEmployees = ecoreFactory.createEReference();
departmentEmployees.setName("employees");
departmentEmployees.setEType(employeeClass);
//指定它可能是一個或多個employee
departmentEmployees.setUpperBound(ETypedElement.UNBOUNDED_MULTIPLICITY);
departmentEmployees.setContainment(true);
departmentClass.getEStructuralFeatures().add(departmentEmployees);
//company能夠包含到一個或多個departments的參考
EReference companyDepartments = ecoreFactory.createEReference();
companyDepartments.setName("department");
companyDepartments.setEType(departmentClass);
companyDepartments.setUpperBound(ETypedElement.UNBOUNDED_MULTIPLICITY);
companyDepartments.setContainment(true);
companyClass.getEStructuralFeatures().add(companyDepartments);
//創建一個包-描述company
EPackage companyPackage = ecoreFactory.createEPackage();
companyPackage.setName("company");
companyPackage.setNsPrefix("company");
companyPackage.setNsURI("http:///com.example.company.ecore");
companyPackage.getEClassifiers().add(employeeClass);
companyPackage.getEClassifiers().add(departmentClass);
companyPackage.getEClassifiers().add(companyClass);
通過使用反射API,你能創建並且初始化一個你的模型的實例:
//得到company工廠
EFactory companyFactory = companyPackage.getEFactoryInstance();
//使用工廠來創建company類的實例並且
//設置company名字
EObject company = companyFactory.create(companyClass);
company.eSet(companyName, "MyCompany");
//創建一個employee類的實例
EObject employee = companyFactory.create(employeeClass);
//使用反射API初始化employee的名字
employee.eSet(employeeName, "John");
//創建一個department類的實例
EObject department = companyFactory.create(departmentClass);
department.eSet(departmentNumber, new Integer(123));
//添加"John"到department
((List)department.eGet(departmentEmployees)).add(employee);
//添加department到company
((List)company.eGet(companyDepartments)).add(department);
四、 數據的串行化和反串行化
為了串行化你的模型實例,你需要把一個你的實例模型的根對象放置到一個資源中。EMForg.eclipse.emf.ecore.resource.Resource接口描述了一個物理的存儲位置(例如文件或URL)並且提供方法以串行化和裝載數據。每一種資源都被存儲在一個ResourceSet中-它代表了一個資源集合-這些資源被一起創建和加載並允許在它們當中進行參考引用。特別地,一個ResourceSet負責跟蹤哪些資源已被裝載並且保證這個ResourceSet中的資源不會被重復裝載。
因為EMF能夠處理多重模型源,例如XML模式,所以指定使用哪些資源來實現(反)串行化你的數據也是很重要的