Home » Modeling » TMF (Xtext) » Importing runtime properties?
Importing runtime properties? [message #553375] |
Tue, 17 August 2010 10: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 #553519 is a reply to message #553393] |
Tue, 17 August 2010 19: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 06:43 |
Alexander Nittka Messages: 1193 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 08: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 #553667 is a reply to message #553638] |
Wed, 18 August 2010 12: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 #553684 is a reply to message #553677] |
Wed, 18 August 2010 13:44 |
|
Hi,
digged some further into your problem and founda solution
(not fully tested so far):
first i made the extension of the model (in my case test2 known to the resource service provider
therefore i added to the UI Plugin.xml
<extension point="org.eclipse.xtext.extension_resourceServiceProvider">
<resourceServiceProvider
class="org.xtext.example.mydsl.ui.MyDslExecutableExtensionFactory:org.eclipse.xtext.ui.resource.IResourceUIServiceProvider"
uriExtension="test2">
</resourceServiceProvider>
</extension>
the same may be done in standalone setup too to get it running standalone
public class MyDslStandaloneSetup extends MyDslStandaloneSetupGenerated{
public static void doSetup() {
new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
}
@Override
public void register(Injector injector) {
super.register(injector);
org.eclipse.xtext.resource.IResourceServiceProvider serviceProvider = injector.getInstance(org.eclipse.xtext.resource.IResourceServiceProvider.class);
org.eclipse.xtext.resource.IResourceServiceProvider.Registry.INSTANCE.getExtensionToFactoryMap().put("test2", serviceProvider);
}
}
then you have to use your own ResourceServiceProvider that is a bit more fexible which resource-uris to be accepted
public class MyResourceServiceProvider extends DefaultResourceServiceProvider {
@Override
public boolean canHandle(URI uri) {
if (uri.toPlatformString(true).endsWith("test2")) {
return true;
}
return super.canHandle(uri);
}
}
public class MyDslRuntimeModule extends org.xtext.example.mydsl.AbstractMyDslRuntimeModule {
public Class<? extends IResourceServiceProvider> bindIResourceServiceProvider() {
return MyResourceServiceProvider.class;
}
}
Note that the stuff you reference has to have a name
else you would have to bind a own qualifiednameprovider too.
and do some stuff that linking will work.
~Christian
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
| |
Re: Importing runtime properties? [message #553707 is a reply to message #553684] |
Wed, 18 August 2010 14: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 #553728 is a reply to message #553707] |
Wed, 18 August 2010 15:45 |
|
Hello,
for the name thing
ensure that your eclass has an eattribute with name "name".
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();
}
Hi,
yes it is a toy projects but i cannot share it yet.
Regards
Christian
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
Re: Importing runtime properties? [message #553792 is a reply to message #553728] |
Wed, 18 August 2010 21: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: Sun Sep 22 08:35:48 GMT 2024
Powered by FUDForum. Page generated in 0.05911 seconds
|