Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to get the 'Name/ID' of any EObject in the workspace
How to get the 'Name/ID' of any EObject in the workspace [message #1831969] Fri, 04 September 2020 05:03 Go to next message
Amar Banerjee is currently offline Amar BanerjeeFriend
Messages: 14
Registered: November 2018
Junior Member
Hi Members,

I have a DSL which refers to EObjects of any other DSL instances inthe workspace. The grammar is something like below.

Template:
	'Name' ID'.'
	'Import' imports+= [ecore::EObject|ID] '.'


The imports refer to all the EObjects in the runtime workspace , where all these EOBjects can come from various other DSLs.

The DSL instance is like
	Name MyTemplate
	Import  ObjectInstanceNameFromOtherDSL


Now the assumption here is that every Eobject will have a name or an ID. I want to extract that name and ID or the EObject (referred in my Template DSL) so that I could process the name and the type of the EObject. In other words in my backend (Generator) I need the EObject name

Is it possible to get the name of the EObject instances ObjectInstanceNameFromOtherDSL.

Any leads on how this should be doable ? Also request the members to please give some sample code.

Cheers,
Amar


[Updated on: Fri, 04 September 2020 05:44]

Report message to a moderator

Re: How to get the 'Name/ID' of any EObject in the workspace [message #1831971 is a reply to message #1831969] Fri, 04 September 2020 06:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
first you should change the grammar to
Template:
	'Name' ID'.'
	'Import' imports+= [ecore::EObject|FQN] '.'

FQN: ID ("." ID)*;


to also cover the names.

for extraction you can do two things.

- use the node model to read the text for the reference with org.eclipse.xtext.nodemodel.util.NodeModelUtils.findNodesForFeature(EObject, EStructuralFeature) (in this case it would be easier to have single valued references (no +=)
- use emf reflection api to get the value (org.eclipse.emf.ecore.EObject.eGet(EStructuralFeature) + org.eclipse.emf.ecore.EObject.eClass() + org.eclipse.emf.ecore.EClass.getEStructuralFeature(String))


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to get the 'Name/ID' of any EObject in the workspace [message #1831972 is a reply to message #1831971] Fri, 04 September 2020 06:21 Go to previous messageGo to next message
Amar Banerjee is currently offline Amar BanerjeeFriend
Messages: 14
Registered: November 2018
Junior Member
Thanks a lot Christian.
Can you also please suggest how to implement this. As I am fairly new to EMF and Xtext I am not able to fully understand the code.

To implement
NodeModelUtils.findNodesForFeature(eobj, estrfeature) 


The eobj is the object Reffered EObject right ? and How do I get the estrfeature object ?

Also I didnt quite understand

org.eclipse.emf.ecore.EObject.eGet(EStructuralFeature) + org.eclipse.emf.ecore.EObject.eClass() + org.eclipse.emf.ecore.EClass.getEStructuralFeature(String)


Could you please explain this.

Thanks again,
Cheers,
Amar
Re: How to get the 'Name/ID' of any EObject in the workspace [message #1831979 is a reply to message #1831972] Fri, 04 September 2020 07:56 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
org.eclipse.emf.ecore.EObject.eClass() gives you the eclass of the eobject
org.eclipse.emf.ecore.EClass.getEStructuralFeature(String) allows you to the the name feature
org.eclipse.emf.ecore.EObject.eGet(EStructuralFeature) allows you to get the value of the name feature

NodeModelUtils.findNodesForFeature(myTemplateObject, MyDslPackage.Literals.TEMPLATE__IMPORTS) (am not sure if _ or __ will give you a list of nodes, which you can ask for text (searching for it will reveal examples)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to get the 'Name/ID' of any EObject in the workspace [message #1831987 is a reply to message #1831979] Fri, 04 September 2020 08:50 Go to previous messageGo to next message
Amar Banerjee is currently offline Amar BanerjeeFriend
Messages: 14
Registered: November 2018
Junior Member
Hi Christian,

I tried this

def getEObjectName(EObject eObj) {
               println("EObject::"+eObj)
		var eClass = eObj.eClass
		var structuralFeature = eClass.getEStructuralFeature("name")
		var name = eObj.eGet(structuralFeature) as EObject
		println("Name::"+name)
		
	}


However the result is

EObject::com.YDL.impl.NameImpl@20e772af (function: false)
Name::com.YDL.impl.NameImpl@20e772af (function: false)


Seems that both the obsjects resturn the same value.

The NameImp comes from another DSL and has an attribute 'name'.

Any guess, what I might be doing wrong ?

Thanks,
Amar

[Updated on: Fri, 04 September 2020 08:53]

Report message to a moderator

Re: How to get the 'Name/ID' of any EObject in the workspace [message #1831988 is a reply to message #1831987] Fri, 04 September 2020 09:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
then your stuff does not hold this statement true

Eobject will have a name or an ID.

cause it has a name object

(you can check if the result from get is a eobject and if yes you can do the get structural feature stuff again)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Fri, 04 September 2020 09:05]

Report message to a moderator

Re: How to get the 'Name/ID' of any EObject in the workspace [message #1831994 is a reply to message #1831988] Fri, 04 September 2020 10:04 Go to previous messageGo to next message
Amar Banerjee is currently offline Amar BanerjeeFriend
Messages: 14
Registered: November 2018
Junior Member
*Update*

var eClass = eObj.eClass
var structuralFeature = eClass.getEStructuralFeature("name")
var name = eObj.eGet(structuralFeature) as EObject
println("Name::" + name)
for (node : getNodes(name)) {
			println("NODE::" + node.text + "  SEMANTIC CLASS::" + node.semanticElement)
}


the type of structuralFeature shows as
org.eclipse.emf.ecore.impl.EReferenceImpl@20271a5b (name: name) (ordered: true, unique: true, lowerBound: 0, upperBound: 1) (changeable: true, volatile: false, transient: false, defaultValueLiteral: null, unsettable: false, derived: false) (containment: false, resolveProxies: true)


Is this desirable ?? Any thoughts ??

Thanks
Amar
Re: How to get the 'Name/ID' of any EObject in the workspace [message #1831997 is a reply to message #1831994] Fri, 04 September 2020 10:55 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
do you have names that are references themselves?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to get the 'Name/ID' of any EObject in the workspace [message #1831999 is a reply to message #1831997] Fri, 04 September 2020 11:06 Go to previous message
Amar Banerjee is currently offline Amar BanerjeeFriend
Messages: 14
Registered: November 2018
Junior Member
Hi Christian,

Finally solved this. Thanks to your and Ed's(From the EMF forum) guidance.
The working code is
	def getEObjectName(EObject eObj) {
		{
			println("EObject::" + eObj)
			var eClass = eObj.eClass
			var structuralFeature = eClass.getEStructuralFeature("name")
			var nodes = NodeModelUtils.findNodesForFeature(eObj, structuralFeature)
			for (node : nodes) {
				if (node.semanticElement.equals(eObj)){
					println("NODE TEXT::"+node.text.trim)
					return node.text.trim
				}
				
			}
		}

	}


Cheers,
Amar
Previous Topic:Xtext / Xtend 2.23 release
Next Topic:Problems with linking in Xtext unit testing with multiple DSLs
Goto Forum:
  


Current Time: Thu Mar 28 09:14:30 GMT 2024

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

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

Back to the top