Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [XPand] Invoke XPand programmatically: how templates are resolved?
[XPand] Invoke XPand programmatically: how templates are resolved? [message #641725] Sat, 27 November 2010 08:58 Go to next message
Federico Tomassetti is currently offline Federico TomassettiFriend
Messages: 190
Registered: July 2009
Location: Dublin
Senior Member

I would like to invoke an XPand template programmatically inside a plugin (I inserted an handler in an XtextEditor to launch code generation).

I am using the org.eclipse.xpand2.Generator class and I am using the EMF Registry metamodel.






Re: [XPand] Invoke XPand programmatically: how templates are resolved? [message #641727 is a reply to message #641725] Sat, 27 November 2010 09:34 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
Hi,

the class to call the generator programatically is XpandFacade. Search the forum or use google to find sample code.

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: [XPand] Invoke XPand programmatically: how templates are resolved? [message #641807 is a reply to message #641727] Sun, 28 November 2010 10:26 Go to previous messageGo to next message
Federico Tomassetti is currently offline Federico TomassettiFriend
Messages: 190
Registered: July 2009
Location: Dublin
Senior Member

Thank you Christian, also using XpandFacade I keep having the same problem: the template definition is not found. I thought Xpand looked into the classpath for the templates (and my template is accessible through classpath) but it is not found. I got this error:


EvaluationException : No Definition templates::Template::formatParser for positionalParserDsl::Format could be found!
	Internal error : element was null

	at org.eclipse.xpand2.XpandFacade.evaluate2(XpandFacade.java:59)
	at org.eclipse.xpand2.XpandFacade.evaluate(XpandFacade.java:43)
	at it.polito.CodeGenerator.generate(CodeGenerator.java:75)
	at it.polito.CodeGeneratorTest.testGenerate(CodeGeneratorTest.java:15)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)


I invoke XpandFacade in this way:

	public void generate(Format format){
		Output output = new MemoryOutput();
		XpandExecutionContextImpl execCtx = new XpandExecutionContextImpl(output, null);
		execCtx.registerMetaModel(new EmfRegistryMetaModel());
		XpandFacade facade = XpandFacade.create(execCtx);
		facade.evaluate("templates::Template::formatParser", format);
	}


Re: [XPand] Invoke XPand programmatically: how templates are resolved? [message #641810 is a reply to message #641725] Sun, 28 November 2010 11:09 Go to previous messageGo to next message
Stephan Krusche is currently offline Stephan KruscheFriend
Messages: 18
Registered: August 2010
Location: Munich
Junior Member
You have 3 potential errors:

1) Does your template definition look like?

«DEFINE formatParser FOR Format»
...
«ENDDEFINE»


2) If the template is in a source folder (that meas the folder is on the build path) then you must write:

facade.evaluate("formatParser", format);


3) You only register the standard EmfRegistryMetamodel. Where is your type (class or interface) Format defined?
Xpand needs to know this. If you have created your own metamodel you must register either the whole ecore file or each package (e.g. positionalParserDsl) that includes types you use in your code generation.

This can e.g. be done using the following code (assuming that positionParserDsl is your package):

EmfMetaModel metamodel = new EmfMetaModel();
String packageName = positionalParserDsl.class.getName();
metamodel.setMetaModelPackage(packageName);
execCtx.registerMetaModel(metamodel);

[Updated on: Sun, 28 November 2010 15:50]

Report message to a moderator

Re: [XPand] Invoke XPand programmatically: how templates are resolved? [message #641816 is a reply to message #641810] Sun, 28 November 2010 11:59 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
Hi,

the problem is as the error messages says: Internal error : element was null => the parameter with the model (here format) may not be null.

Updated: forget about this: then the message would complain about type void - i guess the solution is point 3 of stephans post.

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Sun, 28 November 2010 13:30]

Report message to a moderator

Previous Topic:[MWE] FileNotFoundException after exporting projects to jar
Next Topic:[Xpand / Xtend] Recognizing custom UML Profile
Goto Forum:
  


Current Time: Tue Apr 16 06:13:53 GMT 2024

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

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

Back to the top