Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to make child references with Xtext?
How to make child references with Xtext? [message #556141] Tue, 31 August 2010 03:29 Go to next message
Ian Buchanan is currently offline Ian BuchananFriend
Messages: 2
Registered: July 2009
Junior Member
I want to enable textual entry for an existing EMF model. Basically, this is the model:
* Model contains Device contains Port.
* Model contains Connection with 2 references to Port.
* Device has a unique key within the model but Port is only unique within the context of a Device.

Based on the options I've set for EMF (namely some eKeys), I get XMI serialization like so:
<connection 
  port1="#//@devices[name='host01']/@ports[name='0']
  port2="#//@devices[name='switch01']/@ports[name='1']/>

Note the type of each reference is still a Port but it needs both Device and Port to identify the referenced element.

In Xtext, I would like a simple grammar something like:
Connection returns Connection :
  'connection'
  device1=[Device|NAME] ':' port1=[Port|NUM]
  device2=[Device|NAME] ':' port2=[Port|NUM];

I have experimented with this and even have implemented scoping to help choose the port from the device. However, there's no such attribute in my model as device1 and it would be hard for me to change the model.

How do I specify to Xtext that the reference for port1 should be constructed with a value for a Device NAME and a Port NUM?
Re: How to make child references with Xtext? [message #556149 is a reply to message #556141] Tue, 31 August 2010 06:13 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
You're almost there: the thing mentioned after the pipe ('|') in a reference is not the attribute but the parser rule which populates that attribute. Usually, that's ID which is also the default so you can probably skip the |... altogether.

I'm assuming the rest of your grammar looks something like:
Device:
   'device' name=ID //...


Re: How to make child references with Xtext? [message #556300 is a reply to message #556149] Tue, 31 August 2010 16:20 Go to previous messageGo to next message
Ian Buchanan is currently offline Ian BuchananFriend
Messages: 2
Registered: July 2009
Junior Member
I'm not sure I follow. As far as I understand, the following references (the ports in Connection) are equivalent:
Port:
  'port' name=ID

Connection:
  'connection'
  port1=[Port|ID]
  port2=[Port]

In other words, removing the stuff after '|' only makes an implicit call the the ID rule. I was unaware that the ID rule was special. Is it?

I have my own rules for assigning values to the "name" attribute:
Device:
  'device' name=NAME
  '{' ports+=(Port)* '}';

Port:
  'port' name=NUM

Because I had set things in ecore, I thought those settings would be respected. In particular, in ecore the name attribute for Device is set to ID=true (which I thought meant something very different than the Xtext rule ID). Because Ports are not uniquely named for the whole document (only for a Device), I have the name attribute for Port set to ID=false. Given my model, it feels like I need something like this:
Connection:
  'connection'
  port1=([Device|NAME] ':' [Port|NUM])
  port2=([Device|NAME] ':' [Port|NUM])

I need both a Device value and Port value to have a unique reference to a Port. I imagine this is a problem for Xtext because it's not a 1:1 mapping between a value and a reference. I was hoping it might be possible with some code. Is it?

[Updated on: Tue, 31 August 2010 16:20]

Report message to a moderator

Re: How to make child references with Xtext? [message #556306 is a reply to message #556300] Tue, 31 August 2010 17:03 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
Ian, you're right about the ID rule -I am not.

So, the (implicit call to the) ID rule just makes sure that it is used to gather a name which is then used to resolve the reference. In case a port name is only unique after prepending it with a device name, you'll need scoping to restrict the possibilities for the port: see the section on scoping in the User Guide.


Re: How to make child references with Xtext? [message #556534 is a reply to message #556141] Wed, 01 September 2010 16:43 Go to previous message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 760
Registered: July 2009
Location: Hamburg
Senior Member
The easiest way is to refer to the port only by using its qualified name:

Connection:
port=[Port|PortFQN]...

PortFQN:
ID ':' NUM;

That defines the syntax of the cross reference to be an ID followed by a
colon and a number. Now you have to make sure to calculate the qualified
name for existing ports correctly by implementing your own
IQualifiedNameProvider, like

public class MyFQNProvider extends DefaultDeclarativeQualifiedNameProvider {
String qualifiedname(Port p) {
return p.getDevice().getName() + ":" + p.getName();
}
}

and bind it in your Guice module.

Hope that helps
Jan.



Regards
Jan


Am 31.08.10 05:29, schrieb Ian Buchanan:
> I want to enable textual entry for an existing EMF model. Basically,
> this is the model:
> * Model contains Device contains Port.
> * Model contains Connection with 2 references to Port.
> * Device has a unique key within the model but Port is only unique
> within the context of a Device.
>
> Based on the options I've set for EMF (namely some eKeys), I get XMI
> serialization like so:
>
> <connection port1="#//@devices[name='host01']/@ports[name='0']
> port2="#//@devices[name='switch01']/@ports[name='1']/>
>
> Note the type of each reference is still a Port but it needs both Device
> and Port to identify the referenced element.
>
> In Xtext, I would like a simple grammar something like:
>
> Connection returns Connection :
> 'connection'
> device1=[Device|NAME] ':' port1=[Port|NUM]
> device2=[Device|NAME] ':' port2=[Port|NUM];
>
> I have experimented with this and even have implemented scoping to help
> choose the port from the device. However, there's no such attribute in
> my model as device1 and it would be hard for me to change the model.
>
> How do I specify to Xtext that the reference for port1 should be
> constructed with a value for a Device NAME and a Port NUM?


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com


---
Get professional support from the Xtext committers at www.typefox.io
Previous Topic:Using external ecore models
Next Topic:IFragmentProvider NPEs
Goto Forum:
  


Current Time: Thu Apr 25 19:14:53 GMT 2024

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

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

Back to the top