Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtend template using a Metamodel (ecore)
Xtend template using a Metamodel (ecore) [message #1771147] Tue, 22 August 2017 13:10 Go to next message
Estibaliz Amparan is currently offline Estibaliz AmparanFriend
Messages: 7
Registered: July 2017
Junior Member
Hello everybody!

I have a Metamodel (ecore) and I want to generate a template implementing a metamodel. I am trying to generate a template using Xtend technology.
The steps I followed are:

1) I create a Xtext project for an existing Ecore.
2) Once I have my grammar created, I do right-click in my xtext file (Run As-> Generate Xtext Artifacts).
3) From the generated folders, I open generator folder and modified the generator.xtend file with the next code:

package matlabcode.template.generator

import hu.bme.mit.massif.simulink.Block
import hu.bme.mit.massif.simulink.SimulinkElement
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.AbstractGenerator
import org.eclipse.xtext.generator.IFileSystemAccess2
import org.eclipse.xtext.generator.IGeneratorContext

class MatlabGenerator extends AbstractGenerator {

	override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
		fsa.generateFile('prueba1.m',MatlabCode)
	}
	
	def MatlabCode() {
		'''
		open_system(mdl_03_08_2017) % Open the behavioural model
		save_system('mdl_03_08_2017','mdl_03_08_2017_Faulty')
		delete_line('mdl_03_08_2017_Faulty','«Block.name»/1','Sub/1')
		'''
	}
	
}


4) After that, I run Eclipse Application. Create -> New Java Project. As I have a model file of the metamodel I added it inside the project.

My problem is that nothing appears, the file does not create.
Do you know what is my problem? Or if you know any tutorial about creating a generator code by using ecore metamodel.
Thank you very much!

[Updated on: Mon, 04 September 2017 10:35]

Report message to a moderator

