Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [xpand] dynamic expansion of template
[xpand] dynamic expansion of template [message #508215] Sun, 17 January 2010 12:58 Go to next message
Volker Wegert is currently offline Volker WegertFriend
Messages: 182
Registered: July 2009
Senior Member
Hello,

I just noticed that bug 161915 was closed due to some move operations, but it
doesn't look like the issue has been resolved... This is about determining the
name of the template to expand dynamically at runtime. I could really use this
feature because I do a lot of M2MT before the actual code generation and the
class and method names all come from the transformed model. Without the
dynamic expansion, I have to use a rather unpleasant IF - ELSEIF
sequence. Perhaps this bug should be re-opened?

Thanks
Volker

--
* Volker Wegert * http://www.volker-wegert.de/contact *
"With sufficient thrust, pigs fly just fine. However, this is not necessarily
a good idea. It is hard to be sure where they are going to land, and it could
be dangerous sitting under them as they fly overhead." (RFC 1925)
Re: [xpand] dynamic expansion of template [message #508226 is a reply to message #508215] Sun, 17 January 2010 21:21 Go to previous messageGo to next message
Joerg Reichert is currently offline Joerg ReichertFriend
Messages: 80
Registered: July 2009
Location: Leipzig
Member
Hi Volker,

I believe, the old bug you mentioned is related to the bug I opened in the Eclipse Bugzilla some time ago: 288536.

Joerg


--
Need professional support for Xtext and EMF?
Go to: http://xtext.itemis.com
Re: [xpand] dynamic expansion of template [message #508267 is a reply to message #508226] Mon, 18 January 2010 08:25 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
Hi Volker, Joerg,

it is now possible to inject the ExecutionContext
into the JavaExtension. See
https://bugs.eclipse.org/bugs/show_bug.cgi?id=261603

Can you imagine to solve your use case with that capability?

Can we close bug 288536 as well?

Cheers,
Sven

Joerg Reichert schrieb:
> Hi Volker,
>
> I believe, the old bug you mentioned is related to the bug I opened in
> the Eclipse Bugzilla some time ago:
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=288536
>
> Joerg


--
Need professional support for Xtext and EMF?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : blog.efftinge.de
Re: [xpand] dynamic expansion of template [message #509120 is a reply to message #508226] Thu, 21 January 2010 12:50 Go to previous messageGo to next message
Volker Wegert is currently offline Volker WegertFriend
Messages: 182
Registered: July 2009
Senior Member
Joerg Reichert <joerg.reichert@itemis.de> writes:
> I believe, the old bug you mentioned is related to the bug I opened in the
> Eclipse Bugzilla some time ago:
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=288536

I don't think so. Your bug is about Xtend, I'm talking of making the EXPAND
target in the Xpand language dynamic. Or did I misunderstand you?

Volker



--
* Volker Wegert * http://www.volker-wegert.de/contact *
"INTEL inside" - The world's most widely used warning label.
Re: [xpand] dynamic expansion of template [message #509121 is a reply to message #508267] Thu, 21 January 2010 12:57 Go to previous messageGo to next message
Volker Wegert is currently offline Volker WegertFriend
Messages: 182
Registered: July 2009
Senior Member
Sven Efftinge <sven.efftinge@itemis.de> writes:
> Can you imagine to solve your use case with that capability?

In my case no. What I need is
<<EXPAND getTemplateName(...) FOR foo>>
where getTemplateName is an *expression* that returns the actual name of the
template to expand. Maybe some syntactical tweak is required because
getTemplateName is a name that actually might be DEFINEd - so probably this
would be something like
<<EXPAND @getTemplateName(...) FOR foo>>
to designate a dynamic expansion.

My current workaround is really ugly and hard to maintain:
<<IF getTemplateName(...) == "Foo">>
<<EXPAND generateFoo FOR this>>
<<ELSEIF getTemplateName(...) == "Bar">>
<<EXPAND generateBar FOR this>>
....and so on. A total of 300+ lines of this stuff, enough to hide a lot of
typos.

Thanks
Volker

--
* Volker Wegert * http://www.volker-wegert.de/contact *
"In the spirit of taking things too far, here is a fully functional
5-dimensional analog of Rubik's cube."
(http://www.gravitation3d.com/magiccube5d/)
Re: [xpand] dynamic expansion of template [message #509658 is a reply to message #509121] Sun, 24 January 2010 12:37 Go to previous messageGo to next message
Erhard Weinell is currently offline Erhard WeinellFriend
Messages: 39
Registered: January 2010
Location: Kassel, Germany
Member
Volker Wegert wrote:

> My current workaround is really ugly and hard to maintain:
> <<IF getTemplateName(...) == "Foo">>
> <<EXPAND generateFoo FOR this>>
> <<ELSEIF getTemplateName(...) == "Bar">>
> <<EXPAND generateBar FOR this>>
> ...and so on. A total of 300+ lines of this stuff, enough to hide a lot of
> typos.

So your intend basically is to invoke templates reflectively? Given the
templates are fixed, one could generate the dispatcher template you outline
above automatically. The Xpand parser can be used to find out which
templates there are. EMF generates the Model-Switch classes to handle type
hierarchies in a similar way.

Alternatively, with Sven's remark concerning Java-instance invocations
(#261603) and his idea to treat template expansion like invocations
(#299311), it might be possible to write a reflective dispatcher as Java
extension.

Best regards,
Erhard
Re: [xpand] dynamic expansion of template [message #509667 is a reply to message #508267] Sun, 24 January 2010 15:03 Go to previous messageGo to next message
Joerg Reichert is currently offline Joerg ReichertFriend
Messages: 80
Registered: July 2009
Location: Leipzig
Member
Hi Sven,

the fix of the bug 261603 indeed enables me to meet my requirement:

project structure:
project
+ src
++ javaextension
+++ Helper.java
++ template
+++ Template.xpt
+++ GeneratorExtension.ext
+++ OtherExtension.ext


Template.xpt
«IMPORT ...»

«EXTENSION template::GeneratorExtensions»

«DEFINE ... FOR ...»
	«FILE "test.txt"»
		«helloWorld()»
	«ENDFILE»
«ENDDEFINE»


GeneratorExtension.ext
import ...;
...
String helloWorld() : JAVA javaextension.Helper.getHelloWorld();


Here is one thing to fix: the Xtend editor creates still a marker complaining that "The method getHelloWorld is not public static". This check have to be removed from the editor implementation.

OtherExtension.ext
import ...;
...
String getWorld() : "World";


Helper.java
public class Helper implements IExecutionContextAware {
	private String hello;

	public Helper() {
		hello = "Hello";
	}

	public String getHelloWorld() {
		ExtensionFile file;
		String result = "unknown";
		try {
			file = ParseFacade.file(new FileReader(new File(
					"src/template/OtherExtension.ext")), "OtherExtension");
			ExecutionContextImpl ec = (ExecutionContextImpl) getExecutionContext()
					.cloneWithResource(file);
			ExpressionFacade ef = new ExpressionFacade(ec);
			result = (String) ef.evaluate("getWorld()");
			// setExecutionContext(ec);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return hello + " " + result;
	}

	private ExecutionContext ctx;

	public ExecutionContext getExecutionContext() {
		return ctx;
	}

	public void setExecutionContext(ExecutionContext ctx) {
		this.ctx = ctx;
	}
}


that eventually will produce

generated test.txt
Hello World


conclusion
The solution above only works for Xtend (as the injected ExecutionContext is only able to execute extensions). As I looked into the code it seems not to be possible to enhance the JavaExtensionStatement implementation to also inject the XpandExecutionContext that would enable to call a template method dynamically with that kind of code:

    XpandExecutionContext xpandContext = get XpandExecutionContext();
    XpandFacade facade = XpandFacade.create(xpandContext);
    facade.evaluate("templates::TemplateFile::defineMethod", modelElement);


So it seems to be reasonable to enhance the Xpand language with such a feature as Volker proposed.

Joerg


--
Need professional support for Xtext and EMF?
Go to: http://xtext.itemis.com
Re: [xpand] dynamic expansion of template [message #509670 is a reply to message #509120] Sun, 24 January 2010 15:09 Go to previous messageGo to next message
Joerg Reichert is currently offline Joerg ReichertFriend
Messages: 80
Registered: July 2009
Location: Leipzig
Member
Hi Volker,

no you didn't. Without having tested it before I had assumed that the solution for Xtend would be adaptable for Xpand, too. But it isn't as I pointed out in my previous post.

Joerg


--
Need professional support for Xtext and EMF?
Go to: http://xtext.itemis.com
Re: [xpand] dynamic expansion of template [message #510034 is a reply to message #509670] Tue, 26 January 2010 09:55 Go to previous message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
The injected ExecutionContext actually is an XpandExecutionContext, if
the extension was invoked from an Xpand context (i.e. an Xpand generator
component).

The following code does the trick:

private ExecutionContext ctx;

public void setExecutionContext(ExecutionContext ctx) {
this.ctx = ctx;
}

public void invokeXpand(Object target, String definition) {
if (ctx instanceof XpandExecutionContext) {
XpandExecutionContext ctx2 = (XpandExecutionContext) ctx;
XpandFacade.create(ctx2).evaluate(definition, target);
}
}

Note, that the definition has to be qualified.

Cheers,
Sven


Joerg Reichert schrieb:
> Hi Volker,
>
> no you didn't. Without having tested it before I had assumed that the
> solution for Xtend would be adaptable for Xpand, too. But it isn't as I
> pointed out in my previous post.
>
> Joerg


--
Need professional support for Xtext and EMF?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : blog.efftinge.de
Previous Topic:Howto run MWE workflow programmatically with classpath?
Next Topic:[JET] Break or continue for <c:iterate>
Goto Forum:
  


Current Time: Thu Apr 25 21:38:50 GMT 2024

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

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

Back to the top