Skip to main content


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 18:20 Go to next message
Roberto Salama is currently offline Roberto SalamaFriend
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 19:46 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
hello roberto,

afaik this is not possible. what is the usecase you need this feature for?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: [XPAND] accessing <<FILE ...>> from Java [message #493903 is a reply to message #493749] Wed, 28 October 2009 13:03 Go to previous messageGo to next message
Roberto Salama is currently offline Roberto SalamaFriend
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 13:11 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
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.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: [XPAND] accessing <<FILE ...>> from Java [message #493910 is a reply to message #493905] Wed, 28 October 2009 13:30 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
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>>


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: [XPAND] accessing <<FILE ...>> from Java [message #494100 is a reply to message #493903] Thu, 29 October 2009 08:47 Go to previous message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
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, EMF, Eclipse IDE?
Go to: http://devhub.karakun.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: Fri Apr 26 15:08:53 GMT 2024

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

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

Back to the top