Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Compare » Looking for an EObject inside an EList
Looking for an EObject inside an EList [message #1139137] Tue, 15 October 2013 15:10 Go to next message
d 627 is currently offline d 627Friend
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 Go to previous message
Laurent Goubet is currently offline Laurent GoubetFriend
Messages: 1638
Registered: July 2009
Senior Member
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
Previous Topic:Comparing Objects with EMFCompare
Next Topic:EMF Compare - two equivalent updateReference seen as different
Goto Forum:
  


Current Time: Fri Nov 21 02:32:10 GMT 2014

Powered by FUDForum. Page generated in 0.02513 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software