Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » is EMF right solution for me? - Overriding load / save format
is EMF right solution for me? - Overriding load / save format [message #431842] Tue, 28 July 2009 04:58 Go to next message
Eclipse UserFriend
Originally posted by: switzer.jeff+eclipse.gmail.com

Hello. Sorry for the newbie question.

I've been re-writing an old C++ program of mine as an RCP app. I had a
basic JavaBeans model working that would load / save my model, but when
working on the GUI, I realized I should have a stronger framework/model
to work with, so I've been looking into EMF.

I have a few questions:

For this application, I've reversed engineered a file format (for a
hardware sampler). I want to be able to make changes to the model, and
save the resource in a format that the sampler can read. This means (at
a high level) I cannot load/save resources in XMI format (they need to
be some format that I *partially* know about). For instance, the way I
currently manipulate the files, is by reading the original ~2MB file
into a byte[]. I then load in the objects that I've reversed engineered
(and know the structure of) from the file. When I save the resource, I
write the original byte[] back to disk, then overwrite certain *known*
portions of the data with my manipulated Objects.
If you're still with me, my question is, what's the best way to go about
overriding loading/saving of resources in EMF? I've looked at code like:
resource = editingDomain.getResourceSet().getResource(resourceURI, true);
And the generated test Example, with lines like:
Resource resource =
resourceSet.createResource(URI.createURI("http:///My.esxfile"));
EsxFile root = EsxFileFactory.eINSTANCE.createEsxFile();
resource.getContents().add(root);
resource.save(System.out, null);

My unfamiliarity with EMF/Java standards (Factories / Adapters), is
making it hard for me to grasp where/how I should start with overriding
the default load/save behavior.

Also I'm just wondering if this a common problem, and if you think EMF
is a good solution for me to use?

I've included a link to my initial ecore model. Once I figure out how
to load/save correctly, I will continue to tweak it:
http://skratchdot.com/eclipse/EsxFile.ecore

Thanks for any help you can provide!

-Jeff
Re: is EMF right solution for me? - Overriding load / save format [message #431859 is a reply to message #431842] Tue, 28 July 2009 10:39 Go to previous messageGo to next message
Mario Winterer is currently offline Mario WintererFriend
Messages: 136
Registered: July 2009
Senior Member
Hi Jeff,

see my comments below!

J. Switzer schrieb:
> Hello. Sorry for the newbie question.
>
> I've been re-writing an old C++ program of mine as an RCP app. I had a
> basic JavaBeans model working that would load / save my model, but when
> working on the GUI, I realized I should have a stronger framework/model
> to work with, so I've been looking into EMF.
>
> I have a few questions:
>
> For this application, I've reversed engineered a file format (for a
> hardware sampler). I want to be able to make changes to the model, and
> save the resource in a format that the sampler can read. This means (at
> a high level) I cannot load/save resources in XMI format (they need to
> be some format that I *partially* know about). For instance, the way I
> currently manipulate the files, is by reading the original ~2MB file
> into a byte[]. I then load in the objects that I've reversed engineered
> (and know the structure of) from the file. When I save the resource, I
> write the original byte[] back to disk, then overwrite certain *known*
> portions of the data with my manipulated Objects.
> If you're still with me, my question is, what's the best way to go about
> overriding loading/saving of resources in EMF? I've looked at code like:
> resource = editingDomain.getResourceSet().getResource(resourceURI,
> true);
> And the generated test Example, with lines like:
> Resource resource =
> resourceSet.createResource(URI.createURI("http:///My.esxfile"));
> EsxFile root = EsxFileFactory.eINSTANCE.createEsxFile();
> resource.getContents().add(root);
> resource.save(System.out, null);
>
> My unfamiliarity with EMF/Java standards (Factories / Adapters), is
> making it hard for me to grasp where/how I should start with overriding
> the default load/save behavior.

The correct way to customize loading and saving is to write your own
Resource and ResourceFactory implementation and register the custom
ResourceFactory for the preferred file extension.

Create a .genmodel file based on your Ecore model, select the
EsxFile-Package in the editor and open the Properties View.
+ Set All->Base Package (e.g. to com.skratchdot)
+ Set Model->Resource Type to "Basic" (that will generate a basic
Resource and ResourceFactory)
Generate the code.

In com.skratchdot.EsxFile.util you can find EsxFileResourceFactoryImpl
and EsxFileResourceImpl.
(1) Implement EsxFileResourceImpl#doSave(OutputStream, Map) and
EsxFileResourceImpl#doLoad(OutputStream, Map) so that the contents of
the resource gets saved/loaded.
(2) Register the EsxFileResourceFactoryImpl as resource factory for your
file extension:
<extension point="org.eclipse.emf.ecore.extension_parser">
<parser
class="com.skratchdot.EsxFile.util.EsxFileResourceFactoryImpl "
type="esxfile">
</parser>
</extension>

