How to get the 'Name/ID' of any EObject in the workspace [message #1831969] |
Fri, 04 September 2020 05:03 |
Amar Banerjee 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 |
|
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 #1831979 is a reply to message #1831972] |
Fri, 04 September 2020 07:56 |
|
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 |
Amar Banerjee 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 #1831994 is a reply to message #1831988] |
Fri, 04 September 2020 10:04 |
Amar Banerjee 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
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04717 seconds