Skip to content

Instantly share code, notes, and snippets.

@acerosalazar
Last active April 27, 2017 21:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save acerosalazar/2e8c5e3ad514934da575 to your computer and use it in GitHub Desktop.
Save acerosalazar/2e8c5e3ad514934da575 to your computer and use it in GitHub Desktop.
A template for creating an Xtend based CodeGenerator using the Ecore implementation of the IFML metamodel and a sample model.
package generator
// This code is based on this stackoverflow answer:http://stackoverflow.com/questions/12458852/load-emf-model-instance-in-xtend
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl
import org.eclipse.emf.ecore.EPackage
class CodeGenerator {
def static void main(String[] args) {
// The model.xmi should be located at the project root.
new CodeGenerator().generate("model.xmi")
}
def generate(String file) {
doEMFSetup
val resourceSet = new ResourceSetImpl
val resource = resourceSet.getResource(URI::createURI(file), true)
for (content : resource.contents) {
// <- Entry Point
}
}
def doEMFSetup() {
// All the EPackages of the metamodel should be registered.
// In this example they are declared as extensions of the IFMLEditor jar [Available at: https://github.com/ifml/ifml-editor]
EPackage$Registry::INSTANCE.put("'http://www.omg.org/spec/20130218/core'", IFML.Core.CorePackage::eINSTANCE)
EPackage$Registry::INSTANCE.put("'http://www.omg.org/spec/20130218/ext'", IFML.Extensions.ExtensionsPackage::eINSTANCE)
EPackage$Registry::INSTANCE.put("'http://www.omg.org/spec/20130218/data'", IFML.DataTypes.DataTypesPackage::eINSTANCE)
Resource.Factory.Registry::INSTANCE.extensionToFactoryMap.put("xmi", new XMIResourceFactoryImpl);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment