Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » DomainCompiler in Xtext 2.1
DomainCompiler in Xtext 2.1 [message #757492] Fri, 18 November 2011 16:06 Go to next message
Benoit Lelandais is currently offline Benoit LelandaisFriend
Messages: 19
Registered: November 2011
Junior Member
Hi,

I try to reproduce, in Xtext 2.1, the Jan's blog Xtext 2.0 example.

I have created a DomainmodelTypeProvider class and bound it in the DomainmodelRuntimeModule.

I have also created a DomainmodelCompiler class but I don't find where it can be bound in Xtext 2.1. I don't find where XbaseCompiler is bound to Guice...

Can you help me ?

Thanks for your answer.
Re: DomainCompiler in Xtext 2.1 [message #757497 is a reply to message #757492] Fri, 18 November 2011 16:13 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

the (default) way this is done has change in 2.1. you do everything in the jvmmodelinferrer
and the generation is done by JvmModelGenerator with works on the inferred model.
of course you can still bind your own IGenerator

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: DomainCompiler in Xtext 2.1 [message #757524 is a reply to message #757497] Fri, 18 November 2011 18:31 Go to previous messageGo to next message
Benoit Lelandais is currently offline Benoit LelandaisFriend
Messages: 19
Registered: November 2011
Junior Member
Hi Christian,

Thanks for your answer.

OK, I will try to work with the jvmmodelinferrer.

So, I have to add an 'infer' method for the new rule : the XDecimalLiteral. But, I don't want to create a class corresponding to the XDecimalLiteral instance (with the 'toClass' method of TypesBuilder). I just want to instanciate a Double. I don't see how to do that ?

Re: DomainCompiler in Xtext 2.1 [message #757525 is a reply to message #757524] Fri, 18 November 2011 18:46 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

JvmModelGenerator injects a XbaseCompiler thus no explicit binding is needed as long as you use xbasecompiler
if you want to subclass it you have to add a new binding, not overwrite an existing one

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: DomainCompiler in Xtext 2.1 [message #757526 is a reply to message #757524] Fri, 18 November 2011 18:57 Go to previous messageGo to next message
Max Goltzsche is currently offline Max GoltzscheFriend
Messages: 40
Registered: November 2011
Member
Hey,

If you want to create your own class to can use the .toClass() method.
If you want to have a field of some jvm type you can use the .toField("myFld", typeof(Double)) method.
So if some class inferred from an element of your language contains a reference to your XDecimalLiteral you infer the corresponding jvmfield as
yourInferredJvmTypeThatReferencesXDecimalLiteral.members += xDecimalLiteralReference.toField("decimal", typeof(Double))

Of course, with this approach you have to define the type for every field on your own but anyway, you have to infer every class/field you define with your grammar on your own.

regards,
Max
Re: DomainCompiler in Xtext 2.1 [message #757528 is a reply to message #757526] Fri, 18 November 2011 19:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
So this is what works for me

public class DomainmodelRuntimeModule extends AbstractDomainmodelRuntimeModule {
	
	@Override
	public Class<? extends IValueConverterService> bindIValueConverterService() {
		return DomainmodelValueConverterService.class;
	}
	
	@Override
	public Class<? extends ITypeProvider> bindITypeProvider() {
		return DomainModelTypeProvider.class;
	}
	
	public Class<? extends XbaseCompiler> bindXbaseCompiler() {
		return DomainModelCompiler.class;
	}
	
}


@Singleton
public class DomainModelTypeProvider extends XbaseTypeProvider {
	
	@Override
	protected JvmTypeReference type(XExpression expression, JvmTypeReference rawExpectation, boolean rawType) {
		if (expression instanceof DecimalLiteral) {
			return _type((DecimalLiteral) expression, rawExpectation, rawType);
		}
		return super.type(expression, rawExpectation, rawType);
	}
	
	protected JvmTypeReference _type(DecimalLiteral literal, JvmTypeReference rawExpectation, boolean rawType) {
		return getTypeReferences().getTypeForName(Double.class, 
                literal); 
	}

}


 */
public class DomainModelCompiler extends XbaseCompiler {

	protected void _toJavaExpression(DecimalLiteral expr, IAppendable b) {
		b.append(expr.getIntPart() + "." + expr.getDecimalPart());
	}

	protected void _toJavaStatement(DecimalLiteral expr, IAppendable b, boolean isReferenced) {
		generateComment(expr, b, isReferenced);
	}

}


~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: DomainCompiler in Xtext 2.1 [message #757538 is a reply to message #757528] Fri, 18 November 2011 19:46 Go to previous messageGo to next message
Benoit Lelandais is currently offline Benoit LelandaisFriend
Messages: 19
Registered: November 2011
Junior Member
Thanks a lot.

In fact, I have written the type provider, the model compiler (with some bugs, so thank you for the post) and I just need the lines :

public Class<? extends XbaseCompiler> bindXbaseCompiler() {
return DomainModelCompiler.class;
}

Because I looked in the AbstractDomainmodelRuntimeModule to find a method to override, and the 'bindXbaseCompiler' method is a new one.

Thanks again

Re: DomainCompiler in Xtext 2.1 [message #758328 is a reply to message #757538] Tue, 22 November 2011 18:38 Go to previous messageGo to next message
Max Goltzsche is currently offline Max GoltzscheFriend
Messages: 40
Registered: November 2011
Member
Hey,

now, I understand the topic of the thread^^.

I try to add operators using xtext 2.1.1 like in this article: http://koehnlein.blogspot.com/2011/07/extending-xbase.html

Instead of the extending StaticMethodsFeatureForTypeProvider I extend ExtensionClassNameProvider:
@Singleton
public class UclExtensionClassNameProvider extends ExtensionClassNameProvider {

	@Override
	protected Collection<String> computeLiteralClassNames() {
		Collection<String> result = super.computeLiteralClassNames();

		result.add(DoubleLiteral.class.getName());
		
		return result;
	}
	
	@Override
	protected Multimap<Class<?>, Class<?>> simpleComputeExtensionClasses() {
		Multimap<Class<?>, Class<?>> extensions = super.simpleComputeExtensionClasses();
		
		extensions.put(Double.class, MathExtensions.class);
		
		return extensions;
	}
}

... and bind it in the RuntimeModule:
	public Class<? extends ExtensionClassNameProvider> bindExtensionClassNameProvider() {
		return UclExtensionClassNameProvider.class;
	}

MathExtensions class:
public class MathExtensions {
	
	public static Double operator_plus(Double x, Double y) {
		return x + y;
	}
	
	public static Double operator_minus(Double x, Double y) {
		return x - y;
	}

	public static Double operator_multiply(Double x, Double y) {
		return x * y;
	}
	
	public static Double operator_divide(Double x, Double y) {
		return x / y;
	}
}


When I debug StaticMethodsFeatureForTypeProvider.getVisibleTypesContainingStaticMethods() result the MathExtensions for Double types is returned correctly. Also returned by default is the Comparable extension but I neither can use my MathExtensions nor any default ones like '<' or '=='.
What am I doing wrong.

regards,
Max

[Updated on: Tue, 22 November 2011 18:42]

Report message to a moderator

Re: DomainCompiler in Xtext 2.1 [message #758331 is a reply to message #758328] Tue, 22 November 2011 18:55 Go to previous messageGo to next message
Benoit Lelandais is currently offline Benoit LelandaisFriend
Messages: 19
Registered: November 2011
Junior Member
Hi Max,

I am not an expert but I have done the same extension with Xtext 2.1.0. I have extended the StaticMethodsFeatureForTypeProvider class, like in the Jan's blog example and it works... Perhaps can you try this instead of extending ExtensionClassNameProvider ?
Re: DomainCompiler in Xtext 2.1 [message #758333 is a reply to message #758331] Tue, 22 November 2011 19:26 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

StaticMethodsFeatureForTypeProvider uses ExtensionClassNameProvider so its ok if you extend ExtensionClassNameProvider.
the keypoint is (from jans blog)

Quote:
Let's write a new extension library class holding the operations as static methods. These methods need to be available at runtime, too, as the generated Java code will call them. That's why it is a good idea to put such extension classes into a separate plug-in org.eclipse.xtext.example.domainmodel.lib.


Quote:
If you add the plug-in org.eclipse.xtext.example.domainmodel.lib to the classpath of the project in the runtime workspace, the following domain model should compile


i bet you missed at least the second point

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: DomainCompiler in Xtext 2.1 [message #758336 is a reply to message #758333] Tue, 22 November 2011 20:23 Go to previous message
Max Goltzsche is currently offline Max GoltzscheFriend
Messages: 40
Registered: November 2011
Member
Thanks, that's it: I forgot to add it to the classpath. Rolling Eyes
But normal operators like '==' or '<' I would expect e.g. from that Comparable extension are not yet usable.
Do I have to define them all on my own?

[Updated on: Tue, 22 November 2011 20:30]

Report message to a moderator

Previous Topic:[Xtend2] floats
Next Topic:Mapping to JvmEnumerationType
Goto Forum:
  


Current Time: Fri Apr 19 03:58:43 GMT 2024

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

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

Back to the top