2017-10-11 92 views
0

我正在開發EMF模型的測試套件。我有一個使用圖形編輯器創建的元模型(.ecore文件(類圖))。如何以編程方式在EMF中創建模型實例的子實例

現在我能夠以編程方式創建動態實例,但在我的元模型中我有一個組合(Containment引用),其中我想創建包含的類的子實例(以編程方式)。

請找到以下資料僅供參考

類圖:

metamodel

的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(); 
    }   
    } 
} 

請幫助我從一個部門實例中創建新的子實例編程。

+0

您的代碼格式不正確。這使它很難閱讀。如果代碼格式正確,更多人會願意查看您的問題。 – Lii

回答

2

通過首先獲取作爲特徵值的列表,可以將對象添加到多值特徵中。然後你將一個元素添加到該列表中。

例子:

EStructuralFeature employeeFeature = departmentClass.getEStructuralFeature("employee"); 
@SuppressWarnings("unchecked") // Safe cast as long as only Employees are added 
List<EObject> employees = (List<EObject>) departmentObject.eGet(employeeFeature); 
employees.add(employeeObject); 

有關代碼的幾個注意事項:

  • 這是更好地使用EClass.getEStructuralFeature以獲得正確的功能。這樣,如果功能的順序發生改變,代碼不會中斷。
  • employeeObject如果包含在departmentObject中,則不應將其添加到資源中。只有頂級對象(不包含任何其他對象)應添加到資源中。
相關問題