Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » eAdapter, Notification and AdapterFactory
eAdapter, Notification and AdapterFactory [message #854704] Tue, 24 April 2012 07:08 Go to next message
Ludwig Moser is currently offline Ludwig MoserFriend
Messages: 476
Registered: July 2009
Senior Member
Hello

I've read through the EMF Documentation, which is pretty short for this part. (i only found stuff in the overview)
Usually i find tutorials or Documentation which helps me finding my way, but this time i cant find something matching (searching the wrong place?)

so far as i understood i can _ONLY_ register an adapter for an object.
i created a mini-example for getting a basic understanding.

a EClass Person and a EClass PersonList
<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0"
    xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="model"
    nsURI="http://model/1.0" nsPrefix="emf.model">
  <eClassifiers xsi:type="ecore:EClass" name="Person">
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="nick" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
  </eClassifiers>
  <eClassifiers xsi:type="ecore:EClass" name="PersonList">
    <eStructuralFeatures xsi:type="ecore:EReference" name="persons" upperBound="-1"
        eType="#//Person"/>
  </eClassifiers>
</ecore:EPackage>


so then i wrote a small test app

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.util.EContentAdapter;

import emf.model.ModelFactory;
import emf.model.Person;
import emf.model.PersonList;

public class Main {

	public static void main(String[] args) {
		EContentAdapter adapter = new EContentAdapter() {
			public void notifyChanged(Notification notification) {
				// it does not make any difference if i call super or not?!
				super.notifyChanged(notification);
				System.out.println(String.format("data changed! (%s = '%s')",
						notification.getNotifier().getClass().getSimpleName(),
						notification.getNotifier()));
			}
		};

		PersonList list = ModelFactory.eINSTANCE.createPersonList();
		list.eAdapters().add(adapter);

		Person me = ModelFactory.eINSTANCE.createPerson();
		me.eAdapters().add(adapter);
		System.out.println("added a person");
		list.getPersons().add(me);
		System.out.println("changing persons nick");
		me.setNick("lumo");

		// add more persons
		Person ed = ModelFactory.eINSTANCE.createPerson();
		ed.setNick("Ed");
		System.out.println("adding another person");
		list.getPersons().add(ed);
	}
}


i am able to monitor if anyone got added or removed to/from the list.
but i do not get notifications for changes IN the list (altered elements)
actually i thought calling super.notifyChanged(notification); should exactly do this, but it does not (actually it makes no difference at all if i call it or not)

do i really have to add each person manually to the eadapter?
in the generated code i cant find an AdapterFactory which is mentioned in the documentation.

i tried
ModelPackage.Literals.PERSON.eAdapters().add(adapter);

which has no result on notifications (although it does not produce errors nor warnings)

1) is there a possibility to add a adapter to listen for _ALL_ changes on a Class (eg. PersonList also notifies when a containing Person gets changed)
2) can i add an adapter to ALL available instances of that class eg. Person but it does not only notify me about instance 'me' rather than 'me' and 'ed' from the example (without adding it manually to 'me' and 'ed'


PS: if you have a source/book where i can read through a how to i'd be happy
does it make sense to get this book as it is 4 years old (new version announced at the end of year)
Re: eAdapter, Notification and AdapterFactory [message #854729 is a reply to message #854704] Tue, 24 April 2012 07:33 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
Ludwig,<br>
<br>
Comments below.<br>
<br>
On 24/04/2012 9:08 AM, Ludwig Moser wrote:
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">Hello
<br>
<br>
I've read through the
<a class="moz-txt-link-freetext" href="http://help.eclipse.org/ganymede/index.jsp?topic=/org.eclipse.emf.doc/references/overview/EMF.html">http://help.eclipse.org/ganymede/index.jsp?topic=/org.eclipse.emf.doc/references/overview/EMF.html</a>,
which is pretty short for this part. (i only found stuff in the
overview)
<br>
Usually i find tutorials or Documentation which helps me finding
my way, but this time i cant find something matching (searching
the wrong place?)
<br>
</blockquote>
There is the EMF book...<br>
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">
<br>
so far as i understood i can _ONLY_ register an adapter for an
object.
<br>
</blockquote>
Yes, only on a Notifier.<br>
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">i
created a mini-example for getting a basic understanding.
<br>
<br>
a EClass Person and a EClass PersonList
<br>
<br>
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
<br>
&lt;ecore:EPackage xmi:version="2.0"
<br>
   xmlns:xmi=<a class="moz-txt-link-rfc2396E" href="http://www.omg.org/XMI">"http://www.omg.org/XMI"</a>
xmlns:xsi=<a class="moz-txt-link-rfc2396E" href="http://www.w3.org/2001/XMLSchema-instance">"http://www.w3.org/2001/XMLSchema-instance"</a>
<br>
   xmlns:ecore=<a class="moz-txt-link-rfc2396E" href="http://www.eclipse.org/emf/2002/Ecore">"http://www.eclipse.org/emf/2002/Ecore"</a>
name="model"
<br>
   nsURI=<a class="moz-txt-link-rfc2396E" href="http://model/1.0">"http://model/1.0"</a> nsPrefix="emf.model"&gt;
<br>
 &lt;eClassifiers xsi:type="ecore:EClass" name="Person"&gt;
<br>
   &lt;eStructuralFeatures xsi:type="ecore:EAttribute" name="nick"
eType="ecore:EDataType
<a class="moz-txt-link-freetext" href="http://www.eclipse.org/emf/2002/Ecore#//EString">http://www.eclipse.org/emf/2002/Ecore#//EString</a>"/&gt;
<br>
 &lt;/eClassifiers&gt;
<br>
 &lt;eClassifiers xsi:type="ecore:EClass" name="PersonList"&gt;
<br>
   &lt;eStructuralFeatures xsi:type="ecore:EReference"
name="persons" upperBound="-1"
<br>
       eType="#//Person"/&gt;
<br>
 &lt;/eClassifiers&gt;
<br>
&lt;/ecore:EPackage&gt;
<br>
<br>
<br>
so then i wrote a small test app
<br>
<br>
<br>
import org.eclipse.emf.common.notify.Notification;
<br>
import org.eclipse.emf.ecore.util.EContentAdapter;
<br>
<br>
import emf.model.ModelFactory;
<br>
import emf.model.Person;
<br>
import emf.model.PersonList;
<br>
<br>
public class Main {
<br>
<br>
    public static void main(String[] args) {
<br>
        EContentAdapter adapter = new EContentAdapter() {
<br>
            public void notifyChanged(Notification notification) {
<br>
                // it does not make any difference if i call super
or not?!
<br>
                super.notifyChanged(notification);<br>
</blockquote>
If you look at what calling super actually does, does that answer
your question?<br>
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">               
System.out.println(String.format("data changed! (%s = '%s')",
<br>
                       
notification.getNotifier().getClass().getSimpleName(),
<br>
                        notification.getNotifier()));
<br>
            }
<br>
        };
<br>
<br>
        PersonList list =
ModelFactory.eINSTANCE.createPersonList();
<br>
        list.eAdapters().add(adapter);
<br>
<br>
        Person me = ModelFactory.eINSTANCE.createPerson();
<br>
        me.eAdapters().add(adapter);
<br>
        System.out.println("added a person");
<br>
        list.getPersons().add(me);
<br>
        System.out.println("changing persons nick");
<br>
        me.setNick("lumo");
<br>
<br>
        // add more persons
<br>
        Person ed = ModelFactory.eINSTANCE.createPerson();
<br>
        ed.setNick("Ed");
<br>
        System.out.println("adding another person");
<br>
        list.getPersons().add(ed);
<br>
    }
<br>
}
<br>
<br>
<br>
i am able to monitor if anyone got added or removed to/from the
list.
<br>
</blockquote>
Yes.<br>
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">but
i do not get notifications for changes IN the list (altered
elements)
<br>
actually i thought calling super.notifyChanged(notification);
should exactly do this, but it does not (actually it makes no
difference at all if i call it or not)
<br>
</blockquote>
It will attach the adapter to any EObjects<b> contained </b>by the
EObject already being adapted.  But your PersonList doesn't have any
containment references there are no contained things to listen to.<br>
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">
<br>
do i really have to add each person manually to the eadapter?
<br>
</blockquote>
If you put everything in a resource you could ad the adapter to the
resource and it will attach to all objects contained by that
resource.<br>
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">in
the generated code i cant find an AdapterFactory which is
mentioned in the documentation.
<br>
</blockquote>
In the util package there should, by default, be an
XyzAdapterFactory (ModelAdapterFactory in your case).<br>
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">
<br>
i tried
<br>
ModelPackage.Literals.PERSON.eAdapters().add(adapter);
<br>
</blockquote>
The model itself should never change, so this is pointless.<br>
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">which
has no result on notifications (although it does not produce
errors nor warnings)
<br>
<br>
1) is there a possibility to add a adapter to listen for _ALL_
changes on a Class (eg. PersonList also notifies when a containing
Person gets changed)
<br>
</blockquote>
Person is not contained, it's only referenced via non-containment
reference.<br>
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">2)
can i add an adapter to ALL available instances of that class eg.
Person </blockquote>
No.<br>
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">but
it does not only notify me about instance 'me' rather than 'me'
and 'ed' from the example (without adding it manually to 'me' and
'ed'
<br>
</blockquote>
Note that in order to persist models, all objects need to be
contained in a resource and resources are generally contained in a
resource set, so if you follow that approach (which you'll have to
to eventually to support persistence), you can add the content
adapter to the resource set and you'll be able to monitor all
changes to all objects contained by that resource set.<br>
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">
<br>
<br>
PS: if you have a source/book where i can read through a how to
i'd be happy
<br>
does it make sense to get
<a class="moz-txt-link-freetext" href="http://www.amazon.de/EMF-Eclipse-Modeling-Framework-Edition/dp/0321331885/ref=sr_1_1?ie=UTF8&amp;qid=1335249683&amp;sr=8-1">http://www.amazon.de/EMF-Eclipse-Modeling-Framework-Edition/dp/0321331885/ref=sr_1_1?ie=UTF8&amp;qid=1335249683&amp;sr=8-1</a>
as it is 4 years old (new version announced at the end of year)
<br>
</blockquote>
Yes, everything in the second edition work as documented.<br>
</body>
</html>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: eAdapter, Notification and AdapterFactory [message #854817 is a reply to message #854729] Tue, 24 April 2012 09:01 Go to previous messageGo to next message
Ludwig Moser is currently offline Ludwig MoserFriend
Messages: 476
Registered: July 2009
Senior Member
thanks for your explanations.
actually i do not use the resource yet. i am going to store the mode in a database not in a file nor url.
does it make sense to use everything in a resource even if i do not use it to save/load?
my model will get really big with a lot of loaded objects... (up to 100.000's)

one more question regarding adding the adapters.
i have a controller and views(or any other class).
so now the controller has no personlist nor persons filled. (therefore i can't add a adapter)
then the view opens (checks if there are persons in the controller.. there are none)
then the controller gains data (persons or list of persons)
how can i inform the view?
is this the point where the AdapterFactory and/or resource jumps in?

thanks in advance
Ludwig

EDIT: i just ordered that book! Wink

[Updated on: Tue, 24 April 2012 09:38]

Report message to a moderator

Re: eAdapter, Notification and AdapterFactory [message #854860 is a reply to message #854817] Tue, 24 April 2012 09:50 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Ludwig,

Comments below.

On 24/04/2012 11:01 AM, Ludwig Moser wrote:
> thanks for your explanations.
> actually i do not use the resource yet. i am going to store the mode
> in a database not in a file nor url.
Yes, thing's like Teneo and CDO can help with that.
> does it make sense to use everything in a resource even if i do not
> use it to save/load?
Teneo and CDO both use resources.
> my model will get really big with a lot of loaded objects... (up to
> 100.000's)
CDO can handle models bigger than can fit all at once in the heap.
>
> one more question regarding adding the adapters.
> i have a controller and views(or any other class).
> so now the controller has no personlist nor persons filled. (therefore
> i can't add a adapter)
> then the view opens (checks if there are persons in the controller..
> there are none)
> then the controller gains data (persons or list of persons)
> how can i inform the view?
You're asking all kinds of problems that have out-of the-box solutions.
The generated editors provide views that update automatically. Data
binding lets you do the same.. Teneo provides the ability to persist
resources and their objects in a data base. CDO allows you to
distribute objects across processes and keep them in a repository
(backed by a data base). But you're trying to recreate all this from
the most basic low level parts with only a minimal understanding of how
those parts work, let alone how to assemble them into a working whole.
That doesn't seem like a good approach to me. I can't spend the time to
help you rediscover the solutions to a complex set of problems that are
already solved.
> is this the point where the AdapterFactory and/or resource jumps in?
EMF has support for views that update in the generated editor. Best you
learn how they work. EMF already has a persistence framework too and
things like Teneo and CDO integrate what that, so best you learn how
they work.
>
> thanks in advance
> Ludwig


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: eAdapter, Notification and AdapterFactory [message #854891 is a reply to message #854860] Tue, 24 April 2012 10:32 Go to previous messageGo to next message
Ludwig Moser is currently offline Ludwig MoserFriend
Messages: 476
Registered: July 2009
Senior Member
actually i had my own framework with notifications, interfaces etc. (all synched with ui/views)
this is what i now want to exchange against EMF. that's all - getting the app to a more standardized framework!
in mine i was able to register anything for updates even if there was no element for it.
that is why i am asking here - how this works
(as i cant look through all available plugins for eclipse to find an example how it is done)

i do know that existing generated editor provide that. but i can not use those editors as they do not fit my needs.
its not as simple as playing tetris to fit some parts together (in some cases this might be possible, but not in mine)

probably its a good idea to put my mini example online to show, people like me, how to get started with this stuff.
all i saw yet - is a good stable and quite intuitive framework - but you are lost if you do not own the book. (samples are either puristic or overwhelming.

it was not my intention to drive you crazy or waste your time. sorry if i did so.

greetings from Austria
Ludwig
Re: eAdapter, Notification and AdapterFactory [message #855874 is a reply to message #854891] Wed, 25 April 2012 07:10 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
I can only repeat what I already answered in another thread to you.

Do not track changes through Adapters but only modify your objects
through the Commands framework and inspect the command stack.

And if you really need to fit 100.000's of objects into memory I think
you won't get around CDO because you'll run out of heap fairly soon.

Tom

Am 24.04.12 12:32, schrieb Ludwig Moser:
> actually i had my own framework with notifications, interfaces etc. (all
> synched with ui/views)
> this is what i now want to exchange against EMF. that's all - getting
> the app to a more standardized framework!
> in mine i was able to register anything for updates even if there was no
> element for it.
> that is why i am asking here - how this works
> (as i cant look through all available plugins for eclipse to find an
> example how it is done)
>
> i do know that existing generated editor provide that. but i can not use
> those editors as they do not fit my needs.
> its not as simple as playing tetris to fit some parts together (in some
> cases this might be possible, but not in mine)
>
> probably its a good idea to put my mini example online to show, people
> like me, how to get started with this stuff.
> all i saw yet - is a good stable and quite intuitive framework - but you
> are lost if you do not own the book. (samples are either puristic or
> overwhelming.
>
> it was not my intention to drive you crazy or waste your time. sorry if
> i did so.
>
> greetings from Austria
> Ludwig
Re: eAdapter, Notification and AdapterFactory [message #855888 is a reply to message #855874] Wed, 25 April 2012 07:27 Go to previous messageGo to next message
Ludwig Moser is currently offline Ludwig MoserFriend
Messages: 476
Registered: July 2009
Senior Member
i'll look into the editingdomain and the commands/stack
Re: eAdapter, Notification and AdapterFactory [message #856066 is a reply to message #855888] Wed, 25 April 2012 10:37 Go to previous message
Ludwig Moser is currently offline Ludwig MoserFriend
Messages: 476
Registered: July 2009
Senior Member
i do not know why i should need the editingDomain and the stack.
as far as i can see the main profit of it is undo redo features, which i do not need (actually i do not want it at all)

modifying the objects through commands seems ok (although it blows up the code) - so i would do it if i can see a profit from it.

did i miss an important part of the commandstack&editingdomain?

thanks in advance!
ludwig
Previous Topic:Resource#save(Map) can potentially lose data
Next Topic:Inherit from non-EMF class
Goto Forum:
  


Current Time: Fri Mar 29 09:05:33 GMT 2024

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

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

Back to the top