Skip to main content



      Home
Home » Modeling » EMF » Serialize ecore metamodel without eType prefix
Serialize ecore metamodel without eType prefix [message #1754792] Wed, 22 February 2017 08:03 Go to next message
Eclipse UserFriend
Hello,

I am trying to programmatically load ecore metamodels, change their structure and then write the new versions back to disk.

I have the following code to load and save a metamodel:

	def loadMetamodel(){
		
		Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put( 
				"ecore", new EcoreResourceFactoryImpl()); 

		this.resourceSet = new ResourceSetImpl();
		
		var resource = resourceSet.getResource(this.metamodelPath, true);
		var eObject = resource.getContents().get(0) as EPackage;
		
		EPackage.Registry.INSTANCE.put(eObject.getNsURI(), eObject);

		if (eObject instanceof EPackage) {
			this.metamodel = eObject as EPackage;
		}		
	}
	
	def saveMetamodel(String path){
		var resource = resourceSet.createResource(URI.createURI(path))
	
		resource.getContents().add(this.metamodel)
	
		var options = new HashMap<String, Boolean>();
		options.put(XMIResource.OPTION_SCHEMA_LOCATION, true);	
		
		try {
			var ostream = new FileOutputStream(new File(path));
			resource.save(ostream, options);
			
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}


The problem that I have is the following:

1. Loading an ecore file in this format:

  <eClassifiers xsi:type="ecore:EClass" name="Package" eSuperTypes="#//NamedElement">
    <eStructuralFeatures xsi:type="ecore:EReference" name="classes" ordered="false"
        upperBound="-1" eType="#//Class" containment="true"/>
  </eClassifiers>


2. Saving it, it gets serialized to this format (notice the different eType prefix):

  <eClassifiers xsi:type="ecore:EClass" name="Package" eSuperTypes="#//NamedElement">
    <eStructuralFeatures xsi:type="ecore:EReference" name="classes" ordered="false"
        upperBound="-1" eType="ecore:EClass metamodel.ecore#//Class" containment="true"/>
  </eClassifiers>



How should I do the serialization so that there is no eType prefix with the filename included.

Thanks.

[Updated on: Wed, 22 February 2017 10:08] by Moderator

Re: Serialize ecore metamodel without eType prefix [message #1754819 is a reply to message #1754792] Wed, 22 February 2017 12:38 Go to previous message
Eclipse UserFriend
I wonder what value does this.metamodelPath have? And what value does path in the save method? Note that the save method is removing the package from its containing resource and moving it to a different resource. Perhaps using this.metamodel.eResource().save(...) would be better. In the end, I expect such a serialization difference is caused by the fact that path is not an absolute hierarchical URI (with a scheme and an absolute path) so that references can't be made properly relative.
Previous Topic:[Xcore][gradle] Two mysterious problems.
Next Topic:error in opening xmi file
Goto Forum:
  


Current Time: Fri Oct 31 18:26:11 EDT 2025

Powered by FUDForum. Page generated in 0.05112 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top