Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Loading model objects(About importing models in xtext)
Loading model objects [message #643899] Wed, 08 December 2010 16:14 Go to next message
Darie Moldovan is currently offline Darie MoldovanFriend
Messages: 67
Registered: November 2010
Location: Darmstadt, Germany
Member
Hi everyone,

I have a question regarding XText and some Ecore. I hope I'm submitting this topic into the right category Smile

The xtext grammar is not so important regarding my question, you will understand why.

I built an xtext editor for a special language and want to implement some features, like a context-sensitive autocomplete.

Please read the following extract from a file, which I open with my xtext editor:

reference ref1 ( uri "file1.isr" )
reference ref2 ( uri "file2.bpmn2" )

endpoint A (
	reference ref1
	object obj1
)

endpoint B (
	reference ref1
	object obj2
)

endpoint C (
	reference ref2
	object obj3
)


As you can see, I loaded two models (which are two separate files located in the same project as the file I'm editing) via the reference keyword, which is defined in my xtext grammar as follows:

Reference:
	'reference' name=ID '('
		'uri' importURI=STRING
	')'
;


No big deal, it's all about loading some resources and using them as data models.

As you can see in the example in the beginning of this topic, I have several endpoint elements with some properties. The reference property tells me: "hey, this object has something to do with ref1, for example", where ref1 is defined at the top of the file as "file1.isr". When the user hits Ctrl+space in the second property, namely object, I want the autocompleter to only suggest things from the file1.isr model (let's say only obj1 and obj2 are defined in file1.isr, obj3 is defined in file2.bpmn2).

By default, Xtext adds to the suggestion list of the autocompleter everything it finds from both referenced models, file1.isr and file2.bpmn2, which is not what I want. For example, Xtext makes no difference if I write nonsense, like

endpoint A (
	reference ref1
	object obj3
)


although obj3 is defined in ref2 and not in ref1. The context sensitivity, if I may name it like that, is important to me.

I've tried some things which are pretty hard to explain, especially if I'm not even sure, someone is gonna read/reply to this topic.

But I can give more details if someone is interested. Until then, my question is: HOW can I acces the objects in a referenced model?

I think this is pretty much Ecore and only little Xtext. May be. I'm not interesting in basics, like in which class to customize the autocompleter. I'm more interested in hints like where to look in order to understand how xtext and ecore communicate to manage imported models.

Thank you in advance.
Re: Loading model objects [message #643933 is a reply to message #643899] Wed, 08 December 2010 17:51 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
You have to implement scoping for the object reference.

That should navigate to the referenced 'reference' model and only add
elements contained therein to scope.

It's all explained in the documentation (section scoping).

Sven

Am 12/8/10 5:14 PM, schrieb Darie:
> Hi everyone,
>
> I have a question regarding XText and some Ecore. I hope I'm submitting
> this topic into the right category :)
> The xtext grammar is not so important regarding my question, you will
> understand why.
>
> I built an xtext editor for a special language and want to implement
> some features, like a context-sensitive autocomplete.
> Please read the following extract from a file, which I open with my
> xtext editor:
>
> reference ref1 ( uri "file1.isr" )
> reference ref2 ( uri "file2.bpmn2" )
>
> endpoint A (
> reference ref1
> object obj1
> )
>
> endpoint B (
> reference ref1
> object obj2
> )
>
> endpoint C (
> reference ref2
> object obj3
> )
>
> As you can see, I loaded two models (which are two separate files
> located in the same project as the file I'm editing) via the reference
> keyword, which is defined in my xtext grammar as follows:
>
> Reference:
> 'reference' name=ID '('
> 'uri' importURI=STRING
> ')'
> ;
>
> No big deal, it's all about loading some resources and using them as
> data models.
> As you can see in the example in the beginning of this topic, I have
> several endpoint elements with some properties. The reference property
> tells me: "hey, this object has something to do with ref1, for example",
> where ref1 is defined at the top of the file as "file1.isr". When the
> user hits Ctrl+space in the second property, namely object, I want the
> autocompleter to only suggest things from the file1.isr model (let's say
> only obj1 and obj2 are defined in file1.isr, obj3 is defined in
> file2.bpmn2).
> By default, Xtext adds to the suggestion list of the autocompleter
> everything it finds from both referenced models, file1.isr and
> file2.bpmn2, which is not what I want. For example, Xtext makes no
> difference if I write nonsense, like
> endpoint A (
> reference ref1
> object obj3
> )
>
> although obj3 is defined in ref2 and not in ref1. The context
> sensitivity, if I may name it like that, is important to me.
>
> I've tried some things which are pretty hard to explain, especially if
> I'm not even sure, someone is gonna read/reply to this topic.
>
> But I can give more details if someone is interested. Until then, my
> question is: HOW can I acces the objects in a referenced model?
> I think this is pretty much Ecore and only little Xtext. May be. I'm not
> interesting in basics, like in which class to customize the
> autocompleter. I'm more interested in hints like where to look in order
> to understand how xtext and ecore communicate to manage imported models.
>
> Thank you in advance.


--
Need professional support for Xtext or other Eclipse Modeling technologies?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : http://blog.efftinge.de
Re: Loading model objects [message #643972 is a reply to message #643933] Wed, 08 December 2010 20:57 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
<shameless plug>You also might find some of the following useful: http://dslmeinte.wordpress.com/2010/09/06/tricks-for-impleme nting-scoping/
</shameless plug> Very Happy


Re: Loading model objects [message #647269 is a reply to message #643899] Wed, 05 January 2011 13:44 Go to previous message
Darie Moldovan is currently offline Darie MoldovanFriend
Messages: 67
Registered: November 2010
Location: Darmstadt, Germany
Member
Hello Sven, hello Meinte,

thank you for your replies. You were right, scoping was the solution to my problem. I managed to suggest (in the autocompleter) only the objects from the corresponding resources.

Here's what I wrote in the class which extends the AbstractDeclarativeScopeProvider:

Resource res = currentEnd.getReference().eResource(); // this is the context
		
Resource r = EcoreUtil2.getResource(res, currentEnd.getReference().getImportURI()); // get the resource within the context (for importUri, see my first post)
		
TreeIterator<EObject> resObjs = r.getAllContents(); // get all the objects from the corresponding resource
List<EObject> elements = new ArrayList<EObject>(); // create an auxiliary list just to help us return what we want

while(resObjs.hasNext())
	elements.add(resObjs.next()); // add the elements to the autocomplete list
		
return Scopes.scopeFor(elements);


Thanks again, I hope this post will help others who have the same question as I did.

Cheers
Previous Topic:Outline View not working in Xtext 1.0.1?
Next Topic:Ideas on ways to handle a type definition for fixed fields.
Goto Forum:
  


Current Time: Sat Apr 27 02:00:51 GMT 2024

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

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

Back to the top