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