如何使用Java代码生成EMF模型

时间:2016-10-23 11:07:08

标签: java eclipse eclipse-emf eclipse-emf-ecore

我想用Java代码生成EMF模型。例如,我想创建一个新的Ecore建模项目,并构建一个简单的模型,如许多教程中所见(即像vogella tutorial)。但我不想手工使用GUI来做到这一点。我想学习如何使用EMF-Ecore-API创建一个包含Java代码的模型。

我试图找到有关此主题的教程。但令我失望的是,我对这个话题找不到多少。我唯一能找到的是code snippets to load and modify existing models by code。但没有关于创建新模型。只是查看API并没有帮助我。

这个主题是否有任何来源?如果没有,我如何通过java代码创建EMF模型?

1 个答案:

答案 0 :(得分:2)

IBM有一个关于动态EMF的主题。

但是,如果你熟悉emf中的创作方式,那就非常简单了。每个EPackage都有自己的EFactoryEPackage实例,它们处理创建(工厂)或存储有关元模型本身(epackage)的信息。

Ecore有自己的EPackageEFactory,所以我们可以像这样动态创建新的元模型:

    /*Use the Ecore factory*/
    EcoreFactory ecoreFactory = EcoreFactory.eINSTANCE;
    /*Create your EPackage*/
    EPackage myPackage = ecoreFactory.createEPackage();
    /*set EPackage properties*/
    myPackage.setName("myTest");
    myPackage.setNsPrefix("myTest");
    myPackage.setNsURI("http://com.myTest");

    /*Create your first EClass*/
    EClass myFirstEClass = ecoreFactory.createEClass();
    myFirstEClass.setName("myClass");
    /*Add to your EPackage's EClassifiers*/
    /*EClasses and EDatatypes implement both EClassifiers*/
    myPackage.getEClassifiers().add(myFirstEClass);

    /*Create your first EAtttribute*/
    EAttribute myFirstEAtt = ecoreFactory.createEAttribute();
    myFirstEAtt.setName("name");
    /*Use the EcorePackage Datatypes -> here EString*/
    myFirstEAtt.setEType(EcorePackage.eINSTANCE.getEString());
    /*use EStructuralFeatures to add your EAtt*/
    /*EReferences and EAttributes are both EStructuralfeatures*/
    myFirstEClass.getEStructuralFeatures().add(myFirstEAtt);

更新

    /*Create your second EClass*/
    EClass mySecondEClass = ecoreFactory.createEClass();
    mySecondEClass.setName("mySecondClass");
    myPackage.getEClassifiers().add(mySecondEClass);

    /*now, the firstClass should hold instances of secondClass*/
    /*1. create EReference (Ereferences unlike EAttributes define relationships between EClasses)*/
    EReference secondClassesRef = ecoreFactory.createEReference();
    secondClassesRef.setName("secondClasses");
    /*set containment true -> every EObject must have a Container*/
    secondClassesRef.setContainment(true);
    /*set Type to your EClass*/
    secondClassesRef.setEType(mySecondEClass);
    /*set upperbound -> now the reference is an EList*/
    secondClassesRef.setUpperBound(ETypedElement.UNBOUNDED_MULTIPLICITY);

    /*finally add ERef to EClass*/
    myFirstEClass.getEStructuralFeatures().add(secondClassesRef);

    /*and for example supertypes*/
    myFirstEClass.getESuperTypes().add(mySecondEClass);

现在,您拥有自己的EPackage,其新EClass的{​​{1}}名称类型为EAttribute

现在也可以将新的EPackage保存到这样的EString文件中:

.ecore

反之亦然,如果您想加载现有的ecore EPackage:

    /*
     * Save your EPackage to file ecore file:
     */

    /*Initialize your EPackage*/
    myPackage.eClass();
    Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
    Map<String, Object> m = reg.getExtensionToFactoryMap();
    /*add default .ecore extension for ecore file*/
    m.put(EcorePackage.eNAME, new XMIResourceFactoryImpl());

    // Obtain a new resource set
    ResourceSet resSet = new ResourceSetImpl();
    // create a resource
    Resource resource = null;
    try {
        resource = resSet.createResource(URI.createFileURI("/Your/Path/To/Directory/myTest.ecore"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    /*add your EPackage as root, everything is hierarchical included in this first node*/
    resource.getContents().add(myPackage);

    // now save the content.
    try {
        resource.save(Collections.EMPTY_MAP);
    } catch (IOException e) {
        e.printStackTrace();
    }

更新: Ecore当然有自己的元模型。在Docs中,您可以很好地了解生态系统架构。

因此,如果您想使用动态EMF,您需要了解这一点。正如我向您展示的那样,动态创建 /* * load existing EPackage */ EcorePackage.eINSTANCE.eClass(); /*Initialize your EPackage*/ final Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; final Map<String, Object> m = reg.getExtensionToFactoryMap(); m.put(EcorePackage.eNAME, new XMIResourceFactoryImpl()); final ResourceSet resSet = new ResourceSetImpl(); Resource resource = null; try { resource = resSet.getResource(URI.createFileURI("/Your/Path/To/Directory/myTest.ecore"), true); } catch (Exception e) { e.printStackTrace(); } /*load root and cast to EPackage*/ final EPackage root = (EPackage) resource.getContents().get(0); 非常简单,但您需要知道如何设置ecore模型的基本属性(EClass,EAttributes,EReferences,EType,Contament,supertypes ......) 。一旦你理解了架构,那就很容易了。看一下页面底部的UML-Diagram。

我还更新了上面的代码,向您展示如何初始化EClasses之间的关系