Copyright © 2001 International Business Machines Corp.
 Eclipse Corner Article

 

Creating an EMF Ecore model with inheritance

Summary
This tutorial is a companion to Generating an Extended EMF Model, in which a library model is generated using *mdl imports from Rose or using Annotated Java. In this tutorial, we create the same schoollibrary, but we use EMF Class Diagrams which we construct with EclipseUML.
 If you are interested, here is the way how to do it.

By Harry van Rijn
Januari 13, 2005


 

Overview
First create some EMF models with the EclipseUML. These first models contain base classes.
Then create another EMF model within which classes that inherite from the base classes.
Generate all your java code, and test it on properly working.


Contents

Create EMF model for library
Create genmodel for library
Create EMF model for schoollibrary
Create genmodel for schoollibrary
Generate the EMF Model and Editor Code


 contents

Create EMF model for library

Overview

  • Create a java project (i.e. library) with a src directory.
  • Add a package (what.ever.youlike.library or a.b.c.library).
  • Add a new EMF Class Diagram.
  • Define the Enumeration (BookCategory) and the base Classes (Library, Writer, Book) in the Diagram.
  • Create the associations
  • Save All.

  • Create a java project (i.e. library) with a src directory.


    Add a package (what.ever.youlike.library or a.b.c.library).


    Add a new EMF Class Diagram.


    Define an Enumeration named BookCategory with the Literals:
    Mystery, ScienceFiction and Biography.

    Add attributes to the classes:

    ClassAttributeTypeValue
    LibrarynameEString 
    WriternameEString 
    BooktitleEString 
     pagesEInt100
     categoryBookCategory 

    Create the associations


    Save All.

    You should have now library.ecd and library.ecore in your package:



     contents

    Create genmodel for library

    Overview

  • Create an EMF project (i.e. base_library) based on EMF ecore files.
  • Browse to the ecore file (library.ecore), select it, select the library package, finish.
  • This results in library.genmodel (and a library.ecore duplicate).

  • Create an EMF project (i.e. base_library) . . .


    . . . based on EMF ecore files.


    Browse to the ecore file (library.ecore), select it, next,


    select the library package, finish.


    This results in library.genmodel (and a library.ecore duplicate).



     contents

    Create EMF model for schoollibrary

    Overview

  • Create a java project (i.e. schoollibrary) with a src directory.
  • Add a package (what.ever.youlike.schoollibrary or x.y.z.schoollibrary).
  • Add a new EMF Class Diagram.
  • Define the Classes (Schoollibrary, Schoolbook and Asset) in the Diagram.
  • Save All.

  • Create a java project (i.e. schoollibrary) with a src directory. See step 1

    Add a package (what.ever.youlike.schoollibrary or x.y.z.schoollibrary). See step 1


    Add a new EMF Class Diagram.


    You now have schoollibrary.ecore and schoollibrary.ecd files.


    Define the Classes (SchoolLibrary, SchoolBook and Asset) in the Diagram.


    Now, open the schoollibrary.ecore file, you will get a resource pane.


    In order to add another resource to this pane, right-click on the pane and select Load Resource...


    Browse in your workspace to an library.ecore file, there are 2 of those. Pick the one in the base_library project.


    Now we are on the way to let our SchoolLibrary Class derive from the Library Class

    So unfold the schoollibrary.ecore in the resource pane, and select the SchoolLibray Class

    In the properties pane of the SchoolLibrary you select the far right square button.


    Which show you this popup pane. Select Library and click on Add, then Ok.


    Save All of the schoollibrary project

    Now SchoolLibrary inherits from Library. You schoollibrary.ecd file should be updated.

    You can see an indication of inheritance in the schoollibrary EMF diagram, when you select the SchoolLibrary Class, a solid black arrowhead should be visible.


    The same procedure of selecting a supertype, you can repeat for the SchoolBook Class.


    You will perceive, that Asset is allready there, so you just add the Book Class to get this:



     contents

    Create genmodel for schoollibrary

    Overview

  • Create an EMF project (i.e. base_schoollibrary) based on EMF ecore files.
  • Browse to the ecore file (schoollibrary.ecore), select it, select the schoollibrary package, finish.
  • This results in schoollibrary.genmodel (and a schoollibrary.ecore duplicate).

  • Create an EMF project (i.e. base_schoollibrary) based on EMF ecore files.


    Browse to the ecore file (schoollibrary.ecore), select it, click next.


    After browsing to the library.genmodel file in your workspace, in the next pane appears this:


    From the Root packages, you select the schoollibrary package, and from the Referenced generator models you select the library package (the lower one). A warning may disappear, and a corresponding library package in the Root packages will also disappear, click finish.


    This results in schoollibrary.genmodel (and a schoollibrary.ecore duplicate).

    The tiny arrow in the library package guarantees you that this is inherited from.



     contents

    Generate the EMF Model and Editor Code

    Well now we are at the same point, as when we would have use a *mdl import from Rose.
    So for the rest of this tutorial, I refer to Generating an Extended EMF Model
    Both the library.genmodel must be treated with a Generate All, as the schoollibrary.genmodel. Otherwise you get unresolved stuff.


    Author: Harry van Rijn