Now all URIs that ends in .esxfile will be handled by your
Resource/ResourceFactory:
Resource resource =
resourceSet.getResource(URI.createURI("http:///My.esxfile"), true);


> Also I'm just wondering if this a common problem, and if you think EMF
> is a good solution for me to use?

It is a common problem and there are many resource-implementations out
there (e.g. XMLResourceImpl, XMIResourceImpl, BinaryResourceImpl,
CDOResourceImpl, HibernateResource etc.). I cannot see a reason why you
could not use EMF for your purpose. Even if you don't use the entire EMF
framework you could still use the code generator to get high-quality
JavaBean like data classes with a fine-grained notification mechanism
for free...

> I've included a link to my initial ecore model. Once I figure out how
> to load/save correctly, I will continue to tweak it:
> http://skratchdot.com/eclipse/EsxFile.ecore

I've downloaded the model (I hope you don't mind) and generated model
code. My suggestion: lowercase the package name and carefully design
your containment references! Every object inside a single resource
should be reachable by one single containment reference!

Mario

> Thanks for any help you can provide!
>
> -Jeff
Re: is EMF right solution for me? - Overriding load / save format [message #431861 is a reply to message #431859] Tue, 28 July 2009 12:13 Go to previous messageGo to next message
Jeff S. is currently offline Jeff S.Friend
Messages: 29
Registered: July 2009
Junior Member
Mario-

Thanks alot! That's a huge help. See inline comments.

Mario Winterer wrote:
> Hi Jeff,
>
> see my comments below!
>
> J. Switzer schrieb:
>> Hello. Sorry for the newbie question.
>>
>> I've been re-writing an old C++ program of mine as an RCP app. I had
>> a basic JavaBeans model working that would load / save my model, but
>> when working on the GUI, I realized I should have a stronger
>> framework/model to work with, so I've been looking into EMF.
>>
>> I have a few questions:
>>
>> For this application, I've reversed engineered a file format (for a
>> hardware sampler). I want to be able to make changes to the model,
>> and save the resource in a format that the sampler can read. This
>> means (at a high level) I cannot load/save resources in XMI format
>> (they need to be some format that I *partially* know about). For
>> instance, the way I currently manipulate the files, is by reading the
>> original ~2MB file into a byte[]. I then load in the objects that
>> I've reversed engineered (and know the structure of) from the file.
>> When I save the resource, I write the original byte[] back to disk,
>> then overwrite certain *known* portions of the data with my
>> manipulated Objects.
>> If you're still with me, my question is, what's the best way to go
>> about overriding loading/saving of resources in EMF? I've looked at
>> code like:
>> resource = editingDomain.getResourceSet().getResource(resourceURI,
>> true);
>> And the generated test Example, with lines like:
>> Resource resource =
>> resourceSet.createResource(URI.createURI("http:///My.esxfile"));
>> EsxFile root = EsxFileFactory.eINSTANCE.createEsxFile();
>> resource.getContents().add(root);
>> resource.save(System.out, null);
>>
>> My unfamiliarity with EMF/Java standards (Factories / Adapters), is
>> making it hard for me to grasp where/how I should start with
>> overriding the default load/save behavior.
>
> The correct way to customize loading and saving is to write your own
> Resource and ResourceFactory implementation and register the custom
> ResourceFactory for the preferred file extension.
>
> Create a .genmodel file based on your Ecore model, select the
> EsxFile-Package in the editor and open the Properties View.
> + Set All->Base Package (e.g. to com.skratchdot)
> + Set Model->Resource Type to "Basic" (that will generate a basic
> Resource and ResourceFactory)
> Generate the code.
>

This is a step I was definitely missing. I wouldn't have know to set
Package->Model->Resource Type to "Basic". Thanks alot for that!

