Home » Modeling » TMF (Xtext) » Resolve References through types
Resolve References through types [message #822212] |
Fri, 16 March 2012 06:03  |
Eclipse User |
|
|
|
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 #822777 is a reply to message #822243] |
Sat, 17 March 2012 04:24   |
Eclipse User |
|
|
|
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 04:25] by Moderator
|
|
| | |
Re: Resolve References through types [message #830237 is a reply to message #823102] |
Tue, 27 March 2012 07:14  |
Eclipse User |
|
|
|
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
|
|
|
Goto Forum:
Current Time: Wed Jul 16 12:43:01 EDT 2025
Powered by FUDForum. Page generated in 0.10873 seconds
|