I had this problem where I would add an object to a EObject, and then later in the code it would not be there. This became a large puzzle to me, I am not an expert in EMF, but after much trial and error, I have found the problem.
Containment!! This is a greatly annoying problem. EMF is smart, it sets an owner for an object. If you add it to a EList where .isContainment() returns true, it will remove its self from its previous container and put it in the new one. This is also how the serialization works out if an object is not contained anywhere.
My solution to this, was pretty hacky, though turning out to be extremely useful. I set up a tree of clone() methods where it would clone an object down to its primary information. This is a little expencive, but it is sometimes necessary. For instance I have an object that is a container, I want to generate another container from that with a slightly different state, this is where the clone method is necessary.
The implications from this are that the standard containment list, EObjectContainmentEList, doenst use .equals() when checking for containment, so I had to override this, it is easy, but a bit of a hack. It requires some duplicate code to a super super class, because of some optimazation code in the super class.
0 comments:
Post a Comment