Looking for an EObject inside an EList [message #1139137] |
Tue, 15 October 2013 15:10 |
d 627 Messages: 28 Registered: March 2013 |
Junior Member |
|
|
I got this problem. I am working with different objects that belong to different classes. What I am trying to do is to see if a object is equal or not to another one that is possibly inside another object using a recursive algorithm:
private static void foundObject(EObject obj, EObject set) {
if(EcoreUtil.equals(obj, set)){
System.out.println("The objects are the same: \n\t"+obj+"\n\t"+set);
return;
} else {
System.out.println("The objects are not the same: \n\t"+obj+"\n\t"+set+" "+set.eContents());
if(set.eContents().size()>0){
for(EObject child: set.eContents()){
foundObject(obj, child);
}
}
}
}
And I want to see if a Object of the Assignment class is inside or not the group:
Assignment
Group [Assignment, Keyword]
As you can see, inside the Group object there is a list with the object that I want to find. In this case I am having the next output:
The objects are not the same:
Assignment
Group [Assignment, Keyword]
The objects are the same:
Assignment
Assignment
The objects are not the same:
Assignment
Keyword
Which is right. The problem is, when the initial group gets another Assignment element
Group [Assignment, Keyword, Assignment]
the algorithm does not see the corresponding match and provides an output like this:
The objects are not the same:
Assignment
Group [Assignment, Keyword, Assignment]
The objects are not the same:
Assignment
Assignment
The objects are not the same:
Assignment
RuleCall
The objects are not the same:
Assignment
Keyword
The objects are not the same:
Assignment
Assignment
The objects are not the same:
Assignment
RuleCall
But in my case the second pair of elements are the same (that is why I am using ECoreUtil.equals). Any idea about why this could be happening?
|
|
|
Re: Looking for an EObject inside an EList [message #1140193 is a reply to message #1139137] |
Wed, 16 October 2013 07:34 |
|
EcoreUtil.equals is far from being a foolproof mean of comparing EObjects. What it does is compare structurally and return true if the two objects have the same EClass and equal features. With this in mind, you will most likely have false positives ... and you will also easily have wrong reports of "equal" elements being reported as "different".
EMF Compare 2 would be able to meet your needs here, or you can try subclassing EcoreUtil.EqualityHelper to suit your needs, though that does not seem like an easy endeavour.
Laurent Goubet
Obeo
|
|
|
Powered by
FUDForum. Page generated in 0.01528 seconds