...
Access
...
to
...
models
...
generated
...
with
...
the
...
...
...
...
is
...
easy
...
in
...
Groovy
...
with
...
the
...
help
...
of
...
Groovy
...
Beans
...
and
...
GPath.
...
As
...
an
...
example
...
we
...
use
...
the
...
...
...
.
...
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.
| Code Block |
|---|
LibraryPackage.eINSTANCE def resource = new XMIResourceImpl(URI.createURI('hardboiled.library')) resource.load(null) Library library = (Library) resource.contents\[0\] // get the root element |
Now
...
we
...
are
...
able
...
to
...
query
...
the
...
model
...
using
...
standard
...
Groovy.
...
For
...
example
| Code Block |
|---|
for ( book in library.books ) { println book.author.name + ', ' + book.title + ', ' + book.category + ', ' + book.pages } |
prints
...
out
...
all
...
books.
...
We
...
can
...
...
out
...
all
...
the
...
books
...
with
...
less
...
than
...
240
...
pages
...
with
...
the
...
following
...
statement.
| Code Block |
|---|
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
...
...
...
...
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.
| Code Block |
|---|
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
...
...
...
...
for
...
further
...
details.
...