Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Multiple possible way to reference
Multiple possible way to reference [message #1694501] Tue, 05 May 2015 16:50 Go to next message
Ma Aa is currently offline Ma AaFriend
Messages: 7
Registered: May 2015
Junior Member
Hiho!

I'm currently trying to develop a DSL and I need a way to reference an entity with a second possible way.

This is how the DSL looks:

Entity FirstEntity { Field }
SuperEntity MySuperEntity {
 use FirstEntity
 use FirstEntity as AnotherFirstEntity

 FirstEntity.Field // Works
 AnotherFirstEntity.Field // Does not work
}

This is simplified because of sanity Wink


The "use" is like an import in Java or a "uses" in C#.
I'm currently referencing over [Entity|ID] via the name property, but can't figure out a way to also reference via the alias.

This DSL is going to be used to generate some code which needs to reference those fields a kind of static way (like static Class.static Method)

Is there a way to accomplish what I'm trying to do?

Thanks in advance!
Re: Multiple possible way to reference [message #1694591 is a reply to message #1694501] Wed, 06 May 2015 13:43 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
Sure, there is.
In your scope provider you need to add an AliasedEObjectDescription to the scope for such aliases.
Re: Multiple possible way to reference [message #1694609 is a reply to message #1694591] Wed, 06 May 2015 16:01 Go to previous messageGo to next message
Ma Aa is currently offline Ma AaFriend
Messages: 7
Registered: May 2015
Junior Member
Hey Sven,
thanks for your answer!

I got it working via scope but now I see that it's not exactly what I need.

I want to have a new object (of type entity) when I either say
Import FirstEntity
and
Import FirstEntity as Another

And have two different objects (of type Entity) which I can refer to as FirstEntity.blablubb and Another.blablubb

It's kind of like object creation in Java where you give the reference to the object a name.
The difference is that if I don't want to name them (as FirstEntity as name might already contain business logic) that I can refer to them via the class name.


I hope it was understandable Smile

Thanks!
Markus
Re: Multiple possible way to reference [message #1694610 is a reply to message #1694609] Wed, 06 May 2015 16:05 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13879
Registered: July 2009
Senior Member
Hi,

this is even more easy.

simply adapt the qualified name provider

- use NodeModelUtil.findnodesforfeature to retrive the name of the referenced thing in the reference only case
- use the alias in the other case


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Multiple possible way to reference [message #1694616 is a reply to message #1694610] Wed, 06 May 2015 16:54 Go to previous messageGo to next message
Ma Aa is currently offline Ma AaFriend
Messages: 7
Registered: May 2015
Junior Member
Would this give me 2 different (in case of reference) object?
Because the way I'm doing it is that those two imports have the same reference to an Entity object which then overrides changes in the order of evaluation of those imports
Re: Multiple possible way to reference [message #1694620 is a reply to message #1694616] Wed, 06 May 2015 17:15 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13879
Registered: July 2009
Senior Member
Hi,

i am not sure if i can follow you. can you share a complete example grammar

i thought of something like

Use: 'use' entity=[Entity] ('as' name=ID)?;
Reference: use=[Use] '.' attr=[Attribute]


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Multiple possible way to reference [message #1694621 is a reply to message #1694620] Wed, 06 May 2015 17:17 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13879
Registered: July 2009
Senior Member
Or is it:

Use: 'use' entity=[Entity] ('as' name=ID)?;
Reference: entity=[Entity] '.' attr=[Attribute]

if this is the case go for the way sven proposed
or even create the scope manually

your impl of the scope provider for Reference_Entity would be the interesting point


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Multiple possible way to reference [message #1694627 is a reply to message #1694621] Wed, 06 May 2015 17:58 Go to previous messageGo to next message
Ma Aa is currently offline Ma AaFriend
Messages: 7
Registered: May 2015
Junior Member
Here is a sample grammar:
Supermodel:
	Entity | DomainEntity;

DomainEntity:
	'DomainEntity' name=ID '{'
	imports+=Import*
	referenzen+=Referenz*
	'}';

Referenz:
	referenzierteEntitaet=[EntityElement|QualifizierterName] ('{' referenzen+=Referenz* '}')?
;

QualifizierterName:
	ID '.' ID;

Import:
	'use' usage=[Entity] ('as' alias=ID)?;

Entity:
	'Entity' name=ID '{' elemente+=EntityElement* '}' (alias=ID)?;

EntityElement:
	name=ID typ=Typ;

enum Typ:
	Nummer |
	Text;


And in the use-case I have one file defining an Entity Person:

Entity Person {
	Name Text
	Alter Nummer
	Hausnummer Nummer
}


The DomainEntity has imports to Entities to minimize the scope to only "imported" entities.

Given the following DomainEntity:
DomainEntity DomainE { 
	use Person 
	use Person as NochJemand
	
	Person.Name 
	{
		NochJemand.Alter
	}
}


I only have one object of type Entity, since both of those imports contain a reference to one parsed Entity object (since there is only one "Person" file).
What I want to accomplish is to be able to reference those as, in this case, two object. One is referenced to as Person (just the name of the entity) but since I need to have 2 of those object I thought i could do Aliases.

The
(alias=ID)?;
at the end of the Entity grammar is because of the way I solved the alias problem this morning Razz
If the Person is referenced via an alias I want the codegeneration to use this alias instead of the name.

Thats because it's generating code which is not object oriented, where it's okay to refer as "Person.Name" to one "object" and "NochJemand.Alter" as a different object.

[Updated on: Wed, 06 May 2015 18:01]

Report message to a moderator

Re: Multiple possible way to reference [message #1694629 is a reply to message #1694627] Wed, 06 May 2015 18:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13879
Registered: July 2009
Senior Member
Hi,

but this is a matter of scoping only

pseudoCode:

List<IEObjectDescription> elements = new ArrayList()
for (import : imports) {
for(entityElement : import. usage. elemente) {
elements.add(EObjectDescription.create(QualifiedName.create(if (import.alias!=null) import.alias else import.usage.name,entityElement.name),entityElement))
}
}
return new SimpleScope(IScope.NULLSCOPE, elements, false)


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Multiple possible way to reference [message #1694630 is a reply to message #1694629] Wed, 06 May 2015 18:07 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13879
Registered: July 2009
Senior Member
and: you have to use the node model to find out the name.

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Multiple possible way to reference [message #1694633 is a reply to message #1694630] Wed, 06 May 2015 18:19 Go to previous messageGo to next message
Ma Aa is currently offline Ma AaFriend
Messages: 7
Registered: May 2015
Junior Member
Yeah I was solving it the way you described via the scope, but the Entity referenced is the same in both imports.

Scoping and content assist is working, but the way this works is that the usage which sets the alias sets them for both (since they are the same), so if I generate code based on
if (entity.alias != null) { entity.alias } else { entity.name }
the resulting code for both is the same, since both have the alias set (as they are the same).

The way xtext works (my guess Smile ) I think, is that it parses the one file containing the
Entity Person {....
as one Entity object.
Now I'm trying to reference two objects via those imports, but there is of course only one object. The Import with the alias is setting the alias property of that Entity-object, so the codegeneration is using in both cases
Person.Name and NochJemand.Alter
the same object with the alias set which results in
NochJemand.Name and NochJemand.Alter
.

I tried to clone one object in the scope to reference it in the second import (that every import has a reference to its own Entity object) but cloning doesn't work.

It's my first DSL so I guess there's something I did wrong in the first place, but I hope you now understand what I mean Smile

If the solution is to name all imports (like naming all reference to objects in e.g. java) I'm also down for this.
Re: Multiple possible way to reference [message #1694636 is a reply to message #1694633] Wed, 06 May 2015 18:24 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13879
Registered: July 2009
Senior Member
Hi,

i understand the problem. as i said: you can use NodeModelUtils to find out the actual qualified name for the reference that is used.
(of course you still have only one attribute and entity anway.

or you use two references as i proposed in my first post.


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Cheapest way to traverse a model
Next Topic:Not linked Element appears as != null in Validation
Goto Forum:
  


Current Time: Sun Apr 11 18:37:05 GMT 2021

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

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

Back to the top