06. Using the model code

6. Using the model code #

See the YouTube playlist for all videos.


6.1. Overview #

The generated model code is standard Java code and can be used as such. The following demonstrates how you create objects based on the generated code.

6.2. Example #

Create a new plug-in project called de.vogella.emf.webpage.usingmodel. Add the following dependency to your MANIFEST.MF (under Dependencies click Add).

  • org.eclipse.emf.ecore
  • com.vogella.emf.webpage.model

Create the following class UsingEMFModel in package de.vogella.emf.webpage.usingmodel.

package de.vogella.emf.webpage.usingmodel;

import de.vogella.emf.webpage.model.webpage.Web;
import de.vogella.emf.webpage.model.webpage.Webpage;
import de.vogella.emf.webpage.model.webpage.WebpageFactory;

public class UsingEMFModel {
  public static void main(String[] args) {

    // Retrieve the default factory singleton
    WebpageFactory factory = WebpageFactory eINSTANCE

    // create an instance of myWeb
    Web myWeb = factory.createWeb();
    myWeb.setName("Hallo");
    myWeb.setTitle("This is the Web");
    myWeb.setKeywords("web, the");

    // create a page
    Webpage webpage = factory.createWebpage();
    webpage.setTitle("This is a title");

    // add the page to myWeb
    myWeb.getPages().add(webpage);

    // and so on, and so on
    // as you can see the EMF model can be (more or less) used as standard Java
    System.out.println(myWeb.toString());
  }
}