Reference an object from parent model [message #1814035] |
Mon, 02 September 2019 04:57  |
Eclipse User |
|
|
|
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 #1814059 is a reply to message #1814045] |
Mon, 02 September 2019 08:56   |
Eclipse User |
|
|
|
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);
}
}
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03451 seconds