I want to generate EMF models with Java code. For example, I want to create a new Ecore Modeling Project, and the build a simple model as seen as in many Tutorials (i.e. like the vogella tutorial). But I don't want to do that by hand, using the GUI. I want to learn how to create a model with Java code, using the EMF-Ecore-API.
I tried to find tutorials about this topic. But to my disappointment, I couldn't find much about this topic. The only thing I could find was some code snippets to load and modify existing models by code. But nothing about creating new models. And just looking through the API did not help me.
Are there any sources on this topic? And if not, how can I create EMF models by java code?
IBM has a topic about dynamic EMF.
But it is pretty straight forward, if you're familiar with the way creation works in emf. Every EPackage has its own
EFactoryandEPackageInstances, which handle the creation (factory) or store information about the meta model itself (epackage).Ecore has its own
EPackageandEFactoryso it's absolutely possible to create new meta-models on the fly like this:Update:
Now you have your own
EPackagewith a newEClasswhich has anEAttributename of TypeEStringNow it's also possible to save your new EPackage to an
.ecoreFile like this:and vice versa, if you want to load an existing ecore EPackage:
Update: Ecore has of course its own meta-model. In the Docs you find a great overview of the ecore architecture.
So if you want to use dynamic EMF you need to understand this. As I showed you, it is very simple to create an
EPackagedynamically, but you need to know how to set the basic attributes of the ecore model (EClass, EAttributes, EReferences, EType, containment, supertypes ...). Once you understood the architecture, then it is pretty easy. Have a look at the UML-Diagram at the bottom of the page.I also updated the code above to show you how to initialize relations between EClasses