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 閱讀(273) 評論(0)  編輯  收藏 所屬分類: Eclipse EMF


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2007年6月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          導航

          統計

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 东阳市| 淮滨县| 梅州市| 兴和县| 张北县| 华蓥市| 丁青县| 图木舒克市| 乌什县| 东海县| 桃江县| 定安县| 禄丰县| 贞丰县| 丰都县| 普宁市| 德清县| 五大连池市| 凯里市| 肥东县| 玛纳斯县| 上思县| 黄浦区| 岑巩县| 海林市| 神池县| 克拉玛依市| 新竹县| 东海县| 屯昌县| 元阳县| 阜城县| 凉城县| 浙江省| 天水市| 呼玛县| 调兵山市| 怀化市| 昌邑市| 马鞍山市| 郑州市|