图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模式,所以指定使用哪些资源来实现(反)串行化你的数据也是很重要的 展开更多 (50%) 分享 猜你喜欢
下拉加载更多内容 ↓