0
내가 EMF 모델에 대한 테스트 스위트를 개발하고 있어요에 모델 인스턴스의 자식 인스턴스를 만들 수 있습니다. 그래픽 편집기를 사용하여 만든 메타 모델 (.ecore 파일 (클래스 다이어그램))이 있습니다. 는 프로그래밍 방식으로 EMF
는 지금은 프로그래밍 동적 인스턴스를 만들 수 있어요,하지만 난이 포함 된 클래스 (프로그램)의 자식 인스턴스를 생성 할 것을 특징으로 내 메타 모델에 나는 조성 (봉쇄 참조)이있다.참조 할 수 있도록 아래 정보를 찾아주세요
클래스 다이어그램 :
의 JUnit 테스트 케이스 :
public class DynamicTest extends TestCase
{
public void testCreateModel() throws IOException {
ResourceSet rs = new ResourceSetImpl();
rs.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ecore",
new XMIResourceFactoryImpl());
Resource res = rs.createResource(URI.createFileURI(
"C:/Users/Manoj/Documents/FreshStart/Company/model/company.ecore"));
res.load(null);
EPackage metapackage = (EPackage)res.getContents().get(0);
System.out.println("meta Package "+metapackage.getName());
EFactory employeeFactoryInstance = metapackage.getEFactoryInstance();
EClass employeeClass = (EClass)metapackage.getEClassifier("Employee");
EObject employeeObject = employeeFactoryInstance.create(employeeClass);
EAttribute employeeName = employeeClass.getEAllAttributes().get(0);
EAttribute employeeManager = employeeClass.getEAllAttributes().get(1);
employeeObject.eSet(employeeName, "Manoj");
employeeObject.eSet(employeeManager, "Albert");
String empName = (String)employeeObject.eGet(employeeName);
String empManager = (String)employeeObject.eGet(employeeManager);
ResourceSet resourseSet = new ResourceSetImpl();
resourseSet.getPackageRegistry().put(metapackage.getNsURI(),
metapackage);
ResourseSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put
("*", new XMIResourceFactoryImpl());
Resource resource =
ResourseSet.createResource(URI.createURI("./model/Employee.xmi"));
resource.getContents().add(employeeObject);
Map options = new HashMap();
options.put(XMIResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
try
{
resource.save(options);
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
EPackage metapackage1 = (EPackage)res.getContents().get(0);
EFactory departmentFactoryInstance = metapackage1.getEFactoryInstance();
EClass departmentClass =
(EClass)metapackage1.getEClassifier("Department");
EObject departmentObject =
departmentFactoryInstance.create(departmentClass);
EAttribute departmentName = departmentClass.getEAllAttributes().get(0);
EAttribute departmentNumber =
departmentClass.getEAllAttributes().get(1);
EObject depRef = employeeClass.eContainmentFeature().eContents().get(0);
departmentObject.eSet(departmentName, "SMS");
departmentObject.eSet(departmentNumber, 101);
String depName = (String)departmentObject.eGet(departmentName);
Integer depNumber = (Integer)departmentObject.eGet(departmentNumber);
ResourceSet resSet = new ResourceSetImpl();
resSet.getPackageRegistry().put(metapackage1.getNsURI(), metapackage1);
resSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*",
new XMIResourceFactoryImpl());
Resource res1 =
resSet.createResource(URI.createURI("./model/Department.xmi"));
res1.getContents().add(departmentObject);
Map options1 = new HashMap();
options1.put(XMIResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
try
{
res1.save(options1);
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
이 친절하게 프로그래밍 부서 인스턴스에서 새 자식 인스턴스를 생성하는 나에게 도움이 .
은 당신의 코드가 이상하게 포맷됩니다. 그것은 읽기가 어렵습니다. 코드가 올바로 형식화되어 있으면 더 많은 사람들이 귀하의 문제를 기꺼이 보게 될 것입니다. – Lii