Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Linking of EObjects from different resources, detect saves
Linking of EObjects from different resources, detect saves [message #1220356] Wed, 11 December 2013 04:40 Go to next message
Jens Rabe is currently offline Jens Rabe
Messages: 53
Registered: September 2013
Member
Hello,

I try to ask this question again in this forum and be more specific, I hope someone can help me here because I am still struggling with it.

I have multiple EObjects scattered to multiple resources. Then, I have two interfaces, let's call them User and Use. Use has a many-reference to User, and User has a single-reference to Use. They are both set to EOpposite.

I now have the following requirements:


  1. When anything changes in the "use", a defined operation of all associated "users" has to be called. I solved this using adapters.
  2. The "Use" needs to track when its associated "users" change, and when the "Use" is saved, the "Users" need to be saved, too. These linked saves must not happen BEFORE the "use" is saved, but directly AFTER it.
  3. When Users are added to and removed from the reference from the Use, they have to be tracked for a "deferred save" too
  4. When a new Use is referenced by a User, the one which was previously referenced has to be tracked
  5. When the use is saved, the associated Users (the old one which was removed and the new one which was added) need to be saved, too


I am almost there, I used some adapter magic to track the relevant changes, but I now have the problem to detect saves. I tried to attach an adapter to the resources of the objects they are contained in, and a save produces a characteristic chain of notifications (RESOURCE__TIME_STAMP after RESOURCE__IS_MODIFIED). But loading a resource as of resource.load() produces the same chain of notifications, causing unnecessary and potentially dangerous saves.
My first approach was to load the referenced objects and use EcoreUtil.equals to check if there is a change, but this is expensive and cannot check derived features. In our product, we somewhat misuse the derived status (will be changed in the near future) but as of now we need this workaround.
My second approach was to actually get the derived feature (it is only one), but that lead to proxy resolving, causing the save tracker to be called again because the resource is loaded, and to run into a recursive infinite loop.

Is there a good practice how to auto-save referenced objects?

An alternative would be to synchronize all objects with the same URI, I read about EMF transaction, but we also use Xtext in this environment, and Xtext editors do not use editing domains, EMF transaction is not usable here.
Re: Linking of EObjects from different resources, detect saves [message #1220428 is a reply to message #1220356] Wed, 11 December 2013 11:31 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26010
Registered: July 2009
Senior Member
Jens,

Comments below.

On 11/12/2013 10:40 AM, Jens Rabe wrote:
> Hello,
>
> I try to ask this question again in this forum and be more specific, I
> hope someone can help me here because I am still struggling with it.
>
> I have multiple EObjects scattered to multiple resources. Then, I have
> two interfaces, let's call them User and Use. Use has a many-reference
> to User, and User has a single-reference to Use. They are both set to
> EOpposite.
>
> I now have the following requirements:
>
>
> When anything changes in the "use", a defined operation of all
> associated "users" has to be called. I solved this using adapters.
> The "Use" needs to track when its associated "users" change, and when
> the "Use" is saved, the "Users" need to be saved, too.
Are the resources all on one resource set?
> These linked saves must not happen BEFORE the "use" is saved, but
> directly AFTER it.
But it sounds like this kind of dependency could be circular, i.e., in
the sense that something in resource A uses something in resource B and
vice versa...
> When Users are added to and removed from the reference from the Use,
> they have to be tracked for a "deferred save" too
> When a new Use is referenced by a User, the one which was previously
> referenced has to be tracked
Is it single valued?
> When the use is saved, the associated Users (the old one which was
> removed and the new one which was added) need to be saved, too
>
> I am almost there, I used some adapter magic to track the relevant
> changes, but I now have the problem to detect saves. I tried to attach
> an adapter to the resources of the objects they are contained in, and
> a save produces a characteristic chain of notifications
> (RESOURCE__TIME_STAMP after RESOURCE__IS_MODIFIED). But loading a
> resource as of resource.load() produces the same chain of
> notifications, causing unnecessary and potentially dangerous saves.
> My first approach was to load the referenced objects and use
> EcoreUtil.equals to check if there is a change, but this is expensive
> and cannot check derived features. In our product, we somewhat misuse
> the derived status (will be changed in the near future) but as of now
> we need this workaround.
> My second approach was to actually get the derived feature (it is only
> one), but that lead to proxy resolving, causing the save tracker to be
> called again because the resource is loaded, and to run into a
> recursive infinite loop.
This sounds frightening.
>
> Is there a good practice how to auto-save referenced objects?
Generally the generated editors work with all the resources in a
resource set and when you save the editor, it saves all the resources,
but with the
org.eclipse.emf.ecore.resource.Resource.OPTION_SAVE_ONLY_IF_CHANGED option.
>
> An alternative would be to synchronize all objects with the same URI,
> I read about EMF transaction, but we also use Xtext in this
> environment, and Xtext editors do not use editing domains, EMF
> transaction is not usable here.
There are things I don't understand like whether all the resources are
in one resource set. Certainly if you have bidirectional references
that you change, you modify the objects on both ends so you could track
dirtiness of resources but if you have undo, then you'd like the
resource not to be dirty after undo. Even without situations like the
one you describe, some resource can refer to objects in another resource
and even moving that object around can change the URI used to refer to
that object and can require the referring resources to be saved. That's
why the generated editor takes the brute force approach...
Re: Linking of EObjects from different resources, detect saves [message #1220585 is a reply to message #1220428] Thu, 12 December 2013 10:59 Go to previous message
Jens Rabe is currently offline Jens Rabe
Messages: 53
Registered: September 2013
Member
>> When anything changes in the "use", a defined operation of all
>> associated "users" has to be called. I solved this using adapters.
>> The "Use" needs to track when its associated "users" change, and when
>> the "Use" is saved, the "Users" need to be saved, too.
>Are the resources all on one resource set?

Yes, they are.

>> These linked saves must not happen BEFORE the "use" is saved, but
>> directly AFTER it.
> But it sounds like this kind of dependency could be circular, i.e., in
> the sense that something in resource A uses something in resource B and
> vice versa...

My Use is in resource A and my User is in Resource B. Both are in the same resource set.

>> When Users are added to and removed from the reference from the Use,
>> they have to be tracked for a "deferred save" too
>> When a new Use is referenced by a User, the one which was previously
>> referenced has to be tracked
> Is it single valued?

The reference Use.usedBy(User) is multi-valued, the reference User.use(Use) is single-valued. Both are EOpposites.

>> Is there a good practice how to auto-save referenced objects?
> Generally the generated editors work with all the resources in a
> resource set and when you save the editor, it saves all the resources,
> but with the
> org.eclipse.emf.ecore.resource.Resource.OPTION_SAVE_ONLY_IF_CHANGED option.

We use our own editors, from scratch, and we use Xtext editors. In our own editors, I will add this "save the whole resource set", and I will investigate how to achieve it for Xtext editors.

But thanks in advance for the reply.

[Updated on: Thu, 12 December 2013 10:59]

Report message to a moderator

Previous Topic:EObjectValidator not being regenerated after chnage to xsd
Next Topic:Modifying Ecore file in code
Goto Forum:
  


Current Time: Thu Aug 28 01:33:58 EDT 2014

Powered by FUDForum. Page generated in 0.01721 seconds