Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Retrieve the 'value' of any EObject instance(How to get the 'value/name' of any EObject Instance)
Retrieve the 'value' of any EObject instance [message #1831968] Fri, 04 September 2020 04:56 Go to next message
Amar Banerjee is currently offline Amar BanerjeeFriend
Messages: 14
Registered: November 2018
Junior Member
Hi members,

I am trying to create a model which refers to every other EObjects in the workspace. I cant share the full model, but the model is like this

My Model -> refers -> EObject

Now the EObject can come from any model available in the workspace.
My challenge here is to get the name/value of the referred EObject. Here the assumption is that every EObject will have an attribute as name or value.

My questions are as follows:-


  1. Is this possible with EMF
  2. Is yes, then how
  3. Is no, then can someone please suggest a suitable strategy?


Request the members to see if they could provide some pointers to this problems.

Cheers,
Amar

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

Report message to a moderator

Re: Retrieve the 'value' of any EObject instance [message #1831973 is a reply to message #1831968] Fri, 04 September 2020 06:39 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7127
Registered: July 2009
Senior Member
Hi

EObject has no name so there is no guarantee that all EObjects have a name; quite the contrary.

You may use EcoreUtil.getURI to get an almost certainly unique name for each EObject.

Regards

Ed Willink
Re: Retrieve the 'value' of any EObject instance [message #1831975 is a reply to message #1831973] Fri, 04 September 2020 06:58 Go to previous messageGo to next message
Amar Banerjee is currently offline Amar BanerjeeFriend
Messages: 14
Registered: November 2018
Junior Member
Thanks Ed.
In my workspace I have EObjects which have name and that's my assumption. Is it possile to get the EObject instance name without knowing its Subtype (Model), by just knowing the Structural feature and its value ?
Re: Retrieve the 'value' of any EObject instance [message #1831980 is a reply to message #1831975] Fri, 04 September 2020 07:58 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 31864
Registered: July 2009
Senior Member
You're asking related questions on the TMF forum in this thread:

https://www.eclipse.org/forums/index.php/mv/msg/1105110/1831969/#msg_1831969

Here there appear to be imports involved in your model and your textual grammar imports things by FQN or ID. In Xtext, it uses IQualifiedNameProvider to determine the names of EObjects and each grammar specializes this to compute names for that grammar's EObjects. I imagine you should be using this in that context.

When asking such a question in a context free manner, the answer will be quite different. Of course not every EObject has a name, and the feature (attribute) representing the "conceptual" name could have any name. You could use a utility such as this to get the string value of an attribute called "name":
  public static String getName(EObject eObject)
  {
    EStructuralFeature nameFeature = eObject.eClass().getEStructuralFeature("name");
    if (nameFeature instanceof EAttribute && !nameFeature.isMany())
    {
      EAttribute nameAttribute = (EAttribute)nameFeature;
      Object nameValue = eObject.eGet(nameFeature);
      return EcoreUtil.convertToString(nameAttribute.getEAttributeType(), nameValue);
    }
    return null;
  }


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Retrieve the 'value' of any EObject instance [message #1831991 is a reply to message #1831980] Fri, 04 September 2020 09:41 Go to previous messageGo to next message
Amar Banerjee is currently offline Amar BanerjeeFriend
Messages: 14
Registered: November 2018
Junior Member
Thanks for point it out Ed and extremely sorry for posting in this forum. Actually I was not sure, if this question belonged to EMF or Xtext Forum.
Thanks for your solution. Unfortunately I have not been able to run this. I will try to see what is wrong on my side of the code.

Cheers,
Amar
Re: Retrieve the 'value' of any EObject instance [message #1831996 is a reply to message #1831991] Fri, 04 September 2020 10:13 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 31864
Registered: July 2009
Senior Member
I'll follow the TMF thread. There it appears your EObject in question has an EClass that has an EReference called "name" that references an EClass called "Name" and the toString of an instance the "Name" EClass shows "Name::com.YDL.impl.NameImpl@20e772af (function: false)". This toString result suggests that the "Name" EClass only has a boolean EAttribute named "function" and nothing else that looks like it could conceptually be a name.

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Retrieve the 'value' of any EObject instance [message #1831998 is a reply to message #1831996] Fri, 04 September 2020 11:05 Go to previous message
Amar Banerjee is currently offline Amar BanerjeeFriend
Messages: 14
Registered: November 2018
Junior Member
Hi Ed,

Finally solved this. Thanks to your and Christian's(from the TMF Xtext 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

[Updated on: Fri, 04 September 2020 11:06]

Report message to a moderator

Previous Topic:How to get properties of an element when it is in the form of EObject or Object
Next Topic:emf adoption
Goto Forum:
  


Current Time: Tue May 11 11:50:03 GMT 2021

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

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

Back to the top