Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当XMI无法正确加载容器时加载序列化的Ecore模型_Java_Deserialization_Emf_Eclipse Emf_Ecore - Fatal编程技术网

Java 当XMI无法正确加载容器时加载序列化的Ecore模型

Java 当XMI无法正确加载容器时加载序列化的Ecore模型,java,deserialization,emf,eclipse-emf,ecore,Java,Deserialization,Emf,Eclipse Emf,Ecore,我在内存中动态创建一个Ecore模型的实例,将其序列化为XMI,然后用户可以在我的应用程序运行时对其进行修改。因此,我随后对XMI进行反序列化,并将其与内存中的先前模型版本进行比较。 执行此操作时,模型中的包含引用似乎丢失了。假设底层Ecore模型是这样的:它有一个classState和一个classTransition,而Transition通过名为Transition的包含引用包含在State中,基数为0..*。因此,模型实例的简单序列化XMI可能如下所示: <?xml version

我在内存中动态创建一个Ecore模型的实例,将其序列化为XMI,然后用户可以在我的应用程序运行时对其进行修改。因此,我随后对XMI进行反序列化,并将其与内存中的先前模型版本进行比较。 执行此操作时,模型中的包含引用似乎丢失了。假设底层Ecore模型是这样的:它有一个class
State
和一个class
Transition
,而
Transition
通过名为
Transition
的包含引用包含在
State
中,基数为0..*。因此,模型实例的简单序列化XMI可能如下所示:

<?xml version="1.0" encoding="ASCII"?>
<xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:designmodel="http://www.example.org/designmodel">
  <designmodel:State xmi:id="0f359d4a-5154-462c-90aa-e125197cdb6d" name="Ready">
    <transition href="#7880aa8f-1e86-42e0-a212-e91326292d31"/>
  </designmodel:State>
  <designmodel:Transition xmi:id="7880aa8f-1e86-42e0-a212-e91326292d31" name="switchToWaiting"/>
</xmi:XMI>
ResourceSet savingResSet = new ResourceSetImpl();
savingResSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl());

XMIResource savingRes = (XMIResource) savingResSet.createResource(URI.createFileURI(outputPath), null);
savingRes.getDefaultSaveOptions().put(XMIResource.OPTION_KEEP_DEFAULT_CONTENT, Boolean.TRUE);


//adding the generated elements to the resource
generatedDesignmodelElements().forEach(e -> {
    if(e != null) {
        savingRes.getContents().add(e);
        savingRes.setID(e, UUID.randomUUID().toString());
    }
});

//saving it
try {
    savingRes.save(Collections.EMPTY_MAP);
} catch (IOException e) {
    e.printStackTrace();
ResourceSet resSet = new ResourceSetImpl();      
resSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl());
    XMIResource updatedModel =(XMIResource)resSet.getResource(URI.createFileURI(sourcePath), true);

//iterating over the objects directly contained in the resource (so all State- and Transition-objects get reached here, since they are root objects themselves)
updatedModel.getContents().forEach(updatedModelElement -> {
    System.out.println(updatedModelelement + updatedModelElement.eContainer()); //this is always NULL
});
反序列化如下所示:

<?xml version="1.0" encoding="ASCII"?>
<xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:designmodel="http://www.example.org/designmodel">
  <designmodel:State xmi:id="0f359d4a-5154-462c-90aa-e125197cdb6d" name="Ready">
    <transition href="#7880aa8f-1e86-42e0-a212-e91326292d31"/>
  </designmodel:State>
  <designmodel:Transition xmi:id="7880aa8f-1e86-42e0-a212-e91326292d31" name="switchToWaiting"/>
</xmi:XMI>
ResourceSet savingResSet = new ResourceSetImpl();
savingResSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl());

XMIResource savingRes = (XMIResource) savingResSet.createResource(URI.createFileURI(outputPath), null);
savingRes.getDefaultSaveOptions().put(XMIResource.OPTION_KEEP_DEFAULT_CONTENT, Boolean.TRUE);


//adding the generated elements to the resource
generatedDesignmodelElements().forEach(e -> {
    if(e != null) {
        savingRes.getContents().add(e);
        savingRes.setID(e, UUID.randomUUID().toString());
    }
});

//saving it
try {
    savingRes.save(Collections.EMPTY_MAP);
} catch (IOException e) {
    e.printStackTrace();
ResourceSet resSet = new ResourceSetImpl();      
resSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl());
    XMIResource updatedModel =(XMIResource)resSet.getResource(URI.createFileURI(sourcePath), true);

//iterating over the objects directly contained in the resource (so all State- and Transition-objects get reached here, since they are root objects themselves)
updatedModel.getContents().forEach(updatedModelElement -> {
    System.out.println(updatedModelelement + updatedModelElement.eContainer()); //this is always NULL
});
那么:为什么这个
ecocontainer()
-调用总是为我反序列化的XMIResource返回NULL,但在内存中的XMIResource上调用时行为正常

非常感谢:)

我自己解决了这个问题: 错误在于,我还单独添加了包含的EOObject(因此,除了它们之外,还通过容器对象隐式地添加到资源中)。
在将创建的EObject添加到资源之前,只需检查它是否有容器,然后在最后序列化即可解决所有问题。

很好,您用解决方案发回了!你也可以接受你自己的解决方案,这使得其他人更容易看到它解决了问题。