Pure ecore to Java by Xtend2 [message #701697] |
Mon, 25 July 2011 13:13 |
|
Hi,
a few days ago i started with Xtend2 to generate java code based on a pure ecore file.
I have created different Xtend2 files which are producing java code by Xtend2 richStrings. The Xtend2 files are dealing with the dyanmic emf api to access the required information.
So far everything was really easy. -> Thanks super cool Xtend2!
But right now all outputs are written to stdout console. And that is my problem!
I do not now how i can integrate a pure ecore file (no Xtext grammar exists!) into a mwe2 workflow and how to use the predefined Xtend2 files to generate code to different bundles. I'd like to invoke the generation manually (No auto compile by builders).
After browsing the web I do not have a solution yet!
I found an interesting blog post from Sebastian Zarnekow. His example seams to persist the generated output to a file using the injected extension JavaGeneratorUtils. But i could not find it. (toFileWith('''.....'''))
http://zarnekow.blogspot.com/2011/02/xtend2-whats-in-pipeline.html
Do i have to write my own Generator similar to org.eclipse.xtext.generator.Generator?
As i can remember, Xpand offered an expression called <<FILE ...>> which allowed to specify a target file that was generated inside a java package. Is there a similar access available?
Thanks a lot for your kind help,
Florian Pirchner
|
|
|
Re: Pure ecore to Java by Xtend2 [message #701946 is a reply to message #701697] |
Mon, 25 July 2011 19:10 |
|
Hi,
first two questions: do you generate from .ecore files or you you have a mymm.ecore and instances of it?
In the first case it works out of the box, in the second case you need the generated java classes for your ecore and an iresourceserice provider and it will work with the Xtext Reader and Generator component.
Or you read your models with an std reader componente (the mwe one, not the xtext) and come up with a own generator component. since you use dynamic emf this may be your choice.
The IGenerator that is used by the Generator Component that ships with Xtend has an IFileSytemAccess that allows you to generate files.
~Christian
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
[Updated on: Mon, 25 July 2011 19:13] Report message to a moderator
|
|
|
Re: Pure ecore to Java by Xtend2 [message #701956 is a reply to message #701946] |
Mon, 25 July 2011 19:28 |
|
Hi,
i have an ecore called model.ecore and have generated the java classes.
The input for my generator is the EPackage of my generated ecore (ModelPackage.eINSTANCE). And then i am using the reflective emf api to access the metamodel by the contents of the package and eClasses. But i do not have instances of model.ecore like MyModel.model. I am generating code based on the metamodel.
I think the second choice of your answer would be the perfect match. Are there some examples available how to configure such a workflow? Actually i do not have an idea how to put things together.
I would try to extends the AbstractWorkflowComponent2 and configure it in a mwe2 workflow.
component = SomeReader{
.....
component = MyModelGenerator {
.....
Would this approach be the right way?
Thanks a lot for your answers. Very kind
Florian Pirchner
|
|
|
Re: Pure ecore to Java by Xtend2 [message #701969 is a reply to message #701956] |
Mon, 25 July 2011 19:54 |
|
Hi,
as i said before: this works out of the box
module test.Test
Workflow {
component = org.eclipse.xtext.ecore.EcoreSupport {}
component = org.eclipse.xtext.mwe.Reader {
// lookup all resources on the classpath
// useJavaClassPath = true
// or define search scope explicitly
path = "model"
// this class will be generated by the xtext generator
register = test.TestSetup {}
loadResource = {
slot = "model"
}
}
component = org.eclipse.xtext.generator.GeneratorComponent {
register = test.TestSetup {}
slot = 'model'
outlet = {
path = "src-gen"
}
}
}
package test;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.xtext.ISetup;
import org.eclipse.xtext.service.AbstractGenericModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class TestSetup implements ISetup {
@Override
public Injector createInjectorAndDoEMFRegistration() {
return Guice.createInjector(new AbstractGenericModule(){
public Class<? extends ResourceSet> bindResourceSet() {
return ResourceSetImpl.class;
}
public Class<? extends org.eclipse.xtext.generator.IGenerator> bindIGenerator() {
return test.TestGenerator.class;
}
});
}
}
package test
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IFileSystemAccess
import static extension org.eclipse.xtext.xtend2.lib.ResourceExtensions.*
import org.eclipse.emf.ecore.EPackage
import org.eclipse.emf.ecore.EClass
class TestGenerator implements IGenerator {
override void doGenerate(Resource resource, IFileSystemAccess fsa) {
for(p: resource.allContentsIterable.filter(typeof(EPackage))) {
fsa.generateFile(p.fileName, p.compile)
}
}
def fileName(EPackage p) {
p.name+".txt"
}
def compile(EPackage p) '''
«FOR c : p.EClassifiers.filter(typeof(EClass))»
class «c.name»
«ENDFOR»
'''
}
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: test.emf
Bundle-SymbolicName: test.emf
Bundle-Version: 0.1.0
Require-Bundle: org.eclipse.emf.ecore,
org.eclipse.xtext.xbase.lib;bundle-version="2.0.0",
org.eclipse.xtext.xtend2.lib;bundle-version="2.0.0",
org.eclipse.xtext;bundle-version="2.0.0",
org.eclipse.xtext.ecore;bundle-version="2.0.0",
org.eclipse.emf.mwe2.launch;bundle-version="2.0.0",
org.apache.log4j;bundle-version="1.2.15",
org.apache.commons.logging;bundle-version="1.0.4"
the folder "model" contains the -ecore files to process
~Christian
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
[Updated on: Mon, 25 July 2011 20:11] Report message to a moderator
|
|
|
Re: Pure ecore to Java by Xtend2 [message #701976 is a reply to message #701969] |
Mon, 25 July 2011 20:12 |
|
Hi,
thanks a lot!
For hours I tried different approaches but all ended up in exceptions.
Now i got the missing link. You are using the ISetup to configure the component.
You do not have to write your own generatorComponent since you configure the xtext generator component with you TestGenerator.
Sooo many thanks for you help!
Florian Pirchner
|
|
|
Powered by
FUDForum. Page generated in 0.07923 seconds