Re: Xtend template using a Metamodel (ecore) [message #1771183 is a reply to message #1771147] Tue, 22 August 2017 16:55 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11585
Registered: July 2009
Senior Member
i am not sure if i can follow you
do you try this with an text file or a xmi file?


Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: Xtend template using a Metamodel (ecore) [message #1771232 is a reply to message #1771183] Wed, 23 August 2017 07:27 Go to previous messageGo to next message
Estibaliz Amparan is currently offline Estibaliz AmparanFriend
Messages: 7
Registered: July 2017
Junior Member
Sorry, I will try to explain better.
I have understood that xtend technology includes a template language to generate code. I want to create a template file to generate, in my case, matlab code. To create this template I would like to replace dynamic areas adding information from a metamodel instance. I have a metamodel .ecore and its instance .simulink (in my case).

If I follow this tutorial (https://www.eclipse.org/Xtext/documentation/103_domainmodelnextsteps.html) I am able to get the code generated. But now, if I want to generate code with the information of my model it is not possible for me.

Thank you.
Re: Xtend template using a Metamodel (ecore) [message #1771244 is a reply to message #1771232] Wed, 23 August 2017 10:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11585
Registered: July 2009
Senior Member
You need either to provide a iresourceserviceprovider (google should provide some hints on that)
Or you need to callthe transformation manually e.g. Via command


Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: Xtend template using a Metamodel (ecore) [message #1771861 is a reply to message #1771244] Thu, 31 August 2017 13:22 Go to previous messageGo to next message
Estibaliz Amparan is currently offline Estibaliz AmparanFriend
Messages: 7
Registered: July 2017
Junior Member
Hi Christian,
I after some days of research I found this post https://christiandietrich.wordpress.com/2011/07/29/xtend2-code-generators-with-non-xtext-models/. I figured out it was your blog.

This is what I want to reach. I followed your example but I have some errors.

I have my EMF metamodel:
metamodel.png
Also I created edit and editor projects:
edit-editor.png

Then, I created its model (.simulink) extension.

After that I created a new Plugin Project and a new Xtend class called "MaltabcodeGenerator.xtend" with the following code:


package matlab.code.generator.xtend

import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtext.generator.IFileSystemAccess
import org.eclipse.emf.ecore.EObject
import hu.bme.mit.massif.simulink.SimulinkModel

class MatlabcodeGenerator implements IGenerator {
 
    override void doGenerate(Resource resource, IFileSystemAccess fsa) {
        for (EObject o : resource.contents) {
            o.compile(fsa)
        }
    }
 
    def dispatch void compile(SimulinkModel m, IFileSystemAccess fsa) {
                   fsa.generateFile("Code"+".m", '''
        this is the matlabcode        ''')
    }
 
    def dispatch void compile(EObject m, IFileSystemAccess fsa) { }
 
}


Next, I created "MatlabcodeGeneratorModule", "MatlabcodeGeneratorSetup" and "MatlabcodeGeneratorSupport" java files.

"MatlabcodeGeneratorModule.java":
package matlab.code.generator.xtend;

import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.xtext.generator.IGenerator;
import org.eclipse.xtext.resource.generic.AbstractGenericResourceRuntimeModule;

import matlab.code.generator.xtend.MatlabcodeGenerator;

public class MatlabcodeGeneratorModule extends AbstractGenericResourceRuntimeModule {

		@Override
		protected String getLanguageName(){
			return "hu.bme.mit.massif.simulink.presentation.SimulinkEditor";
		}
		
		@Override
		protected String getFileExtensions(){
			return "simulink";// 
		}
		
	    public Class<? extends IGenerator> bindIGenerator() {
	        return MatlabcodeGenerator.class;
	    }
		
		public Class<? extends ResourceSet> bindResourceSet(){
			return ResourceSetImpl.class;
		}
}


"MatlabcodeGeneratorSetup.java"
package matlab.code.generator.xtend;


import org.eclipse.xtext.ISetup;

import com.google.inject.Guice;
import com.google.inject.Injector;

public class MatlabcodeGeneratorSetup implements ISetup{

	@Override
	public Injector createInjectorAndDoEMFRegistration(){
		return Guice.createInjector(new MatlabcodeGeneratorModule());
	}
}


"MatlabcodeGeneratorSupport.java"
package matlab.code.generator.xtend;

import org.eclipse.xtext.resource.generic.AbstractGenericResourceSupport;
import com.google.inject.Module;

public class MatlabcodeGeneratorSupport extends AbstractGenericResourceSupport {

	@Override
	protected Module createGuiceModule() {
		return new MatlabcodeGeneratorModule();
	}

}


As last step, I created the workflow mwe file.
""MatlabcodeGenerator.mwe2"
module matlab.code.generator.xtend.MatlabcodeGenerator

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

var targetDir = "code-generation"
var modelPath = "model"
 
Workflow {
 
    bean = StandaloneSetup {
        registerGeneratedEPackage = "hu.bme.mit.massif.simulink.SimulinkPackage"
       
    }
 
    component = DirectoryCleaner {
        directory = targetDir
    }
 
    component = matlab.code.generator.xtend.MatlabcodeGeneratorSupport {}
 
    component = org.eclipse.xtext.mwe.Reader {
        path = modelPath
        register = matlab.code.generator.xtend.MatlabcodeGeneratorSetup {}
        loadResource = {
            slot = "code-generation"
        }
    }
 
    component = org.eclipse.xtext.generator.GeneratorComponent {
        register = matlab.code.generator.xtend.MatlabcodeGeneratorSetup {}
        slot = 'model'
        outlet = {
            path = targetDir
        }
    }
}


This is my workspace view:
workspace.png

Until all these steps everything work fine. Now when I run the workflow I get these errors:
errors.png

Can you please help me?
Thank you very much!
Re: Xtend template using a Metamodel (ecore) [message #1771862 is a reply to message #1771861] Thu, 31 August 2017 13:25 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11585
Registered: July 2009
Senior Member
lloks like something is broken with your xmi

Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: Xtend template using a Metamodel (ecore) [message #1771863 is a reply to message #1771862] Thu, 31 August 2017 13:28 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11585
Registered: July 2009
Senior Member
maybe you can turn off the validator in the wf

component = org.eclipse.xtext.mwe.Reader {
validate = org.eclipse.xtext.mwe.Validator.Disabled {}
.....
}

any this dynamic stuff makes me wonder. dont you have a genmodel/java classes generated?


Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: Xtend template using a Metamodel (ecore) [message #1772019 is a reply to message #1771863] Mon, 04 September 2017 07:10 Go to previous messageGo to next message
Estibaliz Amparan is currently offline Estibaliz AmparanFriend
Messages: 7
Registered: July 2017
Junior Member
Hi Christian!

A quick question. In the workflow file, what does the slot parameter have to do?

component = org.eclipse.xtext.mwe.Reader {
        path = modelPath
        register = matlab.code.generator.xtend.MatlabcodeGeneratorSetup {}
        validate = org.eclipse.xtext.mwe.Validator.Disabled {}
        loadResource = {
           slot = "code-generation"
        }

I understand here, it is calling to a folder where the generated file will be saved.

But, in this second part, I am not sure what I should to write.

 component = org.eclipse.xtext.generator.GeneratorComponent {
        register = matlab.code.generator.xtend.MatlabcodeGeneratorSetup {}
        slot = 'model'
        outlet = {
            path = targetDir
        }


Now, when I run the workflow, I get this error:

ERROR mf.mwe2.launch.runtime.Mwe2Launcher - Problems running workflow matlab.code.generator.xtend.MatlabcodeGenerator: Slot 'model' was empty!
java.lang.RuntimeException: Problems running workflow matlab.code.generator.xtend.MatlabcodeGenerator: Slot 'model' was empty!
at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:104)
at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:62)
at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:52)
at org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher.run(Mwe2Launcher.java:78)
at org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher.main(Mwe2Launcher.java:36)
Caused by: java.lang.IllegalStateException: Slot 'model' was empty!
at org.eclipse.xtext.generator.GeneratorComponent.invoke(GeneratorComponent.java:123)
at org.eclipse.emf.mwe2.runtime.workflow.AbstractCompositeWorkflowComponent.invoke(AbstractCompositeWorkflowComponent.java:35)
at org.eclipse.emf.mwe2.runtime.workflow.Workflow.run(Workflow.java:19)
at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:102)
... 4 more



Thanks for your help!
Re: Xtend template using a Metamodel (ecore) [message #1772029 is a reply to message #1772019] Mon, 04 September 2017 09:53 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11585
Registered: July 2009
Senior Member
slot is basically a map to pass stuff between components

slot = 'model'

!=

slot = "code-generation"


Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: Xtend template using a Metamodel (ecore) [message #1772038 is a reply to message #1772029] Mon, 04 September 2017 12:25 Go to previous message
Estibaliz Amparan is currently offline Estibaliz AmparanFriend
Messages: 7
Registered: July 2017
Junior Member
Hi Christian,

I changed it and the error has removed. Also, there was something wrong in my xtend file, but I fix it and it works!

Thanks a lot! bye!
Previous Topic:Error When Using Import and Mixin
Next Topic:grammar modularization within one project
Goto Forum:
  


Current Time: Wed Jan 17 07:41:40 GMT 2018

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

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