Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Scoping the same EObject in an EObject in two contexts
Scoping the same EObject in an EObject in two contexts [message #1735268] Thu, 16 June 2016 17:23 Go to next message
Tobias L. is currently offline Tobias L.Friend
Messages: 24
Registered: July 2010
Junior Member
Hallo *,

I hope the title fit's to my problem (correct wording). A little search in the forum ended with no result.

My DSL looks like:

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Model:
	entities+=Entity*;
	
Entity:
	abstract?='abstract'? name=ID 
		('extends' parent=EntityRef)? 
		('owner' owner=EntityRef)? '{'
		/* attributes, etc. */
	'}';
	
EntityRef:
	target=[Entity] (alias=ID)?;


The ScopeProvider looks like:

class MyDslScopeProvider extends AbstractMyDslScopeProvider {

	@Inject extension ResourceExtension

	override getScope(EObject context, EReference reference) {
		if (reference == MyDslPackage.Literals.ENTITY_REF__TARGET) {
			if (true) /* FIXME isScopeFor"Parent"()? */ {
				context.eContainer.scopeAbstractEntities(reference)
			} else if (true) /* FIXME isScopeFor"Owner"()? */ {
				context.eContainer.scopeNonAbstractEntities(reference)
			}
		} else {
			super.getScope(context, reference)
		}
	}

	def dispatch scopeAbstractEntities(EObject it, EReference ref) {
		println('''[ERR] not supported''')
		super.getScope(it, ref)
	}

	def dispatch scopeAbstractEntities(Entity it, EReference ref) {
		Scopes.scopeFor(eResource.findAllEntities.filter[isAbstract])
	}

	def dispatch scopeNonAbstractEntities(EObject it, EReference ref) {
		println('''[ERR] not supported''')
		super.getScope(it, ref)
	}

	def dispatch scopeNonAbstractEntities(Entity it, EReference ref) {
		Scopes.scopeFor(eResource.findAllEntities.filter[!isAbstract])
	}
}


And now the exciting question: how can i determine whether the ScopeProvider should scope the for the "owner" Attribute or the "parent" Attribute?

Thank you for your support.

Tobias
Re: Scoping the same EObject in an EObject in two contexts [message #1735272 is a reply to message #1735268] Thu, 16 June 2016 19:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13193
Registered: July 2009
Senior Member
yes and no.

inside the scope provider you can when the model is parsed complete (context.eContainingFeature)

unfortunately this will not work inside content assist where the context may still be the entity and not a entityref.

you can circumvent this problems with a change to the grammar rules (metamodel should stay the same)

Entity:
	abstract?='abstract'? name=ID
	(parent=EntityRefParent)?
	(owner=EntityRefOwer)? '{'
	/* attributes, etc. */
	'}';

EntityRefParent:
	{EntityRef} 'extends' target=[Entity] (alias=ID)?;

EntityRefOwer:
	{EntityRef} 'owner' target=[Entity] (alias=ID)?;	


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Scoping the same EObject in an EObject in two contexts [message #1735397 is a reply to message #1735272] Sat, 18 June 2016 10:16 Go to previous message
Tobias L. is currently offline Tobias L.Friend
Messages: 24
Registered: July 2010
Junior Member
Hallo Christian,

thank you for your answer. Your explanation sounds conclusive. Now I had the time to try your solution out....

This solutions is maybe not perfect, but for me works it fine.

The DSL:
Model:
	entities+=Entity*;

Entity:
	abstract?='abstract'? name=ID
		parent=ParentEntityRef?
		owner=OwnerEntityRef?
	'{'
		/* attributes, etc. */
	'}';

ParentEntityRef:
	{EntityRef} 'extends' target=[Entity] (alias=ID)?;

OwnerEntityRef:
	{EntityRef} 'owner' target=[Entity] (alias=ID)?;


The ScopeProvider:
package org.xtext.example.mydsl.scoping

import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.EReference

import static org.xtext.example.mydsl.myDsl.MyDslPackage.Literals.*

class MyDslScopeProvider extends AbstractMyDslScopeProvider {
	override getScope(EObject it, EReference ref) {
		switch (ref) {
			case ENTITY_REF__TARGET: {
				switch (eContainingFeature) {
					case ENTITY__OWNER: {
						// ... works fine
					}
					case ENTITY__PARENT: {
						// ... works fine
					}
					default: {
						// not supported
					}
				}
			}
			default: {
				super.getScope(it, ref)
			}
		}
	}
}


Thank You!

Tobias
Previous Topic:Dispatch inferrer to child elements
Next Topic:xtext editor activator problem
Goto Forum:
  


Current Time: Sat Nov 16 01:32:29 GMT 2019

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

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

Back to the top