Skip to main content



      Home
Home » Modeling » EMF » Find the path between two classes
Find the path between two classes [message #1850200] Mon, 21 February 2022 09:25 Go to next message
Eclipse UserFriend
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 09:26] by Moderator

Re: Find the path between two classes [message #1850204 is a reply to message #1850200] Mon, 21 February 2022 09:48 Go to previous messageGo to next message
Eclipse UserFriend
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 13:50 Go to previous messageGo to next message
Eclipse UserFriend
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 13:57] by Moderator

Re: Find the path between two classes [message #1850210 is a reply to message #1850209] Mon, 21 February 2022 14:00 Go to previous messageGo to next message
Eclipse UserFriend
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...
Re: Find the path between two classes [message #1850214 is a reply to message #1850210] Mon, 21 February 2022 15:46 Go to previous messageGo to next message
Eclipse UserFriend
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 18:02] by Moderator

Re: Find the path between two classes [message #1850221 is a reply to message #1850214] Tue, 22 February 2022 01:52 Go to previous messageGo to next message
Eclipse UserFriend
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.
Re: Find the path between two classes [message #1850225 is a reply to message #1850221] Tue, 22 February 2022 04:24 Go to previous messageGo to next message
Eclipse UserFriend
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 04:28] by Moderator

Re: Find the path between two classes [message #1850232 is a reply to message #1850225] Tue, 22 February 2022 06:04 Go to previous message
Eclipse UserFriend
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".
Previous Topic:Create a runtime model using EMF
Next Topic:Proto-project for UML in EMF
Goto Forum:
  


Current Time: Fri Apr 18 07:15:01 EDT 2025

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

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

Back to the top