Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Serializing Ecore model
Serializing Ecore model [message #413646] Sun, 07 October 2007 10:01 Go to next message
Lorenzo Dalla Vecchia is currently offline Lorenzo Dalla VecchiaFriend
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 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
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 #413648 is a reply to message #413647] Sun, 07 October 2007 20:34 Go to previous messageGo to next message
Lorenzo Dalla Vecchia is currently offline Lorenzo Dalla VecchiaFriend
Messages: 58
Registered: July 2009
Member
>> 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.

Something is wrong then: they are both null.
Maybe the problem is the way I loaded the EPackage from the local metamodel
file.
I used the following code:

// Register factories
Map<String,ResourceFactoryImpl> extMap =
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( );
extMap.put("library", new EcoreResourceFactoryImpl());
extMap.put("*", new XMIResourceFactoryImpl());

// Load metamodel and get packages
Resource mmodel = new ResourceSetImpl().getResource(metamodelURI, true);
EList packages = mmodel.getContents();

The metamodelURI is set to a local path like the following:
file:/dir/dir/dir/model.ecore

--
Lorenzo
Re: Serializing Ecore model [message #413656 is a reply to message #413648] Mon, 08 October 2007 10:02 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33216
Registered: July 2009
Senior Member
Lorenzo,

Generally I would expect "ecore" to use EcoreResourceFactoryImpl and "*"
would be more appropriate for using XMIResourceFactoryImpl. Did you
actually set the nsURI and nsPrefix on the EPackage you created?


Lorenzo Dalla Vecchia wrote:
>>> 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.
>
> Something is wrong then: they are both null.
> Maybe the problem is the way I loaded the EPackage from the local
> metamodel file.
> I used the following code:
>
> // Register factories
> Map<String,ResourceFactoryImpl> extMap =
> Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( );
> extMap.put("library", new EcoreResourceFactoryImpl());
> extMap.put("*", new XMIResourceFactoryImpl());
>
> // Load metamodel and get packages
> Resource mmodel = new ResourceSetImpl().getResource(metamodelURI, true);
> EList packages = mmodel.getContents();
>
> The metamodelURI is set to a local path like the following:
> file:/dir/dir/dir/model.ecore
>
> --
> Lorenzo


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Serializing Ecore model [message #413692 is a reply to message #413656] Tue, 09 October 2007 21:07 Go to previous messageGo to next message
Tom D is currently offline Tom DFriend
Messages: 5
Registered: July 2009
Junior Member
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
Re: Serializing Ecore model [message #413699 is a reply to message #413692] Tue, 09 October 2007 22:20 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33216
Registered: July 2009
Senior Member
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/
Re: Serializing Ecore model [message #413772 is a reply to message #413699] Fri, 12 October 2007 10:17 Go to previous messageGo to next message
Tom is currently offline TomFriend
Messages: 22
Registered: July 2009
Junior Member
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
>>
Re: Serializing Ecore model [message #413773 is a reply to message #413772] Fri, 12 October 2007 10:36 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
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/
Re: Serializing Ecore model [message #414345 is a reply to message #413773] Thu, 01 November 2007 10:20 Go to previous message
Lei Zhang is currently offline Lei ZhangFriend
Messages: 24
Registered: July 2009
Junior Member
This is a multipart message in MIME format.
--=_alternative 003899AC48257386_=
Content-Type: text/plain; charset="US-ASCII"

Yes, saving your graph in ecore and then converting it using Ecore2OWL
converter is a feasible approach.

Lei Zhang
--=_alternative 003899AC48257386_=
Content-Type: text/html; charset="US-ASCII"


<br><font size=2 face="sans-serif">Yes, saving your graph in ecore and
then converting it using Ecore2OWL converter is a feasible approach.</font>
<br>
<br><font size=2 face="sans-serif">Lei Zhang</font>
--=_alternative 003899AC48257386_=--
Previous Topic:Using GEF as a Plug-in dependency
Next Topic:.ecore file not copied to /bin during build
Goto Forum:
  


Current Time: Fri Sep 20 13:28:30 GMT 2024

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

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

Back to the top