Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » generating classes for evaluating Xbase expressions in Xtext?
generating classes for evaluating Xbase expressions in Xtext? [message #892502] Thu, 28 June 2012 13:25 Go to next message
Marcus Mathioudakis is currently offline Marcus MathioudakisFriend
Messages: 18
Registered: June 2012
Junior Member
I am trying to design a little DSL for checking constraints on variables. My grammar at the moment looks like this:

 Start:
    	varDeclarations += XVariableDeclaration*
    	rules+=Constraint*;
    	
    Constraint:
    	{Constraint}
    	'FOR' 'PAYLOAD' payload=PAYLOAD 'ELEMENT' element=ID 'CONSTRAINED BY' constraint=XExpression;
    
    
    
    PAYLOAD:
    	"SimulationSessionEvents"
    	|"stacons"
    	|"any"
    ;



I want to generate instances of a class containing just one method that takes as input a value, maps it to the only variable contained in the constraint(which is also the only variable declared), and checks if the constraint is satisfied.

These instances will then be used by another class, which passes a value through each instance, checking if it's constraint is satisfied.

As I see it I have 2 options:

1. Explicitly generate code for a constraint class, in which case I can just use `XBaseCompiler` to generate the expression evaluation code. However I would then have to load these classes somehow, which seems inelegant, if there is a way to create in memory objects directly.

2. Use the `ModelInferrer` to directly generate in memory objects which can be passed to the other class, so no class loading is required. I am not sure how to generate the xbase expression evaluation code in this case.


After reading all the xtext documentation/tutorials, and playing around with the examples, I am left with the following questions:

Which is the 'best' approach in terms of scalabilty (I may later want to extend the grammar, and/or the functions of he generated classes)?
How exactly would I go about things if I were to follow the ModelInferrer approach?
Is there any other way of doing this?

Any help would be much appreciated
Re: generating classes for evaluating Xbase expressions in Xtext? [message #892638 is a reply to message #892502] Thu, 28 June 2012 22:09 Go to previous message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3107
Registered: July 2009
Senior Member
Quoted from SO:

The best approach is to use the model inferer to create the Java
representation of your DSL elements. The expression is usually assigned
by means of the JvmTypeBuilder#setBody. Have a look at the domain model
example where you'll find the the assignment to the operation body:

members += f.toMethod(f.name, f.type) [
for (p : f.params) {
parameters += p.toParameter(p.name, p.parameterType)
}
body = f.body
]

Another option is to create the code manually:

body = [
append(varName).append(' = new ').append(typeName).append('();')
]

The inferer approach allows for powerful Eclipse integration since type
hierarchy, call hierarchy or go-to-declaration will all honor the
derived Java things.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 28.06.12 15:25, schrieb Marcus Mathioudakis:
> I am trying to design a little DSL for checking constraints on
> variables. My grammar at the moment looks like this:
>
> Start:
> varDeclarations += XVariableDeclaration*
> rules+=Constraint*;
>
> Constraint:
> {Constraint}
> 'FOR' 'PAYLOAD' payload=PAYLOAD 'ELEMENT' element=ID 'CONSTRAINED BY'
> constraint=XExpression;
> PAYLOAD:
> "SimulationSessionEvents"
> |"stacons"
> |"any"
> ;
>
>
> I want to generate instances of a class containing just one method that
> takes as input a value, maps it to the only variable contained in the
> constraint(which is also the only variable declared), and checks if the
> constraint is satisfied.
>
> These instances will then be used by another class, which passes a value
> through each instance, checking if it's constraint is satisfied.
>
> As I see it I have 2 options:
>
> 1. Explicitly generate code for a constraint class, in which case I can
> just use `XBaseCompiler` to generate the expression evaluation code.
> However I would then have to load these classes somehow, which seems
> inelegant, if there is a way to create in memory objects directly.
>
> 2. Use the `ModelInferrer` to directly generate in memory objects which
> can be passed to the other class, so no class loading is required. I am
> not sure how to generate the xbase expression evaluation code in this case.
>
>
> After reading all the xtext documentation/tutorials, and playing around
> with the examples, I am left with the following questions:
>
> Which is the 'best' approach in terms of scalabilty (I may later want to
> extend the grammar, and/or the functions of he generated classes)? How
> exactly would I go about things if I were to follow the ModelInferrer
> approach? Is there any other way of doing this?
>
> Any help would be much appreciated
Previous Topic:Context based expressions
Next Topic:cannot run Junit plugin tests anymore
Goto Forum:
  


Current Time: Fri Feb 21 07:13:55 GMT 2020

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

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

Back to the top