Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Best way of traversing a model: bottom up
Best way of traversing a model: bottom up [message #1389132] Fri, 27 June 2014 11:37 Go to next message
Alexander R is currently offline Alexander RFriend
Messages: 211
Registered: July 2013
Senior Member
Hello everyone,

I've implemented a search algorithm, that finds a specific root-element from a start-element in my model. But I'm not really sure if this function is safe enough or not.

Is there maybe a better algorithms in the API who manages the same job?

Here my algorithme:
        /**
	 * Finds the specific root element or a superClass of the specific root
	 * element. The startingpoint is the startElement.
	 * 
	 * @param startElement
	 *            : an EObject as a startElement
	 * @param specificRootElement
	 *            : The root element you are searching or one of its
	 *            superclasses.
	 * @return EObject: A specific root element
	 * @throws ClassNotFoundException
	 */
	public static <T> EObject findSpecificParentElement(EObject startElement, EClass specificRootElement)
			throws ClassNotFoundException {

		EObject parent = startElement.eContainer();
		EClass parentClass = parent.eClass();

		// if the searched class is the parentclass or if the searched class is
		// one of the superClasses

		if ((parent != null && !(parentClass == specificRootElement || EcoreUtil2.getAllSuperTypes(parentClass).contains(
				specificRootElement)))) {
			return findSpecificParentElement(parent, specificRootElement);
		} else {
			return parent;
		}
	}



regards,

Alex

[Updated on: Fri, 27 June 2014 11:43]

Report message to a moderator

Re: Best way of traversing a model [message #1389164 is a reply to message #1389132] Fri, 27 June 2014 12:22 Go to previous messageGo to next message
Felix Dorner is currently offline Felix DornerFriend
Messages: 295
Registered: March 2012
Senior Member
On 27/06/2014 13:37, Alexander R wrote:
> Hello everyone,
>
> I've implemented a search algorithm, that finds a specific root-element
> from a start-element in my model. But I'm not really sure if this
> function is safe enough or not.

- Be careful whether you want to test the start element itself too.
- Don't throw a ClassNotFoundException there. Not unless you want to
torture callers.
- EcoreUtil2 is provided by?

> Is there maybe a better algorithms in the API who manages the same job?

Maybe you could use EClassifier.isInstance() to save a penny.

Btw, ask such general EMF questions better on the EMF forum.

Felix
Re: Best way of traversing a model: bottom up [message #1389169 is a reply to message #1389132] Fri, 27 June 2014 12:30 Go to previous messageGo to next message
Uli Merkel is currently offline Uli MerkelFriend
Messages: 250
Registered: June 2013
Senior Member
Hi Alexander,

I had a similar question on:
https://www.eclipse.org/forums/index.php/t/781743/

Looks like the answer from Christian Dietrich is the best for your scenario:
EcoreUtil2.getContainerOfType(ctx, Model.class)
to access the root type (i asume it is Model) from any eObject ctx

Greetings from Frankfurt/Germany, Uli

P.S. Just saw that Christian has replied as well

[Updated on: Fri, 27 June 2014 12:32]

Report message to a moderator

Re: Best way of traversing a model [message #1389170 is a reply to message #1389164] Fri, 27 June 2014 12:30 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hmmm,

why not using EcoreUtil2.getContainerOfType?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Best way of traversing a model [message #1389177 is a reply to message #1389170] Fri, 27 June 2014 12:45 Go to previous messageGo to next message
Felix Dorner is currently offline Felix DornerFriend
Messages: 295
Registered: March 2012
Senior Member
On 27/06/2014 14:30, Christian Dietrich wrote:
> Hmmm,
>
> why not using EcoreUtil2.getContainerOfType?

Don't know which EcoreUtil2 you're all talking about :)

Felix
Re: Best way of traversing a model [message #1389186 is a reply to message #1389177] Fri, 27 June 2014 13:04 Go to previous messageGo to next message
Felix Dorner is currently offline Felix DornerFriend
Messages: 295
Registered: March 2012
Senior Member
On 27/06/2014 14:45, Felix Dorner wrote:
> On 27/06/2014 14:30, Christian Dietrich wrote:
>> Hmmm,
>>
>> why not using EcoreUtil2.getContainerOfType?
>
> Don't know which EcoreUtil2 you're all talking about :)

Ok, now I'm propertly informed. I didn't know TMF is a synonym for
Xtext. I thought there was more...

Felix
Re: Best way of traversing a model [message #1389193 is a reply to message #1389170] Fri, 27 June 2014 13:10 Go to previous message
Alexander R is currently offline Alexander RFriend
Messages: 211
Registered: July 2013
Senior Member
Thanks alot for the hints!

I will try out EcoreUtil2.getContainerOfType().

Previous Topic:Could not serialize EObject via backtracking
Next Topic:Using ANTLR or Xtext for YAML editor
Goto Forum:
  


Current Time: Wed Apr 24 22:05:10 GMT 2024

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

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

Back to the top