Find the path between two classes [message #1850200] |
Mon, 21 February 2022 14:25 |
John Henbergs 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 #1850225 is a reply to message #1850221] |
Tue, 22 February 2022 09:24 |
John Henbergs 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
|
|
|
|
Powered by
FUDForum. Page generated in 0.02060 seconds