Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to set value for a name attribute in a composite relationship?
How to set value for a name attribute in a composite relationship? [message #895595] Fri, 13 July 2012 17:33 Go to next message
Cristiano Gavião is currently offline Cristiano GaviãoFriend
Messages: 279
Registered: July 2009
Senior Member
Hi,

I'm using this grammar:
Person:
	'Person:'
	'{'
	'userId' name=ID
	'first name' firstName=STRING
	'last name' lastName=STRING
	('email' email=STRING)?
	'}';

Worker:
	'Worker:'
	'{'
	'person' person=[Person]
	'position' position=[Position]
	'}';



I would like to know what is the best way to set the name for a Worker object or delegate the getName() for the related Person.

thanks for any tip.

regards,

Cristiano
Re: How to set value for a name attribute in a composite relationship? [message #895652 is a reply to message #895595] Sat, 14 July 2012 05:25 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

the basic problem you are facing is that you want to calculate a name which is needed for resolving cross references (and hence must be known before actually resolving cross references), but you need to resolve a cross reference for obtaining it...

In your simple grammar you could solve this issue the following way. In the qualified name provider method you implement for Worker, you do *not* navigate to the person in order to obtain its name but rather use the node model (NodeModelUtils will help you) for the Worker element to get at the actual ID that is used to reference the person.

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: How to set value for a name attribute in a composite relationship? [message #895746 is a reply to message #895652] Sun, 15 July 2012 16:34 Go to previous messageGo to next message
Cristiano Gavião is currently offline Cristiano GaviãoFriend
Messages: 279
Registered: July 2009
Senior Member
Hi Alexander,

Thanks for you tip. Now I could make it work Smile

But I have one doubt. I've created the method below. But it is not taking the parent properly:
	protected QualifiedName qualifiedName(Worker e) {

		String name = null;
		List<INode> nodes = NodeModelUtils.findNodesForFeature(e,
				OrganizationDslPackage.Literals.WORKER__PERSON);
		
		if (!nodes.isEmpty()){
			
			INode first = nodes.get(0);
			name = NodeModelUtils.getTokenText(first);
			return converter.toQualifiedName(name);
		}
		else
			return QualifiedName.create("No name");
	}


I've looked into the DefaultDeclarativeQualifiedNameProvider.getFullyQualifiedName() that is using a recursive calling to do the job...:
	public QualifiedName getFullyQualifiedName(final EObject obj) {
		return cache.get(Tuples.pair(obj, "fqn"), obj.eResource(), new Provider<QualifiedName>(){

			public QualifiedName get() {
				EObject temp = obj;
				QualifiedName qualifiedNameFromDispatcher = qualifiedName.invoke(temp);
				if (qualifiedNameFromDispatcher!=null)
					return qualifiedNameFromDispatcher;
				String name = getResolver().apply(temp);
				if (Strings.isEmpty(name))
					return null;
				QualifiedName qualifiedNameFromConverter = converter.toQualifiedName(name);
				while (temp.eContainer() != null) {
					temp = temp.eContainer();
					QualifiedName parentsQualifiedName = getFullyQualifiedName(temp);
					if (parentsQualifiedName != null)
						return parentsQualifiedName.append(qualifiedNameFromConverter);
				}
				return qualifiedNameFromConverter;
			}
		});
	}


It would be safe to call getFullyQualifiedName() from qualifiedName() ?

thanks,

Cristiano
Re: How to set value for a name attribute in a composite relationship? [message #895800 is a reply to message #895746] Mon, 16 July 2012 06:10 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

your grammar snippet does not indicate that the person's parent's name is part of its name (you reference the person via a simple ID rather than a qualified name).

It would be helpful to know what you want the name of a person and a corresponding worker to be.

The default name provider can work recursively as it assumes that all names can be calculated without resolving cross references. It simply walks up the containment hierarchy (i.e. it stays within the same resource). Your worker could be defined in a different resource.

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: How to set value for a name attribute in a composite relationship? [message #895867 is a reply to message #895800] Mon, 16 July 2012 11:35 Go to previous message
Cristiano Gavião is currently offline Cristiano GaviãoFriend
Messages: 279
Registered: July 2009
Senior Member
Quote:

your grammar snippet does not indicate that the person's parent's name is part of its name (you reference the person via a simple ID rather than a qualified name).


As the worker can be formed only by Persons from the same Organization, it is ok to use ID.

Now I understood what happen and everything is ok.
To exemplify I've attached an image of content assist that uses the scope created by the code below. index.php/fa/10715/0/
Repair that the content assist shows me a name with its parent at side and it inserts rightly the name without the parent after selection. Before, content assist was showing only the name, different from any other proposals in the editor that uses the default.

	protected QualifiedName qualifiedName(Worker e) {

		String name = null;
		List<INode> nodes = NodeModelUtils.findNodesForFeature(e,
				OrganizationDslPackage.Literals.WORKER__PERSON);
		if (!nodes.isEmpty()) {
			INode first = nodes.get(0);
			name = NodeModelUtils.getTokenText(first);
			QualifiedName qualifiedNameFromConverter = converter
					.toQualifiedName(name);
			if (e.eContainer() != null) {
				QualifiedName parentsQualifiedName = getFullyQualifiedName(e
						.eContainer());
				if (parentsQualifiedName != null) {
					return parentsQualifiedName
							.append(qualifiedNameFromConverter);
				} else
					return qualifiedNameFromConverter;
			}
		}
		return QualifiedName.create("No name");
	}


Thanks for your help !

regards,

Cristiano
Previous Topic:[xbase] How to parse Xbase expressions in Java code?
Next Topic:Xtend 2.3 -> Guice Problems
Goto Forum:
  


Current Time: Mon Aug 10 08:42:23 GMT 2020

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

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

Back to the top