Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How can I get the referencing objects for a certain object
icon5.gif  How can I get the referencing objects for a certain object [message #1706086] Fri, 21 August 2015 23:53 Go to next message
Eleanor Richie is currently offline Eleanor RichieFriend
Messages: 125
Registered: August 2014
Senior Member
I have a grammar that looks something like that:

declarations:
'declaration' name=ID '{' declarativeItems+=declarative_items* '}'
;

expression:
'expression' name=ID 'using' declaration=[declarations] '{'
expressions+=multiple_expressions*
'}'
;
-------------------------------------------------------------------
an example:
declaration dec {
int i = 0
int j = 1
int k = 2
}

expression exp1 using dec {

................
}
expression exp2 using dec {
.................
}

-------------------------------------------------------------------
Now in scoping for another grammar rule I kinda need to get all the expressions that cross links with the used dec. I need to get a list of EObjects of (exp1, exp2)
Any idea how to get that? I tried the eCrossReferences but it returns an empty list...
Re: How can I get the referencing objects for a certain object [message #1706091 is a reply to message #1706086] Sat, 22 August 2015 08:13 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Are these references locally or globally?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How can I get the referencing objects for a certain object [message #1706108 is a reply to message #1706091] Sun, 23 August 2015 11:43 Go to previous messageGo to next message
Eleanor Richie is currently offline Eleanor RichieFriend
Messages: 125
Registered: August 2014
Senior Member
actually it can be referenced both locally and globally.
Re: How can I get the referencing objects for a certain object [message #1706111 is a reply to message #1706108] Sun, 23 August 2015 14:27 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Does your language support imports? Ifnod you could use the node text during indexing to store which def is used

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How can I get the referencing objects for a certain object [message #1706112 is a reply to message #1706111] Sun, 23 August 2015 15:03 Go to previous messageGo to next message
Eleanor Richie is currently offline Eleanor RichieFriend
Messages: 125
Registered: August 2014
Senior Member
no, could you please illustrate more in using the node text during indexing?
Re: How can I get the referencing objects for a certain object [message #1706116 is a reply to message #1706112] Sun, 23 August 2015 17:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
that should be straigr forward ?!?

Model:
	declarations+=Declarations*
	expressions+=Expression*
	strangeThing+=StangeThing*
	;
	
	
Declarations:
'declaration' name=ID '{'  '}'
;

Expression:
'expression' name=ID 'using' declaration=[Declarations] '{'
'}'
;

StangeThing:
	"strange" "thing" "for" declaration=[Declarations] "with" expr=[Expression]
;


public class MyDslResourceDescriptionStrategy extends DefaultResourceDescriptionStrategy {
	
	@Override
	public boolean createEObjectDescriptions(EObject eObject, IAcceptor<IEObjectDescription> acceptor) {
		if (eObject instanceof Expression) {
			Map<String, String> data = new HashMap<>();
			data.put("DEF", NodeModelUtils.findNodesForFeature(eObject, MyDslPackage.Literals.EXPRESSION__DECLARATION).get(0).getText());
			IEObjectDescription d = EObjectDescription.create(getQualifiedNameProvider().getFullyQualifiedName(eObject), eObject, data );
			acceptor.accept(d);
		}
		return super.createEObjectDescriptions(eObject, acceptor);
	}

}


class MyDslScopeProvider extends org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider {

	def IScope scope_StrangeThing_expr(StrangeThing ctx, EReference ref) {
		val name = ctx.declaration.name
		val locals = <Declarations>newArrayList
		// todo collect locals manually
		// ...
		Scopes.scopeFor(locals, new FilteringScope(delegateGetScope(ctx,ref),[
			name == getUserData("DEF")
		]))
	}

}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How can I get the referencing objects for a certain object [message #1706118 is a reply to message #1706116] Sun, 23 August 2015 17:46 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
the todo could look like

val locals = EcoreUtil2.getContainerOfType(ctx, Model).expressions.filter[declaration == ctx.declaration]
		


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How can I get the referencing objects for a certain object [message #1706806 is a reply to message #1706118] Mon, 31 August 2015 09:51 Go to previous messageGo to next message
kimi raikonnan is currently offline kimi raikonnanFriend
Messages: 145
Registered: June 2015
Senior Member
Hi,
Is the rule below necessary for the solution ?

StangeThing:
"strange" "thing" "for" declaration=[Declarations] "with" expr=[Expression]
;
Re: How can I get the referencing objects for a certain object [message #1706809 is a reply to message #1706806] Mon, 31 August 2015 10:29 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
of course you need a reference ?!?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How can I get the referencing objects for a certain object [message #1706810 is a reply to message #1706809] Mon, 31 August 2015 10:50 Go to previous messageGo to next message
kimi raikonnan is currently offline kimi raikonnanFriend
Messages: 145
Registered: June 2015
Senior Member
I think its strange to have a rule like this one [StangeThing]
Do you think its possible to get EObjects which "decl" is linked with them, without adding new rule to the two rules in the first question

I need to get the info that "decl" is linked with "exp1" and "exp2"
Re: How can I get the referencing objects for a certain object [message #1706811 is a reply to message #1706810] Mon, 31 August 2015 10:55 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
hi,

if you have a different question than the original question please post a complete grammar and example model.

in this case the question was

StangeThing:
	"strange" "thing" "for" declaration=[Declarations] "with" expr=[Expression]
;


how can i scope expr so that only expressions for the referenced Declaration are in Scope


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How can I get the referencing objects for a certain object [message #1706814 is a reply to message #1706811] Mon, 31 August 2015 11:05 Go to previous messageGo to next message
kimi raikonnan is currently offline kimi raikonnanFriend
Messages: 145
Registered: June 2015
Senior Member
My complete grammar and example model is typical the example provided in the first of the thread, but without this rule "StangeThing"
I hope my question clear now.

[Updated on: Mon, 31 August 2015 11:14]

Report message to a moderator

Re: How can I get the referencing objects for a certain object [message #1706816 is a reply to message #1706814] Mon, 31 August 2015 11:14 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
then i dont get the question:

Now in scoping for another grammar rule I kinda need to get all the expressions that cross links with the used dec. I need to get a list of EObjects of (exp1, exp2) == StrangeThing

how does your another grammar rule look like


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How can I get the referencing objects for a certain object [message #1706817 is a reply to message #1706816] Mon, 31 August 2015 11:28 Go to previous messageGo to next message
kimi raikonnan is currently offline kimi raikonnanFriend
Messages: 145
Registered: June 2015
Senior Member
If this is my grammer
Model:
	declarations+=Declarations*
	expressions+=Expression*
	;
	
Declarations:
'declaration' name=ID '{'  '}'
;

Expression:
'expression' name=ID 'using' declaration=[Declarations] '{'
'}'
;


and this is the example
------------------------------------
declaration dec {
}
expression exp1 using dec {
}
expression exp2 using dec {
}

My question is how could we get EObjects which "decl" is linked with them?
Currently we can get declaration from the expression but how could we get the expression from declaration?

Thanks for your quick replies
Re: How can I get the referencing objects for a certain object [message #1706824 is a reply to message #1706817] Mon, 31 August 2015 12:18 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

my code does this ?!? simply ask the scope provider for decls and filter them


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How can I get the referencing objects for a certain object [message #1706826 is a reply to message #1706824] Mon, 31 August 2015 12:31 Go to previous messageGo to next message
kimi raikonnan is currently offline kimi raikonnanFriend
Messages: 145
Registered: June 2015
Senior Member
we have to change the name of this function "scope_StrangeThing_expr", Right ?
And keep this one "createEObjectDescriptions" as its
Re: How can I get the referencing objects for a certain object [message #1706827 is a reply to message #1706826] Mon, 31 August 2015 12:33 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
hi,

you still did no say "where" you want to do this analysis


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How can I get the referencing objects for a certain object [message #1706830 is a reply to message #1706827] Mon, 31 August 2015 12:38 Go to previous messageGo to next message
kimi raikonnan is currently offline kimi raikonnanFriend
Messages: 145
Registered: June 2015
Senior Member
we have to change the name of this function "scope_StrangeThing_expr"
And keep this one "createEObjectDescriptions" as its
Right ?

what do you mean by "where"?
I will implement this code in class extends "ImportedNamespaceAwareLocalScopeProvider"
Did i understand your question correctly?

[Updated on: Mon, 31 August 2015 12:38]

Report message to a moderator

Re: How can I get the referencing objects for a certain object [message #1706834 is a reply to message #1706830] Mon, 31 August 2015 12:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

at which place in the code you you want to have the question answered:

which expression is uses declaration abc


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How can I get the referencing objects for a certain object [message #1706840 is a reply to message #1706834] Mon, 31 August 2015 12:58 Go to previous messageGo to next message
kimi raikonnan is currently offline kimi raikonnanFriend
Messages: 145
Registered: June 2015
Senior Member
Hi,
In our case we have decl and need to get expressions using it, not the reverse is this clear for you?
We need to extract this info after building the project.
Re: How can I get the referencing objects for a certain object [message #1706842 is a reply to message #1706840] Mon, 31 August 2015 13:02 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
yay but where is the place you ask this project? builder participant generator .....?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How can I get the referencing objects for a certain object [message #1706843 is a reply to message #1706842] Mon, 31 August 2015 13:03 Go to previous messageGo to next message
kimi raikonnan is currently offline kimi raikonnanFriend
Messages: 145
Registered: June 2015
Senior Member
In class extends IXtextBuilderParticipant
Re: How can I get the referencing objects for a certain object [message #1706854 is a reply to message #1706843] Mon, 31 August 2015 13:14 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
yes at this please you can simply ask the scopeprovider.
you dont need a scope method.
simply inject the scopeprovider and ask it for scope for Expression and then filter the results.

alternatively you can use the index directly

(IResourceDesciption/referencedescriptions)


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

[Updated on: Mon, 31 August 2015 13:15]

Report message to a moderator

Re: How can I get the referencing objects for a certain object [message #1706867 is a reply to message #1706854] Mon, 31 August 2015 14:24 Go to previous messageGo to next message
kimi raikonnan is currently offline kimi raikonnanFriend
Messages: 145
Registered: June 2015
Senior Member
how could we use IResourceDesciption/referencedescriptions in this case?
And which scopeprovider to inject?
Re: How can I get the referencing objects for a certain object [message #1706873 is a reply to message #1706867] Mon, 31 August 2015 14:39 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
you can inject IResourceDescriptions. Or use IContainer.Manager (searching will give you some snippets)
IGlobalScopeProvider should be fine


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How can I get the referencing objects for a certain object [message #1706895 is a reply to message #1706873] Mon, 31 August 2015 16:19 Go to previous messageGo to next message
kimi raikonnan is currently offline kimi raikonnanFriend
Messages: 145
Registered: June 2015
Senior Member
IGlobalScopeProvider should be fine in which scenario
also how could we use IContainer.Manager to perform such operation? , i forgot to say that expressions and declarations could be in different files
Re: How can I get the referencing objects for a certain object [message #1706896 is a reply to message #1706895] Mon, 31 August 2015 16:38 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
That is no problem. Simply give it a trY.
With globalscopeprovider
Use the search to find about the manager and visible containers.
(I donthave the time to do the actual taskfor you)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How can I get the referencing objects for a certain object [message #1706904 is a reply to message #1706896] Mon, 31 August 2015 17:36 Go to previous messageGo to next message
kimi raikonnan is currently offline kimi raikonnanFriend
Messages: 145
Registered: June 2015
Senior Member
Hi ,
I am just asking to elaborate more, and to understand what is under the hood.
In IXtextBuilderParticipant i have EObject represent declaration ("declVar") and i added the two lines below to get ResourceDescription and ResourceDescriptions
    IResourceDescriptions descriptions = resourceDescriptionsProvider.getResourceDescriptions(declVar.eResource());
    IResourceDescription resourceDescription = descriptions.getResourceDescription(resource.getURI());
    IContainer yyyy = manager.Containers(resourceDescription, descriptions);

What is next?
Re: How can I get the referencing objects for a certain object [message #1706907 is a reply to message #1706904] Mon, 31 August 2015 17:49 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Have a look at

* org.eclipse.xtext.resource.IContainer.Manager.getVisibleContainers(IResourceDescription, IResourceDescriptions) to get a list of containers.
* a container can be asked for org.eclipse.xtext.resource.ISelectable.getExportedObjectsByType(EClass) - may give you all decls/expresions
* a container can be asked for org.eclipse.xtext.resource.IContainer.getResourceDescriptions()
* a IResourceDescription can be asked for org.eclipse.xtext.resource.IResourceDescription.getReferenceDescriptions()
* a IReferenceDescription has a source and target object uri and the reference it represents - reference should be of type type you search for, target uri should point to the stuff you are looking for
* as said before IReferenceDescription is created for non local refs


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Validation of non-null enum rule
Next Topic:How to support long and short syntax with same grammar
Goto Forum:
  


Current Time: Fri Apr 26 14:23:09 GMT 2024

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

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

Back to the top