Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » InstantiationException while assigning vaiables(Excetion while creating a new variable with Xtend)
InstantiationException while assigning vaiables [message #559332] Thu, 16 September 2010 12:09 Go to next message
Softwareassi is currently offline SoftwareassiFriend
Messages: 7
Registered: August 2010
Junior Member
Hello,

I am working with Xtext and want to do a M2T transformation. Before I do the transformation I want to evaluate my model and maybe add different features to some model elements. So I want to do a M2M transformation using Xtend. For adding the new feature I need to create a new instance of a model element. In the Xtend Reference I found that it is possible to use something like:
let var = new ModelElement....

But when I do so I get an java.lang.InstantiationException in the line where I use the new operator. I set up a small project and tried different things but nothing worked.

My Xtext defintion looks like:
...
State hidden (WS, SL_COMMENT):
	'state' name = ID
	'type'  types += Type
	('actions' '{' (actions+=[Command])+ '}')?
	(transitions+=Transition)*
	'end';

Type:
	StateTypeList | StateTypeListMand;

StateTypeList:
	name = StateType;

StateTypeListMand:
	name = StateType man ?= "!";

enum StateType:
	S1 = 'one' |
 	S2 = "two" |
 	S3 = "three" ;


With the Xtend I want to manipulate the feature TYPES of the STATE rule. So I wrote this Xtend snippet that is called from an Xpand template:
State change(State st) :
	let stype = new StateTypeList:
		stype.setName(  ((StateType)StateType.getStaticProperty("S2").get())  ) ->
		st.types.add(stype) ->
		st;


Is it possible what I try to do or can I just don't do it? Do I have to use other model elements?

I am very thankful for any suggestions.


Re: InstantiationException while assigning vaiables [message #559343 is a reply to message #559332] Thu, 16 September 2010 12:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

this won't work using the JavaBeans Metamodel -> You have to switch back to emf org.eclipse.xtend.typesystem.emf.EmfRegistryMetaModel

component = org.eclipse.xpand2.Generator {
		metaModel = org.eclipse.xtend.typesystem.emf.EmfRegistryMetaModel{}
		expand = "templates::Template::main FOREACH greetings"
		outlet = {
			path = targetDir
		}
		fileEncoding = fileEncoding
	}


and one would rather use a create extension insted of the new operator

import myDsl;

State change(State st) :
	st.types.add(createStateTypeList()) ->
	st
;
		
create StateTypeList this createStateTypeList() :
	this.setName(((StateType)StateType.getStaticProperty("S2").get()))
;


The last thing is that one would rather the transformation be done with XtendComponent than do it with Xpand side-effects.

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: InstantiationException while assigning vaiables [message #559415 is a reply to message #559343] Thu, 16 September 2010 14:55 Go to previous messageGo to next message
Softwareassi is currently offline SoftwareassiFriend
Messages: 7
Registered: August 2010
Junior Member
Thanks a lot. I got it running with switching the meta model and using the create extension.

You are right it might be the better way to use the XtendComponent for doing the model transformation. I also tried to set this up but I get an Excption running my workflow:
...
23   [main] ERROR mf.mwe2.launch.runtime.Mwe2Launcher  - Problems running workflow workflow.MyDslGenerator: 
[ERROR]: Property 'invoke' not specified properly. AbstractExtension file 'templates::Extension' not found.(Element: -UNKNOWN-; Reported by: -UNKNOWN-)
java.lang.RuntimeException: Problems running workflow workflow.MyDslGenerator: 
[ERROR]: Property 'invoke' not specified properly. AbstractExtension file 'templates::Extension' not found.(Element: -UNKNOWN-; Reported by: -UNKNOWN-)
	at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:82)
...

My workflow defintion is the following:
Workflow {
	component = org.eclipse.xtext.mwe.Reader {
		path = modelPath
		register = org.xtext.example.mydsl.MyDslStandaloneSetup {}
		load = {
			slot = "mydsl"
			type = "State"
		}
	}
	
	component = org.eclipse.xtend.XtendComponent {
		metaModel = org.eclipse.xtend.typesystem.emf.EmfRegistryMetaModel{}
		invoke = "templates::Extension::change(mydsl)" 
		outputSlot = "changedmod"
	}

	component = org.eclipse.xpand2.Generator {
		expand = "templates::Template::main FOREACH changedmod"
		outlet = {
			path = targetDir
		}
		fileEncoding = fileEncoding
	}
}


The Xtend file looks the same you descriped before. I searched the internet and tried different things but always the same Exception is coming up. I changed the extension method parameter and some things in the workflow. Any ideas what's wrong there?

I guess also the thing between the XtendComponent to the Generator component is not correct. Do I have to use a Writer in between? Is there any good doc out there where I could get detailed information about MWE2? The official oAW documentation doesn't cover such cases or I missed it somehow.

Sincerly
Re: InstantiationException while assigning vaiables [message #559437 is a reply to message #559415] Thu, 16 September 2010 15:31 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
Two other things to note:
- the error message in the console says that the Xtend file could not be found, so is it on the class path?
- the change function in your Xtend file takes a State as argument, but the mydsl slot would contain a List of State models -I'm not sure the auto-collect would work properly here.


Re: InstantiationException while assigning vaiables [message #559445 is a reply to message #559437] Thu, 16 September 2010 15:48 Go to previous messageGo to next message
Softwareassi is currently offline SoftwareassiFriend
Messages: 7
Registered: August 2010
Junior Member
Thanks for your answer.
The Xtend file is in the class path. I also changed the location of the xtend file. It is odd.
When I run the workflow without the XtendComponent section - so only the Generator section - it runs without any problems.

The thing with the list is true, I haven't seen this. I changed it and also tried to handover the model itself. Still the same problem. Sad

Re: InstantiationException while assigning vaiables [message #559449 is a reply to message #559445] Thu, 16 September 2010 16:25 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

Just to be sure: you have a file Extension.ext in the package templates?

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: InstantiationException while assigning vaiables [message #559483 is a reply to message #559449] Thu, 16 September 2010 18:42 Go to previous messageGo to next message
Softwareassi is currently offline SoftwareassiFriend
Messages: 7
Registered: August 2010
Junior Member
Hi,

sure I have. Like I said, the component = org.eclipse.xpand2.Generator {...} part runs just fine. Only the problem with the XtendComponent part. I read in another forum that somebody has had the same problem but no solution for that. From the Template I also can call the Extension without any problems. I also copied the Extension file to the workflow package... same exception

I still think that I made a mistake in my workflow definition but I cannot really see it. Sad

My project structure is:
src
- model
+ model.myDsl
- templates
+ Extension.ext
+ Templates.xpt
- workflow
+ workflow.mwe2

Maybe I will set up another project and try it again. Thanks for the help.
Re: InstantiationException while assigning vaiables [message #559485 is a reply to message #559483] Thu, 16 September 2010 19:00 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

based on the sample created with the Xtext Project wizard i tried this

/org.xtext.example.mydsl.generator/src/templates/Extensions. ext
import myDsl;

sortedGreetings(Model this) :
	this.greetings.sortBy(g|g.name.toLowerCase());
	
List[Greeting] change(List[Greeting] l) :
	l.collect(e|e.change());
	
Greeting change(Greeting l) :
	l.setName(l.name.toUpperCase()) ->
	l;


/org.xtext.example.mydsl.generator/src/templates/Template.xp t
«IMPORT myDsl»

«EXTENSION templates::Extensions»

«DEFINE main FOR Greeting-»
«FILE name+".txt"-»
This is an example of a generated file.

The input element was "Hello «name»!"

All greetings in the same file:
«FOREACH ((Model)eContainer).sortedGreetings() AS g SEPARATOR ', '»«g.name»«ENDFOREACH»
«ENDFILE-»
«ENDDEFINE»


module workflow.MyDslGenerator

import org.eclipse.emf.mwe.utils.*

var targetDir = "src-gen"
var fileEncoding = "Cp1252"
var modelPath = "src/model"

Workflow {

	component = org.eclipse.xtext.mwe.Reader {
		// lookup all resources on the classpath
		// useJavaClassPath = true

		// or define search scope explicitly
		path = modelPath

		// this class will be generated by the xtext generator 
		register = org.xtext.example.mydsl.MyDslStandaloneSetup {}
		load = {
			slot = "greetings"
			type = "Greeting"
		}
	}
	
	component = org.eclipse.xtend.XtendComponent {
		metaModel = org.eclipse.xtend.typesystem.emf.EmfRegistryMetaModel{}
		invoke = "templates::Extensions::change(greetings)" 
		outputSlot = "greetings2"
	}

	component = org.eclipse.xpand2.Generator {
		metaModel = org.eclipse.xtend.typesystem.emf.EmfRegistryMetaModel{}
		expand = "templates::Template::main FOREACH greetings2"
		outlet = {
			path = targetDir
		}
		fileEncoding = fileEncoding
	}
}



and it works as expected

~Christian




Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: InstantiationException while assigning vaiables [message #559493 is a reply to message #559485] Thu, 16 September 2010 19:51 Go to previous message
Softwareassi is currently offline SoftwareassiFriend
Messages: 7
Registered: August 2010
Junior Member
Thanks a lot for spending your time on that.

I also tried it with a new project and added my language defintion to that. Now it is working as expected. And I can manipulate my model as I like.

I don't know what is wrong in the other project.

Thanks a lot!
Previous Topic:[XPAND] Problems with getting started with defining extension
Next Topic:how to overwrite workflow properties: headless code generation with mwe2
Goto Forum:
  


Current Time: Thu Mar 28 13:49:14 GMT 2024

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

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

Back to the top