Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [XPAND] accessing <<FILE ...>> from Java(Need help on how to access a <<FILE>> created in XPAND from Java)
[XPAND] accessing <<FILE ...>> from Java [message #493741] Tue, 27 October 2009 14:20 Go to next message
Roberto Salama is currently offline Roberto Salama
Messages: 13
Registered: July 2009
Junior Member
Hello:

I have the following .xpt file:

«IMPORT entityDsl»

«EXTENSION templates::Extensions»

«DEFINE main FOR Model-»
«FILE "output.txt"-»
«EXPAND Instance FOREACH this.instances-»
«ENDFILE-»
«ENDDEFINE»

«DEFINE Instance FOR Instance»
«createInstance(this)»
«ENDDEFINE»

The method creatInstance(this) is mapped to a java call in the .ext file:

Void createInstance(Instance i) :
JAVA helper.EntityHandler.createInstance(com.ms.entityDsl.Instanc e);

How can I access the FILE created in the .xpt file from inside the createInstance() java call?

Thanks!

roberto
Re: [XPAND] accessing <<FILE ...>> from Java [message #493749 is a reply to message #493741] Tue, 27 October 2009 15:46 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6142
Registered: July 2009
Senior Member
hello roberto,

afaik this is not possible. what is the usecase you need this feature for?
Re: [XPAND] accessing <<FILE ...>> from Java [message #493903 is a reply to message #493749] Wed, 28 October 2009 09:03 Go to previous messageGo to next message
Roberto Salama is currently offline Roberto Salama
Messages: 13
Registered: July 2009
Junior Member
Thanks for your reply.

While writing XPAND templates, I find myself resorting to Java mainly to leverage Maps which I don't know how to access from XPAND directly.

My use case, in this case, is to manipulate an Entity in java using a couple of maps. So an Entity contains properties, entities are created (ie x = new Foo()) then values are set (ie, x.name = "Pluto"; x.age = 3). In traversing the AST with XPAND, I use a Map to store key value relationships which I then need to serialize. This is just a toy language not a real world use case, but I can see that not being able to use (or not knowing how to use) a Map XPAND requires me to dive into Java. Even though I am serializing, I can see the need to write back to the <<FILE>> from Java.

thanks.

Re: [XPAND] accessing <<FILE ...>> from Java [message #493905 is a reply to message #493903] Wed, 28 October 2009 09:11 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6142
Registered: July 2009
Senior Member
hi,

if i unserstand your problem you create a String with Java and want to wirte this into the Xpand file. So why don't let the extension return the string.
Re: [XPAND] accessing <<FILE ...>> from Java [message #493910 is a reply to message #493905] Wed, 28 October 2009 09:30 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6142
Registered: July 2009
Senior Member
Sorry for the typos in my previous posts. my phases might have been a bit misunderstandable:

here an example who to write code from a Java Extension into a file using the xpand / xtend / java - queue:

Java:
public class MyJavaExtensions {
   public String myXtendFunction(Parameter param) {
       String result = "";
      // do some calculation
return result;
   }
}


Xtend
String myXtendFuntion(Parameter param):
    JAVA path.MyJavaExtensions.myXtendFunction(oath.Parameter);


Xpand
<< FILE "test.txt">>
<<myXtendFuntion(param)>>
<<ENDFILE>>
Re: [XPAND] accessing <<FILE ...>> from Java [message #494100 is a reply to message #493903] Thu, 29 October 2009 04:47 Go to previous message
Karsten Thoms is currently offline Karsten Thoms
Messages: 353
Registered: July 2009
Location: Dortmund
Senior Member

If you just want to assign dynamic values to your Entity instances, use Xtend stdlib ElementProperties extensions. You will find a reference in the docs. In your Java code you could call ElementPropertiesExtensions.get/setProperty().

Coming back to your initial request: You could access the FILE statement from Java with some hacks: You'd need to implement a Callback that is registered to the Generator component. This gets informed on any statement evaluated. Then recognize opening FILE statements and store a handle to this instance in a ThreadLocal. This ThreadLocal must be made accessible from your Java method that needs the File statement. Since this is a real hack you see that's not a common use case.

Best wishes,
~Karsten


Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @kthoms
Blog : www.karsten-thoms.de
Previous Topic:[MWE]: NPE in WorkflowRunner#prepare()
Next Topic:Use case specification and transformation
Goto Forum:
  


Current Time: Tue Jul 29 16:52:15 EDT 2014

Powered by FUDForum. Page generated in 0.01910 seconds