Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Generating code for Boolean expressions(How to address code generation when needed to translate the expression)
icon5.gif  Generating code for Boolean expressions [message #1204225] Sat, 23 November 2013 02:10 Go to next message
David Paz is currently offline David PazFriend
Messages: 5
Registered: November 2013
Junior Member
Hello,

I'm facing the following situation. I have designed an small grammar for declaring validation rules, the particularity is that the expressions on it must be translated to rules to be used by bobthecow/Ruler project on github. This mean that if I have for example this boolean expression on my model:
A2 == B2


The resulting code should be:
$rb['A2']->equalTo($rb['B2'])


If instead, we have had negated the former expression:
!(A2 == B2)


then we should get:
$rb->logicalNot(
  $rb['A2']->equalTo($rb['B2'])
)


The full grammar definition can be seen in the attached file, don't put the url because can not post external urls.

The only way it occur to me to more or less easily generate the code I need is to override the toString() method of my *Impl.java classes generated by Xtext, that way I just call toString() on templates and the hierarchy is walked down adding proper code for the entire expression.

My question is: Is there a better way to do this? Using Xtend classes maybe. The idea of overriding the generated classes don't like me too much specially if the grammar could change in the future.

Thanks in advance for listen and for any help or hint,
David
Re: Generating code for Boolean expressions [message #1205877 is a reply to message #1204225] Sat, 23 November 2013 21:23 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6485
Registered: July 2009
Senior Member
Hi,

you can simply implement ihe IGenerator interface (.e.g. using xtend)
and you can get the translation be done on safe.

have a look at the xtext tutorials in the docs.
they cover that
Re: Generating code for Boolean expressions [message #1206369 is a reply to message #1205877] Sun, 24 November 2013 03:10 Go to previous message
David Paz is currently offline David PazFriend
Messages: 5
Registered: November 2013
Junior Member
Hi Christian,

Thanks for the quick reply. I already got that, maybe I did not explain my self well. My problem is when generating the code for each term or expression part, since it can not be traverse easily. I see the generation for it more like a visitor, which need to traverse the AST down until terminals.

In the mean while I found this post from you advising the dispatch mechanism. With it, I think it can be achieved the polymorphic behavior I want, just call a generate method and let each class of the language to generate its corresponding bits of code.

I will give it a try and let you know if succeeded.

Thanks again, for your time.
David
Previous Topic:Validation In Xtext
Next Topic:Showing validation error at the right eStructural feature
Goto Forum:
  


Current Time: Sun Nov 23 05:05:32 GMT 2014

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

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