Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Generating duplicate method bodies in AbstractModelInferrer
Generating duplicate method bodies in AbstractModelInferrer [message #1805329] Thu, 11 April 2019 21:18 Go to next message
Ben Holland is currently offline Ben HollandFriend
Messages: 12
Registered: April 2019
Junior Member
I'm having a problem where the AbstractModelInferrer generates an empty method body when reusing an expression that was used to generate a previous method body.

In my grammar I have something like:

Function:
  name=ID '{' expression=XExpression '}'
;


In a corresponding AbstractModelInferrer I'd like to generate two corresponding functions for each parsed Function like:

it.members += toMethod(function.name + "_1", someReturnType) [
  it.body = function.expression;
]

it.members += toMethod(function.name + "_2", someReturnType) [
  it.body = function.expression;
]


I tried to use
EcoreUtil2.copy(function.expression)
to copy the relevant parts of the AST, but the AbstractModelInferrer always seems to ignore whatever function is generated second (generates an empty method body) after the first method body is generated. I know it seems pointless to generate two identical method implementations, but I'd eventually like to modify key parts of the expression in the second method being generated so they will not be completely identical. Is there a way to do this?
Re: Generating duplicate method bodies in AbstractModelInferrer [message #1805339 is a reply to message #1805329] Fri, 12 April 2019 04:46 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13064
Registered: July 2009
Senior Member
What about inferring a third one and
Call the third one from the
Two above

body = '''codegoeshere();'''


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Generating duplicate method bodies in AbstractModelInferrer [message #1805359 is a reply to message #1805339] Fri, 12 April 2019 13:49 Go to previous messageGo to next message
Ben Holland is currently offline Ben HollandFriend
Messages: 12
Registered: April 2019
Junior Member
I'm confused by your answer. How would adding a third resolve one of the first two not being generated? Let me make the example a little more explict.

foo { 1 + 1 }


val function : model.functions.get(0);
it.members += toMethod(function.name + "_1", someReturnType) [
  it.body = function.expression;
]

it.members += toMethod(function.name + "_2", someReturnType) [
  it.body = function.expression;
]


generates:

int foo_1(){
  return 1 + 1;
}

int foo_2();


The problem is that after the expression has been inferred once it is not inferred a second time and the body generated is empty. I can't seem to reuse any part of the AST in the AbstractModelInferrer. Is it somehow getting marked as being inferred and then ignored for the second use?

[Updated on: Fri, 12 April 2019 13:51]

Report message to a moderator

Re: Generating duplicate method bodies in AbstractModelInferrer [message #1805361 is a reply to message #1805359] Fri, 12 April 2019 15:25 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13064
Registered: July 2009
Senior Member
no i mean

it.members += toMethod(function.name + "_1", someReturnType) [
  it.body = '''return «function.name»_3();'''
]

it.members += toMethod(function.name + "_2", someReturnType) [
   it.body = '''return «function.name»_3();'''
]

it.members += toMethod(function.name + "_3", someReturnType) [
  it.body = function.expression;
]



Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Generating duplicate method bodies in AbstractModelInferrer [message #1805372 is a reply to message #1805361] Fri, 12 April 2019 17:31 Go to previous messageGo to next message
Ben Holland is currently offline Ben HollandFriend
Messages: 12
Registered: April 2019
Junior Member
Oh I see what you mean. That will not work for what I was thinking because I'd like to generate a modification of the expression for the second function. Specifically what I'd like to do is generate one function with the original expression and the second function with the same expression except all callsites call different "alternative functions" (basically just a renamed version of the function).

So if my DSL specified:

foo { bar(); }


then I would generate:

foo_1(){
  bar();
}

foo_2(){
  bar_alternative();
}


I'm wondering is the post processing approach described in http://xtextcasts.org/episodes/18-model-optimization a better approach to do this?

[Updated on: Fri, 12 April 2019 17:32]

Report message to a moderator

Re: Generating duplicate method bodies in AbstractModelInferrer [message #1805374 is a reply to message #1805372] Fri, 12 April 2019 17:41 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13064
Registered: July 2009
Senior Member
no there is no way. can you be more specific on what EXACTLY you want to modify
where is "bar" and "baralternative" defined
shouldnt you use strategy pattern then.
or a proxy method bar that delegates to two alternatives

you could also consider to move the duplicate handing in the jvmmodelgenerator


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Fri, 12 April 2019 17:48]

Report message to a moderator

Re: Generating duplicate method bodies in AbstractModelInferrer [message #1805452 is a reply to message #1805374] Mon, 15 April 2019 13:15 Go to previous message
Ben Holland is currently offline Ben HollandFriend
Messages: 12
Registered: April 2019
Junior Member
After stubbornly trying to get this to work (I tried copying the expression into an inferred EClass reference with a post processor that sort of worked, but broke the inference logic in another place) I think you are right to suggest a design pattern such as the strategy pattern. Thanks again for the help!
Previous Topic:For each type a ModelInferrer?
Next Topic:Null properties of the EObject in the ScopeProvider
Goto Forum:
  


Current Time: Mon Aug 19 23:38:31 GMT 2019

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

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

Back to the top