> In com.skratchdot.EsxFile.util you can find EsxFileResourceFactoryImpl
> and EsxFileResourceImpl.
> (1) Implement EsxFileResourceImpl#doSave(OutputStream, Map) and
> EsxFileResourceImpl#doLoad(OutputStream, Map) so that the contents of
> the resource gets saved/loaded.
> (2) Register the EsxFileResourceFactoryImpl as resource factory for your
> file extension:
> <extension point="org.eclipse.emf.ecore.extension_parser">
> <parser
> class="com.skratchdot.EsxFile.util.EsxFileResourceFactoryImpl "
> type="esxfile">
> </parser>
> </extension>
>
> Now all URIs that ends in .esxfile will be handled by your
> Resource/ResourceFactory:
> Resource resource =
> resourceSet.getResource(URI.createURI("http:///My.esxfile"), true);
>
>
>> Also I'm just wondering if this a common problem, and if you think EMF
>> is a good solution for me to use?
>
> It is a common problem and there are many resource-implementations out
> there (e.g. XMLResourceImpl, XMIResourceImpl, BinaryResourceImpl,
> CDOResourceImpl, HibernateResource etc.). I cannot see a reason why you
> could not use EMF for your purpose. Even if you don't use the entire EMF
> framework you could still use the code generator to get high-quality
> JavaBean like data classes with a fine-grained notification mechanism
> for free...
>

Great! I'm really doing this as a learning exercise as well, and the
more I learn about EMF, the more I want to use it in future projects...

>> I've included a link to my initial ecore model. Once I figure out how
>> to load/save correctly, I will continue to tweak it:
>> http://skratchdot.com/eclipse/EsxFile.ecore
>
> I've downloaded the model (I hope you don't mind) and generated model
> code. My suggestion: lowercase the package name and carefully design
> your containment references! Every object inside a single resource
> should be reachable by one single containment reference!
>
> Mario
>

No problem at all (that's why I included it :) ). Once I get a little
further along, the project will be open source anyways. I was actually
hoping for some tips like you gave (because I need to nail down the
model before fully implementing the load/save). Now that I think about
it, I should have just shared the .genmodel I had been using.

One more question: do you know of a good reference for some of the
..genmodel Package settings (and other property view settings for
different types of nodes in the .genmodel)?

I've found a good reference for some types of objects (in the Docs
section- by Ed Merck):
http://www.scribd.com/doc/11342394/Refcard-38-Essential-EMF

But I haven't found great descriptions of the features like
Package->Model->Resource Type->Basic (that you alerted me to).

I think I'm going to pick up the "EMF: Eclipse Modeling Framework, 2nd
Edition" book this week (and hopefully it describes some of these features).

Thanks again for your help!

>> Thanks for any help you can provide!
>>
>> -Jeff
Re: is EMF right solution for me? - Overriding load / save format [message #431866 is a reply to message #431861] Tue, 28 July 2009 13:13 Go to previous message
Mario Winterer is currently offline Mario WintererFriend
Messages: 136
Registered: July 2009
Senior Member
See comments inline below!

Jeff S. schrieb:
> Mario-
>
> Thanks alot! That's a huge help. See inline comments.
>
> Mario Winterer wrote:
>> Hi Jeff,
>>
>> see my comments below!
>>
>> J. Switzer schrieb:
>>> Hello. Sorry for the newbie question.
>>>
>>> I've been re-writing an old C++ program of mine as an RCP app. I had
>>> a basic JavaBeans model working that would load / save my model, but
>>> when working on the GUI, I realized I should have a stronger
>>> framework/model to work with, so I've been looking into EMF.
>>>
>>> I have a few questions:
>>>
>>> For this application, I've reversed engineered a file format (for a
>>> hardware sampler). I want to be able to make changes to the model,
>>> and save the resource in a format that the sampler can read. This
>>> means (at a high level) I cannot load/save resources in XMI format
>>> (they need to be some format that I *partially* know about). For
>>> instance, the way I currently manipulate the files, is by reading the
>>> original ~2MB file into a byte[]. I then load in the objects that
>>> I've reversed engineered (and know the structure of) from the file.
>>> When I save the resource, I write the original byte[] back to disk,
>>> then overwrite certain *known* portions of the data with my
>>> manipulated Objects.
>>> If you're still with me, my question is, what's the best way to go
>>> about overriding loading/saving of resources in EMF? I've looked at
>>> code like:
>>> resource =
>>> editingDomain.getResourceSet().getResource(resourceURI, true);
>>> And the generated test Example, with lines like:
>>> Resource resource =
>>> resourceSet.createResource(URI.createURI("http:///My.esxfile"));
>>> EsxFile root = EsxFileFactory.eINSTANCE.createEsxFile();
>>> resource.getContents().add(root);
>>> resource.save(System.out, null);
>>>
>>> My unfamiliarity with EMF/Java standards (Factories / Adapters), is
>>> making it hard for me to grasp where/how I should start with
>>> overriding the default load/save behavior.
>>
>> The correct way to customize loading and saving is to write your own
>> Resource and ResourceFactory implementation and register the custom
>> ResourceFactory for the preferred file extension.
>>
>> Create a .genmodel file based on your Ecore model, select the
>> EsxFile-Package in the editor and open the Properties View.
>> + Set All->Base Package (e.g. to com.skratchdot)
>> + Set Model->Resource Type to "Basic" (that will generate a basic
>> Resource and ResourceFactory)
>> Generate the code.
>>
>
> This is a step I was definitely missing. I wouldn't have know to set
> Package->Model->Resource Type to "Basic". Thanks alot for that!

