Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to register my DSL to Java code generator?
How to register my DSL to Java code generator? [message #949994] Fri, 19 October 2012 14:24 Go to next message
Espinosa CZ is currently offline Espinosa CZFriend
Messages: 27
Registered: July 2009
Junior Member
Hi guys,
Documentation at eclipse.org/Xtext/documentation.html#TutorialCodeGeneration
just laconically says:
First of all, locate the file DomainmodelGenerator.xtend in the package org.eclipse.xtext.example.generator

But DomainmodelGenerator.xtend is not generated anymore for (current Xtext plugin 2.3.1) for DomainModel example project. Or any other. It is confusing.
Does this line suggest that all you need is to create a class extending IGenerator?
I guess it is not enough. You should somehow register your generator.
As a EMF Registration in StandaloneSetup? Somewhere in injector using Guice binding?
How to register the generator? That is my question.
Either by manually coding it or by recreating a specialized workflow as it used to be.

In older times, year ago when I first started to play with Xtext, there used to be "secondary" MWE2 in the sample project. When run it created some basic infrastructure for the generator.
It is suppressed now, I guess, judging from this section from main generator workflow from "new Xtext project template":
Workflow {
    ...
    component = Generator {
                ...
                // generator API
                fragment = generator.GeneratorFragment {
                    generateMwe = false
                    generateJavaMain = false
                }

What the GeneratorFragment generates when both flags are false?
Is the generator generated Generator MWE2 really needed these days?

So I did not create any registration.
I just created an Xtend based Generator:
package my.home.dsl.generator
import org.eclipse.xtext.generator.IGenerator
class DeepCloneGenerator implements IGenerator { ...
	override void doGenerate(Resource resource, IFileSystemAccess fsa) {...}
}


My unit test:

@RunWith(typeof(XtextRunner))
@InjectWith(typeof(DeepCloneInjectorProvider))
class DeepCloneGeneratorTest {
	
	@Inject IGenerator generatorDslToJava  // how to ensure DeepCloneGenerator implementation?
	@Inject ParseHelper<Model> parseHelper 
	
	@Test
	def test() {
		val code = '''
		package a.b.c
		a.b.c.OpeningHours {
			weekDay, from, to
		}'''
		val model = parseHelper.parse(code)
		val fsa = new InMemoryFileSystemAccess()
		generatorDslToJava.doGenerate(model.eResource, fsa)
		println(fsa.files)
		assertEquals(1, fsa.files.size)
		val topClonerJavaFileName = IFileSystemAccess::DEFAULT_OUTPUT + "Cloner.java"
		assertTrue(fsa.files.containsKey(topClonerJavaFileName))
		assertEquals('''
			package 
			public class OpeningHoursCloner {
			}'''.toString, 
			fsa.files.get(topClonerJavaFileName).toString
		)
	}
}

..but the org.eclipse.xtext.xbase.compiler.JvmModelGenerator::doGenerate()
was called instead of expected my.home.dsl.generator.DeepCloneGenerator::doGenerate()

Thank you for any hint
Espinosa
Re: How to register my DSL to Java code generator? [message #950017 is a reply to message #949994] Fri, 19 October 2012 14:47 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13614
Registered: July 2009
Senior Member
Hi,

you have to override the binding

public class MyDslRuntimeModule extends org.xtext.example.mydsl.AbstractMyDslRuntimeModule {

	@Override
	public Class<? extends IGenerator> bindIGenerator() {
		// TODO Auto-generated method stub
		return MyGen.class;
	}
	
}


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to register my DSL to Java code generator? [message #950072 is a reply to message #950017] Fri, 19 October 2012 16:01 Go to previous messageGo to next message
Espinosa CZ is currently offline Espinosa CZFriend
Messages: 27
Registered: July 2009
Junior Member
Thank you Christian!
Excellent and prompt reply.
This fixed my unit test.

For the curious, here is my final code:
package my.home.dsl
public class DeepCloneRuntimeModule extends my.home.dsl.AbstractDeepCloneRuntimeModule {
	@Override
	public Class<? extends IGenerator> bindIGenerator() {
		return DeepCloneGenerator.class;
	}
}


Where DeepCloneRuntimeModule is a class generated by Xtext project template.
It was originally empty class. You have to locate it and add the generator binding.
AbstractDeepCloneRuntimeModule is generated by workflow. It implements Guice Module. It is where default generator implementation - XBase's JvmModelGenerator - binding is defined.
Re: How to register my DSL to Java code generator? [message #950152 is a reply to message #950072] Fri, 19 October 2012 17:25 Go to previous messageGo to next message
Espinosa CZ is currently offline Espinosa CZFriend
Messages: 27
Registered: July 2009
Junior Member
Perhaps its worth to add, not only it fixed unit tests, but also the automated builder code generator when run as Eclipse application. This one binding.

P.S.
Could authors add well configured Generator again to template projects? It used to be there. Thanks.
Re: How to register my DSL to Java code generator? [message #950237 is a reply to message #950152] Fri, 19 October 2012 18:52 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13614
Registered: July 2009
Senior Member
Hi,

the domain model example is intended to work with a generator that automatically transform the jvm model created by the jvmmodelinferrer to java. thus by default the jvmmodelgenerator is used.
see XbaseGeneratorFragment

~Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to register my DSL to Java code generator? [message #950238 is a reply to message #950152] Fri, 19 October 2012 18:52 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13614
Registered: July 2009
Senior Member
Hi,

the domain model example is intended to work with a generator that automatically transform the jvm model created by the jvmmodelinferrer to java. thus by default the jvmmodelgenerator is used.
see XbaseGeneratorFragment

~Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Customizing Serializer
Next Topic:Setting multiple EMF elements based on one grammar element.
Goto Forum:
  


Current Time: Wed Sep 30 00:13:25 GMT 2020

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

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

Back to the top