Home » Modeling » TMF (Xtext) » Resolve References through types
Resolve References through types [message #822212] |
Fri, 16 March 2012 10:03 |
Alex G 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 |
|
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
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 |
Alex G 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
|
|
| | | |
Goto Forum:
Current Time: Fri Apr 19 00:25:49 GMT 2024
Powered by FUDForum. Page generated in 0.02505 seconds
|