Generating an EMF Model

Top    Previous: Prerequisites    Next: Generate the Model Code

Step 1b: Define the Model Using Annotated Java

Instead of importing the model from a Rose diagram, we can start with a set of Java interfaces and classes that correspond to the classes and enumerated types, respectively, in the library model. This code is the bare minimum required to illustrate the desired features. Based on it, an Ecore model and a GenModel will be constructed, which will then drive generation of the remaining code. The code is annotated with "@model" tags in javadoc comments, in order to specify any non-default values for the attributes and references of the Ecore objects.

Library.java
package org.eclipse.example.library;

import java.util.List;

/**
 * @model
 */
public interface Library
{
  /**
   * @model type="Book" containment="true"
   */
  List getBooks();

  /**
   * @model type="Writer" containment="true"
   */
  List getWriters();

  /**
   * @model
   */
  String getName();
}
Book.java
package org.eclipse.example.library;

/**
 * @model
 */
public interface Book
{
  /**
   * @model
   */
  String getTitle();

  /**
   * @model default="100"
   */
  int getPages();

  /**
   * @model opposite="books"
   */
  Writer getAuthor();

  /**
   * @model
   */
  BookCategory getCategory();
}
Writer.java
package org.eclipse.example.library;

/**
 * @model
 */
public interface Writer
{
  /**
   * @model
   */
  String getName();

  /**
   * @model type="Book" opposite="author"
   */
  java.util.List getBooks();
}
BookCategory.java
package org.eclipse.example.library;
/**
 * @model
 */
public class BookCategory
{
  /**
   * @model name="Mystery"
   */
  public static final int MYSTERY = 0;

  /**
   * @model name="ScienceFiction"
   */
  public static final int SCIENCE_FICTION = 1;

  /**
   * @model name="Biography"
   */
  public static final int BIOGRAPHY = 2;
}

Create a new Java project in the workspace:

Create the first Java interface:

Create the other two interfaces and the class in the same way. Of course, to create the class, select "New/Class" from the pop-up menu, instead of "New/Interface".

Create the GenModel:


Top    Previous: Prerequisites    Next: Generate the Model Code