Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Serialization of unordered, optional attributes(How to force serializer to serialize optional attributes?)
Serialization of unordered, optional attributes [message #1823869] Fri, 03 April 2020 09:43 Go to next message
Stefan Kapferer is currently offline Stefan KapfererFriend
Messages: 19
Registered: March 2020
Junior Member
Hi there

I have several rules in my Xtext grammar that use unordered lists of optional attributes like the following:

ContextMap:
  'ContextMap' (name=ID)?
  '{'
    ('type' ('=')? type=ContextMapType)? &
    ('state' ('=')? state=ContextMapState)? &
    ('yetAnotherAttribute' ('=')? yetAnotherAttribute=JustAnExample)?
  '}'
;


In this case the user is able to specify an object 'ContextMap' that has several attributes (type, state, and so on...) that can be declared in any arbitrary order (individual attributes must be optional).

My problem:
If I create/change such an object (EObject) programmatically and serialize it back to the textual representation (DSL), the Xtext serializer always serializes only the first attribute (as declared in the grammar). Even if all attributes have values (not null), only the first attribute makes it to the serialized file. In the example above, only the attribute 'type' is serialized.

How can I force the Xtext serializer to serialize all attributes that are set (not null)?

Thanks for your help!

Re: Serialization of unordered, optional attributes [message #1823870 is a reply to message #1823869] Fri, 03 April 2020 09:47 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
are you sure this is the grammar you use? please make sure you use the correct precedence.
maybe you mean to have

ContextMap:
'ContextMap' (name=ID)?
'{'
( ('type' ('=')? type=ContextMapType)? &
('state' ('=')? state=ContextMapState)? &
('yetAnotherAttribute' ('=')? yetAnotherAttribute=JustAnExample)?)
'}'
;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Serialization of unordered, optional attributes [message #1823871 is a reply to message #1823870] Fri, 03 April 2020 09:52 Go to previous messageGo to next message
Stefan Kapferer is currently offline Stefan KapfererFriend
Messages: 19
Registered: March 2020
Junior Member
Yes, sorry, the brackets got lost when I prepared the example for the post.
The actual grammar can also be found here: https://github.com/ContextMapper/context-mapper-dsl/blob/master/org.contextmapper.dsl/src/org/contextmapper/dsl/ContextMappingDSL.xtext

However, the problem remains the same.
Re: Serialization of unordered, optional attributes [message #1823873 is a reply to message #1823871] Fri, 03 April 2020 10:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
i cannot reproduce this with an hello world grammar.
thus i wonder if there is something in your grammar that causes this.
do you have a reproducing unit test?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Serialization of unordered, optional attributes [message #1824131 is a reply to message #1823873] Wed, 08 April 2020 11:50 Go to previous messageGo to next message
Stefan Kapferer is currently offline Stefan KapfererFriend
Messages: 19
Registered: March 2020
Junior Member
Hmmmm. Indeed, I cannot reproduce it with a hello world grammar as well.
But so far I cannot identity the problem in our grammar/project.

If you could have a quick look if you see something that would be nice. Otherwise I have invest more time to search for the problem.

Project: https://github.com/ContextMapper/context-mapper-dsl
Grammar: https://github.com/ContextMapper/context-mapper-dsl/blob/master/org.contextmapper.dsl/src/org/contextmapper/dsl/ContextMappingDSL.xtext
Re: Serialization of unordered, optional attributes [message #1824136 is a reply to message #1824131] Wed, 08 April 2020 12:36 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
the problem is: i dont have time to create a test for your grammar as well

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Serialization of unordered, optional attributes [message #1827025 is a reply to message #1824136] Wed, 06 May 2020 08:56 Go to previous message
Stefan Kapferer is currently offline Stefan KapfererFriend
Messages: 19
Registered: March 2020
Junior Member
Thanks for the answers Christian.

I was able to solve my problem in the meantime. The problem was that I always set the attribute values to the first value of the corresponding enum. It looks like as if the first value is interpreted as "default" value and is not serialized.

A short example to illustrate this:

If I have such a rule...:

ContextMap:
  'ContextMap' (name=ID)?
  '{'
    ( ('type' ('=')? type=ContextMapType)? &
    ('state' ('=')? state=ContextMapState)? &
    ('yetAnotherAttribute' ('=')? yetAnotherAttribute=JustAnExample)?)
  '}'
;


... and the values for ContextMapType (just an example) are defined in the following enum:

enum ContextMapType:
  SYSTEM_LANDSCAPE | ORGANIZATIONAL
;


... then the value is not serialized if I set it to the first value (in this case SYSTEM_LANDSCAPE).
All other values work.

So I was able to solve this for my case by introducing another first value in the enum. Like this:

enum ContextMapType:
  UNDEFINED | SYSTEM_LANDSCAPE | ORGANIZATIONAL
;


Thereby, my values get serialized and UNDEFINED is the only value for which the serializer ignores the attribute.
Previous Topic:Recommended way to use IOutlineTreeProvider.Background
Next Topic:How to call generators in an LSP command
Goto Forum:
  


Current Time: Fri Apr 19 01:54:30 GMT 2024

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

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

Back to the top