Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Tracking Notification in the Model
Tracking Notification in the Model [message #658484] Tue, 08 March 2011 17:20 Go to next message
Xavipen Mising name is currently offline Xavipen Mising nameFriend
Messages: 59
Registered: March 2011
Member
Hi,

I need to receive any notification in the model to track changes in the infromation and report them. The Edit framework is not good for me because the model is not going to use any GUI but send data updates over the network. After searching a lot i found and old post (2002) on this talking about a SelfInstallingAdapter. I modify that and updated to the code below.

However i would appreciate if somebody could indicate me whether there is another more efficient way of doing this.

Regards,
Javi


public abstract class AbstractEObjectNotificationTracker {

	protected EObject trackedObject = null;
	protected SelfInstallingAdapter adapter = new SelfInstallingAdapter();
	
	abstract protected void process(Notification notification);
	abstract protected void doAdapterRemovedFromObject(EObject eObject);
	abstract protected void doAdapterAddedToObject(EObject eObject);
	
	public void track(EObject eObject){	
		if ( trackedObject != null ){
			trackedObject.eAdapters().remove(adapter);
			doAdapterRemovedFromObject(trackedObject);
			removeTrackingOfContents(trackedObject);
		}
		
		trackedObject = eObject;
		
		if ( trackedObject != null){
			eObject.eAdapters().add(adapter);
			doAdapterAddedToObject(eObject);
			addTrackingOfContents(eObject);
		}
	}
	
	protected void addTrackingOfContents(EObject eObject){
		EObject containedObject;
		for (TreeIterator<EObject> i = EcoreUtil.getAllProperContents(eObject,false); i.hasNext(); )
		{
			containedObject = i.next();
			containedObject.eAdapters().add(adapter);
			doAdapterAddedToObject(containedObject);
		}		
	}

	protected void removeTrackingOfContents(EObject eObject){
		EObject containedObject;
		for (TreeIterator<EObject> i = EcoreUtil.getAllProperContents(eObject,false); i.hasNext(); )
		{
			containedObject = i.next();
			containedObject.eAdapters().remove(adapter);
			doAdapterRemovedFromObject(containedObject);
		}		
	}
	
	private class SelfInstallingAdapter extends AdapterImpl{
		
		public void notifyChanged(Notification notification)
		{
			super.notifyChanged(notification);
			selfAdapt(notification);
			process(notification);

		}

		protected void selfAdapt(Notification notification)
		{
			Object notifier = notification.getNotifier();
			if (notifier instanceof EObject)
			{
				Object feature = notification.getFeature();
				if (feature instanceof EReference && ((EReference)feature).isContainment())
				{
					handleContainment(notification);
				}
			}
		}

		protected void handleContainment(Notification notification)
		{
			switch (notification.getEventType())
			{
			case Notification.SET:
			case Notification.UNSET:
			{	
				EObject oldValue = JavaUtil.<EObject>cast(notification.getOldValue());
				if (oldValue != null)
				{
					System.err.println("Remove Adapter: " + oldValue);
					oldValue.eAdapters().remove(this);
					doAdapterRemovedFromObject(oldValue);
					removeTrackingOfContents(oldValue);
					
				}
				
				EObject newValue = JavaUtil.<EObject>cast(notification.getNewValue());
				if (newValue != null)
				{
					System.err.println("Add Adapter: " + newValue);
					newValue.eAdapters().add(this);
					doAdapterAddedToObject(newValue);
					addTrackingOfContents(newValue);
				}
				break;
			}
			case Notification.ADD:
			{
				EObject newValue = JavaUtil.<EObject>cast(notification.getNewValue());
				if (newValue != null)
				{
					System.err.println("Add Adapter: " + newValue);
					newValue.eAdapters().add(this);
					doAdapterAddedToObject(newValue);
					addTrackingOfContents(newValue);
				}
				break;
			}
			case Notification.ADD_MANY:
			{
				EObject newValue;
				Collection newValues = (Collection)notification.getNewValue();
				for (Iterator i = newValues.iterator(); i.hasNext(); )
				{
					newValue = JavaUtil.<EObject>cast(i.next());
					if (newValue != null)
					{
						System.err.println("Add Adapter: " + newValue);
						newValue.eAdapters().add(this);
						doAdapterAddedToObject(newValue);
						addTrackingOfContents(newValue);
					}
				}
				break;
			}
			case Notification.REMOVE:
			{
				EObject oldValue = JavaUtil.<EObject>cast(notification.getOldValue());
				if (oldValue != null)
				{
					System.err.println("Remove Adapter: " + oldValue);
					oldValue.eAdapters().remove(this);
					doAdapterRemovedFromObject(oldValue);
					removeTrackingOfContents(oldValue);
				}
				break;
			}
			case Notification.REMOVE_MANY:
			{
				EObject oldValue;
				Collection oldValues = (Collection)notification.getOldValue();
				for (Iterator i = oldValues.iterator(); i.hasNext(); )
				{
					oldValue = JavaUtil.<EObject>cast(i.next());
					if (oldValue != null)
					{
						System.err.println("Add Adapter: " + oldValue);
						oldValue.eAdapters().remove(this);
						doAdapterRemovedFromObject(oldValue);
						removeTrackingOfContents(oldValue);
					}
				}
				break;
			}
			}
		}

		public void setTarget(Notifier target)
		{
			System.err.println("Set Target: " + target);							
		}
		
	}
}

Re: Tracking Notification in the Model [message #658486 is a reply to message #658484] Tue, 08 March 2011 17:29 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Javi,

Comments below.

Xavipen wrote:
> Hi,
>
> I need to receive any notification in the model to track changes in
> the infromation and report them.
A ChangeRecorder might be perfect for that.
> The Edit framework is not good for me because the model is not going
> to use any GUI but send data updates over the network. After
> searching a lot i found and old post (2002) on this talking about a
> SelfInstallingAdapter. I modify that and updated to the code below.
EContentAdapter and ChangeRecorders are good examples of this.
>
> However i would appreciate if somebody could indicate me whether there
> is another more efficient way of doing this.
I'm working right now on updates to the change model so that it will be
possible to easily create a ChangeDescription that's a forward delta
which could be sent to a server to update a server-side instance of the
model to match exactly the client side updates. Is that ultimately what
you're after?
>
> Regards,
> Javi
>
>
>
> public abstract class AbstractEObjectNotificationTracker {
>
> protected EObject trackedObject = null;
> protected SelfInstallingAdapter adapter = new
> SelfInstallingAdapter();
>
> abstract protected void process(Notification notification);
> abstract protected void doAdapterRemovedFromObject(EObject eObject);
> abstract protected void doAdapterAddedToObject(EObject eObject);
>
> public void track(EObject eObject){
> if ( trackedObject != null ){
> trackedObject.eAdapters().remove(adapter);
> doAdapterRemovedFromObject(trackedObject);
> removeTrackingOfContents(trackedObject);
> }
>
> trackedObject = eObject;
>
> if ( trackedObject != null){
> eObject.eAdapters().add(adapter);
> doAdapterAddedToObject(eObject);
> addTrackingOfContents(eObject);
> }
> }
>
> protected void addTrackingOfContents(EObject eObject){
> EObject containedObject;
> for (TreeIterator<EObject> i =
> EcoreUtil.getAllProperContents(eObject,false); i.hasNext(); )
> {
> containedObject = i.next();
> containedObject.eAdapters().add(adapter);
> doAdapterAddedToObject(containedObject);
> }
> }
>
> protected void removeTrackingOfContents(EObject eObject){
> EObject containedObject;
> for (TreeIterator<EObject> i =
> EcoreUtil.getAllProperContents(eObject,false); i.hasNext(); )
> {
> containedObject = i.next();
> containedObject.eAdapters().remove(adapter);
> doAdapterRemovedFromObject(containedObject);
> }
> }
>
> private class SelfInstallingAdapter extends AdapterImpl{
>
> public void notifyChanged(Notification notification)
> {
> super.notifyChanged(notification);
> selfAdapt(notification);
> process(notification);
>
> }
>
> protected void selfAdapt(Notification notification)
> {
> Object notifier = notification.getNotifier();
> if (notifier instanceof EObject)
> {
> Object feature = notification.getFeature();
> if (feature instanceof EReference &&
> ((EReference)feature).isContainment())
> {
> handleContainment(notification);
> }
> }
> }
>
> protected void handleContainment(Notification notification)
> {
> switch (notification.getEventType())
> {
> case Notification.SET:
> case Notification.UNSET:
> {
> EObject oldValue =
> JavaUtil.<EObject>cast(notification.getOldValue());
> if (oldValue != null)
> {
> System.err.println("Remove Adapter: " + oldValue);
> oldValue.eAdapters().remove(this);
> doAdapterRemovedFromObject(oldValue);
> removeTrackingOfContents(oldValue);
>
> }
>
> EObject newValue =
> JavaUtil.<EObject>cast(notification.getNewValue());
> if (newValue != null)
> {
> System.err.println("Add Adapter: " + newValue);
> newValue.eAdapters().add(this);
> doAdapterAddedToObject(newValue);
> addTrackingOfContents(newValue);
> }
> break;
> }
> case Notification.ADD:
> {
> EObject newValue =
> JavaUtil.<EObject>cast(notification.getNewValue());
> if (newValue != null)
> {
> System.err.println("Add Adapter: " + newValue);
> newValue.eAdapters().add(this);
> doAdapterAddedToObject(newValue);
> addTrackingOfContents(newValue);
> }
> break;
> }
> case Notification.ADD_MANY:
> {
> EObject newValue;
> Collection newValues =
> (Collection)notification.getNewValue();
> for (Iterator i = newValues.iterator(); i.hasNext(); )
> {
> newValue = JavaUtil.<EObject>cast(i.next());
> if (newValue != null)
> {
> System.err.println("Add Adapter: " + newValue);
> newValue.eAdapters().add(this);
> doAdapterAddedToObject(newValue);
> addTrackingOfContents(newValue);
> }
> }
> break;
> }
> case Notification.REMOVE:
> {
> EObject oldValue =
> JavaUtil.<EObject>cast(notification.getOldValue());
> if (oldValue != null)
> {
> System.err.println("Remove Adapter: " + oldValue);
> oldValue.eAdapters().remove(this);
> doAdapterRemovedFromObject(oldValue);
> removeTrackingOfContents(oldValue);
> }
> break;
> }
> case Notification.REMOVE_MANY:
> {
> EObject oldValue;
> Collection oldValues =
> (Collection)notification.getOldValue();
> for (Iterator i = oldValues.iterator(); i.hasNext(); )
> {
> oldValue = JavaUtil.<EObject>cast(i.next());
> if (oldValue != null)
> {
> System.err.println("Add Adapter: " + oldValue);
> oldValue.eAdapters().remove(this);
> doAdapterRemovedFromObject(oldValue);
> removeTrackingOfContents(oldValue);
> }
> }
> break;
> }
> }
> }
>
> public void setTarget(Notifier target)
> {
> System.err.println("Set Target: " +
> target);
> }
>
> }
> }
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Tracking Notification in the Model [message #658494 is a reply to message #658484] Tue, 08 March 2011 18:00 Go to previous messageGo to next message
Xavipen Mising name is currently offline Xavipen Mising nameFriend
Messages: 59
Registered: March 2011
Member
Hi Ed,

Quote:
Javi,

Comments below.

Xavipen wrote:
> Hi,
>
> I need to receive any notification in the model to track changes in
> the infromation and report them.
A ChangeRecorder might be perfect for that.
> The Edit framework is not good for me because the model is not going
> to use any GUI but send data updates over the network. After
> searching a lot i found and old post (2002) on this talking about a
> SelfInstallingAdapter. I modify that and updated to the code below.
EContentAdapter and ChangeRecorders are good examples of this.


I have just checked the code for EContentAdapter and I have one concern. Having the adding of contents in the setTarget causes repeated calls and the apdapter being added several times under certain circunstances (Because when adding the adapter setTarget is called again). That was the reason why i move it to a track method and left the setTarget method empty in my implementation. How is EContentAdapter dealing with it?

(ChangeRecorders seems to be more for undo capabilities and i do not need it for now)

Quote:
>
> However i would appreciate if somebody could indicate me whether there
> is another more efficient way of doing this.
I'm working right now on updates to the change model so that it will be
possible to easily create a ChangeDescription that's a forward delta
which could be sent to a server to update a server-side instance of the
model to match exactly the client side updates. Is that ultimately what
you're after?

>

This sounds very interesting, specilly when thinking about remoteGUI and is missing for the SOA i know.

In my case, I do not know if i could apply it though. The class in the model are "processing" blocks rather than just data. And i do not want to mirror the processing but only to be able to monitor the configuration and stadistics of these "processing" blocks. Is it possible to define a sort of "ValueObject" like in SOA for and EMF model selecting a subset of the Attributes and operations they implement?
Re: Tracking Notification in the Model [message #658530 is a reply to message #658494] Tue, 08 March 2011 20:43 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Javi,

Comments below.

Xavipen wrote:
> Hi Ed,
>
> Quote:
>> Javi,
>>
>> Comments below.
>>
>> Xavipen wrote:
>> > Hi,
>> >
>> > I need to receive any notification in the model to track changes in
>> > the infromation and report them.
>> A ChangeRecorder might be perfect for that.
>> > The Edit framework is not good for me because the model is not going
>> > to use any GUI but send data updates over the network. After
>> > searching a lot i found and old post (2002) on this talking about a
>> > SelfInstallingAdapter. I modify that and updated to the code below.
>> EContentAdapter and ChangeRecorders are good examples of this.
>
>
> I have just checked the code for EContentAdapter and I have one
> concern. Having the adding of contents in the setTarget causes
> repeated calls and the apdapter being added several times under
> certain circunstances (Because when adding the adapter setTarget is
> called again).
This is a theory you have? Indeed the adapter is added recursively, but
to the children, not more than once to the same object.
> That was the reason why i move it to a track method and left the
> setTarget method empty in my implementation. How is EContentAdapter
> dealing with it?
>
> (ChangeRecorders seems to be more for undo capabilities and i do not
> need it for now)
No, it's one of the uses. It's primarily for tracking what's changed
and for producing a description of what's changed. That description can
be applied to produce the changes being described (thereby supporting
undo/redo). I'm not sure what form you intend to "report" changes...
>
> Quote:
>> >
>> > However i would appreciate if somebody could indicate me whether there
>> > is another more efficient way of doing this.
>> I'm working right now on updates to the change model so that it will be
>> possible to easily create a ChangeDescription that's a forward delta
>> which could be sent to a server to update a server-side instance of the
>> model to match exactly the client side updates. Is that ultimately what
>> you're after?
>>
>
> This sounds very interesting, specilly when thinking about remoteGUI
> and is missing for the SOA i know.
> In my case, I do not know if i could apply it though. The class in the
> model are "processing" blocks rather than just data. And i do not want
> to mirror the processing but only to be able to monitor the
> configuration and stadistics of these "processing" blocks. Is it
> possible to define a sort of "ValueObject" like in SOA for and EMF
> model selecting a subset of the Attributes and operations they implement?
I'm not sure I understand. You might mark some features as transient
because you're not interest in them being persistent...


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Tracking Notification in the Model [message #658632 is a reply to message #658530] Wed, 09 March 2011 10:52 Go to previous messageGo to next message
Xavipen Mising name is currently offline Xavipen Mising nameFriend
Messages: 59
Registered: March 2011
Member
Quote:
Javi,

Comments below.

Xavipen wrote:
> Hi Ed,
>
> Quote:
>> Javi,
>>
>> Comments below.
>>
>> Xavipen wrote:
>> > Hi,
>> >
>> > I need to receive any notification in the model to track changes in
>> > the infromation and report them.
>> A ChangeRecorder might be perfect for that.
>> > The Edit framework is not good for me because the model is not going
>> > to use any GUI but send data updates over the network. After
>> > searching a lot i found and old post (2002) on this talking about a
>> > SelfInstallingAdapter. I modify that and updated to the code below.
>> EContentAdapter and ChangeRecorders are good examples of this.
>
>
> I have just checked the code for EContentAdapter and I have one
> concern. Having the adding of contents in the setTarget causes
> repeated calls and the apdapter being added several times under
> certain circunstances (Because when adding the adapter setTarget is
> called again).
This is a theory you have? Indeed the adapter is added recursively, but
to the children, not more than once to the same object.



That was a question. Wink On the adapter in the forum the setTarget was iterating through all the contents (direct and indirect) of the Notifier, thus things where called twice. That was the reasoning for my question.

I went back to EContent Adapter code and run a couple of test. EContentAdapter only iteracts trough the "direct" contents of the notifier. So it works like a charm. Smile


Quote:
> That was the reason why i move it to a track method and left the
> setTarget method empty in my implementation. How is EContentAdapter
> dealing with it?
>
> (ChangeRecorders seems to be more for undo capabilities and i do not
> need it for now)
No, it's one of the uses. It's primarily for tracking what's changed
and for producing a description of what's changed. That description can
be applied to produce the changes being described (thereby supporting
undo/redo). I'm not sure what form you intend to "report" changes...


We will have a Data repository where different clients will be able to subcribe for different data acoording to there needs.
The EContentAdapter will be use to link the Model and the repo so the repo get's updated when the model changes. (publish, unpublish new data/objects, attributes updates...)

Quote:
>
> Quote:
>> >
>> > However i would appreciate if somebody could indicate me whether there
>> > is another more efficient way of doing this.
>> I'm working right now on updates to the change model so that it will be
>> possible to easily create a ChangeDescription that's a forward delta
>> which could be sent to a server to update a server-side instance of the
>> model to match exactly the client side updates. Is that ultimately what
>> you're after?
>>
>
> This sounds very interesting, specilly when thinking about remoteGUI
> and is missing for the SOA i know.
> In my case, I do not know if i could apply it though. The class in the
> model are "processing" blocks rather than just data. And i do not want
> to mirror the processing but only to be able to monitor the
> configuration and stadistics of these "processing" blocks. Is it
> possible to define a sort of "ValueObject" like in SOA for and EMF
> model selecting a subset of the Attributes and operations they implement?
I'm not sure I understand. You might mark some features as transient
because you're not interest in them being persistent...



It is a bit dificult to explain in words. It is not related about the persistance. There is a beheiviour associated to the class (to simplify different classes will have a different internal thread transient,) Therefore when persisting the objects the beheiviour is also persisted and by modifying the objects in the model you can modify how the data needs to be process in realtime and also define differnet model for different beheiviours, etc...

The missing piece is how to get a remote GUI, (although is not the priority now) and the new feature you are working on could help in the monitoring part. The thing is that i would not like to have the beheiviour of the objects on the client (and having then running any thread there).

I hope this has clarify a bit what´s in my mind. I do not know enough about EMF yet and this was a bit of topic Smile . In any case, one thing i know: The more i learn about the EMF the more i like it Smile Thanks a lot!
Javi

[Updated on: Wed, 09 March 2011 10:54]

Report message to a moderator

Re: Tracking Notification in the Model [message #658737 is a reply to message #658632] Wed, 09 March 2011 16:06 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Javi,

Comments below.

Xavipen wrote:
> Quote:
>> Ed Merks wrote on Tue, 08 March 2011 21:43
>> > Javi,
>> > > Comments below.
>> > > Xavipen wrote:
>> > > Hi Ed,
>> > >
>> > > Quote:
>> > >> Javi,
>> > >>
>> > >> Comments below.
>> > >>
>> > >> Xavipen wrote:
>> > >> > Hi,
>> > >> >
>> > >> > I need to receive any notification in the model to track
>> changes in
>> > >> > the infromation and report them.
>> > >> A ChangeRecorder might be perfect for that.
>> > >> > The Edit framework is not good for me because the model is not
>> going
>> > >> > to use any GUI but send data updates over the network. After
>> > >> > searching a lot i found and old post (2002) on this talking
>> about a
>> > >> > SelfInstallingAdapter. I modify that and updated to the code
>> below.
>> > >> EContentAdapter and ChangeRecorders are good examples of this.
>> > >
>> > >
>> > > I have just checked the code for EContentAdapter and I have one >
>> > concern. Having the adding of contents in the setTarget causes > >
>> repeated calls and the apdapter being added several times under > >
>> certain circunstances (Because when adding the adapter setTarget is >
>> > called again). > This is a theory you have? Indeed the adapter is
>> added recursively, but > to the children, not more than once to the
>> same object.
>>
>>
>>
>> That was a question. ;) On the adapter in the forum the setTarget was
>> iterating through all the contents (direct and indirect) of the
>> Notifier, thus things where called twice. That was the reasoning for
>> my question.
>> I went back to EContent Adapter code and run a couple of test.
>> EContentAdapter only iteracts trough the "direct" contents of the
>> notifier. So it works like a charm. :)
>>
>> Quote:
>> > > That was the reason why i move it to a track method and left the
>> > > setTarget method empty in my implementation. How is
>> EContentAdapter > > dealing with it?
>> > >
>> > > (ChangeRecorders seems to be more for undo capabilities and i do
>> not > > need it for now)
>> > No, it's one of the uses. It's primarily for tracking what's
>> changed > and for producing a description of what's changed. That
>> description can > be applied to produce the changes being described
>> (thereby supporting > undo/redo). I'm not sure what form you intend
>> to "report" changes...
>>
>>
>> We will have a Data repository where different clients will be able
>> to subcribe for different data acoording to there needs.
>> The EContentAdapter will be use to link the Model and the repo so the
>> repo get's updated when the model changes. (publish, unpublish new
>> data/objects, attributes updates...)
>>
>> Quote:
>> > >
>> > > Quote:
>> > >> >
>> > >> > However i would appreciate if somebody could indicate me
>> whether there
>> > >> > is another more efficient way of doing this.
>> > >> I'm working right now on updates to the change model so that it
>> will be
>> > >> possible to easily create a ChangeDescription that's a forward
>> delta
>> > >> which could be sent to a server to update a server-side instance
>> of the
>> > >> model to match exactly the client side updates. Is that
>> ultimately what
>> > >> you're after?
>> > >>
>> > >
>> > > This sounds very interesting, specilly when thinking about
>> remoteGUI > > and is missing for the SOA i know.
>> > > In my case, I do not know if i could apply it though. The class
>> in the > > model are "processing" blocks rather than just data. And i
>> do not want > > to mirror the processing but only to be able to
>> monitor the > > configuration and stadistics of these "processing"
>> blocks. Is it > > possible to define a sort of "ValueObject" like in
>> SOA for and EMF > > model selecting a subset of the Attributes and
>> operations they implement?
>> > I'm not sure I understand. You might mark some features as
>> transient > because you're not interest in them being persistent...
>
>
> It is a bit dificult to explain in words. It is not related about the
> persistance. There is a beheiviour associated to the class (to
> simplify different classes will have a different internal thread
> transient,) Therefore when persisting the objects the beheiviour
I guess the use of the word behavior doesn't help me because I think of
behavior as invoking EOperations.
> is also persisted and by modifying the objects in the model you can
> modify how the data needs to be process in realtime and also define
> differnet model for different beheiviours, etc...
>
> The missing piece is how to get a remote GUI, (although is not the
> priority now) and the new feature you are working on could help in the
> monitoring part.
CDO is awfully helpful with remote GUIs as well, though your scenario
sounds a little "special".
> The thing is that i would not like to have the beheiviour of the
> objects on the client (and having then running any thread there).
> I hope this has clarify a bit what´s in my mind. I do not know enough
> about EMF yet and this was a bit of topic :) . In any case, one thing
> i know: The more i learn about the EMF the more i like it :)
:-)
> Thanks a lot!
> Javi
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:org.eclipse.emf.sdk feature is missing plugin in emf-xsd-Update-2.7.0M5.zip (Indigo)
Next Topic:XSD -> ECORE -> ATL
Goto Forum:
  


Current Time: Thu Apr 18 08:01:09 GMT 2024

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

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

Back to the top