Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Setting up URI imports(Needed setup steps for URI imports to work)
Setting up URI imports [message #734490] Sat, 08 October 2011 01:02 Go to next message
Emilio  is currently offline Emilio Friend
Messages: 2
Registered: October 2011
Junior Member
I've read a lot about using 'uri imports' but somehow I still cannot make it
work for my toy grammar. I implemented
- the ScopeProvider (for local scope),
- the name provider (a simple name provider-- used during exporting),
- modified the 'language workflow
(e.g., add fragment = scoping.ImportURIScopingFragment {},
remove scoping.ImportNamespacesScopingFragment {}, etc.).

Somehow I'm still missing one step (attached is the complete code).

Here is my grammar:
grammar org.xtext.example.globalScope.FilePrint with org.eclipse.xtext.common.Terminals

import "ALINK_HERE //www.eclipse.org/emf/2002/Ecore" as ecore
generate filePrint "A LINK HERE//www.xtext.org/example/globalScope/FilePrint"

//^^^^ I put 'ALINK HERE' instead of http: so I could post this question 

Model:
	(imports+=Import)*
	(images+=Image | prints+=PrintCmd)*
;
	
Image:'image' fileName= ID ';' ; //NOTE I don't use 'name' here .. 

PrintCmd: 'print' image=[Image | FNQ] ';' ; //Is this correct? saw in some posts ..

FNQ returns ecore::EString : ID;

Import : 'import' importURI=STRING ';' ; // feature must be named importURI


All I want is for the 'PrintCmd' rule to be able to refer to 'Images' defined in other
file. For instance, I created a eclipse project with the following two files. The content of file1.deleteMe (deleteMe is the extension of my DSL):
import "file2.deleteMe";
image anImage ;
print anImage;        
print anotherImage; //this gives a 'linking' error


and file2.deleteMe
image anotherImage ;
print anotherImage;  


Any suggestions? Thanks.

Re: Setting up URI imports [message #734532 is a reply to message #734490] Sat, 08 October 2011 08:44 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
Hi,

i guess the basic problem is a erroneous implementation of your simple
name provider. in your case a change of the grammar might do the trick

(1) create a new Xtext project with the wizard
(2) change the workflow like
	    // scoping and exporting API
             fragment = scoping.ImportURIScopingFragment {}
             fragment = exporting.SimpleNamesFragment {}

             // scoping and exporting API
             // fragment = scoping.ImportNamespacesScopingFragment {}
             // fragment = exporting.QualifiedNamesFragment {}
             fragment = builder.BuilderIntegrationFragment {}



and use a grammar like

Model:
	(imports+=Import)*
	(images+=Image | prints+=PrintCmd)*
;
	
Image:'image' fileName= ID ';' ; //NOTE I don't use 'name' here .. => 
you have to change the nameprovider

PrintCmd: 'print' image=[Image | ID] ';' ; // you are using simple names.

Import : 'import' importURI=STRING ';' ;


since you insist on the fileName stuff you have to use a custom
simplenameprovider

package org.xtext.example.mydsl.naming;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.naming.SimpleNameProvider;
import org.xtext.example.mydsl.myDsl.Image;

public class MyDslSimpleNameProvider extends SimpleNameProvider {
	
	public QualifiedName getFullyQualifiedName(EObject obj) {
		if (obj instanceof Image) {
			return QualifiedName.create(((Image)obj).getFileName());
		}
		return super.getFullyQualifiedName(obj);
	}

}


public class MyDslRuntimeModule extends 
org.xtext.example.mydsl.AbstractMyDslRuntimeModule {

	@Override
	public Class<? extends IQualifiedNameProvider> 
bindIQualifiedNameProvider() {
		return MyDslSimpleNameProvider.class;
	}
	
}


and it will work like a charm

~Christian


Am 08.10.2011 03:02, schrieb Emilio:
> Model:
> (imports+=Import)*
> (images+=Image | prints+=PrintCmd)*
> ;
>
> Image:'image' fileName= ID ';' ; //NOTE I don't use 'name' here ..
>
> PrintCmd: 'print' image=[Image | FNQ] ';' ; //Is this correct? saw in some posts ..
>
> FNQ returns ecore::EString : ID;
>
> Import : 'import' importURI=STRING ';' ;


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Setting up URI imports [message #734533 is a reply to message #734532] Sat, 08 October 2011 08:57 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
Hi,

please note: you have to change this too:

//fragment = types.TypesGeneratorFragment {}

~Christian

Am 08.10.2011 10:44, schrieb Christian Dietrich:
> Hi,
>
> i guess the basic problem is a erroneous implementation of your simple
> name provider. in your case a change of the grammar might do the trick
>
> (1) create a new Xtext project with the wizard
> (2) change the workflow like
>
> // scoping and exporting API
> fragment = scoping.ImportURIScopingFragment {}
> fragment = exporting.SimpleNamesFragment {}
>
> // scoping and exporting API
> // fragment = scoping.ImportNamespacesScopingFragment {}
> // fragment = exporting.QualifiedNamesFragment {}
> fragment = builder.BuilderIntegrationFragment {}
>
> 

>
> and use a grammar like
>
>
> Model:
> (imports+=Import)*
> (images+=Image | prints+=PrintCmd)*
> ;
>
> Image:'image' fileName= ID ';' ; //NOTE I don't use 'name' here .. =>
> you have to change the nameprovider
>
> PrintCmd: 'print' image=[Image | ID] ';' ; // you are using simple names.
>
> Import : 'import' importURI=STRING ';' ;
> 

>
> since you insist on the fileName stuff you have to use a custom
> simplenameprovider
>
>
> package org.xtext.example.mydsl.naming;
>
> import org.eclipse.emf.ecore.EObject;
> import org.eclipse.xtext.naming.QualifiedName;
> import org.eclipse.xtext.naming.SimpleNameProvider;
> import org.xtext.example.mydsl.myDsl.Image;
>
> public class MyDslSimpleNameProvider extends SimpleNameProvider {
>
> public QualifiedName getFullyQualifiedName(EObject obj) {
> if (obj instanceof Image) {
> return QualifiedName.create(((Image)obj).getFileName());
> }
> return super.getFullyQualifiedName(obj);
> }
>
> }
> 

>
>
> public class MyDslRuntimeModule extends
> org.xtext.example.mydsl.AbstractMyDslRuntimeModule {
>
> @Override
> public Class<? extends IQualifiedNameProvider>
> bindIQualifiedNameProvider() {
> return MyDslSimpleNameProvider.class;
> }
>
> }
> 

>
> and it will work like a charm
>
> ~Christian
>
>
> Am 08.10.2011 03:02, schrieb Emilio:
>> Model:
>> (imports+=Import)*
>> (images+=Image | prints+=PrintCmd)*
>> ;
>>
>> Image:'image' fileName= ID ';' ; //NOTE I don't use 'name' here ..
>>
>> PrintCmd: 'print' image=[Image | FNQ] ';' ; //Is this correct? saw in
>> some posts ..
>>
>> FNQ returns ecore::EString : ID;
>>
>> Import : 'import' importURI=STRING ';' ;
>
>


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Setting up URI imports [message #735021 is a reply to message #734533] Mon, 10 October 2011 16:50 Go to previous messageGo to next message
Emilio  is currently offline Emilio Friend
Messages: 2
Registered: October 2011
Junior Member
Christian,
Thanks. It worked by just changing my rule grammar from

PrintCmd: 'print' image=[Image | FNQ] ';' ;
FNQ returns ecore::EString : ID;


to

 PrintCmd: 'print' image=[Image | ID] ';' 


(I already had in place all the other changes you suggested).
Re: Setting up URI imports [message #1059503 is a reply to message #735021] Mon, 20 May 2013 05:06 Go to previous messageGo to next message
chris yo is currently offline chris yoFriend
Messages: 146
Registered: February 2013
Senior Member
Do we need to create a custom simplenameprovider for each "filename"?

If I add another grammar like this:
 Model:
 (imports+=Import)*
 (images+=Image | prints+=PrintCmd)*
 (files+=File)*
 ;

 Image:'image' fileName= ID ';' ; //NOTE I don't use 'name' here .. =>
 you have to change the nameprovider

 PrintCmd: 'print' image=[Image | ID] ';' ; // you are using simple names.

 Import : 'import' importURI=STRING ';' ;

 File: 'file' nameOfFile=ID ';' ; // <-- New grammar


Does this mean that I have to create a custom simplenameprovider for nameOfFile also?
How will I know if I should create one or not?
Re: Setting up URI imports [message #1059517 is a reply to message #1059503] Mon, 20 May 2013 08:31 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
Yes . The nameproviders by Default use name=ID

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Setting up URI imports [message #1059520 is a reply to message #1059517] Mon, 20 May 2013 09:11 Go to previous messageGo to next message
chris yo is currently offline chris yoFriend
Messages: 146
Registered: February 2013
Senior Member
Is a custom simplenameprovider necessary?

May I know what it is for? Sorry, but I cannot find any explanation online on why we need a custom simplenameprovider when using importURI.
Re: Setting up URI imports [message #1059525 is a reply to message #1059520] Mon, 20 May 2013 09:35 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
You dont need to but if you compare
ImportedNamespaceAwareLocalScopeProvider
with SimpleLocalScopeProvider
you will see that the latter that is used with import uri scoping
does no relative name resolution so that you always have to use FQNs


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Setting up URI imports [message #1059531 is a reply to message #734490] Mon, 20 May 2013 10:19 Go to previous messageGo to next message
chris yo is currently offline chris yoFriend
Messages: 146
Registered: February 2013
Senior Member
Fine, then I have decided not to implement the custom simplenameproviders.

But when I created 2 files..

file1.deleteMe:

import "file2.deleteMe";
image anImage ;
print anImage;         

file2.deleteMe:

image anotherImage ;
print anotherImage;  



The generated output file only shows the contents of file1.deleteMe. I have made the changes to my workflow based on your suggestion. What other things do I need to change so that contents of file1 is merged with contents of file2?
Re: Setting up URI imports [message #1059532 is a reply to message #1059531] Mon, 20 May 2013 10:25 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
Hi

Imports just make Things visible. The do NOT have include semantics


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Setting up URI imports [message #1059634 is a reply to message #1059532] Tue, 21 May 2013 02:13 Go to previous messageGo to next message
chris yo is currently offline chris yoFriend
Messages: 146
Registered: February 2013
Senior Member
Oh. Is there some tutorial on how to do this? A link or some info will be helpful.
Re: Setting up URI imports [message #1059662 is a reply to message #1059634] Tue, 21 May 2013 06:57 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
no i dont know any

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Getting IEditorInput in validator
Next Topic:A problem with ==, ||, and other
Goto Forum:
  


Current Time: Wed Sep 25 11:06:59 GMT 2024

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

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

Back to the top