Xiaobo Sun

Eclipse-Unix http://umlfact.berlios.de/~s_xsun/

EMF dynamic -> reflection

The situation where an application simply wants to share data without the need for a generated type-safe API. The reflective EMF API is sometimes all one really needs.

EMF provides a dynamic implementation of the reflective API (that is, the EObject interface) which, although slower than the one provided by the generated classes, implements the exact same behavior. If you don't need a type-safe API, then the only advantage of generating Java classes, as opposed to simply using the dynamic implementation, is that they use less memory and provide faster access to the data. The down-side is that the generated classes have to be maintained as the model evolves, and they have to be deployed along with the application. This is the normal trade-off between dynamic and static implementations.

Dynamic:

================create dynamic models (types)=========================

EcoreFactory ecoreFactory = EcoreFactory.eINSTANCE;
EcorePackage ecorePackage = EcorePackage.eINSTANCE;

EClass employeeClass = ecoreFactory.createEClass();
employeeClass.setName("Employee");

EAttribute employeeName = ecoreFactory.createEAttribute();
employeeName.setName("name");
employeeName.setEType(ecorePackage.getEString());
employeeClass.getEAttributes().add(employeeName);
EAttribute employeeManager = ecoreFactory.createEAttribute();
employeeManager.setName("manager");
employeeManager.setEType(ecorePackage.getEBoolean());
employeeClass.getEAttributes().add(employeeManager);

EClass departmentClass = ecoreFactory.createEClass();
departmentClass.setName("Department");

EAttribute departmentName = ecoreFactory.createEAttribute();
departmentName.setName("name");
departmentName.setEType(ecorePackage.getEString());
departmentClass.getEAttributes().add(departmentName);

EAttribute departmentNumber = ecoreFactory.createEAttribute();
departmentNumber.setName("number");
departmentNumber.setEType(ecorePackage.getEInt());
departmentClass.getEAttributes().add(departmentNumber);

EReference departmentEmployees = ecoreFactory.createEReference();
departmentEmployees.setName("employees");
departmentEmployees.setEType(employeeClass);
departmentEmployees.setUpperBound(
EStructuralFeature.UNBOUNDED_MULTIPLICITY);
departmentEmployees.setContainment(true);
departmentClass.getEReferences().add(departmentEmployees);

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

=====================create objects==============================
EFactory companyFactory = companyPackage.getEFactoryInstance();

EObject employee1 = companyFactory.create(employeeClass);
employee1.eSet(employeeName, "John");

EObject employee2 = companyFactory.create(employeeClass);
employee2.eSet(employeeName, "Katherine");
employee2.eSet(employeeManager, Boolean.TRUE);

EObject department = companyFactory.create(departmentClass);
department.eSet(departmentName, "ABC");
department.eSet(departmentNumber, new Integer(123));
((List)department.eGet(departmentEmployees)).add(employee1);
((List)department.eGet(departmentEmployees)).add(employee2);

posted on 2007-06-28 10:25 Xiaobo Sun 阅读(267) 评论(0)  编辑  收藏 所属分类: Eclipse EMF


只有注册用户登录后才能发表评论。


网站导航:
 
<2007年6月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

常用链接

留言簿(3)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