Sunday, November 30, 2008

Xtext Corner #1 - Linking the model

One of the core features of Xtext is the ability, to create a semantic model from a set of input files. While loading the resource, Xtext transforms the parsed AST into a domain model and trys to cross link the model elements.

From an information theory point of view this cannot be solved generally for every imaginable language, but we try to provide reasonable defaults with Xtext. A quick start with a language, that fulfils common usecases, should be as easy as possible. So we based the default linking on some kind of heuristics: Most likely, one would reference a model element by some kind of name, so we look for name features in the model elements and try to match them with the input text for a reference.

Lets consider a small example language for clarification:



The following input file would certainly match the given concrete syntax:



If we take a look at the lines two and three, we notice two objects in the container, that have the same name, but different indexes. The default linking implementation that ships with the M3 Milestone of TMF Xtext would try to link both 'Obj2' for the reference in line one. This seems to be perfectly ok, because our objects can refer to many other objects (note the '+=' notation in the concrete syntax of our language).

But in many cases, the user would expect, that the reference points to a unique object, even if the metamodel allows multiple values for a feature.
So how can we identifiy these ambiguities without implementing the whole linking service ourself? The simple answer is: In TMF Xtext M3, we cannot. But there is a workaround.

What we have to do is to slightly modify our grammar and therefore derive a different metamodel:



The refined 'SampleLang' uses a combination of containment and single value references instead of multi value references. At a first glance, we have greater efforts when writing functions that use our models. So what is the benefit? The default linking implementation works out of the box and implementing constraints on top of that to check our new kind of multi value references is pretty straight forward.

But whats the good news? This workaround does its job. And even better: It will not be required in TMF Xtext M4 which will be released at the end of the year. We thought again about linking and will come up with another default implementation, that is suitable for many more cases, which cannot be handled by the default language services in M3.

1 comment:

Unknown said...

Hi!

I'm writing a grammar, and I used this trick that you mention. Now I have two questions:

1.- The editor doesn't recognize the reference for autocompletion. Is there any fix for this?
2.- I wrote a generator for my models, but they don't recognize the attributes of the referenced objects. Is this caused by the indirect reference? Is there a fix for this?

Thanks in advance

Rodrigo