Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » cross reference in different files
cross reference in different files [message #1744247] Fri, 23 September 2016 15:03 Go to next message
Kostas Triantafyllidis is currently offline Kostas TriantafyllidisFriend
Messages: 15
Registered: March 2011
Junior Member
Hello all,

I have three different grammars and i would like to cross-reference hierarchically.
I will give an example and part of the rules of those grammars to illustrate my problem.

grammar A (iface):

InEvent:
type = String name = ID
;


grammar B (impl):

EventAction:
name = ([iface::InEvent | FQN])
;


grammar C:
Transition:
(input=[impl::EventAction | FQN] '=>' state = [StateDeclaration] ) ;

Grammar B is able to see the InEvents from grammar A, referring to the name ID. I want make a cross reference to this name in grammar C. However, it is not visible due to the fact that it is not ID but it is a cross reference to grammar A (at least this is what i discovered). So, Grammar C can see elements defined in Grammar B when these elements are of ID type.
Then, i thought that a solution could be to update the scope provider. But i am facing the problem that I have to access the rootElement of another file. Does anyone know how can i do that?
my current scope provider is as follows:

override getScope(EObject context, EReference reference)
{
if (context instanceof Transition)
{
if (reference.name == "input")
{
val rootElement = EcoreUtil2.getRootContainer(context)
val importFiles = EcoreUtil2.getAllContentsOfType(rootElement, Import)
println(importFiles );

val candidates = EcoreUtil2.getAllContentsOfType(rootElement, EventAction)
return Scopes.scopeFor(candidates)
}
}
return super.getScope(context, reference);
}

I am able to find the eObjects with the import URIs, but i do not know if this is the right way to proceed.

Thank you in advance.

Kostas
Re: cross reference in different files [message #1744249 is a reply to message #1744247] Fri, 23 September 2016 15:14 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Names have to be attributes by default.
You can use a custom iqualifiednameprovider together with nodemodelutils
To build a name of the cross references text


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: cross reference in different files [message #1744588 is a reply to message #1744249] Wed, 28 September 2016 15:58 Go to previous messageGo to next message
Kostas Triantafyllidis is currently offline Kostas TriantafyllidisFriend
Messages: 15
Registered: March 2011
Junior Member
Thank you for your reply. I have tried your approach. More specifically, i have implemented the qualifiedname provider in grammar B as following:

override def QualifiedName getFullyQualifiedName(EObject ele) {
if (ele instanceof EventAction) {
val List<INode> nodes = NodeModelUtils.findNodesForFeature(ele, ImplementationPackage.Literals.EVENT_ACTION__NAME);
if (nodes.size()>0)
{
return QualifiedName.create(NodeModelUtils.getTokenText(nodes.get(0)));
}
} else {
return super.getFullyQualifiedName(ele);
}
}


Then i made a simple scope in grammar C.
def scope_InputEvent_name(InputEvent t, EReference ref)
{
//return Scopes.scopeFor(t.eCrossReferences)
val scope = delegateGetScope(t, ref);
println("======");
for (el: scope.allElements) {
println(el.name);
}
return delegateGetScope(t, ref);
}

For debugging purposes i have the println which correctly prints the name references :
======
UI.SetResultScreen
UI.Click_Start
UI.Click_Stop
UI.Click_Ok
UI.Click_Cancel
UI.Click_Yes
UI.Click_No

In the Eclipse runtime workspace the ctrl+space works and proposes with the correct reference name options, as you can see in the attached picture. The quickfix also proposes the same references. However, when i select one of the options, i get the error: "Couldn't resolve reference to EventAction 'UI.Click_Cancel' ".

I suspect that there is an issue with namespaces, but i am not sure. The namespaces from the individual grammars are:
A -> UI
B -> Impl
C -> BT.BeepImpl

I am providing you few screenshots from the debug mode, that may help you to locate my error. Am i missing something here? Except the qualifiednameprovider at grammar B should i make any amendment in Grammar C, for instance update the local scope provider? I am using xtext 2.9.2 in case that helps you to find the root of my problem. Thank you in advance.

[Updated on: Wed, 28 September 2016 16:00]

Report message to a moderator

Re: cross reference in different files [message #1744605 is a reply to message #1744588] Wed, 28 September 2016 19:52 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
How does the complete scopeprovider class look like? Does it inherit from abstractdeckarativescopeprovider ?
And how does the grammar look like?

The scope method you posted and the grammar you posted don't match


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: cross reference in different files [message #1744630 is a reply to message #1744605] Thu, 29 September 2016 06:53 Go to previous messageGo to next message
Kostas Triantafyllidis is currently offline Kostas TriantafyllidisFriend
Messages: 15
Registered: March 2011
Junior Member
The scope provider class extends the AbstractDeclarativeScopeProvider. These are the most interesting parts of the two grammars. (The grammars and the scopeprovider are attached as xtext documents.)

Grammar B:

ImlpModel:
'Implementation' name = ID '{'
((('Variables' '{' vars += Var+ '}') ) |
(('Event_Action' eventActions += EventAction))|
(('Check_Action' checkActions += CheckAction))|
(('PreAction' preaction += PreAction)))*
'}'
;

Import:
'import' importURI = STRING
;

//Action has a name and arguments
EventAction:
name = ([iface::InEvent | FQN]) ("("((args += Var)? ("," args += Var)*) ")")? (":" (type = [tTypes::SimpleTypeDecl]))?
"{" actions = (SeqAction|BranchAction) "}"

;


CheckAction:
name = ([iface::OutEvent | FQN]) ("("((args += Var)? ("," args += Var)*) ")")? (":" (type = [tTypes::SimpleTypeDecl]))?
"{" actions = (SeqAction|BranchAction) "}"

;


PreAction:
name = ID ("("((args += Var)? ("," args += Var)*) ")")? (":" (type = [tTypes::SimpleTypeDecl]))?
"{" actions = (SeqAction|BranchAction) "}"


Where iface is the reference to grammar A

Grammar C:

Transition:
(
(input=InputEvent('(' varVal += VariableValue+ ')')?
(
((preaction+=Preactions)? '=>' state=[StateDeclaration])
)
|
(
'=>' defaultState=("PreviousState" | "CurrentState" | "TerminalState")
)
)
|
("Default" "=>"
(
(state=[StateDeclaration])
|
(defaultState=("PreviousState" | "CurrentState" | "TerminalState"))
)
)
)
('{'
(('in' inEvents += InputEvent)|('out' outEvents += InputEvent))+
'}'
)?
;

Preactions:
'::' pa = [impl::PreAction | FQN]
('('(args += VariableValue) (',' args += VariableValue)*')')?
;


VariableValue:
varVal = (ExpressionConstantInt|ExpressionConstantBool|ExpressionConstantString | ExpressionConstantReal | ExpressionAny)
;

InputEvent:
name = [impl::EventAction | FQN]
;


Re: cross reference in different files [message #1744634 is a reply to message #1744630] Thu, 29 September 2016 07:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Does the name already contain a dot on dsl b side? If yes you should use iqualifiednameconverter to create the name. Else a name inside a segment may contain the dot which is not allowed

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: cross reference in different files [message #1744637 is a reply to message #1744634] Thu, 29 September 2016 07:56 Go to previous messageGo to next message
Kostas Triantafyllidis is currently offline Kostas TriantafyllidisFriend
Messages: 15
Registered: March 2011
Junior Member
Yes it contains a dot of the namespace UI. So the provided qualified name is UI.Click_ok. But where should i use the iqualifiednameconverter? Shall i use it inside the extended qualifiednameprovider? Could you give me an example?
Re: cross reference in different files [message #1744641 is a reply to message #1744637] Thu, 29 September 2016 08:24 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Yes

@Inject Xxx xxx;

xxx.toQualifiedname(mystring )


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Thu, 29 September 2016 08:26]

Report message to a moderator

Re: cross reference in different files [message #1744647 is a reply to message #1744641] Thu, 29 September 2016 09:21 Go to previous messageGo to next message
Kostas Triantafyllidis is currently offline Kostas TriantafyllidisFriend
Messages: 15
Registered: March 2011
Junior Member
Thank you a lot for your help. It finally works! I am posting the qualifiednameprovider in case that someone faces the same problem in the future.

public class ImplementationQualifiedNameProvider extends DefaultDeclarativeQualifiedNameProvider {

@Inject
private IQualifiedNameConverter converter = new IQualifiedNameConverter.DefaultImpl();

override def QualifiedName getFullyQualifiedName(EObject ele) { // Copy this to the .impl DSL
if (ele instanceof EventAction) {
val List<INode> nodes = NodeModelUtils.findNodesForFeature(ele, ImplementationPackage.Literals.EVENT_ACTION__NAME);
if (nodes.size()>0)
{
val qn = QualifiedName.create(NodeModelUtils.getTokenText(nodes.get(0)))

return converter.toQualifiedName(qn.toString)
}
} else {
return super.getFullyQualifiedName(ele);
}
}
}
Re: cross reference in different files [message #1744655 is a reply to message #1744647] Thu, 29 September 2016 10:20 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
You don't need the = new .... thing on the field

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Embedded Xtext editor slects all text after using updateModel
Next Topic:Using Java instead of XTend
Goto Forum:
  


Current Time: Tue Apr 23 10:41:01 GMT 2024

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

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

Back to the top