Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to import implicitly the namespace from importURI
How to import implicitly the namespace from importURI [message #1807083] Wed, 22 May 2019 10:59 Go to next message
Cyril Grey is currently offline Cyril GreyFriend
Messages: 1
Registered: May 2019
Junior Member
Hello everyone!

I have a Scoping issue and I hope you could help me ...

I'm trying to achieve the following:
1. Restrict the default GlobalScope only to explicitly imported resources (and some default imports)
2. Access it without use of fully qualified name

So, I can address to elements by their fully qualified name, but I would want to automatically import the namespace, to address to ID directly (like if it was under the same namespace).

How can I add all importURI resources to the current scope?

For example:

<ModelName>.<ElementName> to <ElementName>
in some cases: <ModelName>.<OuterElement>.<InnerElement> to just <InnerElement> ?

Because my actual grammar is too big and irrelevant for this question, let's say that I have the following (very simplified):
	name=ID ':'
	'import' importURI=STRING
	'def' name=ID
	'Hi' name=[NamedElement|QualifiedName] '!'
	ID ('.'ID)*


class CGResourceDescriptionStrategy extends DefaultResourceDescriptionStrategy{
	public static final String INCLUDES = "includes"
	ImportUriResolver uriResolver

	override createEObjectDescriptions(EObject eObject, IAcceptor<IEObjectDescription> acceptor) {
		if(eObject instanceof Model) {
			this.createEObjectDescriptionForModel(eObject, acceptor)
			return true
		else {
			super.createEObjectDescriptions(eObject, acceptor)

	def void createEObjectDescriptionForModel(Model model, IAcceptor<IEObjectDescription> acceptor) {
		val uris = newArrayList()
		val userData = new HashMap<String,String>
		userData.put(INCLUDES, uris.join(","))
		acceptor.accept(EObjectDescription.create(QualifiedName.create(model.eResource.URI.toString), model, userData))


class CGRuntimeModule extends AbstractCGRuntimeModule {
	def Class<? extends IDefaultResourceDescriptionStrategy> bindIDefaultResourceDescriptionStrategy() {
	override Class<? extends IGlobalScopeProvider> bindIGlobalScopeProvider() {


class CGGlobalScopeProvider extends ImportUriGlobalScopeProvider{

static final Splitter SPLITTER = Splitter.on(',');

	IResourceDescription.Manager descriptionManager;

	IResourceScopeCache cache;

	override protected getImportedUris(Resource resource) {
		return cache.get(CGGlobalScopeProvider.getSimpleName(), resource, new Provider<LinkedHashSet<URI>>() {
			override get() {
				val uniqueImportURIs = collectImportUris(resource, new LinkedHashSet<URI>())

				val uriIter = uniqueImportURIs.iterator()
				while(uriIter.hasNext()) {
					if (!EcoreUtil2.isValidUri(resource,
				return uniqueImportURIs

			def LinkedHashSet<URI> collectImportUris(Resource resource, LinkedHashSet<URI> uniqueImportURIs) {
				val resourceDescription = descriptionManager.getResourceDescription(resource)
				val models = resourceDescription.getExportedObjectsByType(ModelPackage.Literals.MODEL)
					val userData = getUserData(CGResourceDescriptionStrategy.INCLUDES)
					if(userData !== null) {
						SPLITTER.split(userData).forEach[uri |
							var includedUri = URI.createURI(uri)
							includedUri = includedUri.resolve(resource.URI)
							if(uniqueImportURIs.add(includedUri)) {
								collectImportUris(resource.getResourceSet().getResource(includedUri, true), uniqueImportURIs)
				return uniqueImportURIs

Re: How to import implicitly the namespace from importURI [message #1807149 is a reply to message #1807083] Thu, 23 May 2019 17:37 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13366
Registered: July 2009
Senior Member
i dont understand your question

what import uri scoping does:
- it filters all resources rejecting these that are not imported from global scope
- => the global scope is still all resource in the resourceset in standalone mode and all inside the project in eclipse
- the name part i did not understand. you may have a look what normal importscope does with aliasedeobjectdescriptions

Need professional support for Xtext, Xpand, EMF?
Go to:
Twitter : @chrdietrich
Blog :
Previous Topic:Separate java application that parses a file
Next Topic:Can Xtext be used to create general-purpose programming languages?
Goto Forum:

Current Time: Wed Feb 26 17:24:22 GMT 2020

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

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

Back to the top