当前位置: 首页 > 知识库问答 >
问题:

如何用Java代码生成EMF模型

蔚弘量
2023-03-14

我想用Java代码生成EMF模型。例如,我想创建一个新的Ecore建模项目,然后构建一个简单的模型,就像很多教程(比如vogella教程)中看到的那样。但我不想用GUI手工完成。我想学习如何使用EMF-Ecore-API用Java代码创建模型。

我试着找关于这个话题的教程。但令我失望的是,我找不到太多关于这个话题的资料。我能找到的唯一东西是一些代码片段,用来按代码加载和修改现有模型。但没有关于创建新模型的内容。只是翻阅API对我没有帮助。

有关于这个话题的消息来源吗?如果不是,我如何通过java代码创建EMF模型?

共有1个答案

毋宸
2023-03-14

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,它的eAttribute名称类型为eString

现在,还可以将新的EPackage保存到.ecore文件中,如下所示:

    /*
     * 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 ePackage,反之亦然:

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

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

 类似资料:
  • 我正在使用Swagger codegen创建用于Spring REST服务器的Java模型,并且想知道如何让Swagger将每个模型声明为JPA实体。 我使用< code > swagger-codegen-maven-plugin 生成如下代码: 正如我现在所拥有的,这是生成的缩写 java 代码: 如何让 Swagger 添加 @Entity 和 @Id JPA 注释,如下所示? 这样,为了让

  • 注: 内容来自官网资料 Java Generated Code 这个页面准确描述 protocol buffer 编译器为任何给定协议定义生成的java代码。proto2和proto3生成的代码之间的任何不同都将被高亮 - 注意在这份文档中描述的是这些生成代码的不同,而不是基本的消息类/接口,后者在两个版本中是相同的。在阅读这份文档之前你应该先阅读 proto2语言指南 和/或 proto3语言指

  • 我正在尝试使用eclipse gmf创建一个基于uml模型的图形编辑器, 为此,我基于我的uml模型创建了一个.gnemodel文件,但在生成的.ecore文件中没有关联

  • 问题内容: 我专门在寻找JPA代码生成技术 首先,哪些项目可以生成符合JPA的代码?(例如HibernateTools) 其次,我还想自定义代码生成实用程序,因为它必须符合我们的企业标准。 如果没有,使用反射生成Java代码的所有可用框架有哪些?所以我可以从头开始写。 注意:我使用eclipse生成JPA代码,并反复对其进行重构以使其兼容。 问题答案: 我也很难理解这个问题,但我将尝试改写: 您在

  • 本文向大家介绍如何用一行代码生成[1,4,9,16,25,36,49,64,81,100]相关面试题,主要包含被问及如何用一行代码生成[1,4,9,16,25,36,49,64,81,100]时的应答技巧和注意事项,需要的朋友参考一下  

  • 我正在努力创建rest客户端,我将调用一个API来提供这个大的json输出。我想知道如何通过输入这个json来自动创建Pojo类来晃动代码gen,并让它为我创建我的pojo类,这将节省手动时间。这是我尝试过的 要为生成PHP客户端,请执行以下操作:http://petstore.swagger.io/v2/swagger.json,请运行以下命令: (如果您使用的是Windows,请将最后一个命令