Home » Modeling » EMF » two instance but same identity : problem with Map.contains
two instance but same identity : problem with Map.contains [message #757461] |
Fri, 18 November 2011 14:26 |
jonathan Missing name Messages: 12 Registered: August 2010 |
Junior Member |
|
|
Hi all,
i have 2 models A and B; A contain one object called "o" for example and B refers this object.
When i load model A then model B, i have in memory two instances of the same object "o" (ie identity); This two instances can be called "o1" and "o2".
The problem is the following :
If i put "o1" in a list and i call list.contains(o2), it return false (normal, it is 2 different instances). But for me, "o1" and "o2" have the same identity so i would like the list returns true.
How can i resolve this? can i overload equals methods? or there a special list which take account the "identity" of emf objects?
Thanks in advance.
[Updated on: Fri, 18 November 2011 14:42] Report message to a moderator
|
|
| | |
Re: two instance but same identity : problem with Map.contains [message #760226 is a reply to message #758154] |
Thu, 01 December 2011 16:10 |
jonathan Missing name Messages: 12 Registered: August 2010 |
Junior Member |
|
|
Hello,
I tried your method in an other situation but without success :
I have one Model with two objects and a bi directional relation between both.
objects. And i succeed to split an instance of this model in two resources like following :
file 1 : object A href="file 2#//object B"
file 2 : object B href="file 1#//object A"
However, when i try to load this two resources even i use the same resource set, i obtain duplicate object in memory : object B referenced by A when i load file 1 is not the same that object B when i load file 2.
I don't know why.
[Updated on: Thu, 01 December 2011 17:01] Report message to a moderator
|
|
| | |
Re: two instance but same identity : problem with Map.contains [message #760369 is a reply to message #760367] |
Fri, 02 December 2011 08:16 |
Ed Merks Messages: 33141 Registered: July 2009 |
Senior Member |
|
|
Jonathan,
Comments below.
On 02/12/2011 8:58 AM, jonathan wrote:
> Ed,
>
> the number of resource evolve like following
> _ I create a resource set "rs"
> _ I get the resource associated with file 1 with
> rs.getResource(<file1>, true)
> _ EMF create the resource with contents but when i check resources
> contain in resource set, there is still one. At this point, i have an
> object B in memory but with attribute no complete when i look in
> content of resource.
Is eIsProxy true for B? If it's true, you can cast it to
InternalEObject and check the value of eProxyURI. What value is that?
If it's false, what's the value of B.eResource().getURI()?
> _ I get the resource associated with file 2 with
> rs.getResource(<file2>, true)
You're sure there are only two resource in the resource set at this point?
> _ EMF create the resource with contents and now, there are both
> resources. However, I obtain an new object B in memory with correct
> attribute but it is not the same instance than before. Of course, i
> have the same difference with both object B.
There must be some reason that the reference of A to B is an unresolved
proxy. But it's almost impossible to imagine that you won't have three
resources in the resource set because calling A.getB() will attempt to
resolve the proxy and that inevitably will lead to a resource being
added to the resource set. So I imagine there are three resources.
In your follow up question, be sure to specify exactly the value of
EcoreUtil.getURI of the two different instances of B.
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| |
Re: two instance but same identity : problem with Map.contains [message #760389 is a reply to message #760386] |
Fri, 02 December 2011 09:14 |
Ed Merks Messages: 33141 Registered: July 2009 |
Senior Member |
|
|
Jonathan,
I'm not sure why you'd need to resolve a proxy before assigning null,
but EcoreUtil.resolveAll can be used to force all proxies to resolve.
On 02/12/2011 10:08 AM, jonathan wrote:
> Ed,
> i forgot to tell you something : i check memory using debugger of
> eclipse. And until now, i have not used A.getB() in my code. I
> understand what happened. After calling A.getB(), EMF lazy load object
> B correctly in memory by resolving the proxy while before call,
> debugger display a "stub" of object B.
>
> However, when i use only A.setB(null), there no proxy resolution
> before set B to null expect if i call A.getB() to force proxy
> resolution before call A.setB(null).
>
> is there an better elegant method to force the proxy resolution at the
> beginning than call getB() before setB()?
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| | | |
Re: two instance but same identity : problem with Map.contains [message #760418 is a reply to message #760414] |
Fri, 02 December 2011 10:25 |
Ed Merks Messages: 33141 Registered: July 2009 |
Senior Member |
|
|
Jonathan,
No, I'm just remarking that it seems unusual to modify an object without
inspecting the current value in the first place. After all, setting it
to null when it's already null is pointless. In an editor, commands
would be used, and they'd need to record the original value for undo
support.
On 02/12/2011 11:20 AM, jonathan wrote:
> Ed,
>
> until now, i have not token habit to control the value of attribute
> (or reference here) before to assign it. But i suppose in EMF, the
> cost of get function is lesser than a call of set function.
> That why you suggest me to control the value with calling get function
> before doing a assignement?
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| |
Goto Forum:
Current Time: Fri Apr 26 00:51:18 GMT 2024
Powered by FUDForum. Page generated in 0.04064 seconds
|