Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Mixing up XEpressions and my custom expressions(Generating Expression )
Mixing up XEpressions and my custom expressions [message #1058089] Fri, 10 May 2013 22:07 Go to next message
Leandro Marques is currently offline Leandro MarquesFriend
Messages: 11
Registered: July 2009
Junior Member
I performed a search is this topic but could not understand someone else's problem in my context, so I decided to create a new topic.

I am creating a [still] simple grammar as follows:

Entity:
	entities+=SocialMachine*
;
	
SocialMachine:
    'SocialMachine' name=ValidID ('relates to' depends+=[SocialMachine|ValidID] (',' depends+=[SocialMachine|ValidID])*)? '{'
        (generalConstraints=GeneralConstraint ';'?)?
        (constructors+=ComputationalUnit)*
        (wrapperInterface+=ProvidedService)*
    '}'
;

ComputationalUnit:
    {ComputationalUnit}
    'constructor' '(' (parameters+=FullJvmFormalParameter)* (',' parameters+=FullJvmFormalParameter)* ')'
    body=XBlockExpression
;

ProvidedService:
    {ProvidedService}
    'op' name=ValidID ('returns' returnType=JvmTypeReference)? '(' (parameters+=FullJvmFormalParameter)* (',' parameters+=FullJvmFormalParameter)* ')' '{'
        body += (XExpressionInsideBlock | SocialMachineOpCall)*
    '}' (opConstraints+=OperationConstraint)*  
;

SocialMachineOpCall returns XExpression:
    {SocialMachineOpCall}
    smCallSource=[SocialMachine]'.'opTarget=[ProvidedService]';'?
;

OperationConstraint:
    'constraint' type+=ConstraintType+
;

GeneralConstraint:
    'general constraint' type=ConstraintType
;

//Four constraint types that can be used in specific or general constraints
enum ConstraintType:
    REQUESTS_PER_PERIOD | PRE_AUTH_SM | REDUCED_RESOURCE | UNLIMITED
;


My first problem is related to the SocialMachineOpCall as I would like to call a ProvidedService described inside a SocialMachine. When I use the '.' (dot) operator it gives me a grammar anbiguity that I am not able to fix.

My second problem is related to code generation. As can be seen:
body += (XExpressionInsideBlock | SocialMachineOpCall)*


The body of a ProvidedService is either a XExpressionInsideBlock or a SocialMachineOpCall. To generate the body of a Java method based on a XBlockExpresion is easy, as I tried before:

...
for (op : sm.wrapperInterface) {
                        members += op.toMethod(op.name, op.returnType) [
                            for (p : op.parameters) {
                                parameters += p.toParameter(p.name, p.parameterType)
                            }
                            body = op.body
                        ]
                    }
...


However, for now on I can not simply associate the body of a method with mixed XExpression and my custom expression (based on service calls). How can I get XExpressions separated from SocialMachineOpCall to generate method bodies in target Java classes?
Re: Mixing up XEpressions and my custom expressions [message #1058180 is a reply to message #1058089] Mon, 13 May 2013 06:01 Go to previous messageGo to next message
michael m is currently offline michael mFriend
Messages: 17
Registered: March 2013
Junior Member
2. Question:
Will something like this not do?
if (op.body instanceof XExpressionInsideBlock ) {
  val expInsideBlock = op.body as XExpressionInsideBlock
  body = expInsideBlock
}
else {
  val socialOpCall = op.body as SocialMachineOpCall
  body = [
    it.append(.... do your custom generation of socialOpCall here ... )
  ]
}

[Updated on: Mon, 13 May 2013 06:02]

Report message to a moderator

Re: Mixing up XEpressions and my custom expressions [message #1058390 is a reply to message #1058180] Mon, 13 May 2013 20:27 Go to previous messageGo to next message
Leandro Marques is currently offline Leandro MarquesFriend
Messages: 11
Registered: July 2009
Junior Member
Hi Michael B, thanks for your response, but unfortunately it does not work for me. My intention is to mix up XExpresionos lines of code with my custom java based lines of code, for example
XExpression
XIfExpression
--> My custom line of code
--> My custom line of code
XExpression
XForLoopExpression
--> My custom LOC 


Your solution considers separated blocks of XExpression and my custom expressions when, in fact, I need to mix them up. Even I use two IFs, instead of If-else block in my Model Inferrer the following LOC
body = expInsideBlock

is actually erasing the previous content of the body. In this way, I also verified if the call to:
body [
  it.append(param)
]

can be done by using a XExpression as a param, however, you can only add a CharSequence or a JVMType.

Then I have my hands tied up. How can I construct my function (or method) body by adding line by line expressions, considering these expressions being as XExpressions or My Custom Expressions?

Thanks
Re: Mixing up XEpressions and my custom expressions [message #1058573 is a reply to message #1058390] Tue, 14 May 2013 13:44 Go to previous messageGo to next message
michael m is currently offline michael mFriend
Messages: 17
Registered: March 2013
Junior Member
You could handle all expressions manually if nobody has a better idea.
Check if "op.body" is a XBlockExpression and iterate over all expressions and handle them manually (its quiete some work though and it should be recursive).
E.g. check if expressionInBlock is of type XIfExpression or CustomType and generate the appropriate strings (append them via it.append(generatedStrings)).
Re: Mixing up XEpressions and my custom expressions [message #1059030 is a reply to message #1058573] Thu, 16 May 2013 10:08 Go to previous messageGo to next message
Ian McDevitt is currently offline Ian McDevittFriend
Messages: 70
Registered: December 2012
Location: Belfast
Member
I tried something like this once although not mixing the expressions quite as closely. I defined my own expression rules, which you can do easily enough by looking at how Xbase does it, but I found the biggest problem was scoping. I had to subclass the XbaseScopeProvider so that my DSL elements could tell what was in scope (and vice versa).
This did not always work as planned and in fact I believe the Xbase SP is deprecated in 2.4 (there is a post from Lorenzo Bettini somewhere on that) and I have since decided not to include Xbase in my DSL and enhanced the DSL a bit instead.
Re: Mixing up XEpressions and my custom expressions [message #1059031 is a reply to message #1059030] Thu, 16 May 2013 10:20 Go to previous messageGo to next message
Ian McDevitt is currently offline Ian McDevittFriend
Messages: 70
Registered: December 2012
Location: Belfast
Member
Also remembered for the above case, in processing each of my own expressions I had to generate a closure function for each which I collected in a list so I could combine them into the body by using a loop of func(i).apply(it) in the body=[].
And then I had to call the XbaseCompiler directly to process the XExpressions. All in all it diverted me a lot away from the main features I wanted to work on.

That may give you some ideas if you need to progress that way, but in the end it was the loss of the scope provider that changed my mind.
Re: Mixing up XEpressions and my custom expressions [message #1059256 is a reply to message #1059031] Thu, 16 May 2013 16:47 Go to previous messageGo to next message
Hallvard Traetteberg is currently offline Hallvard TraettebergFriend
Messages: 673
Registered: July 2009
Location: Trondheim, Norway
Senior Member
Hi,

Using Xbase is easiest if you can infer a Java model that gets the
scopes right for you. In practice this means inferring a combination of
fields in a class and parameters of methods that provide the appropriate
context for Xbase expressions. If several Xbase expressions need to be
part of the same body, you need to infer a method for each of them, and
generate calls to these methods in the appropriate places in the body.
This is conceptually similar to the closures mentioned below.

E.g. suppose you have a DSL that lets you write variable declarations
and if-like statements:

model a
var int a, int b, int c
when a == b do print(b) otherwise print(c)

This may require inferring something like

class A {
int a;
int b;
int c;

// infer a method with the XExpression a == b as its body
// no need for explicit code generation
boolean bodyExpr1() {
return a == b;
}

// infer a method with the XExpression print(b) as its body
// no need for explicit code generation
void bodyExpr2() {
print(b);
}

// infer a method with the XExpression print(c) as its body
// no need for explicit code generation
void bodyExpr3() {
print(c);
}

// infer a method that combines calls to the above methods
// in place of the XExpressions
void body() {
if (bodyExpr1()) bodyExpr2();
else bodyExpr3();
}
}

Of course, you may also have more local scopes that needs to be added as
parameters of the methods and arguments to corresponding calls.

If the inferred model gets the scoping right, you don't have to make
your own scope provider (unless you need it for other constructs).

Hallvard

On 16.05.13 03.20, Ian McDevitt wrote:
> Also remembered for the above case, in processing each of my own
> expressions I had to generate a closure function for each which I
> collected in a list so I could combine them into the body by using a
> loop of func(i).apply(it) in the body=[]. And then I had to call the
> XbaseCompiler directly to process the XExpressions. All in all it
> diverted me a lot away from the main features I wanted to work on.
>
> That may give you some ideas if you need to progress that way, but in
> the end it was the loss of the scope provider that changed my mind.
Re: Mixing up XEpressions and my custom expressions [message #1059484 is a reply to message #1059030] Sun, 19 May 2013 15:21 Go to previous message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 05/16/2013 12:08 PM, Ian McDevitt wrote:
> I tried something like this once although not mixing the expressions
> quite as closely. I defined my own expression rules, which you can do
> easily enough by looking at how Xbase does it, but I found the biggest
> problem was scoping. I had to subclass the XbaseScopeProvider so that my
> DSL elements could tell what was in scope (and vice versa). This did not
> always work as planned and in fact I believe the Xbase SP is deprecated
> in 2.4 (there is a post from Lorenzo Bettini somewhere on that) and I
> have since decided not to include Xbase in my DSL and enhanced the DSL a
> bit instead.

From what I understand, currently, XbaseScopeProvider is used by the
content assist, while the one that is used by Xbase is
XbaseBatchScopeProvider... what I'm doing is factor out all custom
scoping mechanisms in a common class, e.g., ScopeProviderHelper, and
then have both a custom XbaseScopeProvider and a custom
XbaseBatchScopeProvider which delegate to my ScopeProviderHelper...

cheers
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it


Previous Topic:Problem with context in Xbase Interpreter
Next Topic:Auto complition
Goto Forum:
  


Current Time: Sat Apr 27 03:08:41 GMT 2024

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

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

Back to the top