Home » Modeling » TMF (Xtext) » Importing runtime properties?
| Importing runtime properties? [message #553375] |
Tue, 17 August 2010 06:54  |
Eric Messages: 13 Registered: August 2010 |
Junior Member |
|
|
Hi everybody.
I'm new to xText, and I was wondering whether there's a chance of doing the following (I know it might sound a little bit weird...):
let's say I have such a grammar:
GR = 'hello' | ITEM-ID
ITEM-ID = ...
Now, what I'd like to have, as rule for ITEM-ID, is somewhat difficult to explain. I'll try to do my best 
I have an EMF model, with an entity called Item, which contains a field (an integer, maybe) called ID. I can create instances of Item at runtime, and these instances' class will be ItemImpl. Now, let's say that I have two instances whose IDs are 10 and 20. Then, my rule should be able to adapt dinamically, thus being
ITEM-ID = 10 | 20
If I deleted one of these two instances (let's say the one with ID=20), I'd like the rule to become:
ITEM-ID = 10
Is this possible? I apologize in advance if this is completely absurd (or if I haven't been that clear), but looking at the documention I haven't been able to find anything that looks similar to what I want to do, but maybe there's some special feature that it's not listed in there.
Anyway, thank you very much for your attention, and please excuse me if my English is not that good
|
|
| | |
| Re: Importing runtime properties? [message #553383 is a reply to message #553375] |
Tue, 17 August 2010 07:10   |
Alexander Nittka Messages: 1146 Registered: July 2009 |
Senior Member |
|
|
Hi,
it is not possible to change the grammar during runtime. The language
infrastructure is fixed until regenerated and newly deployed.
What you seem to want is cross references to the objects in your EMF
model. You can use meta model import in the grammar and then let
reference point to the corresponding types. See the Xtext documentation
with respect to cross references and imports.
Alex
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
|
|
| |
| Re: Importing runtime properties? [message #553392 is a reply to message #553382] |
Tue, 17 August 2010 07:39   |
Alexander Nittka Messages: 1146 Registered: July 2009 |
Senior Member |
|
|
Hi,
please do read the Xtext documentation chapter 3 (at least twice) and
also have a look at the examples that ship with Xtext to get a feeling
for the grammar language and the basic concepts.
In the grammar you do not reference the instances but declare that at a
certain position the reference to an instance of a paricular type is
expected. The grammar does not care about whether there are no or a
million instances.
//see in particular chapter 3.2.2.2
import "platformURItoTheEcoreModelRepresentingTheMetamodel" as type
Model: elements+=HelloInstance*;
HelloInstance: 'hello' reference=[type::Item|QualifiedName];
QualifiedName: /*define how the syntactic representation of the names
you reference the instances with looks like*/
;
Note that you might have to adapt the scoping in order to make visible
the actual instances (as Meinte was pointing out).
Alex
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
|
|
|
| Re: Importing runtime properties? [message #553393 is a reply to message #553387] |
Tue, 17 August 2010 07:47   |
Alexander Nittka Messages: 1146 Registered: July 2009 |
Senior Member |
|
|
> How is this different?
Your original idea is changing the language according to the objects
present in the world.
Assuming the existence only of Item10 in your approach "Hello Item10" is
syntactically valid and "Hello Item20" is syntactically invalid.
With the correct handling of the matter using cross references and
having a specific representation for the names "Hello Item10" and "Hello
Item20" are both syntactically valid. The second however will produce a
linking error as no correctly typed object with name "Item20" can be found.
Alex
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
|
|
|
| Re: Importing runtime properties? [message #553519 is a reply to message #553393] |
Tue, 17 August 2010 15:12   |
Eric Messages: 13 Registered: August 2010 |
Junior Member |
|
|
Alexander, thank you very much for your help, and for having pointed me to the right part of the documentation.
I've managed to do something, but now unfortunately it seems like I need further help 
Still, before asking for technical support, I'd like to check whether I've understood correctly some fundamental things.
So, I've been able to reference an external EMF model. I've used the "import" directive, something like this:
'hello' reference=[model::Item|QualifiedName];
QualifiedName: ID ('.' ID)*;
(when I type "model::" "Item" and "Person" appear, which is correct since they are the entities I define in the model.
In the mwe2 file, I've added
fragment = ecore.EcoreGeneratorFragment {
referencedGenModels = 'platform:/resource/model/model.genmodel'
}
Now, as far as I've understood, the QualifiedName is used to search (in the scope, I still don't know how to refine it but that's not important right now) for instances of Item with that name.
In fact, if I write (in the editor)
an error message appears, saying "Couldn't resolve reference to Item 'example'"
That's normal, I guess: the scope is basically unknown to me, but - most of all - I don't know which are the names of my entities of Item.
How can I know the name of the instances? Am I out-of-track or is this the problem I've to solve at first? If there's a way of referring to the existing instancies such that the editor raises an error if those don't exist, and it doesn't if they exist, then that would basically be enough to me 
I hope I'm not that much out-of-track, but - in case - really, you've to excuse me: I've read the documentation, but it's a bit difficult to me understanding it in details: I don't have a big Java background and it's the first time I'm "playing" with EMF as well 
Again, thanks in advance for your help... and your patience
|
|
| |
| Re: Importing runtime properties? [message #553599 is a reply to message #553519] |
Wed, 18 August 2010 02:43   |
Alexander Nittka Messages: 1146 Registered: July 2009 |
Senior Member |
|
|
Hi,
> Still, before asking for technical support, I'd like to check whether
> I've understood correctly some fundamental things.
Well, you have fundamentally misunderstood one thing.
> So, I've been able to reference an external EMF model. I've used the
> "import" directive, something like this:
In the grammar you do not import the model you want to reference, but
the meta model.
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
....
EcoreReference:
'ecoreObject' eobjectRef=[ecore::EClass|QualifiedName];
//if the elements you want to reference are not of type EClass,
//use the corresponding type instead
....
Xtext will now look for all visible EClasses when linking etc.
Alex
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
|
|
|
| Re: Importing runtime properties? [message #553624 is a reply to message #553599] |
Wed, 18 August 2010 04:43   |
Eric Messages: 13 Registered: August 2010 |
Junior Member |
|
|
| Alexander Nittka wrote on Wed, 18 August 2010 02:43 |
Well, you have fundamentally misunderstood one thing.
In the grammar you do not import the model you want to reference, but
the meta model.
|
What you mean is that I'm importing the definition of a model, not the model itself (which means that I'm not importing any instance of the model), right?
| Quote: |
EcoreReference:
'ecoreObject' eobjectRef=[ecore::EClass|QualifiedName];
//if the elements you want to reference are not of type EClass,
//use the corresponding type instead
|
Ok: then my use of
'hello' reference=[model::Item|ID];
should be correct, I guess: I'm trying to reference elements of type Item.
| Quote: |
Xtext will now look for all visible EClasses when linking etc.
|
So, in my case, Xtext will look for all visible elements of type Item: is that correct?
|
|
| | |
| Re: Importing runtime properties? [message #553638 is a reply to message #553633] |
Wed, 18 August 2010 05:45   |
Alexander Nittka Messages: 1146 Registered: July 2009 |
Senior Member |
|
|
> Now, the important question is:
> what's the name of the instances I'm searching for?
How should I know? You said there were models containing the instances.
With the above code snippet (importing ecore), all visible EClasses were
proposed to me, including those I put in a custom .ecore file.
You give way too little background about your use case.
Alex
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
|
|
|
| Re: Importing runtime properties? [message #553667 is a reply to message #553638] |
Wed, 18 August 2010 08:34   |
Eric Messages: 13 Registered: August 2010 |
Junior Member |
|
|
| Alexander Nittka wrote on Wed, 18 August 2010 05:45 |
With the above code snippet (importing ecore), all visible EClasses were
proposed to me, including those I put in a custom .ecore file.
|
Ok, I've tried that and it works. It shows me the names of the EClasses that I define in a .ecore file contained in the same project, while it doesn't show me those that are defined in other projects in the workspace.
| Quote: |
You give way too little background about your use case.
|
You're right, I guess I haven't been able to detail exactly what I want to do. I'll use some screenshots.
This is my "runtime" Eclipse:

This is the grammar I define:
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
import 'platform:/resource/org.xtext.example.mydsl/My.ecore' as types
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
Model:
greetings+=Greeting*;
Greeting:
('Hello' name=ID '!') | ('Goodbye' reference=[types::Concept|ID]) | ('Hi' eobjectRef=[ecore::EClass|ID]);
Now, as you can see in the first screenshot, I can refer to instances of EClass ("Hi sample" works).
Now, in the "runtime Eclipse" I've created an instance of the model "My.ecore". In this model, I've created an instance of the class Concept. The second screenshot shows exactly this.

However, when I type "Goodbye " I get no hints. In fact, it seems like xText were unable to find the reference. Now,my hypothesis is that this might be related to the fact that this instance I'm trying to refer to is not actually directly accessible from the workspace: it is contained in a model. This would relate to the scoping issue you were pointing out before. However, even if it were visible (which maybe it is!) I don't know which name it owns. For the classes defined in the ecore model I've noticed that the field "name" is examined, but I've no clue what would identify my entities.
|
|
| | | |
| Re: Importing runtime properties? [message #553693 is a reply to message #553677] |
Wed, 18 August 2010 09:53   |
Alexander Nittka Messages: 1146 Registered: July 2009 |
Senior Member |
|
|
My guess is that the models corresponding to the metamodel you import in
the Xtext grammar are not automatically processed for the index that is
used for resolving references (models created with Xtext editors are).
As to possible solutions, I can suggest none for which I could give you
further assistance.
Alex
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
|
|
|
| Re: Importing runtime properties? [message #553707 is a reply to message #553684] |
Wed, 18 August 2010 10:46   |
Eric Messages: 13 Registered: August 2010 |
Junior Member |
|
|
| Christian Dietrich wrote on Wed, 18 August 2010 09:44 | Hi,
digged some further into your problem and founda solution
|
First of all, Christian and Alex, thank you very much for your precious help!
So, I've followed Christian's instructions (I've no idea how to verify Alex's hypothesis) and I've added the snippets he posted. Still, I don't get any hint, but - as Christian was pointing out - I guess this is due to
| Quote: |
Note that the stuff you reference has to have a name
|
How should I ensure that my instances have a name? Should I add some attribute to the classes in the meta-model?
I should point out that everything compiles perfectly and the ResourceServiceProvided I've added is surely working (I've checked whether it gets accessed). Christian, have you tried your code in a toy-project? In case, could you post it, so I can search for differences?
Thanks in advance.
|
|
| |
| Re: Importing runtime properties? [message #553792 is a reply to message #553728] |
Wed, 18 August 2010 17:34  |
Eric Messages: 13 Registered: August 2010 |
Junior Member |
|
|
| Christian Dietrich wrote on Wed, 18 August 2010 11:45 | Hello,
for the name thing
ensure that your eclass has an eattribute with name "name".
|
Christian, you totally solved my problem, that works as a charm! I actually had already tried this solution while I was waiting for your answer, but I was using "Name" and apparently it's case-sensitive. Anyway, your help has been absolutely fundamental to me 
| Quote: | [code]or create your own org.eclipse.xtext.naming.DefaultDeclarativeQualifiedNameProv ider subclass and bind it for org.eclipse.xtext.naming.IQualifiedNameProvider in your Runtime Module
in your NameProvider write a method like this
public String qualifiedName(Todo2 todo) {
return todo2.getAnAttributeThatActsAsName();
}
|
Still, I will try to implement also this second solution, since this would allow me to do exactly what I was trying to achieve in my first post, and that would be even better.
| Quote: |
yes it is a toy projects but i cannot share it yet.
|
Ok, no problem. If new problems will arise I'll ask for help again, but I think that now the biggest problem is solved 
|
|
|
Goto Forum:
Current Time: Tue Oct 08 08:01:31 EDT 2013
Powered by FUDForum. Page generated in 0.02272 seconds
|