Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Importing runtime properties?
Importing runtime properties? [message #553375] Tue, 17 August 2010 10:54 Go to next message
Eric  is currently offline Eric Friend
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 Smile

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 Smile
Re: Importing runtime properties? [message #553381 is a reply to message #553375] Tue, 17 August 2010 11:08 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
You need a combination of things to do this with Xtext:
- reference Item instances directly from the grammar (not through some ID);
- provide scoping for references (assignments) to Item instances in such a way that the scoping provider is aware of your runtime ItemImpl instances.


Re: Importing runtime properties? [message #553382 is a reply to message #553381] Tue, 17 August 2010 11:19 Go to previous messageGo to next message
Eric  is currently offline Eric Friend
Messages: 13
Registered: August 2010
Junior Member
Meinte Boersma wrote on Tue, 17 August 2010 07:08
You need a combination of things to do this with Xtext:
- reference Item instances directly from the grammar (not through some ID);



First of all, thank you for your kind reply. I think I'll have to work on this step-by-step, 'cause I'm kinda new to these concepts.

I'm not sure I understood what you mean. You're saying that I should reference instances directly from the grammar, but there's a chance that there isn't even a single "living" instance. Are you saying that my rule should be something like the following?

GR = 'hello' | ItemImpl.ID

If so, how do I reference the ItemImpl class?

Again, thanks in advance.
Re: Importing runtime properties? [message #553383 is a reply to message #553375] Tue, 17 August 2010 11:10 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1159
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 #553387 is a reply to message #553383] Tue, 17 August 2010 11:38 Go to previous messageGo to next message
Eric  is currently offline Eric Friend
Messages: 13
Registered: August 2010
Junior Member
Alexander Nittka wrote on Tue, 17 August 2010 07:10
Hi,

it is not possible to change the grammar during runtime. The language
infrastructure is fixed until regenerated and newly deployed.


Ok, this seems to inhibit me from doing what I wanted to do.

Quote:
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.


How is this different?

Is this ( http://www.eclipse.org/Xtext/documentation/1_0_0/xtext.html# grammarImport) the documentation you're referring to?

Thanks in advance
Re: Importing runtime properties? [message #553392 is a reply to message #553382] Tue, 17 August 2010 11:39 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1159
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 11:47 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1159
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 19:12 Go to previous messageGo to next message
Eric  is currently offline Eric Friend
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 Sad

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)

hello example


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 Smile

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 Smile

Again, thanks in advance for your help... and your patience Wink
Re: Importing runtime properties? [message #553594 is a reply to message #553519] Wed, 18 August 2010 07:18 Go to previous messageGo to next message
Eric  is currently offline Eric Friend
Messages: 13
Registered: August 2010
Junior Member
Quote:

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)*;




Sorry, actually what I'm using is the following:

'hello' reference=[model::Item|ID];

Re: Importing runtime properties? [message #553599 is a reply to message #553519] Wed, 18 August 2010 06:43 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1159
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 Go to previous messageGo to next message
Eric  is currently offline Eric Friend
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 #553628 is a reply to message #553624] Wed, 18 August 2010 08:47 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1159
Registered: July 2009
Senior Member
> 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?

yes.

> So, in my case, Xtext will look for all visible elements of type Item:
> is that correct?

yes.

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 #553633 is a reply to message #553628] Wed, 18 August 2010 09:29 Go to previous messageGo to next message
Eric  is currently offline Eric Friend
Messages: 13
Registered: August 2010
Junior Member
Alexander Nittka wrote on Wed, 18 August 2010 04:47


> So, in my case, Xtext will look for all visible elements of type Item:
> is that correct?

yes.

Alex


Great Smile

Ok, therefore, in a reference such as

reference=[model::type|syntax]


"syntax" is a rule responsible for checking whether the text input is correct or not. If it's correct, then a reference to an object with that name (identifier?) is searched.

In my example, the editor lets me write

hello example

(I'm using ID)

while I cannot write something as

hello examp.le

(which would require ID ('.' ID)*)

Now, the important question is:
what's the name of the instances I'm searching for?

I'm very confused about this...
Re: Importing runtime properties? [message #553638 is a reply to message #553633] Wed, 18 August 2010 09:45 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1159
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 12:34 Go to previous messageGo to next message
Eric  is currently offline Eric Friend
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:

http://img832.imageshack.us/img832/7954/screenshot1h.th.jpg

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.

http://img704.imageshack.us/img704/5558/screenshot2lm.th.jpg

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 #553673 is a reply to message #553667] Wed, 18 August 2010 12:53 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6499
Registered: July 2009
Senior Member
Hello,

Update: Sorry misunderstood your problem.

~Christian

[Updated on: Wed, 18 August 2010 13:04]

Report message to a moderator

Re: Importing runtime properties? [message #553677 is a reply to message #553673] Wed, 18 August 2010 13:06 Go to previous messageGo to next message
Eric  is currently offline Eric Friend
Messages: 13
Registered: August 2010
Junior Member
Christian Dietrich wrote on Wed, 18 August 2010 08:53
Hello,

have you tried to put both model and My.ecore into a src folder of a java project in your runtime application?

~Christian


[EDIT] Ok, don't worry - thank you for having examined my problem Smile

[Updated on: Wed, 18 August 2010 13:08]

Report message to a moderator

Re: Importing runtime properties? [message #553684 is a reply to message #553677] Wed, 18 August 2010 13:44 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6499
Registered: July 2009
Senior Member
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
Re: Importing runtime properties? [message #553693 is a reply to message #553677] Wed, 18 August 2010 13:53 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1159
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 14:46 Go to previous messageGo to next message
Eric  is currently offline Eric Friend
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 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6499
Registered: July 2009
Senior Member
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
Re: Importing runtime properties? [message #553792 is a reply to message #553728] Wed, 18 August 2010 21:34 Go to previous message
Eric  is currently offline Eric Friend
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! Smile 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 Smile

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 Smile
Previous Topic:[XText Grammar] Define one value mapping for different features
Next Topic:Guice Provision Errors
Goto Forum:
  


Current Time: Thu Nov 27 12:26:35 GMT 2014

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

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