Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Find the path between two classes
Find the path between two classes [message #1850200] Mon, 21 February 2022 14:25 Go to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
Hi all,

I wanted to ask if there is a method or a way to find the path between two classes in EMF. Let's consider the following example.

Root
-Child 1
--Grandchild1
---Grandgrandchild1
---Grandgrandchild2
--Grandchild2
---Grandgrandchild3
--Grandchild3
---Grandgrandchild4

Is there a way to find the path from Root to GrandGrandChild2? (by path I mean to have all the names of the EReferences) from root to grandgrandchild2 or vice versa. The solution should work no matter the number of nesting.

[Updated on: Mon, 21 February 2022 14:26]

Report message to a moderator

Re: Find the path between two classes [message #1850204 is a reply to message #1850200] Mon, 21 February 2022 14:48 Go to previous messageGo to next message
Pavel Vlasov is currently offline Pavel VlasovFriend
Messages: 7
Registered: March 2013
Junior Member
I use this code - https://github.com/Nasdanika/core/blob/master/ncore/src/main/java/org/nasdanika/ncore/util/NcoreUtil.java#L156 - it computes containment path taking eKeys and EMaps into account. You can borrow it and modify to your needs.
There is also relativeContainmentPath() method planned - https://github.com/Nasdanika/core/blob/master/ncore/src/main/java/org/nasdanika/ncore/util/NcoreUtil.java#L301 - it should give you an idea how to compute a path from, say Grandgrandchild3 to Grandgrandchild4
Re: Find the path between two classes [message #1850209 is a reply to message #1850204] Mon, 21 February 2022 18:50 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
Hey,

Thank you for your answer. Is there any simpler way to this? Some sort of DFS or something?
It could either be to traverse all branches until you find the target element or to find the bath between a source and target element

[Updated on: Mon, 21 February 2022 18:57]

Report message to a moderator

Re: Find the path between two classes [message #1850210 is a reply to message #1850209] Mon, 21 February 2022 19:00 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33143
Registered: July 2009
Senior Member
There is also org.eclipse.emf.ecore.util.EcoreUtil.getRelativeURIFragmentPath(EObject, EObject) available and you can look at how that's implemented. It's not entirely clear what kind of "path" you want to build, but it seems you want some kind ".." to navigate up to the common ancestor first...

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Find the path between two classes [message #1850214 is a reply to message #1850210] Mon, 21 February 2022 20:46 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
I would like to store in an array all the references leading from one object to another, thus getting the path (in terms of reference names).

Let us say that this would be the metamodel:

index.php/fa/41687/0/

And I would like to find the path from Root to B21. The path would be: b.b2.b21

[Updated on: Mon, 21 February 2022 23:02]

Report message to a moderator

Re: Find the path between two classes [message #1850221 is a reply to message #1850214] Tue, 22 February 2022 06:52 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33143
Registered: July 2009
Senior Member
I suggest looking at the implementation of EcoreUtil.getRelativeURIFragmentPath. In general, if an object's eContainer() != null then org.eclipse.emf.ecore.EObject.eContainmentFeature() will be != null and you can get the name of the reference from that.

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Find the path between two classes [message #1850225 is a reply to message #1850221] Tue, 22 February 2022 09:24 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
I tried to do it using the following block of code, using some logic from dfs algorithm and using stack but when I run it everything freezes. And I am also getting this warning:
// Warning: Cannot infer type from recursive usage. Type 'Object' is used.

def findPath  (EClass input, EClass output){ 
   if (input == output){
   	print("self.")
   }
   else {
      for (referenceIterator : input.EReferences){ 
      if (!isVisited.contains(referenceIterator.EReferenceType)){
      	stackReference.add(referenceIterator) 
      	stackClass.add(referenceIterator.EReferenceType) 
      	isVisited.add(referenceIterator.EReferenceType) 
      	if (referenceIterator.EReferenceType == output){
      		print("self")
      		while(stackClass.iterator.hasNext)
      		print("." + stackClass.iterator.next)
      	}
      	else if (referenceIterator.EReferenceType != output && !referenceIterator.EReferenceType.EReferences.empty) {
      		findPath(referenceIterator.EReferenceType, output)
      	}
      	else if (referenceIterator.EReferenceType.EReferences.empty){
      		stackClass.pop 
      		stackReference.pop
      		findPath(stackClass.last, output)
      	}     	
      	}
      }  
      if (!stackClass.isEmpty() && !stackReference.isEmpty())  {
      	  stackClass.pop
      	  stackReference.pop 
      	  findPath(stackClass.last, output)
      }
      
      }
}

[Updated on: Tue, 22 February 2022 09:28]

Report message to a moderator

Re: Find the path between two classes [message #1850232 is a reply to message #1850225] Tue, 22 February 2022 11:04 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33143
Registered: July 2009
Senior Member
Sorry, I cannot develop your code for you nor to debug your code. You're writing it in Xtend and the message you describe is from Xtend's IDE. I'd suggest declaring an explicit return type or void on your "def".

Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Create a runtime model using EMF
Next Topic:Proto-project for UML in EMF
Goto Forum:
  


Current Time: Thu May 02 09:28:39 GMT 2024

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

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

Back to the top