Friday, February 17, 2012

First Eclipse Modeling Framework (EMF) example - 3

In the previous article we've created a domain model for a Library Management System, and we've generated a set of corresponding Java classes, by using Eclipse Modeling Framework (EMF).

Now, we're going to test those classes through a new Eclipse project, which references the one with the generated classes.

So, we now create a new plug-in project:
                                     File --> Create --> New --> Project... --> Plug-in Project.


And, in folder src, we create a new class with a stub for method main:


And we create, in method main(), a few lines of code:

package javaUsingEMFModel.LibrarySystem;

import org.eclipse.emf.ecore.*;
import LibraryModel.*;
import LibraryModel.impl.*;
import LibraryModel.util.*;

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

// Inicialize the LibraryModelPackageImpl
        LibraryModelPackageImpl.init();

        LibraryModelFactory factory = LibraryModelFactory.eINSTANCE;
       
        // Create a new Book
        Book book = factory.createBook();

        // Give a title to the book
        book.setTitle("Os Lusiadas");
       
        // Create an Author, and define his/her name and surname
        Author author1 = factory.createAuthor();
        author1.setName("Luis");
        author1.setSurname("Camoes");
       
        // Add that author as an author of the previously created book
        book.getAuthors().add(author1);
       
       
        System.out.println(book.getTitle());
    }
}

This is only the beginning. Now you should create instances of the remaining classes, inter-relate them, and query its attribute values and the values of the relation attributes.

Related articles:
        - First Eclipse Modeling Framework (EMF) example - 1
        - First Eclipse Modeling Framework (EMF) example - 2




No comments:

Post a Comment