Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Using Xtext and Code Generator without Runtime(Using Xtext and Code Generator without Runtime)
Using Xtext and Code Generator without Runtime [message #1478261] Tue, 18 November 2014 14:33 Go to next message
Parisa Moosavi is currently offline Parisa MoosaviFriend
Messages: 16
Registered: November 2014
Location: Fulda
Junior Member

Hi everybody,

I have developed a dsl and a generator for this dsl to generate some java files.
Now I want to use this generator to generate the java files but according a special file which must be an input and it must be available without Eclipse Runtime Version or Workbench.

Bests,
Parisa


Bests,
Parisa
Re: Using Xtext and Code Generator without Runtime [message #1478298 is a reply to message #1478261] Tue, 18 November 2014 15:11 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

Create a main class that reads your DSL files and invoke the generator. Then create a launch configuration. With the export wizard you can generate an executable Jar.

Alternatively provide a MWE2 workflow that does the process mentioned above. Then create a launch config for class org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher. Proceed like above.

Regards,
~Karsten


Need professional support for Xtext, EMF, Eclipse IDE?
Go to: http://devhub.karakun.com
Twitter : @kthoms
Blog : www.karsten-thoms.de
Re: Using Xtext and Code Generator without Runtime [message #1478299 is a reply to message #1478261] Tue, 18 November 2014 15:11 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

(1) adapt the generatorFragment in the workflow to create a java main

fragment = generator.GeneratorFragment auto-inject {
    			generateJavaMain = true
    		}

(2) regenerate the language. you find now a java main generated for the generator (you may use this one or adapt it for your needs)
(3) run the main once in eclipse
(4) use the export -> java -> runnable jar file wizard to create a all in one jar


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using Xtext and Code Generator without Runtime [message #1479208 is a reply to message #1478299] Wed, 19 November 2014 08:44 Go to previous messageGo to next message
Parisa Moosavi is currently offline Parisa MoosaviFriend
Messages: 16
Registered: November 2014
Location: Fulda
Junior Member

Hi,

Thanks a lot for your help. I have done till step 3. How can I run main? it needs a EMF Resource. What should I sent to this method as parameter?

bests,
Parisa


Bests,
Parisa
Re: Using Xtext and Code Generator without Runtime [message #1479209 is a reply to message #1479208] Wed, 19 November 2014 08:45 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
hi the parameter is the model file to process

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using Xtext and Code Generator without Runtime [message #1479223 is a reply to message #1479209] Wed, 19 November 2014 08:59 Go to previous messageGo to next message
Parisa Moosavi is currently offline Parisa MoosaviFriend
Messages: 16
Registered: November 2014
Location: Fulda
Junior Member

Actually I have changed the main method like following:
public static void main(String[] args) {
		String s="C:\\ ... \\org.xtext.example.pro1\\pro1\\pro1.pro1";
		//if (args.length==0) {
		//	System.err.println("Aborting: no path to EMF resource provided!");
		//	return;
		//}
		Injector injector = new org.xtext.example.mydsl.Pro1StandaloneSetup().createInjectorAndDoEMFRegistration();
		Main main = injector.getInstance(Main.class);
		
		main.runGenerator(s);
	}

but I get this error:
Exception in thread "main" java.lang.RuntimeException: Cannot create a resource for 'c:\ ... \org.xtext.example.pro1\pro1\pro1.pro1'; a registered resource factory is needed
	at org.eclipse.xtext.resource.XtextResourceSet.getResource(XtextResourceSet.java:198)


and pro1.pro1 ist the file which I have created in Runtime Workbench.


Bests,
Parisa
Re: Using Xtext and Code Generator without Runtime [message #1479232 is a reply to message #1479223] Wed, 19 November 2014 09:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
hi,

are you sure the file extension of your dsl is pro1?
and the uri should be file:///c:.... i think


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using Xtext and Code Generator without Runtime [message #1479246 is a reply to message #1479232] Wed, 19 November 2014 09:24 Go to previous messageGo to next message
Parisa Moosavi is currently offline Parisa MoosaviFriend
Messages: 16
Registered: November 2014
Location: Fulda
Junior Member

Hi,

Yes I have generated pro1.pro1 file myself and I generated all java files which I need with this Model and my generator (But all of them in runtime).

I have only copied pro1.pro1 from my runtime workbench and pased it in a folder in my xtext project because I want to run it without runtime.

Want I want to have is following:
I want to do a Rightclick on a xml file in my project and select an option for example "Generate File".
and then the dsl that I have developed can make a model from this xml file and then generate all the java files that I need. Smile


Bests,
Parisa
Re: Using Xtext and Code Generator without Runtime [message #1479270 is a reply to message #1479246] Wed, 19 November 2014 09:45 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
sorry but that should work. can you try this with a brand new mydsl project? the Pro1StandaloneSetup should have registered the resource factory.
can you post the content of the generated base class of Pro1StandaloneSetup


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using Xtext and Code Generator without Runtime [message #1479275 is a reply to message #1479270] Wed, 19 November 2014 09:50 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
hi,

are this righclick thing will not work this way in eclipse. be aware of that


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using Xtext and Code Generator without Runtime [message #1479284 is a reply to message #1479275] Wed, 19 November 2014 10:00 Go to previous messageGo to next message
Parisa Moosavi is currently offline Parisa MoosaviFriend
Messages: 16
Registered: November 2014
Location: Fulda
Junior Member

This is my Pro1StandaloneSetup.java
/*
* generated by Xtext
*/
package org.xtext.example.mydsl;

/**
 * Initialization support for running Xtext languages 
 * without equinox extension registry
 */
public class Pro1StandaloneSetup extends Pro1StandaloneSetupGenerated{

	public static void doSetup() {
		new Pro1StandaloneSetup().createInjectorAndDoEMFRegistration();
	}
}




Bests,
Parisa
Re: Using Xtext and Code Generator without Runtime [message #1479314 is a reply to message #1479284] Wed, 19 November 2014 10:34 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
i actually ment Pro1StandaloneSetupGenerated

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using Xtext and Code Generator without Runtime [message #1479356 is a reply to message #1479314] Wed, 19 November 2014 11:12 Go to previous messageGo to next message
Parisa Moosavi is currently offline Parisa MoosaviFriend
Messages: 16
Registered: November 2014
Location: Fulda
Junior Member

/*
* generated by Xtext
*/
package org.xtext.example.mydsl;

import org.eclipse.emf.ecore.EPackage;
import org.eclipse.xtext.ISetup;
import org.eclipse.emf.ecore.resource.Resource;

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

/**
 * Generated from StandaloneSetup.xpt!
 */
@SuppressWarnings("all")
public class Pro1StandaloneSetupGenerated implements ISetup {

	public Injector createInjectorAndDoEMFRegistration() {
		org.eclipse.xtext.common.TerminalsStandaloneSetup.doSetup();

		Injector injector = createInjector();
		register(injector);
		return injector;
	}
	
	public Injector createInjector() {
		return Guice.createInjector(new org.xtext.example.mydsl.Pro1RuntimeModule());
	}
	
	public void register(Injector injector) {
	if (!EPackage.Registry.INSTANCE.containsKey("http://www.xtext.org/example/mydsl/Pro1")) {
		EPackage.Registry.INSTANCE.put("http://www.xtext.org/example/mydsl/Pro1", org.xtext.example.mydsl.pro1.Pro1Package.eINSTANCE);
	}

		org.eclipse.xtext.resource.IResourceFactory resourceFactory = injector.getInstance(org.eclipse.xtext.resource.IResourceFactory.class);
		org.eclipse.xtext.resource.IResourceServiceProvider serviceProvider = injector.getInstance(org.eclipse.xtext.resource.IResourceServiceProvider.class);
		Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("pro1", resourceFactory);
		org.eclipse.xtext.resource.IResourceServiceProvider.Registry.INSTANCE.getExtensionToFactoryMap().put("pro1", serviceProvider);
		


	}
}




Bests,
Parisa
Re: Using Xtext and Code Generator without Runtime [message #1479368 is a reply to message #1479356] Wed, 19 November 2014 11:21 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
hi having a look at this i have no idea why that doesnt work.
did you try it with a brand new mydsl project?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using Xtext and Code Generator without Runtime [message #1479402 is a reply to message #1479368] Wed, 19 November 2014 11:57 Go to previous messageGo to next message
Parisa Moosavi is currently offline Parisa MoosaviFriend
Messages: 16
Registered: November 2014
Location: Fulda
Junior Member

Hi,
I have tried it with mydsl but I have still the error
Exception in thread "main" java.lang.RuntimeException: Cannot create a resource for 'c:\ ... \org.xtext.example.mydsl\dslmodel\sample.mydsl'; a registered resource factory is needed
	at org.eclipse.xtext.resource.XtextResourceSet.getResource(XtextResourceSet.java:198)
	at org.eclipse.xtext.resource.SynchronizedXtextResourceSet.getResource(SynchronizedXtextResourceSet.java:26)
	at org.xtext.example.mydsl.generator.Main.runGenerator(Main.java:50)
	at org.xtext.example.mydsl.generator.Main.main(Main.java:32)


Bests,
Parisa
Re: Using Xtext and Code Generator without Runtime [message #1479408 is a reply to message #1479402] Wed, 19 November 2014 12:02 Go to previous messageGo to next message
Parisa Moosavi is currently offline Parisa MoosaviFriend
Messages: 16
Registered: November 2014
Location: Fulda
Junior Member

Parisa Moosavi wrote on Wed, 19 November 2014 12:57
Hi,
I have tried it with mydsl but I have still the error
Exception in thread "main" java.lang.RuntimeException: Cannot create a resource for 'c:\ ... \org.xtext.example.mydsl\dslmodel\sample.mydsl'; a registered resource factory is needed
	at org.eclipse.xtext.resource.XtextResourceSet.getResource(XtextResourceSet.java:198)
	at org.eclipse.xtext.resource.SynchronizedXtextResourceSet.getResource(SynchronizedXtextResourceSet.java:26)
	at org.xtext.example.mydsl.generator.Main.runGenerator(Main.java:50)
	at org.xtext.example.mydsl.generator.Main.main(Main.java:32)

  • Attachment: 1.png
    (Size: 26.81KB, Downloaded 123 times)


Bests,
Parisa
Re: Using Xtext and Code Generator without Runtime [message #1479409 is a reply to message #1479408] Wed, 19 November 2014 12:04 Go to previous messageGo to next message
Parisa Moosavi is currently offline Parisa MoosaviFriend
Messages: 16
Registered: November 2014
Location: Fulda
Junior Member

I have send a picture that shows my new mydsl project.
What is src-gen ?
My model is in dslmodel folder.


Bests,
Parisa
Re: Using Xtext and Code Generator without Runtime [message #1479444 is a reply to message #1479409] Wed, 19 November 2014 12:37 Go to previous messageGo to next message
Parisa Moosavi is currently offline Parisa MoosaviFriend
Messages: 16
Registered: November 2014
Location: Fulda
Junior Member

How can I register a resource factory ?

Bests,
Parisa
Re: Using Xtext and Code Generator without Runtime [message #1479465 is a reply to message #1479444] Wed, 19 November 2014 12:57 Go to previous messageGo to next message
Parisa Moosavi is currently offline Parisa MoosaviFriend
Messages: 16
Registered: November 2014
Location: Fulda
Junior Member

I have find it out if I paste my pro1.pro1 or sample.mydsl in the root directory of the project then main method can work fine.

Bests,
Parisa
Re: Using Xtext and Code Generator without Runtime [message #1479470 is a reply to message #1479465] Wed, 19 November 2014 13:03 Go to previous messageGo to next message
Parisa Moosavi is currently offline Parisa MoosaviFriend
Messages: 16
Registered: November 2014
Location: Fulda
Junior Member

but I can not see the generated java files ?

Bests,
Parisa
Re: Using Xtext and Code Generator without Runtime [message #1479471 is a reply to message #1479470] Wed, 19 November 2014 13:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
where did you look for? did you debug your IGenerator?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using Xtext and Code Generator without Runtime [message #1479486 is a reply to message #1479471] Wed, 19 November 2014 13:16 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
p.s. it by default generates to src-gen as it looks

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using Xtext and Code Generator without Runtime [message #1479540 is a reply to message #1479486] Wed, 19 November 2014 14:13 Go to previous messageGo to next message
Parisa Moosavi is currently offline Parisa MoosaviFriend
Messages: 16
Registered: November 2014
Location: Fulda
Junior Member

but they are not my java classes , only dsl classes.

Bests,
Parisa
Re: Using Xtext and Code Generator without Runtime [message #1479541 is a reply to message #1479540] Wed, 19 November 2014 14:14 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
sorry without any reproducable example i cannot help.
did you debug your igenerator as i suggested?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using Xtext and Code Generator without Runtime [message #1479608 is a reply to message #1479541] Wed, 19 November 2014 15:26 Go to previous messageGo to next message
Parisa Moosavi is currently offline Parisa MoosaviFriend
Messages: 16
Registered: November 2014
Location: Fulda
Junior Member

Yes, I have done the four steps wich you suggested. I have called the mydsl.jar file and sent the name of sample.mydsl file as argument.
But nothing happened.


Bests,
Parisa
Re: Using Xtext and Code Generator without Runtime [message #1479619 is a reply to message #1479608] Wed, 19 November 2014 15:37 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

as i cannot reproduce this with the xtext hello world example i cannot tell what goes wrong in your example.
either you share your complete code or have to debug the problem yourself.
try it with the hello world example yourself. then have a look what is different in your case.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using Xtext and Code Generator without Runtime [message #1480495 is a reply to message #1479619] Thu, 20 November 2014 08:21 Go to previous messageGo to next message
Parisa Moosavi is currently offline Parisa MoosaviFriend
Messages: 16
Registered: November 2014
Location: Fulda
Junior Member

Hi,

Thanks a lot. I can now generate what I need without runtime. Smile
Is there a Handbook about Xtext?
For example for what I wanted to do, I thought that I must write a lot to do it but with your suggestion I have done it very easier than I thought.

Where I can find this documentations?




Bests,
Parisa
Re: Using Xtext and Code Generator without Runtime [message #1480505 is a reply to message #1480495] Thu, 20 November 2014 08:31 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

i am not sure what you are talking about.
there is http://www.eclipse.org/Xtext/documentation.html
https://wiki.eclipse.org/Xtext/FAQ
a tons of blogs
and Lorenzo Bettinis Book "Implementing Domain-Specific Languages with Xtext and Xtend"
but i dont know if they cover this topic

in general for advanced stuff you have to look at the code


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:How to trigger Java code generation from Xtext generated Xtend sources
Next Topic:Removing unnecessary files in ProjectCreator
Goto Forum:
  


Current Time: Tue Apr 16 21:32:32 GMT 2024

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

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

Back to the top