Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Reference an object from parent model
Reference an object from parent model [message #1814035] Mon, 02 September 2019 08:57 Go to next message
Alexandra Tritean is currently offline Alexandra TriteanFriend
Messages: 20
Registered: February 2019
Junior Member
Hello,

I have the following DSL grammar. Please ignore the parts that are not defined in this snippet because they are not relevant in this context.

Context:
	'context' name=CTXT_FQN ('extends' parentContext=[Context|CTXT_FQN] )? '{' 
	('data-dictionary' '{'
	attributes+=(Attribute)*
	'}')?
	('entity-catalog' '{'
	entities+=Entity*
	'}')?
	'}';

Attribute:
	AttributeSimpleType | AttributeEntityType;

AttributeSimpleType:
	name=ID type=(AttributeBaseType | AttributeStringType | AttributeDecimalType) funcName=STRING (desc=STRING)? ';';

Entity:
	name=ID funcName=STRING (desc=STRING)? ('extends' parentEntity=[Entity|ENTITY_FQN])? '{'
	features+=Feature*
	'}';

Feature:
	(type=FeatureAttributeType | FeatureEntityType)
	(mandatory?='mandatory')?
	(identifier?='identifier')?
	(affinity?='affinity')?
	';';

FeatureAttributeType:
	(type=[AttributeSimpleType|ID]|type=[AttributeSimpleType|ATTRIBUTE_FQN]);

terminal VERSION:
    INT '.' INT '.' INT;

terminal CTXT_FQN:
    ID '-' VERSION ;

terminal ENTITY_FQN:
	CTXT_FQN'.'ID;

terminal ATTRIBUTE_FQN:
	CTXT_FQN'.'ID;


What I'm trying to achieve is to be able to define a feature for an entity with an attribute that is available either in my current context, either in the parent context of my current context.
I will try to give you a little example to make it more clear.

context Base_Context-1.0.0  extends Another_Context-1.0.0{
    data-dictionary {
        attribute1 integer 'attribute1' "Description1";
        attribute2 boolean 'attribute2' "" ;
    }

    entity-catalog {
        entity1 'entity1'{
             attribute1
            Another_Context-1.0.0.attribute_anotherContext -> the part that I'm struggling with
       }
    }
}

context Another_Context-1.0.0{
    data-dictionary {
        attribute_anotherContext integer 'attribute' "Description";
    }
}


What I've tried so far was to create my own scope provider like below (and many other forms), but I didn't manage to make it work.

class MyDslScopeProvider extends AbstractMetamodelDslScopeProvider {
	
	val ePackage = MetamodelDslPackage.eINSTANCE;
	override getScope(EObject context, EReference reference){
		if(reference == ePackage.featureAttributeType_Type){
			return scopeForFeature(context,reference)
		}
		return super.getScope(context,reference)
	}

	def protected IScope scopeForFeature(EObject context, EReference reference){
		val List<Attribute> attributes=newArrayList;
		val container = context.eContainer.eContainer as Context
		setFeatures(container, attributes);
		return Scopes::scopeFor(attributes, [QualifiedName.create((it.eContainer as Context).name.split("\\.")).append(it.name)],IScope.NULLSCOPE);

	}
	
        def protected void setFeatures(Context context, List<Attribute> attributes){
		attributes.addAll(context.attributes);
		if(context.parentContext!==null){
			setFeatures(context.parentContext,attributes);
		}
	}


I can provide more details if needed.

Any suggestion would be more than welcomed.

Thank you!

Kind Regards,
Alexandra
Re: Reference an object from parent model [message #1814045 is a reply to message #1814035] Mon, 02 September 2019 10:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

your grammar is incomplete but it looks like the ATTRIBUTE_FQN and ENTITY_FQN overlap and thus the ATTRIBUTE_FQN can never match.
(there is a error message regarding this if you run the workflow)

=> what about merging the two


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Reference an object from parent model [message #1814059 is a reply to message #1814045] Mon, 02 September 2019 12:56 Go to previous messageGo to next message
Alexandra Tritean is currently offline Alexandra TriteanFriend
Messages: 20
Registered: February 2019
Junior Member
Hi Christian,

Thank you for the quick reply.

I didn't realized that they are overlapping and this can create a big mess.

Anyway, I made the following changes but I'm still not able to achieve my goal.
terminal ATTRIBUTE_FQN:
	CTXT_FQN'-'ID;

class MyDslScopeProvider extends AbstractMetamodelDslScopeProvider {
	
	val ePackage = MetamodelDslPackage.eINSTANCE;
	override getScope(EObject context, EReference reference){
		if(reference == ePackage.featureAttributeType_Type && context instanceof Entity){
			return scopeForFeature(context,reference)
		}
		return super.getScope(context,reference)
	}

	def protected IScope scopeForFeature(EObject context, EReference reference){
		val List<Attribute> attributes=newArrayList;
		val container = context.eContainer as Context
		setFeatures(container, attributes);
		return Scopes::scopeFor(attributes, [QualifiedName.create((it.eContainer as Context).name+"-"+it.name)],IScope.NULLSCOPE);
	}
	
        def protected void setFeatures(Context context, List<Attribute> attributes){
		attributes.addAll(context.attributes);
		if(context.parentContext!==null){
			setFeatures(context.parentContext,attributes);
		}
	}



Re: Reference an object from parent model [message #1814061 is a reply to message #1814059] Mon, 02 September 2019 13:57 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
QualifiedName.create((it.eContainer as Context).name+"-"+it.name) is bad if the name contains a .
you have to create the segments in a way that a single segment does not contain a .

why didnt you keep the previous grammar and scoping and replace both FQN with this one:

terminal GENERAL_FQN:
CTXT_FQN'.'ID;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Reference an object from parent model [message #1814073 is a reply to message #1814061] Tue, 03 September 2019 06:07 Go to previous message
Alexandra Tritean is currently offline Alexandra TriteanFriend
Messages: 20
Registered: February 2019
Junior Member
I'm clearly a newbie. :)

terminal GENERAL_FQN:
CTXT_FQN'.'ID;
-> this one works like a charm.

Thank you very much for your help! I really appreciate your implication on Xtext section.
Previous Topic:Couldn't resolve reference to Field 'x'
Next Topic:Xtext / Xtend 2.19 Release
Goto Forum:
  


Current Time: Fri Apr 26 11:02:20 GMT 2024

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

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

Back to the top