Home » Modeling » EMF » EMF Maps and useEquals()
| |
| Re: EMF Maps and useEquals() [message #428976 is a reply to message #428975] |
Mon, 06 April 2009 07:58   |
Eclipse User |
|
|
|
Hi Ed
Thanks for your answer.
I use a command to add the map entry:
EClass eClass= XyzModelPackage.Literals.STRING_TO_EOBJECT_MAP_ENTRY;
entry= XyzModelFactory.eINSTANCE.create(eClass);
entry.setKey(key);
entry.setValue(value);
Command addCommand= AddCommand.create(editingDomain, owner,
XyzModelPackage.Literals.MYPROPERTIES, entry);
commandStack.execute(addCommand);
Obviously there is something wrong with that, but currently I can't
figure out what. Which part of EMaps am I getting wrong?
I'm happy with some pointers to where I can find help.
On 6.4.2009 13:20 Uhr, Ed Merks wrote:
> Bruno,
>
> Comments below.
>
> Bruno Ziswiler wrote:
>> Hello
>>
>> I use EMF together with Hibernate and I encounter a similar problem to
>> the one discussed here
>> http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg0 8544.html
>>
>> I have modeled a StringToEObjectMap map in my ECore model according to
>> this recipe
>> http://wiki.eclipse.org/index.php/EMF/FAQ#How_do_I_create_a_ Map_in_EMF.3F
>>
>> The problem that arises is that everytime I insert a new (key,value)
>> pair into this map a new entry is created even if the key already exists.
> If you use put, that won't happen. If you insert new entries, yes, but
> why are you doing that?
>> This is due to the fact that internally the map uses a
>> DelegateEObjectContainmentEList for its keys. This (super-)class's
>> useEquals()-method always returns false. Of course this doesn't work
>> for all strings (depending on how they are created).
> This applies for the map entries themselves. The keys are compared using
> equals. But an EMap is just a list of entries and it's possible to add
> entries with conflicting keys. The validator will complain about that
> though.
>>
>> Is there a solution for this or am I using the map wrongly?
> You've not actually explained how you've used the map, other than to say
> you insert a new key/value pair, which is not generally the best way to
> use a map, and not even possible with a regular Java map where you must
> use put.
>>
>> Thanks
>> Bruno
|
|
|
| Re: EMF Maps and useEquals() [message #428978 is a reply to message #428976] |
Mon, 06 April 2009 08:12   |
Eclipse User |
|
|
|
Bruno,
Comments below.
Bruno Ziswiler wrote:
> Hi Ed
>
> Thanks for your answer.
>
> I use a command to add the map entry:
>
> EClass eClass= XyzModelPackage.Literals.STRING_TO_EOBJECT_MAP_ENTRY;
> entry= XyzModelFactory.eINSTANCE.create(eClass);
> entry.setKey(key);
> entry.setValue(value);
> Command addCommand= AddCommand.create(editingDomain, owner,
> XyzModelPackage.Literals.MYPROPERTIES, entry);
> commandStack.execute(addCommand);
>
> Obviously there is something wrong with that, but currently I can't
> figure out what. Which part of EMaps am I getting wrong?
That's fine if you want to add a new entry, but if you want to update an
existing entry you must find it and update (set) the value feature of
that existing entry with a set command or replace that entry with a new
one with a set command at the index of the existing entry.
>
> I'm happy with some pointers to where I can find help.
I.e., you need to do what put does under the covers...
>
>
>
> On 6.4.2009 13:20 Uhr, Ed Merks wrote:
>> Bruno,
>>
>> Comments below.
>>
>> Bruno Ziswiler wrote:
>>> Hello
>>>
>>> I use EMF together with Hibernate and I encounter a similar problem to
>>> the one discussed here
>>> http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg0 8544.html
>>>
>>> I have modeled a StringToEObjectMap map in my ECore model according to
>>> this recipe
>>> http://wiki.eclipse.org/index.php/EMF/FAQ#How_do_I_create_a_ Map_in_EMF.3F
>>>
>>>
>>> The problem that arises is that everytime I insert a new (key,value)
>>> pair into this map a new entry is created even if the key already
>>> exists.
>> If you use put, that won't happen. If you insert new entries, yes, but
>> why are you doing that?
>>> This is due to the fact that internally the map uses a
>>> DelegateEObjectContainmentEList for its keys. This (super-)class's
>>> useEquals()-method always returns false. Of course this doesn't work
>>> for all strings (depending on how they are created).
>> This applies for the map entries themselves. The keys are compared using
>> equals. But an EMap is just a list of entries and it's possible to add
>> entries with conflicting keys. The validator will complain about that
>> though.
>>>
>>> Is there a solution for this or am I using the map wrongly?
>> You've not actually explained how you've used the map, other than to say
>> you insert a new key/value pair, which is not generally the best way to
>> use a map, and not even possible with a regular Java map where you must
>> use put.
>>>
>>> Thanks
>>> Bruno
|
|
|
| Re: EMF Maps and useEquals() [message #428984 is a reply to message #428978] |
Mon, 06 April 2009 10:05   |
Eclipse User |
|
|
|
That's exactly what I tried in the first place. The problem arises when
I run ChangeRecorder.beginRecording() and call
ChangeRecorder.endRecording(). BasicEList.validate() then throws an
assertion error saying "The 'no null' constraint is violated".
The first version of the code that I had, and of which I believe did
exactly what you say, was:
int idx= owner.getProperties().indexOf(key);
StringToEObjectMapEntryImpl entry= null;
Command command= null;
if (idx < 0) {
EClass eClass= XyzModelPackage.Literals.STRING_TO_EOBJECT_MAP_ENTRY;
entry= (StringToEObjectMapEntryImpl)
XyzModelFactory.eINSTANCE.create(eClass);
entry.setKey(key);
entry.setValue(value);
command= AddCommand.create(editingDomain, owner,
XyzModelPackage.Literals.MYELEMENT__MYPROPERTIES, entry);
} else {
command= SetCommand.create(editingDomain, entry,
XyzModelPackage.Literals.STRING_TO_EOBJECT_MAP_ENTRY__VALUE, value);
}
commandStack.execute(command);
Does this look as if it should work?
On 6.4.2009 14:12 Uhr, Ed Merks wrote:
> Bruno,
>
> Comments below.
>
>
> Bruno Ziswiler wrote:
>> Hi Ed
>>
>> Thanks for your answer.
>>
>> I use a command to add the map entry:
>>
>> EClass eClass= XyzModelPackage.Literals.STRING_TO_EOBJECT_MAP_ENTRY;
>> entry= XyzModelFactory.eINSTANCE.create(eClass);
>> entry.setKey(key);
>> entry.setValue(value);
>> Command addCommand= AddCommand.create(editingDomain, owner,
>> XyzModelPackage.Literals.MYPROPERTIES, entry);
>> commandStack.execute(addCommand);
>>
>> Obviously there is something wrong with that, but currently I can't
>> figure out what. Which part of EMaps am I getting wrong?
> That's fine if you want to add a new entry, but if you want to update an
> existing entry you must find it and update (set) the value feature of
> that existing entry with a set command or replace that entry with a new
> one with a set command at the index of the existing entry.
>>
>> I'm happy with some pointers to where I can find help.
> I.e., you need to do what put does under the covers...
>>
>>
>>
>> On 6.4.2009 13:20 Uhr, Ed Merks wrote:
>>> Bruno,
>>>
>>> Comments below.
>>>
>>> Bruno Ziswiler wrote:
>>>> Hello
>>>>
>>>> I use EMF together with Hibernate and I encounter a similar problem to
>>>> the one discussed here
>>>> http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg0 8544.html
>>>>
>>>> I have modeled a StringToEObjectMap map in my ECore model according to
>>>> this recipe
>>>> http://wiki.eclipse.org/index.php/EMF/FAQ#How_do_I_create_a_ Map_in_EMF.3F
>>>>
>>>>
>>>> The problem that arises is that everytime I insert a new (key,value)
>>>> pair into this map a new entry is created even if the key already
>>>> exists.
>>> If you use put, that won't happen. If you insert new entries, yes, but
>>> why are you doing that?
>>>> This is due to the fact that internally the map uses a
>>>> DelegateEObjectContainmentEList for its keys. This (super-)class's
>>>> useEquals()-method always returns false. Of course this doesn't work
>>>> for all strings (depending on how they are created).
>>> This applies for the map entries themselves. The keys are compared using
>>> equals. But an EMap is just a list of entries and it's possible to add
>>> entries with conflicting keys. The validator will complain about that
>>> though.
>>>>
>>>> Is there a solution for this or am I using the map wrongly?
>>> You've not actually explained how you've used the map, other than to say
>>> you insert a new key/value pair, which is not generally the best way to
>>> use a map, and not even possible with a regular Java map where you must
>>> use put.
>>>>
>>>> Thanks
>>>> Bruno
|
|
|
| Re: EMF Maps and useEquals() [message #428986 is a reply to message #428984] |
Mon, 06 April 2009 10:18   |
Eclipse User |
|
|
|
Bruno,
Comments below.
Bruno Ziswiler wrote:
> That's exactly what I tried in the first place. The problem arises
> when I run ChangeRecorder.beginRecording() and call
> ChangeRecorder.endRecording(). BasicEList.validate() then throws an
> assertion error saying "The 'no null' constraint is violated".
>
> The first version of the code that I had, and of which I believe did
> exactly what you say, was:
>
>
> int idx= owner.getProperties().indexOf(key);
Did you mean this to be indexOfKey? I imagine the above would return -1
always.
> StringToEObjectMapEntryImpl entry= null;
> Command command= null;
>
> if (idx < 0) {
> EClass eClass= XyzModelPackage.Literals.STRING_TO_EOBJECT_MAP_ENTRY;
> entry= (StringToEObjectMapEntryImpl)
> XyzModelFactory.eINSTANCE.create(eClass);
> entry.setKey(key);
> entry.setValue(value);
> command= AddCommand.create(editingDomain, owner,
> XyzModelPackage.Literals.MYELEMENT__MYPROPERTIES, entry);
>
> } else {
> command= SetCommand.create(editingDomain, entry,
> XyzModelPackage.Literals.STRING_TO_EOBJECT_MAP_ENTRY__VALUE, value);
> }
> commandStack.execute(command);
>
> Does this look as if it should work?
>
>
> On 6.4.2009 14:12 Uhr, Ed Merks wrote:
>> Bruno,
>>
>> Comments below.
>>
>>
>> Bruno Ziswiler wrote:
>>> Hi Ed
>>>
>>> Thanks for your answer.
>>>
>>> I use a command to add the map entry:
>>>
>>> EClass eClass= XyzModelPackage.Literals.STRING_TO_EOBJECT_MAP_ENTRY;
>>> entry= XyzModelFactory.eINSTANCE.create(eClass);
>>> entry.setKey(key);
>>> entry.setValue(value);
>>> Command addCommand= AddCommand.create(editingDomain, owner,
>>> XyzModelPackage.Literals.MYPROPERTIES, entry);
>>> commandStack.execute(addCommand);
>>>
>>> Obviously there is something wrong with that, but currently I can't
>>> figure out what. Which part of EMaps am I getting wrong?
>> That's fine if you want to add a new entry, but if you want to update an
>> existing entry you must find it and update (set) the value feature of
>> that existing entry with a set command or replace that entry with a new
>> one with a set command at the index of the existing entry.
>>>
>>> I'm happy with some pointers to where I can find help.
>> I.e., you need to do what put does under the covers...
>>>
>>>
>>>
>>> On 6.4.2009 13:20 Uhr, Ed Merks wrote:
>>>> Bruno,
>>>>
>>>> Comments below.
>>>>
>>>> Bruno Ziswiler wrote:
>>>>> Hello
>>>>>
>>>>> I use EMF together with Hibernate and I encounter a similar
>>>>> problem to
>>>>> the one discussed here
>>>>> http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg0 8544.html
>>>>>
>>>>> I have modeled a StringToEObjectMap map in my ECore model
>>>>> according to
>>>>> this recipe
>>>>> http://wiki.eclipse.org/index.php/EMF/FAQ#How_do_I_create_a_ Map_in_EMF.3F
>>>>>
>>>>>
>>>>>
>>>>> The problem that arises is that everytime I insert a new (key,value)
>>>>> pair into this map a new entry is created even if the key already
>>>>> exists.
>>>> If you use put, that won't happen. If you insert new entries, yes, but
>>>> why are you doing that?
>>>>> This is due to the fact that internally the map uses a
>>>>> DelegateEObjectContainmentEList for its keys. This (super-)class's
>>>>> useEquals()-method always returns false. Of course this doesn't work
>>>>> for all strings (depending on how they are created).
>>>> This applies for the map entries themselves. The keys are compared
>>>> using
>>>> equals. But an EMap is just a list of entries and it's possible to add
>>>> entries with conflicting keys. The validator will complain about that
>>>> though.
>>>>>
>>>>> Is there a solution for this or am I using the map wrongly?
>>>> You've not actually explained how you've used the map, other than
>>>> to say
>>>> you insert a new key/value pair, which is not generally the best
>>>> way to
>>>> use a map, and not even possible with a regular Java map where you
>>>> must
>>>> use put.
>>>>>
>>>>> Thanks
>>>>> Bruno
|
|
|
| Re: EMF Maps and useEquals() [message #429033 is a reply to message #428986] |
Tue, 07 April 2009 01:20   |
Eclipse User |
|
|
|
That's it! I'm terribly sorry you had to waste your time by helping me
debug such a trivial mistake. I owe you a drink at EclipseCon 2010! :-)
On 6.4.2009 16:18 Uhr, Ed Merks wrote:
> Bruno,
>
> Comments below.
>
>
> Bruno Ziswiler wrote:
>> That's exactly what I tried in the first place. The problem arises
>> when I run ChangeRecorder.beginRecording() and call
>> ChangeRecorder.endRecording(). BasicEList.validate() then throws an
>> assertion error saying "The 'no null' constraint is violated".
>>
>> The first version of the code that I had, and of which I believe did
>> exactly what you say, was:
>>
>>
>> int idx= owner.getProperties().indexOf(key);
> Did you mean this to be indexOfKey? I imagine the above would return -1
> always.
>> StringToEObjectMapEntryImpl entry= null;
>> Command command= null;
>>
>> if (idx < 0) {
>> EClass eClass= XyzModelPackage.Literals.STRING_TO_EOBJECT_MAP_ENTRY;
>> entry= (StringToEObjectMapEntryImpl)
>> XyzModelFactory.eINSTANCE.create(eClass);
>> entry.setKey(key);
>> entry.setValue(value);
>> command= AddCommand.create(editingDomain, owner,
>> XyzModelPackage.Literals.MYELEMENT__MYPROPERTIES, entry);
>>
>> } else {
>> command= SetCommand.create(editingDomain, entry,
>> XyzModelPackage.Literals.STRING_TO_EOBJECT_MAP_ENTRY__VALUE, value);
>> }
>> commandStack.execute(command);
>>
>> Does this look as if it should work?
>>
>>
>> On 6.4.2009 14:12 Uhr, Ed Merks wrote:
>>> Bruno,
>>>
>>> Comments below.
>>>
>>>
>>> Bruno Ziswiler wrote:
>>>> Hi Ed
>>>>
>>>> Thanks for your answer.
>>>>
>>>> I use a command to add the map entry:
>>>>
>>>> EClass eClass= XyzModelPackage.Literals.STRING_TO_EOBJECT_MAP_ENTRY;
>>>> entry= XyzModelFactory.eINSTANCE.create(eClass);
>>>> entry.setKey(key);
>>>> entry.setValue(value);
>>>> Command addCommand= AddCommand.create(editingDomain, owner,
>>>> XyzModelPackage.Literals.MYPROPERTIES, entry);
>>>> commandStack.execute(addCommand);
>>>>
>>>> Obviously there is something wrong with that, but currently I can't
>>>> figure out what. Which part of EMaps am I getting wrong?
>>> That's fine if you want to add a new entry, but if you want to update an
>>> existing entry you must find it and update (set) the value feature of
>>> that existing entry with a set command or replace that entry with a new
>>> one with a set command at the index of the existing entry.
>>>>
>>>> I'm happy with some pointers to where I can find help.
>>> I.e., you need to do what put does under the covers...
>>>>
>>>>
>>>>
>>>> On 6.4.2009 13:20 Uhr, Ed Merks wrote:
>>>>> Bruno,
>>>>>
>>>>> Comments below.
>>>>>
>>>>> Bruno Ziswiler wrote:
>>>>>> Hello
>>>>>>
>>>>>> I use EMF together with Hibernate and I encounter a similar
>>>>>> problem to
>>>>>> the one discussed here
>>>>>> http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg0 8544.html
>>>>>>
>>>>>> I have modeled a StringToEObjectMap map in my ECore model
>>>>>> according to
>>>>>> this recipe
>>>>>> http://wiki.eclipse.org/index.php/EMF/FAQ#How_do_I_create_a_ Map_in_EMF.3F
>>>>>>
>>>>>>
>>>>>>
>>>>>> The problem that arises is that everytime I insert a new (key,value)
>>>>>> pair into this map a new entry is created even if the key already
>>>>>> exists.
>>>>> If you use put, that won't happen. If you insert new entries, yes, but
>>>>> why are you doing that?
>>>>>> This is due to the fact that internally the map uses a
>>>>>> DelegateEObjectContainmentEList for its keys. This (super-)class's
>>>>>> useEquals()-method always returns false. Of course this doesn't work
>>>>>> for all strings (depending on how they are created).
>>>>> This applies for the map entries themselves. The keys are compared
>>>>> using
>>>>> equals. But an EMap is just a list of entries and it's possible to add
>>>>> entries with conflicting keys. The validator will complain about that
>>>>> though.
>>>>>>
>>>>>> Is there a solution for this or am I using the map wrongly?
>>>>> You've not actually explained how you've used the map, other than
>>>>> to say
>>>>> you insert a new key/value pair, which is not generally the best
>>>>> way to
>>>>> use a map, and not even possible with a regular Java map where you
>>>>> must
>>>>> use put.
>>>>>>
>>>>>> Thanks
>>>>>> Bruno
|
|
|
| Re: EMF Maps and useEquals() [message #429059 is a reply to message #429033] |
Tue, 07 April 2009 06:17  |
Eclipse User |
|
|
|
Bruno,
Let's blame Java. If the indexOf method were of type E rather than
Object, the compiler would have caught the mistake. :-P
Maybe you'll be at Eclipse Summit Europe this year. I hope to be there...
Bruno Ziswiler wrote:
> That's it! I'm terribly sorry you had to waste your time by helping me
> debug such a trivial mistake. I owe you a drink at EclipseCon 2010! :-)
>
>
> On 6.4.2009 16:18 Uhr, Ed Merks wrote:
>> Bruno,
>>
>> Comments below.
>>
>>
>> Bruno Ziswiler wrote:
>>> That's exactly what I tried in the first place. The problem arises
>>> when I run ChangeRecorder.beginRecording() and call
>>> ChangeRecorder.endRecording(). BasicEList.validate() then throws an
>>> assertion error saying "The 'no null' constraint is violated".
>>>
>>> The first version of the code that I had, and of which I believe did
>>> exactly what you say, was:
>>>
>>>
>>> int idx= owner.getProperties().indexOf(key);
>> Did you mean this to be indexOfKey? I imagine the above would return -1
>> always.
>>> StringToEObjectMapEntryImpl entry= null;
>>> Command command= null;
>>>
>>> if (idx < 0) {
>>> EClass eClass= XyzModelPackage.Literals.STRING_TO_EOBJECT_MAP_ENTRY;
>>> entry= (StringToEObjectMapEntryImpl)
>>> XyzModelFactory.eINSTANCE.create(eClass);
>>> entry.setKey(key);
>>> entry.setValue(value);
>>> command= AddCommand.create(editingDomain, owner,
>>> XyzModelPackage.Literals.MYELEMENT__MYPROPERTIES, entry);
>>>
>>> } else {
>>> command= SetCommand.create(editingDomain, entry,
>>> XyzModelPackage.Literals.STRING_TO_EOBJECT_MAP_ENTRY__VALUE, value);
>>> }
>>> commandStack.execute(command);
>>>
>>> Does this look as if it should work?
>>>
>>>
>>> On 6.4.2009 14:12 Uhr, Ed Merks wrote:
>>>> Bruno,
>>>>
>>>> Comments below.
>>>>
>>>>
>>>> Bruno Ziswiler wrote:
>>>>> Hi Ed
>>>>>
>>>>> Thanks for your answer.
>>>>>
>>>>> I use a command to add the map entry:
>>>>>
>>>>> EClass eClass= XyzModelPackage.Literals.STRING_TO_EOBJECT_MAP_ENTRY;
>>>>> entry= XyzModelFactory.eINSTANCE.create(eClass);
>>>>> entry.setKey(key);
>>>>> entry.setValue(value);
>>>>> Command addCommand= AddCommand.create(editingDomain, owner,
>>>>> XyzModelPackage.Literals.MYPROPERTIES, entry);
>>>>> commandStack.execute(addCommand);
>>>>>
>>>>> Obviously there is something wrong with that, but currently I can't
>>>>> figure out what. Which part of EMaps am I getting wrong?
>>>> That's fine if you want to add a new entry, but if you want to
>>>> update an
>>>> existing entry you must find it and update (set) the value feature of
>>>> that existing entry with a set command or replace that entry with a
>>>> new
>>>> one with a set command at the index of the existing entry.
>>>>>
>>>>> I'm happy with some pointers to where I can find help.
>>>> I.e., you need to do what put does under the covers...
>>>>>
>>>>>
>>>>>
>>>>> On 6.4.2009 13:20 Uhr, Ed Merks wrote:
>>>>>> Bruno,
>>>>>>
>>>>>> Comments below.
>>>>>>
>>>>>> Bruno Ziswiler wrote:
>>>>>>> Hello
>>>>>>>
>>>>>>> I use EMF together with Hibernate and I encounter a similar
>>>>>>> problem to
>>>>>>> the one discussed here
>>>>>>> http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg0 8544.html
>>>>>>>
>>>>>>>
>>>>>>> I have modeled a StringToEObjectMap map in my ECore model
>>>>>>> according to
>>>>>>> this recipe
>>>>>>> http://wiki.eclipse.org/index.php/EMF/FAQ#How_do_I_create_a_ Map_in_EMF.3F
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> The problem that arises is that everytime I insert a new
>>>>>>> (key,value)
>>>>>>> pair into this map a new entry is created even if the key already
>>>>>>> exists.
>>>>>> If you use put, that won't happen. If you insert new entries,
>>>>>> yes, but
>>>>>> why are you doing that?
>>>>>>> This is due to the fact that internally the map uses a
>>>>>>> DelegateEObjectContainmentEList for its keys. This (super-)class's
>>>>>>> useEquals()-method always returns false. Of course this doesn't
>>>>>>> work
>>>>>>> for all strings (depending on how they are created).
>>>>>> This applies for the map entries themselves. The keys are compared
>>>>>> using
>>>>>> equals. But an EMap is just a list of entries and it's possible
>>>>>> to add
>>>>>> entries with conflicting keys. The validator will complain about
>>>>>> that
>>>>>> though.
>>>>>>>
>>>>>>> Is there a solution for this or am I using the map wrongly?
>>>>>> You've not actually explained how you've used the map, other than
>>>>>> to say
>>>>>> you insert a new key/value pair, which is not generally the best
>>>>>> way to
>>>>>> use a map, and not even possible with a regular Java map where you
>>>>>> must
>>>>>> use put.
>>>>>>>
>>>>>>> Thanks
>>>>>>> Bruno
|
|
|
Goto Forum:
Current Time: Wed Nov 05 12:00:25 EST 2025
Powered by FUDForum. Page generated in 0.05278 seconds
|