Step 2 :: Model :: Relationships § EReference between Course and Student, New Child → EReference EType of the reference to “Student” upper bound to “-1”, the equivalent of “many”. set the property Containment to “true”
Step 2 :: Model :: Relationships § EReference between Student and Computer, New Child → EReference EType of the reference to “Computer” upper bound to “1” (default) set the property Containment to “false” § Save all
Generator Model § Create a generator model. This allows to configure properties for the code generation that are not part of the model itself. § Generate a maximum of four different plugins for a defined model: 1. Model plugin contains all entities, packages and factories to create instances of the model. 2. Edit plugin contains providers to display a model in a UI. For example, the providers offer a label for every model element, which can be used to display an entity showing an icon and a name. 3. Editor plugin is a generated example editor to create and modify instances of a model. 4. Test plugin contains templates to write tests cases for a model.
Step 3 :: Generate Code § Right click the model folder in the project then § New → Other… → EMF Generator Model → Next § and enter myModel.genmodel as the file name. § Select Ecore model as the model importer. § select Browse Workspace… and select our previously created myModel.ecore
Step 3 :: Generate Code § Based on the generator model, we can now generate the source code. § EMF allows you to generate 4 different plugins. § To generate the plugins, right-click on the root node of the genmodel and select the plugin. For our tutorial, please select “generate all”.
Code § There following is generated § Java interface (Package) § Java implementation class (Package.Impl) § Factory interface and implementation class (Package.Util) Also, § Edit (View) § Editor § Test
Code § These are singletons, to access the instances use § XXXPackage.eINSTANCE § XXXFactory.eINSTANCE § Use the Factory to create instances of your model classes, e.g: TaskList t = ExampleFactory.eINSTANCE.createTaskList(); § Use the Package to access the meta-model definition, e.g: EClass c = ExamplePackage.eINSTANCE.getTaskList(); List attrs = c.getEAttributes();
Why is This Better than Writing POJOs? § plain old Java object (POJO) § We have generated over 1,000 LOC, § Even very simple code is considered to be worth $1 per LOC. So, $1,000 just by clicking some buttons