从代码创建和修改ecore文件及其实例 [英] Create and modify ecore files and their instances from code

查看:135
本文介绍了从代码创建和修改ecore文件及其实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题有两个部分:


1)如何创建和/或修改然后存储EMF ecore文件(ecore元模型我的scala / java代码中有.ecore后缀)



2)如何创建和/或修改ecore文件的实例(即符合我的scala / java代码中的生态元模型?


我正在寻找一些可能的方式来做这些,使用XML API直接操作他们对应的XML文件。



提供代码或其引用的引用非常感谢。



ps。作为背景思想,我想知道是否可以使用单个API来执行上述任务,我们可以查看一个ecore文件作为Ecore.ecore的模型/实例。

解决方案

基本概念(资源,资源集,资源工厂和注册表):



在回答这个问题之前,我将解释一下ecore API中的一些概念。前两个概念是资源 ResourceSet 。资源是持久资源(如ecore文件)的程序级表示,ResourceSet只是一组这些资源。每个ecore元模型文档以及模型文档(符合其元模型)都是一种资源。因此,使用这些文件的第一步是将它们的程序级表示作为资源集中的资源。



另外两个概念是资源工厂注册表。工厂类用于生成资源,注册管理机构正在跟踪资源集中这些工厂的列表。根据我们的资源存储方式,我们可以使用不同的工厂来操纵它们。例如,EcoreResourceFactoryImpl ,XMLResourceFactoryImpl 和 XMIResourceFactoryImpl 是工厂实现的一些示例,可以分别用于处理 ecore xml xmi 文件。如果我们想使用这些工厂来处理资源集中的资源,我们需要先把它们放在resourceSet的注册表列表中。所以,对于我上面提到的每个resourceSet,都有一个注册表列表。



所有上述都说,我们来看看如何加载和修改ecore文件(元模型)和一个实例文件(模型)发生在一个java代码中。 >

首先,我们需要创建一个resourceSet来表示我们想要处理的持久资源:

 ResourceSet resourceSet = new ResourceSetImpl(); 

然后在该资源集的注册表中,我们需要注册我们想要使用的工厂: / p>

  resourceSet.getResourceFactoryRegistry()。getExtensionToFactoryMap()。put(ecore,new EcoreResourceFactoryImpl()); 
resourceSet.getResourceFactoryRegistry()。getExtensionToFactoryMap()。put(xmi,new XMIResourceFactoryImpl());

上面两行代码只需注册EcoreResourceFactoryImpl 和 XMIResourceFactoryImpl 分别为ecore和xmi文件工厂(请注意,emore 和 xmi 是文件扩展名)。我假设我的元模型文件扩展名是ecore,我的模型文件扩展名是xmi。



注册这些工厂后,我们现在可以要求我们的ResourceSet加载元元(即, ecore)文件如下:

 资源myMetaModel = resourceSet.getResource(URI.createFileURI(./ univ.ecore),真正); 

univ.ecore是我的ecore文件的名称。



要加载模型文件,我们需要再走一步!我们需要先在我们的资源集中注册一件事。那就是在我们的资源集中的包的注册表列表中注册我们的ecore元模型的包。为了做到这一点,我们需要首先得到我们的ecore软件包的编程级别表示如下:

  EPackage univEPackage =(EPackage)myMetaModel .getContents()得到(0); 

然后,在包的注册表列表: / p>

  resourceSet.getPackageRegistry()。put(http://gholizadeh.org,univEPackage); 

我们现在可以加载我们的模型(xmi文件)。我们使用以下代码:

 资源myModel = resourceSet.getResource(URI.createURI(./univModel.xmi ),true); 

现在我们将我们的元模型和模型文件都带到编程层面,我们可以简单的操纵他们在代码中。



更改元模型:



例如,创建一个新的类一个ecore文件,我们使用 EcoreFactory API:我们首先获取下面这个类的一个实例:

  EcoreFactory theCoreFactory = EcoreFactory.eINSTANCE; 

然后创建一个EClass如下:



EClass adultEClass = theCoreFactory.createEClass();

  

然后为了保持此类,我们需要将其添加到我们加载的ecore包分类器的列表中bellow:

  univEPackage.getEClassifiers()。add(adultEClass); 

为了进行更改,您需要更熟悉 ecore API



更改型号:



为了更改模型,我们需要创建类型为EObject的对象。与上述EcoreFactory类似,我们需要一个工厂来做到这一点。但是,而不是EcoreFactory,我们需要一个对象工厂。对于每个ecore包,都有一个特定的对象工厂,类型为 EFactory ,我们可以得到如下:

  EFactory univInstance = univEPackage.getEFactoryInstance ();请注意,上述代码中的univEPackage代表了我们的ecore软件包(请参见上面的一些段落)



<在这样做之后,我们准备为我们的模型创建对象。例如

  EObject adultObject = univInstance.create(adultEClass); 

在我们的模型中创建一个类型为adultEClass的对象。请注意,为了持久化这个新创建的对象,我们需要将其添加到资源的内容(代表我们的模型,即myModel)。由于我们的持久性文件是xmi格式,它只有一个根,我们需要把所有的对象放在列表中,并将这个列表添加到我们的资源中:

 的EList< EObject> ModelObjects = new BasicEList< EObject>(); 
ModelObjects.add(adultObject);

myModel.getContents()。addAll(ModelObjects);

存储模型和元模型文件:



最后,在我们修改了元模型和模型元素后,我们需要将它们再次存储在相应的文件中。这通过调用其相应资源的save方法完成:

  myModel.save(null); 

myMetaodel.save(null);


My question has two parts:

1) How can I create and/or modify and then store EMF ecore files (ecore metamodels with .ecore suffixes) from my scala/java code?

2) How can I create and/or modify an instance of an ecore file (i.e. a model conforming to an ecore metamodel) from my scala/java code?

