Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How do I customize grammar for my topology feature?
How do I customize grammar for my topology feature? [message #1760287] Tue, 25 April 2017 14:19 Go to next message
Susie Agerholm is currently offline Susie AgerholmFriend
Messages: 54
Registered: April 2017
Member
Hi,

I would like to do a grammar feature that deviates a bit from the metamodel - but I am in doubt as to how to proceed and if the feature below can be implemented solely as addition to grammar?...

The problem:
In the metamodel instances of the Joint class references a parent (instance of Link class), and a child (also instance of Link class). See grammar below for details. This forms a tree topology. I would like the user to be able to define this tree using a graphviz inspired concrete syntax like seen in the TOPOLOGY rule below. The arrows represent edges=Joint instance in the topology.

TOPOLOGY:
('Link' name=ID ' -> ')+ 'Link' name=ID
;

I am thinking it would be very cool if the topology definition could generate stubs for the Link and Joint definitions in the editor. I am pretty sure I'll need a ValueConverter no matter what- but for starters I have to figure out if this topology topology can be done with grammar only or I need to update my metamodel with some kind of class or other kind of hook for this?



//-----This is my current grammar

grammar org.xtext.urdf.Dsl with org.eclipse.xtext.common.Terminals

import "http://www.eclipse.org/emf/2002/Ecore" as ecore
import "http://www.example.org/uRDF" as uRDF

Robot returns uRDF::Robot:
	'Robot' name=ID
	 link+=Link+  
	 joint+=Joint* 
;

Link returns uRDF::Link:
	'Link' name=ID
	[...]
;

Joint returns uRDF::Joint:
	'Joint' name=ID
	'ChildOf' ChildOf=[uRDF::Link|STRING]
	'ParentOf' ParentOf=[uRDF::Link|STRING]
	[...]
;

Re: How do I customize grammar for my topology feature? [message #1760288 is a reply to message #1760287] Tue, 25 April 2017 14:30 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

i did not get your question, can you please elaborate.

do you want to have a different syntax that creates the tree? or just for the joints?

so you want to have syntax a (show should that look like) that creates an ast that looks like in the metamodel?
maybe you need a

- derived or manual second metamodel that is closer to your wanted synatax
- use a IDerivedStateComputer (maybe in comb. with SyntheticLinkingSupport) to create the instance of the external metamodel


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How do I customize grammar for my topology feature? [message #1760296 is a reply to message #1760288] Tue, 25 April 2017 15:17 Go to previous messageGo to next message
Susie Agerholm is currently offline Susie AgerholmFriend
Messages: 54
Registered: April 2017
Member
Hi Christian, thank you for a very reply! Very Happy

Here are some more details:
The metamodel I am trying to do a grammar for sort of has two different levels of description.
- the lower level of description tell us about the individual parts of the robot and their attributes (links are physical shapes, that have attributes like geometry, mass, etc - and joints connect the links, joints have a joint type, certain limits for bending, etc etc)
- the higher level of description tells us about the topology of the robot - how the individual joints and links are connected in child/parent relations.

The description would be a lot more easy to read, if this higher level of description was made more explicit by adding a special topology syntax for it - and removing it from the part, that defines individual Links and Joints and their attributes...

The concrete syntax for the topology should look something like this - arrows represent Joints connecting Links:
Link name=RobotsUpperArm -> Link name=RobotsLowerArm etc...

I hope this were the details you were looking for. If not let me know and I'll explain further Smile
Re: How do I customize grammar for my topology feature? [message #1760301 is a reply to message #1760296] Tue, 25 April 2017 15:45 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
I still am not sure if I got you.

How would the ast for the concrete syntax look like (not the ast you want at the end

Did you have a look at iderivedstatecomputer

http://xtextcasts.org/episodes/18-model-optimization


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Tue, 25 April 2017 16:08]

Report message to a moderator

Re: How do I customize grammar for my topology feature? [message #1760326 is a reply to message #1760301] Tue, 25 April 2017 20:43 Go to previous messageGo to next message
Susie Agerholm is currently offline Susie AgerholmFriend
Messages: 54
Registered: April 2017
Member
Hi Christian, thank you very much for your help! I am looking into the link you sent and working on some feedback Smile
Re: How do I customize grammar for my topology feature? [message #1760675 is a reply to message #1760326] Mon, 01 May 2017 12:19 Go to previous messageGo to next message
Susie Agerholm is currently offline Susie AgerholmFriend
Messages: 54
Registered: April 2017
Member
This is in addition to my posts above. I now have a better idea of how to describe my problem Smile

This is my grammar:

//------------------------------------
Robot returns uRDF::Robot:
'Robot' name=ID
(link+=Link | joint+=Joint)*
;

Link returns uRDF::Link:
'Link' name=ID
[omitted: a lot of other not required attributes...]
;

Joint returns uRDF::Joint:
'Joint' name=ID
'ChildOf' ChildOf=[uRDF::Link|ID]
'ParentOf' ParentOf=[uRDF::Link|ID]
[omitted: a lot of other not required attributes...]
//---------------------------

For convenience, I would like to have two diff concrete syntaxes for instantiating links and joints:
- a shorthand for stubbing out links and joints
- a more verbose style for declaring all attributes to created links and joints...

Shorthand syntax goes like this (arrow=Joint):
Link link1 -> Link link2 -> Link3 etc...

Stubs are instantiated in the more verbose style, that can be extended by user with missing attributes :

//------------------------------
Link link1

Link link2

Link link3

Joint link1_link2
childOf link1
parentOf link2

Joint link2_link3
childOf link2
parentOf link3

//-------------------------------

I hope this explanation makes more sense than earlier versions Smile

At the moment I am working on the object instantiation on demand that is needed from the shortcut syntax. I am following directions from this topic:
https://www.eclipse.org/forums/index.php/t/1024478/

I can track the Person object being instantiated in the debugger and it is returned from GreetingLinkingService. But my outline view doesn't update yet, so I guess I also need to save the new Person object to my AST, but where and how do I do this?
Re: How do I customize grammar for my topology feature? [message #1760676 is a reply to message #1760675] Mon, 01 May 2017 12:28 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
HI,

this still makes no sense to me.
for object instantiation on demand (without a grammar) you need a IDerivedStateComputer.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How do I customize grammar for my topology feature? [message #1760799 is a reply to message #1760676] Wed, 03 May 2017 07:05 Go to previous messageGo to next message
Susie Agerholm is currently offline Susie AgerholmFriend
Messages: 54
Registered: April 2017
Member
thank you for your reply Smile I have been looking into iDerivedStateComputer - but seems like it is only for attaching created objects temporarily to the AST?
the api says clients should use discardDerivedState method to revert all the changes they did during installDerivedState?

Bettini book contains an example where iDerivedStateComputer is used to add statistics temporarily to the AST but these are not meant to be persisted..

But I want the objects that I create 'by reference' in my grammar to be fully instantiated and added permanently to the AST. Can this be achieved with iDerivedStateComputer without corrupting the AST?

Re: How do I customize grammar for my topology feature? [message #1760810 is a reply to message #1760799] Wed, 03 May 2017 08:38 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
what do you mean by persisted? saving?
the objects are in the ast as long as you work with the ast.
they are not inside the physical files?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How do I customize grammar for my topology feature? [message #1760813 is a reply to message #1760810] Wed, 03 May 2017 09:13 Go to previous messageGo to next message
Susie Agerholm is currently offline Susie AgerholmFriend
Messages: 54
Registered: April 2017
Member
yes the objects created by reference in my grammar should be saved along with all other objects, so I can access them further down the chain with my codegenerator etc. I guess my question boils down to can I add objects to the AST with installDerivedState and leave them there (=not calling discardDerivedState) for the full lifecycle of the resource set without corrupting it?
Re: How do I customize grammar for my topology feature? [message #1760814 is a reply to message #1760813] Wed, 03 May 2017 09:32 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 83
Registered: January 2016
Location: Kiel
Member

discardDerivedState is not called before downstream processing (like validation or code generation) happens. It is merely there because installDerivedState is called twice (pre and post indexing). So the state from the first call needs to be discarded before it installs it the second time.
Re: How do I customize grammar for my topology feature? [message #1760815 is a reply to message #1760814] Wed, 03 May 2017 09:33 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 83
Registered: January 2016
Location: Kiel
Member

So you should implement both and can expect your derived state to be installed whenever you access the resource through getContents().
Re: How do I customize grammar for my topology feature? [message #1760819 is a reply to message #1760815] Wed, 03 May 2017 09:47 Go to previous messageGo to next message
Susie Agerholm is currently offline Susie AgerholmFriend
Messages: 54
Registered: April 2017
Member
Oh I see - I think that is exactly what I needed to know! Thank you very much for your help Smile
Re: How do I customize grammar for my topology feature? [message #1760968 is a reply to message #1760287] Thu, 04 May 2017 16:46 Go to previous messageGo to next message
Andrzej Wasowski is currently offline Andrzej WasowskiFriend
Messages: 15
Registered: June 2015
Junior Member

Sven, Christian,

I'm working with Susie on this, and I follow this discussion as well. The solution suggested seems to be adaptable to what we need, but I would like to ask the same question in a different way, just to rule out that we are missing something obvious.

The case we are looking at, is not very exotic: we have a DSL where objects can be used without declaration, but some objects can be declared (in here the objects are "links"). So basically, we want a reference to be resolved to an existing object (link) if it was already declared, and otherwise, if the object is not declared, it should be declared and instantiated automatically.

Since automatic variable definition is seen in some programming languages, I was wondering if Xtext had some direct support for this. Are we on the right track using IDerivedState? Or is there a more direct way in the Xtext DSL?
Re: How do I customize grammar for my topology feature? [message #1760969 is a reply to message #1760968] Thu, 04 May 2017 17:25 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
well this again depends on how you want your ast/objects to look like (what "instantiate" means etc), how your scoping rules look like etc.

and no xtext has no direct support for that.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How do I customize grammar for my topology feature? [message #1761045 is a reply to message #1760969] Fri, 05 May 2017 14:56 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 83
Registered: January 2016
Location: Kiel
Member

I usually solve that by
- modeling the name of such an object as a cross reference within the grammar
- disable the errors for such broken cross references since it is not an error but a declaration (override LinkingDiagnosticMessageProvider#getUnresolvedProxyMessage to return null in such cases).
- implement an 'IQualifiedNameProvider' to get the name from the object's node (see 'NodeModelUtils')
Re: How do I customize grammar for my topology feature? [message #1761061 is a reply to message #1761045] Fri, 05 May 2017 19:36 Go to previous messageGo to next message
Andrzej Wasowski is currently offline Andrzej WasowskiFriend
Messages: 15
Registered: June 2015
Junior Member

Thanks, this is useful. We will look into these options.
Re: How do I customize grammar for my topology feature? [message #1763351 is a reply to message #1761061] Sat, 13 May 2017 07:01 Go to previous messageGo to next message
Susie Agerholm is currently offline Susie AgerholmFriend
Messages: 54
Registered: April 2017
Member
I am trying to generate and add objects to the AST 'by reference' using IDErivedStateComputer - details above. When I try to attach my generated object to the rest of the AST (line 6 below), a runtime error is thrown (operation cancelled??).

In the debugger it looks like it is caused by my attempt to access and modify contents of the link list - it is a legit List, so add method is available, but add seems to be invalidated at runtime in this context. Both robot, topo and link list are instatiated at this point.

The robot interface does not allow it, but its impl class has a setter for the ref to link list but it does not seem rigth to program to an impl class... Or maybe my timing for trying to add stuf to the AST is off? Not prelinkingphase seems to be only choice that allows me to do this...

Any suggestions how to proceed from here would be greatly appreciated Very Happy

1 override installDerivedState(DerivedStateAwareResource resource, boolean preLinkingPhase) {
2	if  (!preLinkingPhase) {   
3		//traverse all Topology...
4 			resource.allContents.filter(Topology).forEach[ topo | 
5				//create new Link and add to tree
6				topo.getRobot().link.add(
7			   	//create new Link (if not already exists)
8			   		MyDslFactory.eINSTANCE.createLink => [
9						name = "helloworld"
10						//add more stuff to created link	
11			   	]);
code omitted...



Re: How do I customize grammar for my topology feature? [message #1763352 is a reply to message #1763351] Sat, 13 May 2017 07:23 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi

Hard to tell without having the example grammar as well.

I link is a list as produced by += in the grammar it should be instantiaed by default
And you should be able to call add as you do without any problem

Thus it would be nice to have a

- complete sample grammar (created a new one if you can't share)
- the complete computer
- a sample model file


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How do I customize grammar for my topology feature? [message #1763369 is a reply to message #1763352] Sun, 14 May 2017 16:36 Go to previous messageGo to next message
Susie Agerholm is currently offline Susie AgerholmFriend
Messages: 54
Registered: April 2017
Member
I found the problem - it was due to the bi-directional relation between robot and topology , that is contained in robot. I did not realize that this relation needed to be explicitly present in the grammar from both robot 'side' and topology 'side' like so:
robot my_robot { topology robot my_robot [other stuff in topology obj...] }
- but adding it fixed the problem.

This is making concrete syntax very heavy weight though, and also seems a bit redundant so would rather not do it. Is it really necessary to do this - or is there something I am missing?
Re: How do I customize grammar for my topology feature? [message #1763371 is a reply to message #1763369] Sun, 14 May 2017 17:25 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
since i have neither a complete grammar nor a metamodel it is hard to tell ...)

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How do I customize grammar for my topology feature? [message #1763434 is a reply to message #1763371] Tue, 16 May 2017 07:07 Go to previous messageGo to next message
Susie Agerholm is currently offline Susie AgerholmFriend
Messages: 54
Registered: April 2017
Member
Here is the grammar and a snapshot of the metamodel :)

grammar org.xtext.example.mydsl2.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl2/MyDsl"

Robot returns Robot:
	'Robot' name=ID
	 'Topology' topologies+=Topology+
	 (link+=Link | joint+=Joint )*	 
;

Topology:  
	//is reference to containing robot really necessary here?
	//we already know topology is contained by robot?
	'robot' robot=[Robot|ID] parent=STRING ('->' child=Topology)?;

Link returns Link:
	'Link' name=ID
;

Joint returns Joint:
	'Joint' name=ID
	'ChildOf' ChildOf=[Link|ID]
	'ParentOf' ParentOf=[Link|ID]
	
;


index.php/fa/29351/0/
  • Attachment: bidir.PNG
    (Size: 35.14KB, Downloaded 614 times)
Re: How do I customize grammar for my topology feature? [message #1763438 is a reply to message #1763434] Tue, 16 May 2017 07:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
do you have a sample model as well?
and a complete computer..


do make it so hard for me to reproduce ....


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How do I customize grammar for my topology feature? [message #1763554 is a reply to message #1763438] Wed, 17 May 2017 11:39 Go to previous messageGo to next message
Susie Agerholm is currently offline Susie AgerholmFriend
Messages: 54
Registered: April 2017
Member
1 override installDerivedState(DerivedStateAwareResource resource, boolean preLinkingPhase) {
2	if  (!preLinkingPhase) {   
3		//traverse all Topology...
4 			resource.allContents.filter(Topology).forEach[ topo | 
5				//create new Link and add to tree
6				topo.getRobot().link.add(
7			   	//create new Link (if not already exists)
8			   		MyDslFactory.eINSTANCE.createLink => [
9						name = "helloworld"
10						//add more stuff to created link	
11			   	]);
code omitted...


The error was in line 6 with topo.getRobot().

I did not think I needed to refer to the bidirectional relation from both sides so I had altered my grammar so it was legal to say:

Sample model file:
Robot robo Topology link1 -> link2 [...]

This will yield an error in IDerivedStateComputers installDerivedState method due to the fact that the bidirectional relation was not made explicit from both sides like so:

Sample model file:
Robot robo Topology robot robo link1 -> link2

But this concrete syntax just seems so redundant, so I'm thinking there must be some way around it...
Re: How do I customize grammar for my topology feature? [message #1763558 is a reply to message #1763554] Wed, 17 May 2017 11:57 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
sorry i still cannot follow you. thus i cannot help you.

you have model x that is parsed.
then you have the derived state computer that produces model y.

that ever you do there lies in your hand.

but i dont see anything that i can reproduce .....

i have never seens an example model i can use. nor how the ast should look like you want to produce.

a robot has topologies.
a topology has references to (other ?!? robots???)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:ParallelResourceLoader - documentation?
Next Topic:Creating hyperlinks in Xtext
Goto Forum:
  


Current Time: Wed Apr 24 18:43:22 GMT 2024

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

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

Back to the top