Access to models generated with the Eclipse Modeling Framework is easy in Groovy with the help of Groovy Beans and GPath.

As an example we use the EMF tutorial. This model contains three classes Book, Writer and Library and an enumeration BookCategory. From this model EMF generates Java code. There are two special classes: a package class and a factory class. We need the package class for reading the model. We have to instantiate it and load a file with data as following.

def resource = new XMIResourceImpl(URI.createURI('hardboiled.library'))
Library library = (Library) resource.contents\[0\]        // get the root element

Now we are able to query the model using standard Groovy. For example

for ( book in library.books ) {
    println + ', ' + book.title + ', ' + book.category + ', ' + book.pages 

prints out all books. We can print out all the books with less than 240 pages with the following statement.

println library.books.grep { it.pages < 240 }.title.join(", ")

All the objects in an EMF model are constructed with methods from a factory (LibraryFactory in this example). The Groovy EMF Builder provides an interface for constructing models and model elements. It takes an EMF factory as an argument. In the following snippet three objects are created in the model: a Library, a Writer and a Book.

def builder = new EMFBuilder(LibraryFactory)
def writer
def library = builder.Library( name : 'Hardboiled Library') {
  writers {
    writer = Writer( name : 'Raymond Chandler')     
  books { 
             Book ( title: 'The Big Sleep', pages: 234, category: BookCategory.MYSTERY_LITERAL, author: writer)     

The braces indicate the containment relationships writers and books of the class Library.
See the homepage of the Groovy EMF Builder for further details.