Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » importing from java in custom expressions
importing from java in custom expressions [message #1722540] Fri, 05 February 2016 14:16 Go to next message
Daniel Cardin is currently offline Daniel CardinFriend
Messages: 109
Registered: July 2009
Senior Member
Hello Christian,

When using a custom xbase compiler, how do you deal with additional imports?
I am trying to create a new literal for "measure", ie. value + unit of measurement.
Since, the character '~' seems unused, I'm using it at the moment for parsing.

val length = ~150 mm

Measure returns xbase::XExpression:
	{Measure} '~' measure=XNumberLiteral unit=ID
;


I have modified the compiler to generate the equivalent code

Measure.valueOf(150, SI.MILLI(SI.METER))

How do I make these extensions automatically imported ?
I have modified my custom featurescopes

val measure = EcoreUtil2.getContainerOfType(featureCall, Measure)
		if (measure != null) {
			newSession = session.addImports [ importer |
				var JvmType type = typeReferences.findDeclaredType("javax.measure.units.SI", featureCall.eResource());
				if (type instanceof JvmDeclaredType) {
					importer.importStaticExtension(type as JvmDeclaredType, true);
				}
			]
		}


But it's never called... maybe because it's not considered a feature call when you output the code directly from the compiler appender?

What is the magic that's needed here?

Thanks !
Re: importing from java in custom expressions [message #1722543 is a reply to message #1722540] Fri, 05 February 2016 14:36 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
How does the compiler look like

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: importing from java in custom expressions [message #1722545 is a reply to message #1722543] Fri, 05 February 2016 14:49 Go to previous messageGo to next message
Daniel Cardin is currently offline Daniel CardinFriend
Messages: 109
Registered: July 2009
Senior Member
Type compiler:

	def dispatch computeTypes(Measure attribute, ITypeComputationState state) {
		if (attribute.unit != null) {
			val typeRef = state.referenceOwner.
				newUnknownTypeReference('''org.jscience.physics.measures.Measure<javax.measure.quantities.«typeByUnit(attribute.unit)»>''')
			state.acceptActualType(typeRef)
		}
	}

	def String typeByUnit(String unit) {
		switch unit {
			case "mm",
			case 'cm',
			case 'dm',
			case 'm': 'Length'
			case 'mm2',
			case 'cm2',
			case 'dm2',
			case 'm2': 'Area'
			case 'mm3',
			case 'cm3',
			case 'dm3',
			case 'm3': 'Volume'
			case 'mg',
			case 'g',
			case 'kg': 'Mass'
		}
	}


The compiler:

	def void _toJavaStatement(Measure measure, ITreeAppendable appendable, boolean isReferenced) {
		appendable.append('''Measure.valueOf(«number.value», SI.MILLI(SI.METER))''')
	}


I have also tried with :

	def void _toJavaStatement(Measure measure, ITreeAppendable appendable, boolean isReferenced) {
		val measureType = typeReferences.findDeclaredType("org.jscience.physics.measures.Measure", measure).type
		appendable.append(measureType.type)

		val milli = typeReferences.findDeclaredType("javax.measure.units.SI", measure).type
		val number = measure.measure as XNumberLiteral
		
		appendable.append('''.valueOf(«number.value»,''')
		appendable.append(milli.type)
		appendable.append('''.MILLI(SI.METER))''')
	}


Is that the better way? To get the type references there ?

There are many constants in the package, it would not be fun to import everything manually... an import static javax.measure.units.SI.* would be much better.

Thanks !
Re: importing from java in custom expressions [message #1722548 is a reply to message #1722545] Fri, 05 February 2016 15:56 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
hi your dont need to adapt featurescopes. you have to adapt compiler

i asume you have your types in a lib plugin your can put on the dependencies of the yourdsl plugin.
appendable.append(Measure).append('''.valueOf(«number.value», SI.MILLI(SI.METER))''')


if not the second approach is ok


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: importing from java in custom expressions [message #1722557 is a reply to message #1722548] Fri, 05 February 2016 18:00 Go to previous messageGo to next message
Daniel Cardin is currently offline Daniel CardinFriend
Messages: 109
Registered: July 2009
Senior Member
Is it right to assume that you cannot import anything that is not subsequently "written" to the appender, whether it's in the inferrer or elsewhere ?
Because if I could output a : import static javax.measure.units.SI.* in the resulting java class, it would be best.

Alternatively, since I know exactly what I want to import, maybe I could have default imports ?

does the technique of overriding ImportedNamespaceAwareLocalScopeProvider still work ?
Or is that just to define the scope and then you need to "use" a reference for it to be imported in the end java class ?

[Updated on: Fri, 05 February 2016 18:01]

Report message to a moderator

Re: importing from java in custom expressions [message #1722566 is a reply to message #1722557] Fri, 05 February 2016 20:04 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
There is no real nice solution for that

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Regarding rule precedence and Optional Category
Next Topic:How to implement generalization in XText ?
Goto Forum:
  


Current Time: Thu Mar 28 11:01:48 GMT 2024

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

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

Back to the top