Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » EMF Notification(Getting the id of a deleted model element)
EMF Notification [message #1690969] Wed, 01 April 2015 10:46 Go to next message
Jimi Ogunyomi is currently offline Jimi OgunyomiFriend
Messages: 4
Registered: April 2015
Junior Member
Hi,

I have a problem regarding the use of EMF Notification API. I have attached an EContentAdapter to all model elements in my model. Whenever I make changes to any of the model elements, I do get change notifications. I then use the information (particularly the model element id) contained in the notification to do other things.

The problem I have is that whenever I delete a model element, I have not been able to figure out how to retrieve the id of the deleted element. If anyone has idea of how to do this (get the id of a deleted element), please can you let me know?

I also attach a simple Java program that demonstrates this problem.

Thanks.

Jimi
Re: EMF Notification [message #1691013 is a reply to message #1690969] Wed, 01 April 2015 15:04 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Jimi,

You example depends on things I don't have. This looks all bogus to me:

EContentAdapter adapter = new EContentAdapter() {

public Notifier getTarget() {
return null;
}

public boolean isAdapterForType(Object type) {
return false;
}

public void notifyChanged(Notification notification) {
super.notifyChanged(notification);
processNotification(notification);
}

public void setTarget(Notifier newTarget) {
// This is particularly bad because you've subverted the walking of the
tree.

}

};

resource.eAdapters().add(adapter);

// This whole loop isn't needed.
for(EObject obj : resource.getContents()) {
obj.eAdapters().add(adapter);
for(EObject o : obj.eContents()) {
o.eAdapters().add(adapter);
}
}

An EContentAdapter will remove itself from any object removed from the
containment tree.

How is a content adapter is related to you issue of determine an
element's ID?




On 01/04/2015 4:29 PM, Jimi Ogunyomi wrote:
> Hi,
>
> I have a problem regarding the use of EMF Notification API. I have attached an EContentAdapter to all model elements in my model. Whenever I make changes to any of the model elements, I do get change notifications. I then use the information (particularly the model element id) contained in the notification to do other things.
>
> The problem I have is that whenever I delete a model element, I have not been able to figure out how to retrieve the id of the deleted element. If anyone has idea of how to do this (get the id of a deleted element), please can you let me know?
>
> I also attach a simple Java program that demonstrates this problem.
>
> Thanks.
>
> Jimi


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: EMF Notification [message #1691049 is a reply to message #1691013] Wed, 01 April 2015 20:26 Go to previous messageGo to next message
Jimi Ogunyomi is currently offline Jimi OgunyomiFriend
Messages: 4
Registered: April 2015
Junior Member
Hi Ed,

Thanks for your reply.

You are absolutely correct, the code fragment as you pointed out is unnecessary. I have also refactored the code, it only now depends on emf.ecore.*

Let me try to better explain the issue I'm facing. Using the content adapter, when I update an existing model element, I get a notification that includes, among other things the model element's id, the changed feature of the model element, the old value, the new value, etc. I get a similar kind of notification when I create and add a new model element -- I can get the id of the added element and do other things with the added element partly because I know its id.

The challenge I'm facing is that when I delete a model element, I get at least two notifications. The first notification is from the root element (e.g., package), and the old value (gotten by notification.getOldValue) is the deleted model element. For the second notification, the notifier is the deleted model element itself. So, I'm asking if there is a way to possibly get the id of the deleted model element. When the notifier is the deleted element, I tried EcoreUtil.getID((EObject) notification.getNotifier()), and EcoreUtil.getID((EObject) notification.getOldValue()) when the notifier is the root element. But the id is always null.

Thanks.

Jimi
Re: EMF Notification [message #1691063 is a reply to message #1691049] Thu, 02 April 2015 04:59 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Jimi,

The term "ID" is kind of overload. If the EObject has an EAttribute
with iD true, then you can get the IDE of that object directly; it's the
value of it's feature attribute; this is referred to as an intrinsic
ID. If the resource manages the ID, then the object only has an ID
while attached to the resource; this is referred to as an extrinsic ID.
If I change your code to this:

EContentAdapter adapter = new EContentAdapter() {
public void notifyChanged(Notification notification) {
super.notifyChanged(notification);
// Note that you are still totally messed up because weren't calling
super. You should at least look at the source code to understand what
you're overriding and subverting when you override and don't call super.

//only interested in getting the id of a deleted element
EObject deletedElement = null;

//the notifier is the root element
if(notification.getEventType() == Notification.REMOVE
&& notification.getNotifier() instanceof EPackage) {
deletedElement = (EObject) notification.getOldValue();
System.out.println("model element id: " +
EcoreUtil.getID(deletedElement));
}

//the notifier is the deleted model element
if(notification.getEventType() == Notification.SET &&
notification.getNotifier() instanceof EClass) {
deletedElement = (EObject) notification.getNotifier();
System.out.println("model element id: " +
EcoreUtil.getID(deletedElement));
}

}

@Override
protected void setTarget(EObject target) {
System.err.println("#" + EcoreUtil.getID(target));
// It's clear this is always null.
System.err.println("#" + EcoreUtil.getURI(target));
// This always gives the URI.
super.setTarget(target);
}
};

So your model has no intrinsic IDs so yes of course you always see
null. Note that you can override (while calling super) on something
like setTarget(EObject)/unsetTarget(EObject) to track the adapter being
added and removed. When it's added you could record in a map
information that's not available when the object is removed from the
tree (and hence when the adapter is removed from it).


On 01/04/2015 10:26 PM, Jimi Ogunyomi wrote:
> Hi Ed,
>
> Thanks for your reply.
>
> You are absolutely correct, the code fragment as you pointed out is unnecessary. I have also refactored the code, it only now depends on emf.ecore.*
>
> Let me try to better explain the issue I'm facing. Using the content adapter, when I update an existing model element, I get a notification that includes, among other things the model element's id, the changed feature of the model element, the old value, the new value, etc. I get a similar kind of notification when I create and add a new model element -- I can get the id of the added element and do other things with the added element partly because I know its id.
>
> The challenge I'm facing is that when I delete a model element, I get at least two notifications. The first notification is from the root element (e.g., package), and the old value (gotten by notification.getOldValue) is the deleted model element. For the second notification, the notifier is the deleted model element itself. So, I'm asking if there is a way to possibly get the id of the deleted model element. When the notifier is the deleted element, I tried EcoreUtil.getID((EObject) notification.getNotifier()), and EcoreUtil.getID((EObject) notification.getOldValue()) when the notifier is the root element. But the id is always null.
>
> Thanks.
>
> Jimi


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: EMF Notification [message #1691084 is a reply to message #1691063] Thu, 02 April 2015 08:17 Go to previous message
Jimi Ogunyomi is currently offline Jimi OgunyomiFriend
Messages: 4
Registered: April 2015
Junior Member
Hi Ed,

That solves my problem. Thanks a lot for helping.

Cheers,
Jimi
Previous Topic:[CDO] Changing datatypes between model versions
Next Topic:Databinding to map entry
Goto Forum:
  


Current Time: Fri Apr 26 07:30:39 GMT 2024

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

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

Back to the top