How to compare equality between EObject when they contained unordered EList?
Asked Answered
M

3

8

I'm still a novice in EMF so maybe my question doesn't really make sense or I assume wrong things.

I'm working on a model-driven project, so I defined an ecore metamodel and generated the associate code. Now I'm currently trying to make unit tests and I need to be able to test equality between objects and more particularly between objects which extend EObject.

I tried to use EcoreUtil.equals() to make my tests but it always returns false as my objects contains references in lists (class EList) that are not ordered the same way. However, I explicitly defined in my metamodel that references are not ordered: I want to use them more like Set than List.

So, I finally decided to implements my own equals methods in my genereated *Impl Class, even if its discouraged in the javadoc, but it there another way, more elegant, to test the structural equality between EMF objects without taking into account the order of lists?

Thanks!

Mervin answered 30/8, 2012 at 15:8 Comment(0)
C
2

You can implement your own class of utilities where you code your own comparison for unordered lists using the EObject default equals method.

You can base your implementation in the EqualityHelper.equals(List list1, List list2) using list "contains" instead of going by index as that method does.

Checani answered 16/4, 2013 at 20:32 Comment(0)
N
0

I wrote the following utility methods in Xtend:

static def boolean equalsTo(Set<? extends EObject> eObjectSet1, Set<? extends EObject> eObjectSet2) {
    contains(eObjectSet1, eObjectSet2) && contains(eObjectSet2, eObjectSet1)
}

static def boolean contains(Set<? extends EObject> eObjectSet1, Set<? extends EObject> eObjectSet2) {
    eObjectSet1.forall[eObject1 | eObjectSet2.exists[eObject2 | EcoreUtil.equals(eObject1, eObject2)]]
}
Noisemaker answered 27/1, 2014 at 12:18 Comment(1)
I don't know Xtend, but to me it seems line this code does not take in the consideration duplicate elements in the lists. So [1] and [1, 1, 1] will be reported as equal.Ankylosis
A
0

This could probably be solved by using an EMap, with the element of the former list as the key and an arbitrary other type, for example EString, as value.

If an element should be in the set, map it to the empty string.

EMap<SomeClass, EString> someSet;

If the list could contain multiple elements, map it to and integer that gives the number of elements.

Ankylosis answered 10/9, 2016 at 16:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.