In fact this setting isn't really required because it just generates the
body of the Resource and ResourceFactory, which you can also do 'per hand'.

>> In com.skratchdot.EsxFile.util you can find EsxFileResourceFactoryImpl
>> and EsxFileResourceImpl.
>> (1) Implement EsxFileResourceImpl#doSave(OutputStream, Map) and
>> EsxFileResourceImpl#doLoad(OutputStream, Map) so that the contents of
>> the resource gets saved/loaded.
>> (2) Register the EsxFileResourceFactoryImpl as resource factory for
>> your file extension:
>> <extension point="org.eclipse.emf.ecore.extension_parser">
>> <parser
>> class="com.skratchdot.EsxFile.util.EsxFileResourceFactoryImpl "
>> type="esxfile">
>> </parser>
>> </extension>
>>
>> Now all URIs that ends in .esxfile will be handled by your
>> Resource/ResourceFactory:
>> Resource resource =
>> resourceSet.getResource(URI.createURI("http:///My.esxfile"), true);
>>
>>
>>> Also I'm just wondering if this a common problem, and if you think
>>> EMF is a good solution for me to use?
>>
>> It is a common problem and there are many resource-implementations out
>> there (e.g. XMLResourceImpl, XMIResourceImpl, BinaryResourceImpl,
>> CDOResourceImpl, HibernateResource etc.). I cannot see a reason why
>> you could not use EMF for your purpose. Even if you don't use the
>> entire EMF framework you could still use the code generator to get
>> high-quality JavaBean like data classes with a fine-grained
>> notification mechanism for free...
>>
>
> Great! I'm really doing this as a learning exercise as well, and the
> more I learn about EMF, the more I want to use it in future projects...

We started (almost) the same way and now we're using EMF all around our
application...
One little hint: If you plan to use EMF for more than just creating java
beans, make sure that all model modifications are done via EMF-commands
(either existing or custom ones). Do not directly modify the model (via
setters and getters) - except from within your own commands - as this
will get you into troubles if you want to use extended EMF features like
undo/redo, EMF Transaction API, and so on.
I'm writing this because in the beginning we didn't consider this and
had to work hard to fix that.

>>> I've included a link to my initial ecore model. Once I figure out
>>> how to load/save correctly, I will continue to tweak it:
>>> http://skratchdot.com/eclipse/EsxFile.ecore
>>
>> I've downloaded the model (I hope you don't mind) and generated model
>> code. My suggestion: lowercase the package name and carefully design
>> your containment references! Every object inside a single resource
>> should be reachable by one single containment reference!
>>
>> Mario
>>
>
> No problem at all (that's why I included it :) ). Once I get a little
> further along, the project will be open source anyways. I was actually
> hoping for some tips like you gave (because I need to nail down the
> model before fully implementing the load/save). Now that I think about
> it, I should have just shared the .genmodel I had been using.
>
> One more question: do you know of a good reference for some of the
> .genmodel Package settings (and other property view settings for
> different types of nodes in the .genmodel)?

Usually you do not have to tweak lots of the .genmodel parameters - the
default values are very useful. I consider it more important to
carefully design the model and use the correct settings there (e.g.
derived or transient attributes, containment vs. references, etc.)

> I've found a good reference for some types of objects (in the Docs
> section- by Ed Merck):
> http://www.scribd.com/doc/11342394/Refcard-38-Essential-EMF
>
> But I haven't found great descriptions of the features like
> Package->Model->Resource Type->Basic (that you alerted me to).
>
> I think I'm going to pick up the "EMF: Eclipse Modeling Framework, 2nd
> Edition" book this week (and hopefully it describes some of these
> features).

Yes. Good idea! This book is really helpful!

> Thanks again for your help!

You're welcome!

>>> Thanks for any help you can provide!
>>>
>>> -Jeff
Previous Topic:[CDO] FactoryNotFoundException: Factory not found: org.eclipse.net4j.serverProtocols[cdo]
Next Topic:polymorphism="explicit" not working for Criteriaor load Query
Goto Forum:
  


Current Time: Thu Dec 12 21:11:36 GMT 2019

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

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

Back to the top