Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Resolve References through types
Resolve References through types [message #822212] Fri, 16 March 2012 10:03 Go to next message
Alex G is currently offline Alex GFriend
Messages: 96
Registered: January 2012
Member
Hi!

I have the following model for specifying graphs:
Vertex u
Vertex v[1]
Vertex v[10]
Edge u -> v[1]
Edge u -> v[10]
for(i in {3,4,5}) {
   Vertex v[i]
   Edge u -> v[i]
}


The vertex defines an object and the edge references to this object. The vertex name is not a data type, since it consists of a name ("v") and eventually an index ("10" or "i"). The index can have to types: integer or ID. If the index is an integer, it shall reference to the integer index of the corresponding vertex. If the index is an ID, it shall reference to the ID defined in the for-Block. Therefore I have implemented the following XText meta model: (the set expression "{3,4,5}" is not of interest here)

Model: statements += Statement*;
Statement: Vertex | Edge | ForStatement;

IntID: INT; // wrap INT to a ecore::String data type

ForStatement:
	"for" paramlist=ParameterList '{'
		(statements+=Vertex |
		statements+=Edge)*
	'}';

ParameterList: '(' param+=Parameter ("," param+=Parameter)* ')';
Parameter: name=ID;

SimpleVertex: {SimpleVertex} prefix=ID;
IndexedVertex: {IndexedVertex} prefix=ID index=IndexList;

IndexList: '[' indexitem+=Index ("," indexitem+=Index)* ']';
Index: IntIndex | IDIndex;
IntIndex: {IntIndex} name=IntID;
IDIndex: {IDIndex} refparam = ParameterRef;
ParameterRef: ref = [Parameter | ID];

Vertex: "Vertex" (SimpleVertex | IndexedVertex);
Edge: "Edge" source=VertexRef '->' target=VertexRef;

VertexRef: IndexedVertexRef | SimpleVertexRef;

SimpleVertexRef: {SimpleVertexRef} refvertexname = [SimpleVertex | ID];
	
IndexedVertexRef: {IndexedVertexRef} refvertexname = [IndexedVertex | ID] refindex = IndexRefList;
	
IntIndexRef: refintindex = [IntIndex | IntID];
IDIndexRef: refidindex = [Parameter | ID];
IndexRef: IntIndexRef | IDIndexRef;
IndexRefList: '[' indexrefitem+=IndexRef ("," indexrefitem+=IndexRef)* ']';


In this case I reference the vertices in two steps:
(1) Find the vertex ID (like "v")
(2) If the vertex has an index with an integer, reference to this one

But now I have two vertices with same name: "v[1]" and "v[10]" have both name "v". If I want to reference one of the indices, I see only the first upcoming index in the model (here "1"), since the vertex "v" is referenced to the first "v".

My question: How can I reference/link to the whole vertex at once, like for example:
IndexedVertexRef: {IndexedVertexRef} refvertexname = [IndexedVertex | IndexedVertex]
// say: "reference to the indexed vertex by its whole string representation, if it has only integers as indices"

without creating a data type for the index list.
The problem with creating a data type for the index is, that I would get a scope on indexed vertices which can be further indexed, like
for(i in {3,4,5}) {
   Vertex v[i]
   Edge u -> v[i] // this shall be allowed
   Edge u -> v[1][i] // this shall be forbidden
}


Do I need in some way to customize the Linker?

Thank you in advance!

Alex.
Re: Resolve References through types [message #822243 is a reply to message #822212] Fri, 16 March 2012 10:55 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13425
Registered: July 2009
Senior Member
Hi,
box
if you do the indexing+grammar right this will work out of the

let us abstract from ypur grammar and say it would look like:

Model:
	defintions+=Definition*
	usages+=Usage;
	
Definition:
	'define' name=ID '[' index=INT ']';
		
Usage:
	'use' ref=[Definition|DefinintionName]
;


DefinintionName:
	ID '[' INT ']'
;


then the only thing you have to do is that a Definition
is indexed under its DefintionName (xxx[0])

=> You customize
(a) IQualifiedNameProvider if you want to change the Defintions name in general
(b) the DefaultResourceDescriptionsStrategy if you want to index it additionally under the DefintionName

public class MyDslQNP extends DefaultDeclarativeQualifiedNameProvider {

	QualifiedName qualifiedName(Definition d) {
		return QualifiedName.create(d.getName()+"["+d.getIndex()+"]");
	}
	
}


~Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Resolve References through types [message #822777 is a reply to message #822243] Sat, 17 March 2012 08:24 Go to previous messageGo to next message
Alex G is currently offline Alex GFriend
Messages: 96
Registered: January 2012
Member
Hi Christian!

First of all, thank you for your answer!

You described the easy case. But consider you want expressions like

Vertex v[1,2]
Vertex v[1,3]
for(i in {4,5,6}) {
   Vertex v[1,i]
}
Edge v[1,2] -> v[1,3]
for(k in {10,11,12}) {
   Edge v[1,k] -> v[1,2]
}

In this case, if the indexed vertex has no parameters (statically referencable), I can give him the whole name, like "v[1,2]", as you described. If the vertex has parametric index in its definition ("Vertex v[1,i]") then I want at least to reference the prefix of the vertex ("v") to its definition and the parameter ("i") to its definition as well in the for statement. After resolving the both for statements (by generating the model into itself) the vertices will be static and therefor only after gernation an unresolved reference error will occur, since the two sets where the parameters "i" and "k" run through are not the same.

Here is a small model, how I accomplished the demand (changing and extending the model from above):
IndexedVertex:
	prefix=ID 
	((=>(resolved?='[' intindexlist=IntIndexList)) |
		('[' indexlist=IndexList)
	) ']';
IntIndexList: indexitem+=IntIndex ("," indexitem+=IntIndex)*;

VertexRef: 
	=>ResolvedIndexVertexRef |
	UnresolvedIndexVertexRef |
	SimpleVertexRef;

ResolvedIndexVertexRef:
	refvertexname = [IndexedVertex | IntIndexVertexDataType];

UnresolvedIndexVertexRef:
	refvertexname = [IndexedVertex | ID] refindex = IndexRefList;

IntIndexVertexDataType:
	ID '[' INT ("," INT)* ']';


The qualified names and scoping progrmaming are straight forward.
Furthermore one can validate the correct integer indices, if an unresolved index is used, for example
for(i in {1,2}) {
   Vertex v[1,4,i,10]
   Edge v[1,4,i,5] -> v[1,4,i,10] // after validation "v[1,4,i,5]" will become an error message, since up to "v[1,4," it can be consumed, "i" is referenced and "5" is not visible on the path "v"->"1"->"4"->"*"
}

For this validation process, one case use this blog entry about path expressions:
http://dslmeinte.wordpress.com/2010/08/16/path-expressions-in-entity-models/


I hope this post helps someone else, too.

Alex.

[Updated on: Sat, 17 March 2012 08:25]

Report message to a moderator

Re: Resolve References through types [message #822831 is a reply to message #822777] Sat, 17 March 2012 10:26 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13425
Registered: July 2009
Senior Member
Hi,

yes! what about doing this with checks and not with scoping. you never have to do everything with scoping

~Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Sat, 17 March 2012 10:28]

Report message to a moderator

Re: Resolve References through types [message #823102 is a reply to message #822831] Sat, 17 March 2012 20:04 Go to previous messageGo to next message
Alex G is currently offline Alex GFriend
Messages: 96
Registered: January 2012
Member
Hi!

Christian Dietrich wrote on Sat, 17 March 2012 06:26
Hi,

yes! what about doing this with checks and not with scoping. you never have to do everything with scoping



Because I need references/linking, validation and content assist, which come all together out of the box, if using scoping. The disadvantage of using scoping is of course a grammar, which is hard to read. So it is a question of trade-off: Where is more work to be done - in defining a specified grammar and use default XText nature (one central place to work on) vs. implementing and controlling many fragments.
Re: Resolve References through types [message #830237 is a reply to message #823102] Tue, 27 March 2012 11:14 Go to previous message
Frank Schumacher is currently offline Frank SchumacherFriend
Messages: 1
Registered: March 2012
Junior Member
Dear All,

thanks for the hints how to use scopes and references. I have a similar problem as stated in the original post. Basically I have also vertices and edges. Additionally, my vertices have attributes (i.e. ports). How can I access these attributes via cross references? Example: I have a vertex v with a dimensionality [i] and an attribute (port) p. I would like to write A[0].p1 -> A[1].p1.

Any ideas? Thanks
Frank
Previous Topic:Erros when using Xtext in a composite
Next Topic:XText and GMF integration using multi page editor
Goto Forum:
  


Current Time: Wed Apr 08 06:17:53 GMT 2020

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

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

Back to the top