Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load EMF Model instance in XTend

I am building a code generator in XTend where I already have an input model and meta model. That is, I use ATL to generate the input model for my XTend code generator (as part of a transformation sequence to gradually lower the abstraction level, instead of at once; this is the reason i'm not using xtext to create the syntax).

So to be very clear, my input model for the code generator is a file in XMI format and NOT in the grammar of the xtext project (not even using that)! And i think this is causing me problems/confusion.

I created a new XText project using Existing models, right clicked on the .text file, run as , generate artefacts, and then i did the same for the mwe2 file.

What is the next step, am I doing it right? How can I start my code generator? All the examples are from the POV that you use XText to create a DSL. I have an EMF meta model, and an XMI based instance of that. How to process that further using XTend?

Any hint or pointer to a tutorial is helpful.

Solution:

The solution was as Sven suggested in my accepted answer, but also I would like to note that you need to use a genmodel to generate Java artifacts from your meta model. This link shows how: http://www.vogella.com/articles/EclipseEMF/article.html , see section 4. This may appear all too logical, but i think it's worth noting anyway.

like image 657
Marten Sytema Avatar asked Sep 17 '12 12:09

Marten Sytema


People also ask

How to add EMF to Eclipse?

Install EMF via the Help Install New Software… ​ menu entry in Eclipse. Select Modeling and install EMF - Eclipse Modeling Framework SDK and the Ecore Diagram Editor (SDK). The second entry allows you to create models.


1 Answers

If you have an XMI and just want to generate code from it, you don't need Xtext at all. Just start with a Java project (I'd use a plug-in project, to reuse the dependency management) and start coding:

import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.EPackage
import org.eclipse.emf.ecore.resource.Resource$Factory$Registry
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl

class MyCodeGenerator {

  def static void main(String[] args) {
    new MyCodeGenerator().generate("mymodel.xmi")
  }

  def generate(String file) {
    doEMFSetup
    val resourceSet = new ResourceSetImpl
    val resource = resourceSet.getResource(URI.createURI(file), true)
    for (content : resource.contents) {
      generateCode(content)
    }
  }

  def dispatch generateCode(MySpecialType it) '''
    public class «name» {
      «FOR member : members»
      «ENDFOR»
    }
  '''

  def dispatch generateCode(MyMember it) '''
    private «type» «name»;
    ...
  '''

  def doEMFSetup() {
//    EPackage$Registry.INSTANCE.put(MyPackage.eINSTANCE.nsURI, MyPackage.eINSTANCE)
    Resource$Factory.Registry.INSTANCE.extensionToFactoryMap.put("xmi", new XMIResourceFactoryImpl);
  }

}

The dependencies you need to add to your Manifest :

Require-Bundle: org.eclipse.xtend.lib,
 com.google.guava,
 org.eclipse.xtext.xbase.lib,
 org.eclipse.emf.common,
 org.eclipse.emf.ecore,
 org.eclipse.emf.ecore.xmi
like image 56
Sven Efftinge Avatar answered Oct 09 '22 19:10

Sven Efftinge