Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Building a map collection
Building a map collection [message #1799901] Mon, 17 December 2018 03:52 Go to next message
Eclipse UserFriend
I have a grammar where I can store MoClasses in a map collection, see momDsl.xtext attached.
It works, but current syntax looks as follows:

{
Class: MyTopClass
Attributes:
MyAttribute1 "test"
{
Class: MySubClass
Attributes:
}
}


I would like to change it to this:

{
Class: MyTopClass
{
Attributes:
MyAttribute1 "test"
Class: MySubClass
{
Attributes:
}
}
}


How would I do that?

Br Johan
  • Attachment: momDsl.xtext
    (Size: 0.55KB, Downloaded 87 times)
Re: Building a map collection [message #1799903 is a reply to message #1799901] Mon, 17 December 2018 04:03 Go to previous messageGo to next message
Eclipse UserFriend
hi am not sure if i understand your problem.
dont see if this is a problem about the map part or the definition part.

do you want to have innerClasses? if yes how shall that look like syntax wise?
how do you distinguish a inner class from a new outer class?

Re: Building a map collection [message #1799906 is a reply to message #1799903] Mon, 17 December 2018 04:37 Go to previous messageGo to next message
Eclipse UserFriend
Hi

EMF has a form of support for Map features using an EMap, which is actually a list. Using it is quite tricky and not nearly as efficient as a traditional Map.

I'm pretty sure that Xtext has no autosynthesis of an EMF EMap, so you will need to use a manual definition, possibly using an EStringToStringMapEntry-like intermediate to facilitate the Xtext parsing. Populating an efficient Map is something you will have to orchestrate in a later stage.

Regards

Ed Willink
Re: Building a map collection [message #1799921 is a reply to message #1799906] Mon, 17 December 2018 08:47 Go to previous messageGo to next message
Eclipse UserFriend
Thanks for quick reply. Yes I want to have inner classes. Since Xtext does not support left recursion I was looking for how to deal with it and found this:

https://stackoverflow.com/questions/18006833/xtext-dealing-with-left-recursion-grammar

I don't need sets so I removed that. And now I simplifed the grammar by removing Term, so the gammar looks as below.
But as you say Christian I am not sure how to handle the references with this grammar.

Br Johan

grammar org.xtext.mom.MomDsl with org.eclipse.xtext.common.Terminals

generate momDsl "http://www.xtext.org/mom/MomDsl"


Model:
collection += Collection*
moInstances+=Mo*
;

Collection: ( => Map );

Map: '{' {Map} ( entries += MapEntry ( ',' entries += MapEntry )* )? '}';
MapEntry: key=Mo value=MapValue;

MapValue: ( {MapValue} | Collection );


Mo:
'MoId:' name=ID
'Class:' class=STRING
'Attributes:' moAttributes+=MoAttribute*
;

MoAttribute:
name=ID
value=AttributeValue
;

AttributeValue:
(value=STRING | 'MoRef:' moRef=[Mo])
;
Re: Building a map collection [message #1799927 is a reply to message #1799921] Mon, 17 December 2018 09:51 Go to previous messageGo to next message
Eclipse UserFriend
no i mean:

if you have a

Class X
Class Y
Class Z
and Y and Z both children of X or is Z and child of Y
Re: Building a map collection [message #1799929 is a reply to message #1799927] Mon, 17 December 2018 10:22 Go to previous messageGo to next message
Eclipse UserFriend
What I am after is this:

Class A
{
Class B // Child of A
{
Class C // Child of B
{}
},
Class D // Child of A
{ }
}

Then each class should have attibutes.
Attributes can either contain a value or a reference to another class object.

An attribute in Class D should shall be able to refer to Class A, B or C.
The reference must be relative so reference from Class D to C should be "B.C".




Re: Building a map collection [message #1799931 is a reply to message #1799929] Mon, 17 December 2018 10:34 Go to previous messageGo to next message
Eclipse UserFriend
i still dont understand what your example models/asts are for. your grammar has no "{" in syntax.
besides what: what references point to is ususally a matter of scoping

[Updated on: Mon, 17 December 2018 10:35] by Moderator

Re: Building a map collection [message #1799967 is a reply to message #1799931] Tue, 18 December 2018 03:05 Go to previous messageGo to next message
Eclipse UserFriend
Ok I reworked the grammar a bit and just added a parentGroup reference instead.
I think this will work for my case. I understand I need to add scoping also, will look at that next.

grammar org.xtext.mom.MomDsl with org.eclipse.xtext.common.Terminals

generate momDsl "http://www.xtext.org/mom/MomDsl"


Model:
moInstances+=Mo*
;

Mo:
'MoId:' name=ID
'Class:' class=STRING
'ParentMo:' belongTo=[Mo]
'Attributes:' moAttributes+=MoAttribute*
;

MoAttribute:
name=ID
value=Choice
;

Choice: (AttributeValue | MoRef);

AttributeValue:
value=STRING
;

MoRef:
'MoRef:' moRef=[Mo]
;
Re: Building a map collection [message #1800148 is a reply to message #1799967] Thu, 20 December 2018 08:32 Go to previous messageGo to next message
Eclipse UserFriend
I reworked the grammar based on an Xtext example so now I use abstract Classes and abstract Instances. New grammar:

grammar org.xtext.mom2.MomDsl with org.eclipse.xtext.common.Terminals

generate momDsl "http://www.xtext.org/mom2/MomDsl"

Domainmodel:
(classElements+=AbstractMoClass)*
(instanceElements+=AbstractMoInstance)*

;

ClassDeclaration:
'Class' name=QualifiedName '{'
(elements+=AbstractMoClass)*
'}';

AbstractMoClass:
ClassDeclaration | AbstractAttributes | Import;

InstanceDeclaration:
'Instance' name=QualifiedName 'Class' class=[ClassDeclaration|QualifiedName] '{'
(elements+=AbstractMoInstance)*
'}';

AbstractMoInstance:
InstanceDeclaration | InstanceAttributes; //| Import;

QualifiedName:
ID ('.' ID)*;

Import:
'import' importedNamespace=QualifiedNameWithWildcard;

QualifiedNameWithWildcard:
QualifiedName '.*'?;


AbstractAttributes:
Attributes
;

Attributes:
name=QualifiedName
attributes+=Attribute*
;

Attribute:
name=ID ':'
type=STRING
;

InstanceAttributes:
'Attributes' attributes=[AbstractAttributes|QualifiedName]
;


Based on this I am able to first create the MO Classes and then create instances for those MO Classes as follows:

Class my.mom
{
Class ManagedElement
{
Class Equipment
{
Class FieldReplaceableUnit
{
attributes
fruId : 'test'
Class RiPort
{
attributes
riPortRef1 : ''
riPortRef2 : ''
}
}
Class RiLink
{

}
}
}
}

Instance my.instance Class my.mom
{
Instance ME1 Class my.mom.ManagedElement
{
Instance Eqm1 Class my.mom.ManagedElement.Equipment
{
Instance FRU1 Class my.mom.ManagedElement.Equipment.FieldReplaceableUnit
{
Instance Ri_A Class my.mom.ManagedElement.Equipment.FieldReplaceableUnit.RiPort {}
Instance Ri_B Class my.mom.ManagedElement.Equipment.FieldReplaceableUnit.RiPort {}
Instance Ri_C Class my.mom.ManagedElement.Equipment.FieldReplaceableUnit.RiPort {}

}
Instance FRU2 Class my.mom.ManagedElement.Equipment.FieldReplaceableUnit
{
Attributes my.mom.ManagedElement.Equipment.FieldReplaceableUnit.attributes
Instance Ri_A Class my.mom.ManagedElement.Equipment.FieldReplaceableUnit.RiPort {}
Instance Ri_B Class my.mom.ManagedElement.Equipment.FieldReplaceableUnit.RiPort {}
Instance Ri_C Class my.mom.ManagedElement.Equipment.FieldReplaceableUnit.RiPort {}
}
Instance CPRI1 Class my.mom.ManagedElement.Equipment.RiLink
{
Attributes my.mom.ManagedElement.Equipment.FieldReplaceableUnit.RiPort.attributes
}

}
}

}

But now I am struggeling with the attributes. I want to be able to define a set of attributes for each class that should then be inherited for each instance of that class.
I found this related topic but cannot quite figure out how to solve it:
https://www.eclipse.org/forums/index.php/t/1086056/

Br Johan

Re: Building a map collection [message #1800151 is a reply to message #1800148] Thu, 20 December 2018 08:42 Go to previous message
Eclipse UserFriend
implement scoping for

InstanceAttributes:
'Attributes' attributes=[AbstractAttributes|QualifiedName]
;
Previous Topic:Standalone setup parsing threadsafety
Next Topic:Builds with xtend-maven-plugin started failing
Goto Forum:
  


Current Time: Sat Jun 21 08:08:49 EDT 2025

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

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

Back to the top