Home » Modeling » EMF » Serializing Ecore model
Serializing Ecore model [message #413646] |
Sun, 07 October 2007 10:01 |
Lorenzo Dalla Vecchia Messages: 58 Registered: July 2009 |
Member |
|
|
Hi everyone.
With the help of this ng and refering to this article on devx
(http://www.devx.com/Java/Article/29093/0/page/3), I succeeded in doing the
following:
- loading an Ecore MetaModel (modeling level 2) obtaining its Java objects
representation
- using the above metamodel to programmatically create a conforming instance
Model (modeling level 1) using classes like EObject, EClass, etc.
As a last step, I know need to serialize the model into an Ecore file.
I'm using the following code, partly inspired by the devx article:
public void saveModel(String path, EObject rootElement)
throws IOException
{
// Prepare ResourceSet and needed factories
ResourceSet resourceSet = new ResourceSetImpl();
Map<String,ResourceFactoryImpl> extMap =
resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap();
extMap.put("*", new XMIResourceFactoryImpl());
// Register necessary packages
resourceSet.getPackageRegistry().put(myPackage1.getNsURI(),
myPackage1);
resourceSet.getPackageRegistry().put(myPackage2.getNsURI(),
myPackage2);
// Create the resource
Resource resource =
resourceSet.createResource(URI.createFileURI(path));
List<EObject> objectList = resource.getContents();
objectList.add(rootElement);
// Serialize model
resource.save(null);
}
The two parameters of the above method are the path of a local file to save
to and the EObject that is the root of my model, instantiated
programmatically.
The resulting Ecore files seems ok, but with a little flaw: it does not set
a default namespace (xmlns) in the root element, and therefore the element
names can't be correctly mapped to the Ecore file of the metamodel.
How can I fix that? I already tried using an EcoreResourceFactoryImpl
instead of XMIResourceFactoryImpl without any difference in the generated
file. Other ideas?
Thank you.
--
Lorenzo
|
|
|
Re: Serializing Ecore model [message #413647 is a reply to message #413646] |
Sun, 07 October 2007 11:36 |
Ed Merks Messages: 33216 Registered: July 2009 |
Senior Member |
|
|
Lorenzo,
Comments below.
Lorenzo Dalla Vecchia wrote:
> Hi everyone.
>
> With the help of this ng and refering to this article on devx
> (http://www.devx.com/Java/Article/29093/0/page/3), I succeeded in
> doing the following:
>
> - loading an Ecore MetaModel (modeling level 2) obtaining its Java
> objects representation
> - using the above metamodel to programmatically create a conforming
> instance Model (modeling level 1) using classes like EObject, EClass,
> etc.
>
> As a last step, I know need to serialize the model into an Ecore file.
Typically I would refer a .ecore file containing an EPackage as a Ecore
file.
> I'm using the following code, partly inspired by the devx article:
>
> public void saveModel(String path, EObject rootElement)
> throws IOException
> {
> // Prepare ResourceSet and needed factories
> ResourceSet resourceSet = new ResourceSetImpl();
> Map<String,ResourceFactoryImpl> extMap =
> resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap();
> extMap.put("*", new XMIResourceFactoryImpl());
>
> // Register necessary packages
> resourceSet.getPackageRegistry().put(myPackage1.getNsURI(),
> myPackage1);
> resourceSet.getPackageRegistry().put(myPackage2.getNsURI(),
> myPackage2);
>
> // Create the resource
> Resource resource =
> resourceSet.createResource(URI.createFileURI(path));
It's often a good idea to use new File(path).getAbsolutePath() or
getCanonicalPath() to ensure you have a fully qualified path name.
> List<EObject> objectList = resource.getContents();
> objectList.add(rootElement);
>
> // Serialize model
> resource.save(null);
> }
>
> The two parameters of the above method are the path of a local file to
> save to and the EObject that is the root of my model, instantiated
> programmatically.
>
> The resulting Ecore files seems ok, but with a little flaw: it does
> not set a default namespace (xmlns) in the root element, and therefore
> the element names can't be correctly mapped to the Ecore file of the
> metamodel.
What's the value of rootElement.eClass().getEPackage().getNsURI() and
getNsPrefix(). Both should generally be non-null and the former should
be universally unique.
> How can I fix that? I already tried using an EcoreResourceFactoryImpl
> instead of XMIResourceFactoryImpl without any difference in the
> generated file. Other ideas?
It sounds like you've either not specified the namespace URI or the
namespace prefix of the package appropriately.
>
> Thank you.
>
> --
> Lorenzo
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| | | | | |
Re: Serializing Ecore model [message #413773 is a reply to message #413772] |
Fri, 12 October 2007 10:36 |
Ed Merks Messages: 33216 Registered: July 2009 |
Senior Member |
|
|
Tom,
It's best to ask about it on the EODM newsgroup, which I've added to the
"to" list of the reply.
Tom wrote:
> Thanks so much. I really don't need 2-way conversion. I would just
> like to have my ".model" files converted to OWL so that the diagrams
> created from within the gmf editor can be stored in an OWL db. I tried
> to use EODM Ecore2OWL, but havent found a way to turn my models into
> actual ecore documents for that. I appreciate your help!
>
> Thanks,
> Tom
>
> Ed Merks wrote:
>
>> Tom,
>
>> Unless there is a two way converter I'm not sure how saving Ecore as
>> OWL will be reversible...
>
>
>> Tom wrote:
>>> Should I use the same approach to serialize to OWL? However, there
>>> is no
> OWLResourceFactoryImpl extant. I have seen ecore2owl, so instead I
> could save to ecore then run that converter. What would be the
> preferred method, especially within the context of a GMF project,
> saving graph instances to OWL?
>>>
>>> Thanks!
>>> Tom
>>>
>
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| |
Goto Forum:
Current Time: Fri Sep 20 13:28:30 GMT 2024
Powered by FUDForum. Page generated in 0.04848 seconds
|