Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » On-the-fly declaration in cross-reference
On-the-fly declaration in cross-reference [message #1840806] Sun, 25 April 2021 10:33 Go to next message
Bertalan Zoltán Péter is currently offline Bertalan Zoltán PéterFriend
Messages: 4
Registered: April 2021
Junior Member
I am trying to create a grammar for an existing metamodel. Here is a relevant part of the grammar:

Ingredient: amount=INT name=EString ;

Container: 'container' name=EString ;

ContainmentRelation: ingredient=[Ingredient] '->' container=[Container] ;


I would want to be able to declare Containers and then write ContainmentRelations which describe Ingredients being contained inside the Containers. But I do not want to have to predeclare my Ingredients and then write the ContainmentRelations, I would like to be able to simply write a ContainmentRelation where on the left side of the arrow (->) I could declare a new Ingredient.

Here is an example; instead of this:

4 'carrot' // this is an ingredient
2 'potato' // this is another ingredient
container 'pot' // this is a container

// now the containment relation:
carrot -> pot
potato -> pot


I would want to be able to do this:

container 'pot'

4 'carrot' -> pot
2 'potato' -> pot


----

I naturally tried to change the ingredient=[Ingredient] cross reference to simply Ingredient, but understandably I get Cannot add supertype 'ContainmentRelation' to sealed type 'Ingredient'.

Do I need to change my metamodel? I suppose it would work if I added another relation between Ingredient and ContainmentRelation that would be a composition but this does not really make sense since the Ingredient should not be 'owned' by the ContainmentRelation but by some larger wrapping object to all thes called Recipe (not present in my examples for simplicity).

[Updated on: Sun, 25 April 2021 15:53]

Report message to a moderator

Re: On-the-fly declaration in cross-reference [message #1840807 is a reply to message #1840806] Sun, 25 April 2021 16:22 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
looks like a usecase for https://xtextcasts.org/episodes/18-model-optimization (+ Using NodeModelUtils to find out the name in the reference)

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: On-the-fly declaration in cross-reference [message #1840808 is a reply to message #1840807] Sun, 25 April 2021 16:40 Go to previous messageGo to next message
Bertalan Zoltán Péter is currently offline Bertalan Zoltán PéterFriend
Messages: 4
Registered: April 2021
Junior Member
Thanks, I don't immediately see how this is a similar use case but I will take a better look later. I see that following this I would have to change my grammar for ContainmentRelation so that it includes an Ingredient declaration not just a reference, but as I mentioned I cannot do this without first changing my metamodel somehow from what I understand.
Re: On-the-fly declaration in cross-reference [message #1840809 is a reply to message #1840808] Sun, 25 April 2021 17:16 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hard to tell how to do it most easy without knowing the metamodel.
I assume it has a place to store the ingredients


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: On-the-fly declaration in cross-reference [message #1840815 is a reply to message #1840809] Sun, 25 April 2021 18:38 Go to previous messageGo to next message
Bertalan Zoltán Péter is currently offline Bertalan Zoltán PéterFriend
Messages: 4
Registered: April 2021
Junior Member
FWIW this is how the metamodel would look for this minimal example (the actual model is more complex but I think my example should be sufficient to exaplain the problem):

               +--------+
        +----<>| Recipe |<>---------------------------------+
        |      |        |<>------+                          |
        |      +--------+        |                          |
        |                        |                          |
        |                        |                          |
        v [0..*] ingredients     v [0..*] containers        |
  +--------------+          +------------+                  |
  | Ingredient   |          | Container  |                  v [0..*] crels
  +--------------+          +------------+           +----------------+
  | amount: EInt |               ^ [1..1] container  | ContainmentRel |
  +--------------+               |                   +----------------+
        ^ [1..1] ingredient      |                      |   |
        |                        +----------------------+   |
        |                                                   |
        |                                                   |
        +---------------------------------------------------+
Re: On-the-fly declaration in cross-reference [message #1840817 is a reply to message #1840815] Mon, 26 April 2021 04:32 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
so in this case you can store the ingredient decls in the ingredients list of the recipe

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: On-the-fly declaration in cross-reference [message #1840828 is a reply to message #1840817] Mon, 26 April 2021 09:05 Go to previous messageGo to next message
Bertalan Zoltán Péter is currently offline Bertalan Zoltán PéterFriend
Messages: 4
Registered: April 2021
Junior Member
Of course, that's what I would want. But how do I write in Xtext that an Ingredient can be declared inside a ContainmentRelation (and stored with the other Ingredients of the Recipe)? I cannot do

ContainmentRelation: Ingredient '->' container=[Container] ;


because in my metamodel, ContainmentRelation only has a reference (that is not a containment reference, marked with <> on my ASCII diagram) to an Ingredient. Is this where the post processor shown in the XtextCast you sent comes in?

[Updated on: Mon, 26 April 2021 09:06]

Report message to a moderator

Re: On-the-fly declaration in cross-reference [message #1840830 is a reply to message #1840828] Mon, 26 April 2021 09:43 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
this is want i meant
in the derived satte computer go through the

ContainmentRelation: ingredient=[Ingredient] '->' container=[Container] ;
read the texts from ingredient=[Ingredient] via node module utils.
and create the ingredients


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Couldn't resolve reference to EClassifier 'XXX' in Xtext
Next Topic:How could I create a new Xtext project from existing ecore model then there is no brackets in xtext?
Goto Forum:
  


Current Time: Fri Apr 19 13:26:45 GMT 2024

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

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

Back to the top