Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Traverse an EMF tree
Traverse an EMF tree [message #541873] Tue, 22 June 2010 20:54 Go to next message
bachra04  is currently offline bachra04 Friend
Messages: 148
Registered: April 2010
Senior Member
Hi Experts,

I want to traverse an EMF tree to get all the classes and put them inside a map. Is there any sample code doing that?

Thanks
Re: Traverse an EMF tree [message #541894 is a reply to message #541873] Wed, 23 June 2010 02:13 Go to previous messageGo to next message
Bryan Hunt is currently offline Bryan HuntFriend
Messages: 366
Registered: July 2009
Senior Member
Yes, there is a very simple way to do this. Assuming you generated your code from an ecore model, you whould have generated a switch class in the util package. Create a class that extends that switch class and override the methods for your parent and child types. For example:

public class ModelTraversal extends ModelSwitch<Boolean>
{
  @Override
  public Boolean caseParentType(ParentType object)
  {
    for(AbstractType child : object.getChildren())
      doSwitch(child);

    return Boolean.TRUE;
  }

  @Override
  public Boolean case ChildType(ChildType object)
  {
    // do something with the child
    return Boolean.TRUE;
  }
}


and to use the switch:

ModelTraversal mt = new ModelTraversal();
mt.doSwitch(model);
Re: Traverse an EMF tree [message #541928 is a reply to message #541873] Wed, 23 June 2010 08:53 Go to previous messageGo to next message
Cedric Brun is currently offline Cedric BrunFriend
Messages: 431
Registered: July 2009
Senior Member
Hi,

EMF perfectly fits with Google Collections, you can write somethings like

Iterator<MyClass> it =
Iterators.filter(root.eAllContents(),my.package.MyClass.clas s);
while (it.hasNext())
{
MyClass clazz = it.next();
//process the class
}




bachra04 wrote:

> Hi Experts,
>
> I want to traverse an EMF tree to get all the classes and put them inside
> a map. Is there any sample code doing that?
>
> Thanks


http://cedric.brun.io news and articles on eclipse and eclipse modeling.
Re: Traverse an EMF tree [message #542379 is a reply to message #541928] Thu, 24 June 2010 16:18 Go to previous messageGo to next message
bachra04  is currently offline bachra04 Friend
Messages: 148
Registered: April 2010
Senior Member
Are you referring here to java.util.Iterator?
I couldn't find filter method ?
Re: Traverse an EMF tree [message #542381 is a reply to message #542379] Thu, 24 June 2010 16:21 Go to previous messageGo to next message
bachra04  is currently offline bachra04 Friend
Messages: 148
Registered: April 2010
Senior Member
Sorry I got it Google collection library Surprised
Re: Traverse an EMF tree [message #542387 is a reply to message #542379] Thu, 24 June 2010 16:34 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
com.google.common.collect.Iterators.

bachra04 wrote:
> Are you referring here to java.util.Iterator?
> I couldn't find filter method ?


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Traverse an EMF tree [message #542411 is a reply to message #542387] Thu, 24 June 2010 18:14 Go to previous messageGo to next message
bachra04  is currently offline bachra04 Friend
Messages: 148
Registered: April 2010
Senior Member
I had issues with adding to my created plugin, is there any documentation or example on how to add this library toyour plugin ?

Thanks
Re: Traverse an EMF tree [message #542417 is a reply to message #542411] Thu, 24 June 2010 18:24 Go to previous messageGo to next message
bachra04  is currently offline bachra04 Friend
Messages: 148
Registered: April 2010
Senior Member
I had the following error:

Package 'com.google.common.collect' does not exist in this plug-in
Re: Traverse an EMF tree [message #542420 is a reply to message #542417] Thu, 24 June 2010 18:49 Go to previous messageGo to next message
bachra04  is currently offline bachra04 Friend
Messages: 148
Registered: April 2010
Senior Member
More info:

- Access restriction: The type Iterators is not accessible due to restriction on required library C:\dev\google-collect-0.5-20080322.110711-2.jar
- Access restriction: The method filter(Iterator<?>, Class<EObject>) from the type Iterators is not accessible due to restriction on required library C:\dev\google-
collect-0.5-20080322.110711-2.jar
Re: Traverse an EMF tree [message #542432 is a reply to message #542420] Thu, 24 June 2010 19:38 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
It's not an EMF question. I think the Orbit project has a p2 repository
with a bundlized version of this library.


bachra04 wrote:
> More info:
>
> - Access restriction: The type Iterators is not accessible due to
> restriction on required library
> C:\dev\google-collect-0.5-20080322.110711-2.jar
> - Access restriction: The method filter(Iterator<?>,
> Class<EObject>) from the type Iterators is not accessible due to
> restriction on required library C:\dev\google-
> collect-0.5-20080322.110711-2.jar


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Traverse an EMF tree [message #542763 is a reply to message #542432] Fri, 25 June 2010 22:30 Go to previous message
bachra04  is currently offline bachra04 Friend
Messages: 148
Registered: April 2010
Senior Member
Yep Thanks Ed, I already fix the problem
Previous Topic:[CDO] xbcmodel
Next Topic:[net4j]Missing org.eclipse.net4.db.mysql
Goto Forum:
  


Current Time: Fri Apr 26 07:48:03 GMT 2024

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

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

Back to the top