Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xbase: TypeComputer for own generated Java Classes
Xbase: TypeComputer for own generated Java Classes [message #1775061] Tue, 24 October 2017 09:18 Go to next message
René Bärnreuther is currently offline René BärnreutherFriend
Messages: 31
Registered: October 2017
Member
Hello, it's me again! :-)

After understanding how to type simple types correctly with the xbase type computer, I tried implementing a version for Classes as Types.

The Grammer is, simplified, the following:
Program:
	{Program}
	body = XBlockExpression
	types += Type
;

Variable returns XVariableDeclaration:
	{Variable} writeable?='variable' simpleName=ID ':' (typing=DataType);

Type:
	(Enumeration | Composition);

DataType:
	(SimpleDataType | ComplexDataType | ListType);

ListType:
	=> ('list' '<' type=DataType) '>';

ComplexDataType:
	{ComplexDataType} typeName=[Type];

Composition:
	{Composition} "type" name=ID ":" "structure" "{" (attributes+=Attribute)* "}";

Attribute:
	{Attribute}
	"attribute" name=ID ":" typing=DataType ";";

Enumeration:
	{Enumeration} "type" name=ID ":" "enumeration" "{" values+=EnumTypeValue ("," values+=EnumTypeValue)* "}";

EnumTypeValue:
	name=ID;

SimpleDataType:
	{SimpleDataType} typeName=('integer' | 'string' | 'rational' | 'boolean');

@Override 
XPrimaryExpression returns XExpression:
/* Default XPrimaryExpressions */ | 
	Variable;


I try to get the type for ComplexDataType which is a reference to a created type.
The user should be able to program software like this:
{
	variable a : TestStruc
	a.a = 3
}
type TestStruc : structure {
	attribute a : integer;
}


The parsing seems to work. But the type of a is CompositionImpl a instead of TestStruc. The second line (a.a = 3) doesn't work either, but I think it depends on the type of the first one.

The TypeComputer for the Compositions looks like the following currently:
	/* THE PROBLEM IS PROBABLY HERE BECAUSE THE TYPE DOESN'T GET CALCULATED CORRECTLY */
			var cdt = vr.typing as ComplexDataType
			if (cdt.typeName instanceof Enumeration) {
				state.assignType(vr, getRawTypeForName((cdt.typeName as Enumeration).class, state)) // is this the right way?
			} else if (cdt.typeName instanceof Composition) { /* Composition */
				var comp = cdt.typeName as Composition
				state.assignType(vr, getRawTypeForName((comp as Composition).class, state))
			// TODO Here is the mistake I guess 
			}



I suppose the error is in the "state.assignType.." line, but I haven't found a way to set the type, in the example "TestStruc" correctly. What do I have to change here?

A working sample can be found here: https://github.com/rehne93/dsl-complex-type-mapping

The grammar extension is .mydsl. The relevant parts are in the compiler and the typing package.

The compiler seems to work, the variable gets generated as "TestStruc a = new TestStruc()"; but the feature call uses the type of "CompositionImpl" which doesn't work obviously.


If there are any more relevant questions, feel free to ask.
As always, thanks a lot in advance!


Re: Xbase: TypeComputer for own generated Java Classes [message #1775064 is a reply to message #1775061] Tue, 24 October 2017 09:31 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
i would have expected something like

getRawTypeForName("this is the inferred name of the type", state)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xbase: TypeComputer for own generated Java Classes [message #1775088 is a reply to message #1775064] Tue, 24 October 2017 13:11 Go to previous messageGo to next message
René Bärnreuther is currently offline René BärnreutherFriend
Messages: 31
Registered: October 2017
Member
The inferred type of the name should be "TestStruct" in the given example, right?
The function doesn't take this for me. As a String in
 getRawTypyeForName(comp.name, state)
the method doesn't allow String as parameter. comp.name is the value "TestStruct".
The only possibility (I see) would be to add it as a Qualifiedname in
getRawType(QualifiedName, JvmType, LightweightreferenceType)
but here I got the problem to get the JvmType and the LightweightreferenceType.
Is there any way to access the created class TestStruct? If I add something like "Composition" it works, but then the type isnt shown correctly. It only shows "TestStruct" and the TestStruct class as derived type.

I'm using Xtext 2.12 if this matters.

[Updated on: Tue, 24 October 2017 13:13]

Report message to a moderator

Re: Xbase: TypeComputer for own generated Java Classes [message #1775108 is a reply to message #1775088] Tue, 24 October 2017 16:27 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
well yes you have to handcraft you a method like that youself from

org.eclipse.xtext.xbase.typesystem.computation.AbstractTypeComputer.getTypeForName(Class<?>, ITypeComputationState)
org.eclipse.xtext.xbase.typesystem.computation.AbstractTypeComputer.findDeclaredType(Class<?>, ITypeComputationState)
org.eclipse.xtext.xbase.typesystem.computation.AbstractTypeComputer.findDeclaredType(Class<?>, ITypeReferenceOwner)
org.eclipse.xtext.common.types.util.TypeReferences.findDeclaredType(String, Notifier)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xbase: TypeComputer for own generated Java Classes [message #1775109 is a reply to message #1775108] Tue, 24 October 2017 16:30 Go to previous messageGo to next message
René Bärnreuther is currently offline René BärnreutherFriend
Messages: 31
Registered: October 2017
Member
Thank you, I will try and see how far I can get.
Re: Xbase: TypeComputer for own generated Java Classes [message #1775114 is a reply to message #1775109] Tue, 24 October 2017 17:42 Go to previous messageGo to next message
René Bärnreuther is currently offline René BärnreutherFriend
Messages: 31
Registered: October 2017
Member
I got far enough, I think.
I added the following to my typecomputer and called my getTypeForName-Method to retrieve the correct type.
def getTypeForName(String typename, ITypeComputationState state) {
		var type = findDeclaredType(typename, state)
		var owner = state.referenceOwner
		if (type === null) {
			return owner.newUnknownTypeReference(typename)
		}
		return owner.toLightweightTypeReference(type)
	}

	def findDeclaredType(String name, ITypeComputationState state) {
		if (name === null) {
			System.out.println("Name = null which is an error")
		}
		var declaredtype = findDeclaredType(name, state.referenceOwner.contextResourceSet)
		return declaredtype
	}

	def findDeclaredType(String name, Notifier context) {
		if (context === null) {
                      /* ERROR */
		}
		var resourceSet = EcoreUtil2.getResourceSet(context)
		if (resourceSet === null) {
			return null;
		}
		var typeProvider = typeProviderFactory.findOrCreateTypeProvider(resourceSet)
		try {
			val result = typeProvider.findTypeByName(name)
			return result
		} catch (RuntimeException e) {
			return null;
		}
	}


and it works like a charm now, even the scoping works perfectly fine!
(In case somebody in the future might need this.)

I really appreciate your help, thanks a lot :-).
Re: Xbase: TypeComputer for own generated Java Classes [message #1775115 is a reply to message #1775114] Tue, 24 October 2017 19:52 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
created https://github.com/eclipse/xtext-extras/issues/192

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Xtext 2.8.x Project Building in Xtext 2.10 with Maven Not Workign
Next Topic:how to retrieve the Injector
Goto Forum:
  


Current Time: Tue Apr 16 09:46:22 GMT 2024

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

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

Back to the top