I am looking to see if there are some possible ways of doing these, other that manipulating directly their corresponding XML files using XML API's.

Providing a code spinet or a reference to it is very much appreciated.

ps. As a background thought, I am wondering if I can use a single API for performing both of the above tasks, as we can look to an ecore file as a model/instance of Ecore.ecore.

解决方案

Basic Concepts (Resource, ResourceSet, Resource Factory and Registry):

Before answering this question I will explain some concepts in ecore API. The First two concepts are Resource and ResourceSet. Resource is a program level representation of a persistent resource (like an ecore file), and ResourceSet is simply a set of these kind of resources. Each ecore metamodel document as well as a model document (which conforms to its metamodel) is a resource. Therefore the first step to work with these files is to provide a program level representation of them as resources in a resourceSet.

Another two concepts are Resource Factory and Registry. Factory classes are used to generate resources, and registries are keeping track of list of these factories in resourceSets. Depending on how our resource are stored, we can use different factories to manipulate them. For example, EcoreResourceFactoryImpl, XMLResourceFactoryImpl, and XMIResourceFactoryImpl are some examples of factory implementations that can be used to handle, respectively, ecore, xml, and xmi files. If we want to use these factories to handle resources in a resourceSet we need to put them in the registry list of resourceSet first. So, for each resourceSet that I mentioned above, there is a registry list.

With all the above being said, let's see how loading and modifying an ecore file (metamodel) and an instance file (model) happens in a java code.

First, We need to create a resourceSet to represent our persistent resources we would like to work with:

ResourceSet resourceSet = new ResourceSetImpl(); 

Then in the registry of this resourceSet, we need to register the Factories we want to work with:

resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ecore", new EcoreResourceFactoryImpl());
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", new  XMIResourceFactoryImpl());

Above two lines of code simply register EcoreResourceFactoryImpl and XMIResourceFactoryImpl as, respectively, ecore and xmi file factories (note that ecore and xmi are file extensions there). I assumed that my metamodel file extension is ecore and my model file extension is xmi.

After registering these Factories, we can now ask our ResourceSet to load our metamode (i.e., ecore) file as below:

Resource myMetaModel= resourceSet.getResource(URI.createFileURI("./univ.ecore"), true);

univ.ecore is the name of my ecore file.

For loading a model file we need to take one further step! We need to first register one more thing in the our resourceSet. That is to register the package of our ecore metamodel in the registry list of packages in our resource set. For doing this we need to first get a programming level representation of our ecore package as bellow:

EPackage univEPackage = (EPackage) myMetaModel.getContents().get(0);

And then, register this package in the registry list of packages of our resource set as below:

resourceSet.getPackageRegistry().put("http://gholizadeh.org", univEPackage);

We are now ready to load our model (xmi file). we use the following code for this:

Resource myModel = resourceSet.getResource( URI.createURI( "./univModel.xmi"), true);

Now we have brought both of our metamode and model files to the programming level, and we can simply manipulate them in code.

Change the Metamodel:

for example, for creating a new Class in an ecore file, we use EcoreFactory API: we first obtain an instance of this class as below:

EcoreFactory theCoreFactory = EcoreFactory.eINSTANCE;

and then create an EClass as the following:

EClass adultEClass= theCoreFactory.createEClass();

Then for keeping this Class, we need to add it to the list of our loaded ecore package classifiers as bellow:

univEPackage.getEClassifiers().add(adultEClass);

For doing aditional changes you need to get more familiar with ecore API.

Change the Model:

for changing a model, we need to create objects of type EObject. Similar to EcoreFactory in the above, we need a factory to do this. But instead of EcoreFactory, we need an object factory. For each ecore package there is an specific object factory of type EFactory that we can get as the following:

EFactory univInstance = univEPackage.getEFactoryInstance();

Note that univEPackage in above code, represents our ecore package (see some paragraphs above). After doing this, we are ready to create objects for our model. For example

EObject adultObject = univInstance.create(adultEClass);

create an object of type adultEClass, in our model. Note that for persisting this newly created object we need to add it to the content of our resource (that represent our model, i.e., myModel). Since our persistent file is in xmi format and it has only one root we need to put all of our objects in a list and add this list to our resource:

EList<EObject> ModelObjects = new BasicEList<EObject>(); 
ModelObjects.add(adultObject);

myModel.getContents().addAll(ModelObjects);

Storing Model and Metamodel files:

Finally, after we modified our metamodel and model elements we need to store them again in their corresponding files. This is simply done by calling save method of their corresponding Resources:

myModel.save(null);

myMetaodel.save(null);

这篇关于从代码创建和修改ecore文件及其实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