| 1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| 2 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| 4 |
<head> |
| 5 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> |
| 6 |
<link rel="stylesheet" href="images/../../../css/book.css" type="text/css"/> |
| 7 |
<link rel="stylesheet" href="images/../../../css/emf-book.css" type="text/css"/> |
| 8 |
<title>EMF Overview</title> |
| 9 |
</head> |
| 10 |
<body lang="EN-US" xml:lang="EN-US"> |
| 11 |
<h1>The Eclipse Modeling Framework (EMF) Overview</h1> |
| 12 |
<p> |
| 13 |
Last updated: June 16, 2005 (accessibility update) |
| 14 |
</p> |
| 15 |
<p> |
| 16 |
This paper presents a basic overview of EMF and its code generator patterns. For |
| 17 |
a more complete description of all the features of EMF, refer to |
| 18 |
<a href="https://www.informit.com/title/9780321331885" target="_blank">EMF: Eclipse Modeling |
| 19 |
Framework, Second Edition</a> (Addison-Wesley Professional, 2008) or to the |
| 20 |
Javadoc for the framework classes themselves. |
| 21 |
</p> |
| 22 |
|
| 23 |
<h2>Contents</h2> |
| 24 |
<p><a href="#introduction">Introduction</a><br/> |
| 25 |
<a href="#definition">Defining an EMF Model</a><br/> |
| 26 |
<a href="#generation">Generating a Java Implementation</a><br/> |
| 27 |
<a href="#programming">Using the Generated EMF Classes</a><br/> |
| 28 |
<a href="#advanced">Advanced Topics</a></p> |
| 29 |
|
| 30 |
<h2><a name="introduction">Introduction</a></h2> |
| 31 |
<p> |
| 32 |
EMF is a Java framework and code generation facility for building tools and |
| 33 |
other applications based on a structured model. For those of you that have |
| 34 |
bought into the idea of object-oriented modeling, EMF helps you rapidly turn |
| 35 |
your models into efficient, correct, and easily customizable Java code. For |
| 36 |
those of you that aren't necessarily sold on the value of formal models, EMF is |
| 37 |
intended to provide you with the same benefits and a very low cost of entry. |
| 38 |
</p> |
| 39 |
<p> |
| 40 |
So, what do we mean when we say model? When talking about modeling, we generally |
| 41 |
think about things like Class Diagrams, Collaboration Diagrams, State Diagrams, |
| 42 |
and so on. UML (Unified Modeling Language) defines a (the) standard notation for |
| 43 |
these kinds of diagrams. Using a combination of UML diagrams, a complete model |
| 44 |
of an application can be specified. This model may be used purely for |
| 45 |
documentation or, given appropriate tools, it can be used as the input from |
| 46 |
which to generate part of or, in simple cases, all of an application. |
| 47 |
</p> |
| 48 |
<p> |
| 49 |
Given that this kind of modeling typically requires expensive Object Oriented |
| 50 |
Analysis and Design (OOA/D) tools, you might be questioning our assertion, |
| 51 |
above, that EMF provides a low cost of entry. The reason we can say that is |
| 52 |
because an EMF model requires just a small subset of the kinds of things that |
| 53 |
you can model in UML, specifically simple definitions of the classes and their |
| 54 |
attributes and relations, for which a full-scale graphical modeling tool is |
| 55 |
unnecessary. |
| 56 |
</p> |
| 57 |
<p> |
| 58 |
While EMF uses XMI (XML Metadata Interchange) as its canonical form of a model |
| 59 |
definition<sup><a class="footnote" href="#fn1">[1]</a><a name="ref1"> </a></sup>, |
| 60 |
you have several ways of getting your model into that form: |
| 61 |
</p> |
| 62 |
<ul><li>Create the XMI document directly, using an XML or text editor</li> |
| 63 |
<li>Export the XMI document from a modeling tool such as Rational Rose</li> |
| 64 |
<li>Annotate Java interfaces with model properties</li> |
| 65 |
<li>Use XML Schema to describe the form of a serialization of the model</li></ul> |
| 66 |
<p> |
| 67 |
The first approach is the most direct, but generally only appeals to XML gurus. |
| 68 |
The second choice is the most desirable if you are already using full-scale |
| 69 |
modeling tools. The third approach provides pure Java programmers a low-cost way |
| 70 |
to get the benefits of EMF and its code generator using just a basic Java |
| 71 |
development environment (for example, Eclipse's Java Development Tools). The |
| 72 |
last approach is most applicable in creating an application that must read or |
| 73 |
write a particular XML file format. |
| 74 |
</p> |
| 75 |
<p> |
| 76 |
Once you specify an EMF model, the EMF generator can create a corresponding set |
| 77 |
of Java implementation classes. You can edit these generated classes to add |
| 78 |
methods and instance variables and still regenerate from the model as needed: |
| 79 |
your additions will be preserved during the regeneration. If the code you added |
| 80 |
depends on something that you changed in the model, you will still need to |
| 81 |
update the code to reflect those changes; otherwise, your code is completely |
| 82 |
unaffected by model changes and regeneration. |
| 83 |
</p> |
| 84 |
<p> |
| 85 |
In addition to simply increasing your productivity, building your application |
| 86 |
using EMF provides several other benefits like model change notification, |
| 87 |
persistence support including default XMI and schema-based XML serialization, a |
| 88 |
framework for model validation, and a very efficient reflective API for |
| 89 |
manipulating EMF objects generically. Most important of all, EMF provides the |
| 90 |
foundation for interoperability with other EMF-based tools and applications. |
| 91 |
</p> |
| 92 |
<p> |
| 93 |
EMF consists of two fundamental frameworks: the core framework and EMF.Edit. The |
| 94 |
core framework provides basic generation and runtime support to create Java |
| 95 |
implementation classes for a model. EMF.Edit extends and builds on the core |
| 96 |
framework, adding support for generating adapter classes that enable viewing |
| 97 |
and command-based (undoable) editing of a model, and even a basic working |
| 98 |
model editor. The following sections describe the main features of the core |
| 99 |
EMF framework. EMF.Edit is described in a separate paper, |
| 100 |
<a href="../../references/overview/EMF.Edit.html">EMF.Edit Overview</a>. For instructions on how to run the EMF and EMF.Edit generator, |
| 101 |
refer to <a href="../../tutorials/clibmod/clibmod.html">Tutorial: Generating an |
| 102 |
EMF Model</a>. |
| 103 |
</p> |
| 104 |
<h3>EMF Relation to OMG MOF</h3> |
| 105 |
<p> |
| 106 |
For those of you that are familiar with OMG (Object Management Group) MOF (Meta |
| 107 |
Object Facility), you may be wondering how EMF relates to it. Actually, EMF |
| 108 |
started out as an implementation of the MOF specification but evolved from there |
| 109 |
based on the experience we gained from implementing a large set of tools using |
| 110 |
it. EMF can be thought of as a highly efficient Java implementation of a core |
| 111 |
subset of the MOF API. However, to avoid any confusion, the MOF-like core meta |
| 112 |
model in EMF is called Ecore. |
| 113 |
</p> |
| 114 |
<p> |
| 115 |
In the current proposal for MOF 2.0, a similar subset of the MOF model, which it |
| 116 |
calls EMOF (Essential MOF), is separated out. There are small, mostly naming |
| 117 |
differences between Ecore and EMOF; however, EMF can transparently read and |
| 118 |
write serializations of EMOF. |
| 119 |
</p> |
| 120 |
<h2><a name="definition">Defining an EMF Model</a></h2> |
| 121 |
<p> |
| 122 |
To help describe EMF, we'll start by assuming we have a trivial, one-class model |
| 123 |
like this: |
| 124 |
</p> |
| 125 |
<img src="images/EMF/image001.gif" width="87" height="89" alt="One-class model: Book with title : String and pages : int"/> |
| 126 |
<p> |
| 127 |
The model shows a single class called Book with two attributes: title of type |
| 128 |
String and pages of type int. |
| 129 |
</p> |
| 130 |
<p> |
| 131 |
Our model definition, trivial as it is, can be provided to the EMF code |
| 132 |
generator in a number of ways. |
| 133 |
</p> |
| 134 |
<h3>UML</h3> |
| 135 |
<p> |
| 136 |
If you have a modeling tool that works with |
| 137 |
EMF<sup><a class="footnote" href="#fn2">[2]</a><a name="ref2"> </a></sup>, |
| 138 |
you can simply draw the class diagram as shown above. |
| 139 |
</p> |
| 140 |
<h3>XMI</h3> |
| 141 |
<p> |
| 142 |
Alternatively, we could describe the model directly in an XMI document that |
| 143 |
would look something like this: |
| 144 |
</p> |
| 145 |
<pre> |
| 146 |
<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" |
| 147 |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 148 |
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" |
| 149 |
name="library "nsURI="http:///library.ecore" nsPrefix="library"> |
| 150 |
<eClassifiers xsi:type="ecore:EClass" name="Book"> |
| 151 |
<eStructuralFeatures xsi:type="ecore:EAttribute" name="title" |
| 152 |
eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/> |
| 153 |
<eStructuralFeatures xsi:type="ecore:EAttribute" name="pages" |
| 154 |
eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"/> |
| 155 |
</eClassifiers> |
| 156 |
</ecore:EPackage></pre> |
| 157 |
<p> |
| 158 |
The XMI document contains all the same information as the class diagram, but a |
| 159 |
little less compactly. Every class and attribute in a diagram has a |
| 160 |
corresponding class or attribute definition in the XMI document. |
| 161 |
</p> |
| 162 |
<h3>Annotated Java</h3> |
| 163 |
<p> |
| 164 |
For those of you that have neither a graphical modeling tool nor an interest in |
| 165 |
trying to enter all the XMI syntax by hand, a third option is available for |
| 166 |
describing your model. Since the EMF generator is a code-merging generator, |
| 167 |
by providing partial Java interfaces (annotated with model information) |
| 168 |
ahead of time, the generator can use the interfaces as its generation |
| 169 |
metadata and merge the generated code with the rest of the implementation. |
| 170 |
</p> |
| 171 |
<p> |
| 172 |
We could have defined our Book model class in Java like this: |
| 173 |
</p> |
| 174 |
<pre> |
| 175 |
/** |
| 176 |
* @model |
| 177 |
*/ |
| 178 |
public interface Book |
| 179 |
{ |
| 180 |
/** |
| 181 |
* @model |
| 182 |
*/ |
| 183 |
String getTitle(); |
| 184 |
|
| 185 |
/** |
| 186 |
* @model |
| 187 |
*/ |
| 188 |
int getPages(); |
| 189 |
}</pre> |
| 190 |
<p> |
| 191 |
With this approach, we provide all the model information in the form of Java |
| 192 |
interfaces with standard get |
| 193 |
methods<sup><a class="footnote" href="#fn3">[3]</a><a name="ref3"> </a></sup> |
| 194 |
to identify the attributes and references. The @model tag is used to identify to |
| 195 |
the code generator which interfaces, and parts of those interfaces, correspond |
| 196 |
to model elements and therefore require code generation. |
| 197 |
</p> |
| 198 |
<p> |
| 199 |
For our simple example, all of our model information is actually available |
| 200 |
though Java introspection of this interface, so no additional model information |
| 201 |
is needed. In the general case, however, the @model tag may be followed by |
| 202 |
additional details about the model element. If for example, we wanted the pages |
| 203 |
attribute to be read-only (that is, no generation of a set method), we would |
| 204 |
need to add the following to the annotation: |
| 205 |
</p> |
| 206 |
<pre> |
| 207 |
/** |
| 208 |
* @model changeable="false" |
| 209 |
*/ |
| 210 |
int getPages();</pre> |
| 211 |
<p> |
| 212 |
Because only information that is different from the default needs to be |
| 213 |
specified, annotations can be kept simple and concise. |
| 214 |
</p> |
| 215 |
<h3>XML Schema</h3> |
| 216 |
<p> |
| 217 |
Sometimes, you might want to describe a model with a schema that specifies how |
| 218 |
instance serializations should look. This can be useful for writing an |
| 219 |
application that must use XML to integrate with an existing application or to |
| 220 |
comply with a standard. Here is how we would specify a schema that's equivalent |
| 221 |
to our simple book model: |
| 222 |
</p> |
| 223 |
<pre> |
| 224 |
<xsd:schema targetNamespace="http:///library.ecore" |
| 225 |
xmlns="http:///library.ecore" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> |
| 226 |
<xsd:complexType name="Book"> |
| 227 |
<xsd:sequence> |
| 228 |
<xsd:element name="title" type="xsd:string"/> |
| 229 |
<xsd:element name="pages" type="xsd:integer"/> |
| 230 |
</xsd:sequence> |
| 231 |
</xsd:complexType> |
| 232 |
</xsd:schema></pre> |
| 233 |
<p> |
| 234 |
This approach differs somewhat from the other three, mainly because EMF must |
| 235 |
apply certain restrictions to the serialization that it eventually uses, to |
| 236 |
ensure compliance with the schema. As a result, the model that is a created for |
| 237 |
a schema looks slightly different from one specified in one of the other ways. |
| 238 |
The details of these differences are beyond the scope of this overview. |
| 239 |
</p> |
| 240 |
<p> |
| 241 |
In the remainder of this paper, we'll use UML diagrams for their clarity and |
| 242 |
conciseness. All of the modeling concepts we'll illustrate could also be |
| 243 |
expressed using annotated Java or directly with XMI, and most have XML Schema |
| 244 |
equivalents. Regardless of how the information is provided, the code generated |
| 245 |
by EMF will be the same. |
| 246 |
</p> |
| 247 |
<h2><a name="generation">Generating a Java Implementation</a></h2> |
| 248 |
<p> |
| 249 |
For each class in the model, a Java interface and corresponding implementation |
| 250 |
class will be generated. In our example, the generated interface for Book |
| 251 |
looks like this: |
| 252 |
</p> |
| 253 |
<pre> |
| 254 |
public interface Book extends EObject |
| 255 |
{ |
| 256 |
String getTitle(); |
| 257 |
void setTitle(String value); |
| 258 |
|
| 259 |
int getPages(); |
| 260 |
void setPages(int value); |
| 261 |
}</pre> |
| 262 |
<p> |
| 263 |
Each generated interface contains getter and setter methods for each attribute |
| 264 |
and reference of the corresponding model class. |
| 265 |
</p> |
| 266 |
<p> |
| 267 |
Interface Book extends the base interface EObject. EObject is the EMF equivalent |
| 268 |
of java.lang.Object, that is, it is the base of every EMF class. EObject and its |
| 269 |
corresponding implementation class EObjectImpl (which we will look at later) |
| 270 |
provide a relatively lightweight base class that lets Book participate in the |
| 271 |
EMF notification and persistence frameworks. Before we start looking at what |
| 272 |
exactly EObject brings into the mix, let's continue looking at how EMF generates |
| 273 |
Book. |
| 274 |
</p> |
| 275 |
<p> |
| 276 |
Each generated implementation class includes implementations of the getters and |
| 277 |
setters defined in the corresponding interface, plus some other methods required |
| 278 |
by the EMF framework. |
| 279 |
</p> |
| 280 |
<p> |
| 281 |
Class BookImpl will include, among other things, implementations of the title |
| 282 |
and pages accessors. The pages attribute, for example, has the following |
| 283 |
generated implementation: |
| 284 |
</p> |
| 285 |
<pre> |
| 286 |
public class BookImpl extends EObjectImpl implements Book |
| 287 |
{ |
| 288 |
... |
| 289 |
protected static final int PAGES_EDEFAULT = 0; |
| 290 |
protected int pages = PAGES_EDEFAULT; |
| 291 |
|
| 292 |
public int getPages() |
| 293 |
{ |
| 294 |
return pages; |
| 295 |
} |
| 296 |
|
| 297 |
public void setPages(int newPages) |
| 298 |
{ |
| 299 |
int oldPages = pages; |
| 300 |
pages = newPages; |
| 301 |
if (eNotificationRequired()) |
| 302 |
eNotify(new ENotificationImpl(this, Notification.SET, ..., oldPages, pages)); |
| 303 |
} |
| 304 |
|
| 305 |
... |
| 306 |
}</pre> |
| 307 |
<p> |
| 308 |
The generated get method is optimally efficient. It simply returns an instance |
| 309 |
variable representing the attribute. |
| 310 |
</p> |
| 311 |
<p> |
| 312 |
The set method, although a little more complicated, is also quite efficient. In |
| 313 |
addition to setting the instance variable pages, the set method needs to send |
| 314 |
change notification to any observers that may be listening to the object by |
| 315 |
calling the eNotify() method. To optimize the case where there are no observers |
| 316 |
(for example, in a batch application), construction of the notification object |
| 317 |
(ENotificationImpl) and the call to eNotify() are guarded by a call to |
| 318 |
eNotificationRequired(). The default implementation of eNotificationRequired() |
| 319 |
simply checks if there are any observers (adapters) attached to the object. |
| 320 |
Therefore, when EMF objects are used without observers, the call to |
| 321 |
eNotificationRequired() amounts to nothing more than an efficient null pointer |
| 322 |
check, which is inlined when using a JIT compiler. |
| 323 |
</p> |
| 324 |
<p> |
| 325 |
The generated accessor patterns for other types of attributes, like the |
| 326 |
String-typed title attribute, have some minor differences but are fundamentally |
| 327 |
the same as those shown for |
| 328 |
pages<sup><a class="footnote" href="#fn4">[4]</a><a name="ref4"> </a></sup>. |
| 329 |
</p> |
| 330 |
<p> |
| 331 |
The generated accessors for references, especially two-way ones, are a little |
| 332 |
more complicated and start to show the real value of the EMF generator. |
| 333 |
</p> |
| 334 |
<h3>One-way references</h3> |
| 335 |
<p> |
| 336 |
Let's expand our example model with another class Writer that has an association |
| 337 |
with class Book: |
| 338 |
</p> |
| 339 |
<img src="images/EMF/image002.gif" width="303" height="89" alt="One-way reference: Book has a single author of class Writer, which contains a name : String"/> |
| 340 |
<p> |
| 341 |
The association between a book and its writer is, in this example, a single |
| 342 |
one-way reference. The reference (role) name used to access the Writer from |
| 343 |
a Book is author. |
| 344 |
</p> |
| 345 |
<p> |
| 346 |
Running this model through the EMF generator will, in addition to generating the |
| 347 |
new interface Writer and implementation class WriterImpl, generate |
| 348 |
additional get and set methods in interface Book: |
| 349 |
</p> |
| 350 |
<pre> |
| 351 |
Writer getAuthor(); |
| 352 |
void setAuthor(Writer value);</pre> |
| 353 |
<p> |
| 354 |
Since the author reference is one-way, the implementation of the setAuthor() |
| 355 |
method looks much like a simple data setter, like the earlier one for |
| 356 |
setPages(): |
| 357 |
</p> |
| 358 |
<pre> |
| 359 |
public void setAuthor(Writer newAuthor) |
| 360 |
{ |
| 361 |
Writer oldAuthor = author; |
| 362 |
author = newAuthor; |
| 363 |
if(eNotificationRequired()) |
| 364 |
eNotify(new ENotificationImpl(this, ...)); |
| 365 |
}</pre> |
| 366 |
<p> |
| 367 |
The only difference is that here we're setting an object pointer instead of just |
| 368 |
a simple data field. |
| 369 |
</p> |
| 370 |
<p> |
| 371 |
Because we're dealing with an object reference, however, the getAuthor() method |
| 372 |
is a little more complicated. This is because the get method for some types of |
| 373 |
references, including the type of author, needs to deal with the possibility |
| 374 |
that the referenced object (in this case Writer) may persist in a different |
| 375 |
resource (document) from the source object (in this case Book). Because the EMF |
| 376 |
persistence framework uses a lazy loading scheme, an object pointer (in this |
| 377 |
case author) may at some point in time be a proxy for the object, instead of the |
| 378 |
actual referenced |
| 379 |
object<sup><a class="footnote" href="#fn5">[5]</a><a name="ref5"> </a></sup>. |
| 380 |
As a result, the getAuthor() method looks like this: |
| 381 |
</p> |
| 382 |
<pre> |
| 383 |
public Writer getAuthor() |
| 384 |
{ |
| 385 |
if (author != null && author.eIsProxy()) |
| 386 |
{ |
| 387 |
Writer oldAuthor = author; |
| 388 |
author = (Writer)eResolveProxy((InternalEObject)author); |
| 389 |
if (author != oldAuthor) |
| 390 |
{ |
| 391 |
if (eNotificationRequired()) |
| 392 |
eNotify(new ENotificationImpl(this, Notification.RESOLVE, ...)); |
| 393 |
} |
| 394 |
} |
| 395 |
return author; |
| 396 |
}</pre> |
| 397 |
<p> |
| 398 |
Instead of simply returning the author instance variable, we first call the |
| 399 |
inherited framework method eIsProxy() method to check if the reference is a |
| 400 |
proxy, and then call eResolveProxy() if it is. The latter method calls |
| 401 |
EcoreUtil.resolve(), a static utility method that attempts to load the target |
| 402 |
object's document, and consequently the object, using the proxy's URI. If |
| 403 |
successful, it will return the resolved object. If, however, the document fails |
| 404 |
to load, it will just return the proxy |
| 405 |
again<sup><a class="footnote" href="#fn6">[6]</a><a name="ref6"> </a></sup>. |
| 406 |
</p> |
| 407 |
<h3>Two-way references</h3> |
| 408 |
<p> |
| 409 |
Now that we understand how proxy resolution affects the get pattern for certain |
| 410 |
types of references, we can look at how the set pattern changes when an |
| 411 |
association is made two-way. Let's change our one-way author association to |
| 412 |
this: |
| 413 |
</p> |
| 414 |
<img src="images/EMF/image003.gif" width="303" height="89" alt="Two-way reference: an author may have written 0 or more books, but a book can only have one author"/> |
| 415 |
<p> |
| 416 |
The association is now two-way, as indicated by the lack of an arrowhead on the |
| 417 |
Writer end of the association line. The role name used to access Books from a |
| 418 |
Writer is books. |
| 419 |
</p> |
| 420 |
<p> |
| 421 |
If we regenerate our model, the getAuthor() method will be unaffected, but |
| 422 |
setAuthor() will now look like this: |
| 423 |
</p> |
| 424 |
<pre> |
| 425 |
public void setAuthor(Writer newAuthor) |
| 426 |
{ |
| 427 |
if (newAuthor != author) |
| 428 |
{ |
| 429 |
NotificationChain msgs = null; |
| 430 |
if (author != null) |
| 431 |
msgs = ((InternalEObject)author).eInverseRemove(this, ..., msgs); |
| 432 |
if (newAuthor != null) |
| 433 |
msgs = ((InternalEObject)newAuthor).eInverseAdd(this, ..., msgs); |
| 434 |
msgs = basicSetAuthor(newAuthor, msgs); |
| 435 |
if (msgs != null) msgs.dispatch(); |
| 436 |
} |
| 437 |
else if (eNotificationRequired()) |
| 438 |
eNotify(new ENotificationImpl(this, ...)); // send "touch" notification |
| 439 |
}</pre> |
| 440 |
<p> |
| 441 |
As you can see, when setting a two-way reference like author, the other end of |
| 442 |
the reference needs to be set as well (by calling eInverseAdd()). We also need |
| 443 |
to remove the inverse of any previous author (by calling eInverseRemove()) |
| 444 |
because in our model the author reference is singular (that is, a book can only |
| 445 |
have one |
| 446 |
author)<sup><a class="footnote" href="#fn7">[7]</a><a name="ref7"> </a></sup> |
| 447 |
and therefore this book cannot be in more than one Writer's books reference. |
| 448 |
Finally, we set the author reference by calling another generated method |
| 449 |
(basicSetAuthor()) which looks like this: |
| 450 |
</p> |
| 451 |
<pre> |
| 452 |
public NotificationChain basicSetAuthor(Writer newAuthor, NotificationChain msgs) |
| 453 |
{ |
| 454 |
Writer oldAuthor = author; |
| 455 |
author = newAuthor; |
| 456 |
if (eNotificationRequired()) |
| 457 |
{ |
| 458 |
ENotificationImpl notification = new ENotificationImpl(this, ...); |
| 459 |
if (msgs == null) msgs = notification; else msgs.add(notification); |
| 460 |
} |
| 461 |
return msgs; |
| 462 |
}</pre> |
| 463 |
<p> |
| 464 |
This method looks very similar to the one-way reference set method, except that |
| 465 |
if the msgs argument is non-null, the notification gets added to it, intsead of |
| 466 |
being fired |
| 467 |
directly<sup><a class="footnote" href="#fn8">[8]</a><a name="ref8"> </a></sup>. |
| 468 |
Because of all the forward/reverse adding/removing during a two-way reference |
| 469 |
set operation, as many as four (three in this particular example) different |
| 470 |
notifications may be generated. A NotificationChain is used to collect all these |
| 471 |
individual notifications so their firing can be deferred until after all the |
| 472 |
state changes have been made. The queued-up notifications are sent by calling |
| 473 |
msgs.dispatch(), as shown in the setAuthor() method, above. |
| 474 |
</p> |
| 475 |
<h3>Multiplicity-many references</h3> |
| 476 |
<p> |
| 477 |
You may have noticed in our example that the books association (from Writer to |
| 478 |
Book) is multiplicity many (that is, 0..*). In other words, one writer may |
| 479 |
have written many books. Multiplicity-many references (that is, any |
| 480 |
reference where the upper bound is greater than 1) in EMF are manipulated |
| 481 |
using a collection API, so only a get method is generated in the |
| 482 |
interface: |
| 483 |
</p> |
| 484 |
<pre> |
| 485 |
public interface Writer extends EObject |
| 486 |
{ |
| 487 |
... |
| 488 |
EList getBooks(); |
| 489 |
}</pre> |
| 490 |
<p> |
| 491 |
Notice that getBooks() returns an EList as opposed to a java.util.List. |
| 492 |
Actually, they are almost the same. EList is an EMF subclass of |
| 493 |
java.util.List that adds two move methods to the API. Other than that, from |
| 494 |
a client perspective, you can consider it a standard Java List. For |
| 495 |
example, to add a book to the books association, you can simply call: |
| 496 |
</p> |
| 497 |
<pre> |
| 498 |
aWriter.getBooks().add(aBook);</pre> |
| 499 |
<p> |
| 500 |
or to iterate over them you would do something like this: |
| 501 |
</p> |
| 502 |
<pre> |
| 503 |
for (Iterator iter = aWriter.getBooks().iterator(); iter.hasNext(); ) |
| 504 |
{ |
| 505 |
Book book = (Book)iter.next(); |
| 506 |
... |
| 507 |
}</pre> |
| 508 |
<p> |
| 509 |
As you can see, from a client perspective, the API for manipulating |
| 510 |
multiplicity-many references is nothing special. However, because the books |
| 511 |
reference is part of a two-way association (it's the inverse of Book.author), |
| 512 |
we still need to do all the fancy inverse handshaking that we showed for the |
| 513 |
setAuthor() method. Looking at the implementation of the getBooks() method in |
| 514 |
WriterImpl shows us how the multiplicity-many case gets handled: |
| 515 |
</p> |
| 516 |
<pre> |
| 517 |
public EList getBooks() |
| 518 |
{ |
| 519 |
if (books == null) |
| 520 |
{ |
| 521 |
books = new EObjectWithInverseResolvingEList(Book.class, this, |
| 522 |
LibraryPackage.WRITER__BOOKS, LibraryPackage.BOOK__AUTHOR); |
| 523 |
} |
| 524 |
return books; |
| 525 |
}</pre> |
| 526 |
<p> |
| 527 |
The getBooks() method returns a special implementation class, |
| 528 |
EObjectWithInverseResolvingEList, which is constructed with all the information |
| 529 |
it needs to do the reverse handshaking during add and remove calls. EMF actually |
| 530 |
provides 20 different specialized EList |
| 531 |
implementations<sup><a class="footnote" href="#fn9">[9]</a><a name="ref9"> </a></sup> |
| 532 |
to efficiently implement all types of multiplicity-many features. |
| 533 |
For one-way associations (that is, those with no inverse) we use |
| 534 |
EObjectResolvingEList. If the reference doesn't need proxy resolution we'd use |
| 535 |
EObjectWithInverseEList or EObjectEList, and so on. |
| 536 |
</p> |
| 537 |
<p> |
| 538 |
So for our example, the list used to implement the books reference is created |
| 539 |
with the argument LibraryPackage.BOOK__AUTHOR (a generated static int constant |
| 540 |
representing the reverse feature). This will be used during the add() call to |
| 541 |
call eInverseAdd() on the Book, similar to the way eInverseAdd() was called on |
| 542 |
the Writer during setAuthor(). Here's what eInverseAdd() looks like in class |
| 543 |
BookImpl: |
| 544 |
</p> |
| 545 |
<pre> |
| 546 |
public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, |
| 547 |
Class baseClass, NotificationChain msgs) |
| 548 |
{ |
| 549 |
if (featureID >= 0) |
| 550 |
{ |
| 551 |
switch (eDerivedStructuralFeatureID(featureID, baseClass)) |
| 552 |
{ |
| 553 |
case LibraryPackage.BOOK__AUTHOR: |
| 554 |
if (author != null) |
| 555 |
msgs = ((InternalEObject)author).eInverseRemove(this, .., msgs); |
| 556 |
return basicSetAuthor((Writer)otherEnd, msgs); |
| 557 |
default: |
| 558 |
... |
| 559 |
} |
| 560 |
} |
| 561 |
... |
| 562 |
}</pre> |
| 563 |
<p> |
| 564 |
It first calls eInverseRemove() to remove any previous author (as we described |
| 565 |
previously when we looked at the setAuthor() method), and then it calls |
| 566 |
basicSetAuthor() to actually set the reference. Although our particular example |
| 567 |
only has one two-way reference, eInverseAdd() uses a switch statement that |
| 568 |
includes a case for every two-way reference available on class |
| 569 |
Book<sup><a class="footnote" href="#fn10">[10]</a><a name="ref10"> </a></sup>. |
| 570 |
</p> |
| 571 |
<h3><a name="containment">Containment references</a></h3> |
| 572 |
<p> |
| 573 |
Let's add a new class, Library, which will act as the container for Books. |
| 574 |
</p> |
| 575 |
<img src="images/EMF/image004.gif" width="308" height="89" alt="Containment reference: a Library contains 0 or more books"/> |
| 576 |
<p> |
| 577 |
The containment reference is indicated by the black diamond on the Library end |
| 578 |
of the association. In full, the association indicates that a Library |
| 579 |
aggregates, by value, 0 or more Books. By-value aggregation (containment) |
| 580 |
associations are particularly important because they identify the parent or |
| 581 |
owner of a target instance, which implies the physical location of the object |
| 582 |
when persisted. |
| 583 |
</p> |
| 584 |
<p> |
| 585 |
Containment affects the generated code in several ways. First of all, because a |
| 586 |
contained object is guaranteed to be in the same resource as its container, |
| 587 |
proxy resolution isn't needed. Therefore, the generated get method in |
| 588 |
LibraryImpl will use a non-resolving EList implementation class: |
| 589 |
</p> |
| 590 |
<pre> |
| 591 |
public EList getBooks() |
| 592 |
{ |
| 593 |
if (books == null) |
| 594 |
{ |
| 595 |
books = new EObjectContainmentEList(Book.class, this, ...); |
| 596 |
} |
| 597 |
return books; |
| 598 |
}</pre> |
| 599 |
<p> |
| 600 |
In addition to not performing proxy resolution, an EObjectContainmentEList also |
| 601 |
implements the contains() operation very efficiently (that is, in a constant |
| 602 |
time, vs. linear time in the general case). This is particularly important |
| 603 |
because duplicate entries are not allowed in EMF reference lists, so contains() |
| 604 |
is called during add() operations as well. |
| 605 |
</p> |
| 606 |
<p> |
| 607 |
Because an object can only have one container, adding an object to a containment |
| 608 |
association also means removing the object from any container it's currently in, |
| 609 |
regardless of the actual association. For example, adding a Book to a Library's |
| 610 |
books list may involve removing it from some other Library's books list. That's |
| 611 |
no different than any other two-way association where the inverse has |
| 612 |
multiplicity 1. Let's assume, however, that the Writer class also had a |
| 613 |
containment association to Book, called ownedBooks. Then, if a given book |
| 614 |
instance is in the ownedBooks list of some Writer, when we add it to a Library's |
| 615 |
books reference, it would need to be removed from the Writer first. |
| 616 |
</p> |
| 617 |
<p> |
| 618 |
To implement this kind of thing efficiently, the base class EObjectImpl has an |
| 619 |
instance variable (eContainer) of type EObject that it uses to store the |
| 620 |
container generically. As a result, containment references are always |
| 621 |
implicitly two-way. To access the Library from a Book, you can write something |
| 622 |
like this: |
| 623 |
</p> |
| 624 |
<pre> |
| 625 |
EObject container = book.eContainer(); |
| 626 |
if (container instanceof Library) |
| 627 |
library = (Library)container;</pre> |
| 628 |
<p> |
| 629 |
If you want to avoid the downcast, you can change the association to be |
| 630 |
explicitly two-way instead: |
| 631 |
</p> |
| 632 |
<img src="images/EMF/image005.gif" width="308" height="89" alt="Two-way containment reference: a Library contains 0 or more books; books are contained by a Library"/> |
| 633 |
<p> |
| 634 |
and let EMF generate a nice typesafe get method for you: |
| 635 |
</p> |
| 636 |
<pre> |
| 637 |
public Library getLibrary() |
| 638 |
{ |
| 639 |
if (eContainerFeatureID != LibraryPackage.BOOK__LIBRARY) return null; |
| 640 |
return (Library)eContainer; |
| 641 |
}</pre> |
| 642 |
<p> |
| 643 |
Notice that the explicit get method uses the eContainer variable from |
| 644 |
EObjectImpl instead of a generated instance variable as we saw previously |
| 645 |
for non-container references (like getAuthor(), |
| 646 |
above)<sup><a class="footnote" href="#fn11">[11]</a><a name="ref11"> </a></sup>. |
| 647 |
</p> |
| 648 |
<h3>Enumeration attributes</h3> |
| 649 |
<p> |
| 650 |
So far, we've looked at how EMF handles simple attributes and various types of |
| 651 |
references. Another commonly used type of attribute is an enumeration. |
| 652 |
Enumeration-type attributes are implemented using the Java typesafe enum |
| 653 |
pattern<sup><a class="footnote" href="#fn12">[12]</a><a name="ref12"> </a></sup>. |
| 654 |
</p> |
| 655 |
<p> |
| 656 |
If we add an enumeration attribute, category, to class Book: |
| 657 |
</p> |
| 658 |
<img src="images/EMF/image006.gif" width="354" height="108" alt="Enumeration attribute and definition: Book has a category of class BookCategory, which is an enumeration of Mystery, ScienceFiction, and Biography"/> |
| 659 |
<p> |
| 660 |
and regenerate the implementation classes, interface Book will now include a |
| 661 |
getter and setter for category: |
| 662 |
</p> |
| 663 |
<pre> |
| 664 |
BookCategory getCategory(); |
| 665 |
void setCategory(BookCategory value);</pre> |
| 666 |
<p> |
| 667 |
In the generated interface, the category methods use a typesafe enumeration |
| 668 |
class called BookCategory. This class defines static constants for the |
| 669 |
enumeration's values and other convenience methods, like this: |
| 670 |
</p> |
| 671 |
<pre> |
| 672 |
public final class BookCategory extends AbstractEnumerator |
| 673 |
{ |
| 674 |
public static final int MYSTERY = 0; |
| 675 |
public static final int SCIENCE_FICTION = 1; |
| 676 |
public static final int BIOGRAPHY = 2; |
| 677 |
|
| 678 |
public static final BookCategory MYSTERY_LITERAL = |
| 679 |
new BookCategory(MYSTERY, "Mystery"); |
| 680 |
public static final BookCategory SCIENCE_FICTION_LITERAL = |
| 681 |
new BookCategory(SCIENCE_FICTION, "ScienceFiction"); |
| 682 |
public static final BookCategory BIOGRAPHY_LITERAL = |
| 683 |
new BookCategory(BIOGRAPHY, "Biography"); |
| 684 |
|
| 685 |
public static final List VALUES = Collections.unmodifiableList(...)); |
| 686 |
|
| 687 |
public static BookCategory get(String name) |
| 688 |
{ |
| 689 |
... |
| 690 |
} |
| 691 |
|
| 692 |
public static BookCategory get(int value) |
| 693 |
{ |
| 694 |
... |
| 695 |
} |
| 696 |
|
| 697 |
private BookCategory(int value, String name) |
| 698 |
{ |
| 699 |
super(value, name); |
| 700 |
} |
| 701 |
}</pre> |
| 702 |
<p> |
| 703 |
As shown, the enumeration class provides static int constants for the |
| 704 |
enumerations's values as well as static constants for the enumeration's |
| 705 |
singleton literal objects themselves. The int constants have the same names as |
| 706 |
the model's literal names<sup><a class="footnote" href="#fn13">[13]</a><a name="ref13"> </a></sup>. |
| 707 |
The literal constants have the same names with _LITERAL appended. |
| 708 |
</p> |
| 709 |
<p> |
| 710 |
The constants provide convenient access to the literals when, for example, |
| 711 |
setting the category of a book: |
| 712 |
</p> |
| 713 |
<pre> |
| 714 |
book.setCategory(BookCategory.SCIENCE_FICTION_LITERAL);</pre> |
| 715 |
<p> |
| 716 |
The BookCategory constructor is private and therefore the only instances of the |
| 717 |
enumeration class that will ever exist are the ones used for the statics |
| 718 |
MYSTERY_LITERAL, SCIENCE_FICTION_LITERAL, and BIOGRAPHY_LITERAL. As a result, |
| 719 |
equality comparisons (that is .equals() calls) are never needed. Literals can |
| 720 |
always be reliably compared using the simpler and more efficient == operator, |
| 721 |
like this: |
| 722 |
</p> |
| 723 |
<pre> |
| 724 |
book.getCategory() == BookCategory.MYSTERY_LITERAL</pre> |
| 725 |
<p> |
| 726 |
When comparing against many values, a switch statement using the int values is |
| 727 |
better yet: |
| 728 |
</p> |
| 729 |
<pre> |
| 730 |
switch (book.getCategory().value()) { |
| 731 |
case BookCategory.MYSTERY: |
| 732 |
// do something ... |
| 733 |
break; |
| 734 |
case BookCategory.SCIENCE_FICTION: |
| 735 |
... |
| 736 |
}</pre> |
| 737 |
<p> |
| 738 |
For situations where only the literal name (String) or value (int) is available, |
| 739 |
convenience get() methods, which can be used to retrieve the corresponding |
| 740 |
literal object, are also generated in the enumeration class. |
| 741 |
</p> |
| 742 |
<h3>Factories and packages</h3> |
| 743 |
<p> |
| 744 |
In addition to the model interfaces and implementation classes, EMF generates |
| 745 |
at least two more interfaces (and implementation classes): a factory and a |
| 746 |
package. |
| 747 |
</p> |
| 748 |
<p> |
| 749 |
The factory, as its name implies, is used for creating instances of your model |
| 750 |
classes, while the package provides some static constants (for example, the |
| 751 |
feature constants used by the generated methods) and convenience methods for |
| 752 |
accessing your model's |
| 753 |
metadata<sup><a class="footnote" href="#fn14">[14]</a><a name="ref14"> </a></sup>. |
| 754 |
</p> |
| 755 |
<p> |
| 756 |
Here is the factory interface for the book example: |
| 757 |
</p> |
| 758 |
<pre> |
| 759 |
public interface LibraryFactory extends EFactory |
| 760 |
{ |
| 761 |
LibraryFactory eINSTANCE = new LibraryFactoryImpl(); |
| 762 |
|
| 763 |
Book createBook(); |
| 764 |
Writer createWriter(); |
| 765 |
Library createLibrary(); |
| 766 |
|
| 767 |
LibraryPackage getLibraryPackage(); |
| 768 |
}</pre> |
| 769 |
<p> |
| 770 |
As shown, the generated factory provides a factory method (create) for each |
| 771 |
class defined in the model, an accessor for your model's package, and a static |
| 772 |
constant reference (that is, eINSTANCE) to the factory singleton. |
| 773 |
</p> |
| 774 |
<p> |
| 775 |
The LibraryPackage interface provides convenient access to all the metadata of |
| 776 |
our model: |
| 777 |
</p> |
| 778 |
<pre> |
| 779 |
public interface LibraryPackage extends EPackage |
| 780 |
{ |
| 781 |
... |
| 782 |
LibraryPackage eINSTANCE = LibraryPackageImpl.init(); |
| 783 |
|
| 784 |
static final int BOOK = 0; |
| 785 |
static final int BOOK__TITLE = 0; |
| 786 |
static final int BOOK__PAGES = 1; |
| 787 |
static final int BOOK__CATEGORY = 2; |
| 788 |
static final int BOOK__AUTHOR = 3; |
| 789 |
... |
| 790 |
|
| 791 |
static final int WRITER = 1; |
| 792 |
static final int WRITER__NAME = 0; |
| 793 |
... |
| 794 |
|
| 795 |
EClass getBook(); |
| 796 |
EAttribute getBook_Title(); |
| 797 |
EAttribute getBook_Pages(); |
| 798 |
EAttribute getBook_Category(); |
| 799 |
EReference getBook_Author(); |
| 800 |
|
| 801 |
... |
| 802 |
}</pre> |
| 803 |
<p> |
| 804 |
As you can see, the metadata is available in two forms: int constants and the |
| 805 |
Ecore meta objects themselves. The int constants provide the most efficient way |
| 806 |
to pass around meta information. You may have noticed that the generated methods |
| 807 |
use these constants in their implementations. Later, when we look at how EMF |
| 808 |
adapters can be implemented, you'll see that the constants also provide the most |
| 809 |
efficient way to determine what has changed when handling notifications. Also, |
| 810 |
just like the factory, the generated package interface provides a static |
| 811 |
constant reference to its singleton implementation. |
| 812 |
</p> |
| 813 |
<h3>Generating classes with super classes</h3> |
| 814 |
<p> |
| 815 |
Let's say we want to create a subclass, SchoolBook, of our Book model class, |
| 816 |
like this: |
| 817 |
</p> |
| 818 |
<img src="images/EMF/image007.gif" width="164" height="198" alt="Single inheritance: a SchoolBook extends Book"/> |
| 819 |
<p> |
| 820 |
The EMF generator handles single inheritance as you'd expect: the generated |
| 821 |
interface extends the super interface: |
| 822 |
</p> |
| 823 |
<pre> |
| 824 |
public interface SchoolBook extends Book</pre> |
| 825 |
<p> |
| 826 |
and the implementation class extends the super implementation class: |
| 827 |
</p> |
| 828 |
<pre> |
| 829 |
public class SchoolBookImpl extends BookImpl implements SchoolBook</pre> |
| 830 |
<p> |
| 831 |
As in Java itself, multiple interface inheritance is supported, but each EMF |
| 832 |
class can only extend one implementation base class. Therefore, when we have a |
| 833 |
model with multiple inheritance, we need to identify which of the multiple bases |
| 834 |
should be used as the implementation base class. The others will then be simply |
| 835 |
treated as mixin interfaces, with their implementations generated into the |
| 836 |
derived implementation class. |
| 837 |
</p> |
| 838 |
<p> |
| 839 |
Consider the following example: |
| 840 |
</p> |
| 841 |
<img src="images/EMF/image008.gif" width="284" height="213" alt="Multiple inheritance: a SchoolBook extends Book and Asset (Asset has a value : float)"/> |
| 842 |
<p> |
| 843 |
Here we've made SchoolBook derive from two classes: Book and Asset. We've |
| 844 |
identified Book as the implementation base (extended) class as |
| 845 |
shown<sup><a class="footnote" href="#fn15">[15]</a><a name="ref15"> </a></sup>. |
| 846 |
If we regenerate the model, interface SchoolBook will now extend the two |
| 847 |
interfaces: |
| 848 |
</p> |
| 849 |
<pre> |
| 850 |
public interface SchoolBook extends Book, Asset</pre> |
| 851 |
<p> |
| 852 |
The implementation class looks the same as before, only now it includes |
| 853 |
implementations of the mixed-in methods getValue() and setValue(): |
| 854 |
</p> |
| 855 |
<pre> |
| 856 |
public class SchoolBookImpl extends BookImpl implements SchoolBook |
| 857 |
{ |
| 858 |
public float getValue() |
| 859 |
{ |
| 860 |
... |
| 861 |
} |
| 862 |
|
| 863 |
public void setValue(float newValue) |
| 864 |
{ |
| 865 |
... |
| 866 |
} |
| 867 |
|
| 868 |
... |
| 869 |
}</pre> |
| 870 |
<h3>Customizing the generated implementation classes</h3> |
| 871 |
<p> |
| 872 |
You can add behavior (methods and instance variables) to the generated Java |
| 873 |
classes without having to worry about losing your changes if you later decide to |
| 874 |
modify the model and then regenerate. For example, let's add a method, |
| 875 |
isRecommended(), to class Book. To do this you simply go ahead and add the new |
| 876 |
method signature to the Java interface Book: |
| 877 |
</p> |
| 878 |
<pre> |
| 879 |
public interface Book ... |
| 880 |
{ |
| 881 |
boolean isRecommended(); |
| 882 |
... |
| 883 |
}</pre> |
| 884 |
<p> |
| 885 |
and its implementation in class BookImpl: |
| 886 |
</p> |
| 887 |
<pre> |
| 888 |
public boolean isRecommended() |
| 889 |
{ |
| 890 |
return getAuthor().getName().equals("William Shakespeare"); |
| 891 |
}</pre> |
| 892 |
<p> |
| 893 |
The EMF generator won't wipe out this change because it isn't a generated method |
| 894 |
to begin with. Every method generated by EMF includes a Javadoc comment that |
| 895 |
contains an @generated tag, like this: |
| 896 |
</p> |
| 897 |
<pre> |
| 898 |
/** |
| 899 |
* ... |
| 900 |
* @generated |
| 901 |
*/ |
| 902 |
public String getTitle() |
| 903 |
{ |
| 904 |
return title; |
| 905 |
}</pre> |
| 906 |
<p> |
| 907 |
Any method in the file that doesn't contain this tag (like isRecommended()) will |
| 908 |
be left untouched whenever we regenerate. In fact, if we want to change the |
| 909 |
implementation of a generated method, we can do that by removing the @generated |
| 910 |
tag from |
| 911 |
it<sup><a class="footnote" href="#fn16">[16]</a><a name="ref16"> </a></sup>: |
| 912 |
</p> |
| 913 |
<pre> |
| 914 |
/** |
| 915 |
* ... |
| 916 |
* <strike>@generated</strike> // (removed) |
| 917 |
*/ |
| 918 |
public String getTitle() |
| 919 |
{ |
| 920 |
// our custom implementation ... |
| 921 |
}</pre> |
| 922 |
<p> |
| 923 |
Now, because of the missing @generated tag, the getTitle() method is considered |
| 924 |
to be user code; if we regenerate the model, the generator will detect the |
| 925 |
collision and simply discard the generated version of the method. |
| 926 |
</p> |
| 927 |
<p> |
| 928 |
Actually, before discarding a generated method, the generator first checks if |
| 929 |
there is another generated method in the file with the same name, but with Gen |
| 930 |
appended. If it finds one, then instead of discarding the newly generated |
| 931 |
version of the method it redirects the output to it. For example, if we want to |
| 932 |
extend the generated getTitle() implementation, instead of completely discarding |
| 933 |
it, then we can do that by simply renaming it like this: |
| 934 |
</p> |
| 935 |
<pre> |
| 936 |
/** |
| 937 |
* ... |
| 938 |
* @generated |
| 939 |
*/ |
| 940 |
public String getTitleGen() |
| 941 |
{ |
| 942 |
return title; |
| 943 |
}</pre> |
| 944 |
<p> |
| 945 |
and then adding our override as a user method that does whatever we want: |
| 946 |
</p> |
| 947 |
<pre> |
| 948 |
public String getTitle() |
| 949 |
{ |
| 950 |
String result = getTitleGen(); |
| 951 |
if (result == null) |
| 952 |
result = ... |
| 953 |
return result; |
| 954 |
}</pre> |
| 955 |
<p> |
| 956 |
If we regenerate now, the generator will detect the collision with our user |
| 957 |
version of getTitle(), but because we also have the @generated getTitleGen() |
| 958 |
method in the class, it will redirect the newly generated implementation to it, |
| 959 |
instead of discarding it. |
| 960 |
</p> |
| 961 |
<h3>Operations in EMF models</h3> |
| 962 |
<p> |
| 963 |
In addition to attributes and references, you can add operations to your model |
| 964 |
classes. If you do, the EMF generator will generate their signature into the |
| 965 |
interface and a method skeleton into the implementation class. EMF does not |
| 966 |
model behavior, so the implementation must be provided by user-written Java |
| 967 |
code. |
| 968 |
</p> |
| 969 |
<p> |
| 970 |
This may be done by removing the @generated tag from the generated |
| 971 |
implementation, as described above, and adding the code right there. |
| 972 |
Alternatively, the Java code can be included right in the model. In Rose, you |
| 973 |
can enter it in the text box on the Semantics tab of an Operation Specification |
| 974 |
dialog. The code will then be stored in the EMF model as an annotation on the |
| 975 |
operation<sup><a class="footnote" href="#fn17">[17]</a><a name="ref17"> </a></sup>, |
| 976 |
and will be generated into its body. |
| 977 |
</p> |
| 978 |
<h2><a name="programming">Using the Generated EMF Classes</a></h2> |
| 979 |
<h3>Creating and accessing instances</h3> |
| 980 |
<p> |
| 981 |
Using the generated classes, a client program can create and initialize a Book |
| 982 |
with the following simple Java statements: |
| 983 |
</p> |
| 984 |
<pre> |
| 985 |
LibraryFactory factory = LibraryFactory.eINSTANCE; |
| 986 |
|
| 987 |
Book book = factory.createBook(); |
| 988 |
|
| 989 |
Writer writer = factory.createWriter(); |
| 990 |
writer.setName("William Shakespeare"); |
| 991 |
|
| 992 |
book.setTitle("King Lear"); |
| 993 |
book.setAuthor(writer);</pre> |
| 994 |
<p> |
| 995 |
Because the Book to Writer association (author) is two-way, the inverse |
| 996 |
reference (books) is automatically initialized. We can verify this by iterating |
| 997 |
over the books reference like this: |
| 998 |
</p> |
| 999 |
<pre> |
| 1000 |
System.out.println("Shakespeare books:"); |
| 1001 |
for (Iterator iter = writer.getBooks().iterator(); iter.hasNext(); ) |
| 1002 |
{ |
| 1003 |
Book shakespeareBook = (Book)iter.next(); |
| 1004 |
System.out.println(" title: " + shakespeareBook.getTitle()); |
| 1005 |
}</pre> |
| 1006 |
<p> |
| 1007 |
Running this program would produce output something like this: |
| 1008 |
</p> |
| 1009 |
<pre> |
| 1010 |
Shakespeare books: |
| 1011 |
title: King Lear</pre> |
| 1012 |
<h3>Saving and loading resources</h3> |
| 1013 |
<p> |
| 1014 |
To create a document named mylibrary.xmi containing the above model, all we need |
| 1015 |
to do is create an EMF resource at the beginning of the program, put the book |
| 1016 |
and writer into the resource, and call save() at the end: |
| 1017 |
</p> |
| 1018 |
<pre> |
| 1019 |
// Create a resource set. |
| 1020 |
ResourceSet resourceSet = new ResourceSetImpl(); |
| 1021 |
|
| 1022 |
// Register the default resource factory -- only needed for stand-alone! |
| 1023 |
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( |
| 1024 |
Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl()); |
| 1025 |
|
| 1026 |
// Get the URI of the model file. |
| 1027 |
URI fileURI = URI.createFileURI(new File("mylibrary.xmi").getAbsolutePath()); |
| 1028 |
|
| 1029 |
// Create a resource for this file. |
| 1030 |
Resource resource = resourceSet.createResource(fileURI); |
| 1031 |
|
| 1032 |
// Add the book and writer objects to the contents. |
| 1033 |
resource.getContents().add(book); |
| 1034 |
resource.getContents().add(writer); |
| 1035 |
|
| 1036 |
// Save the contents of the resource to the file system. |
| 1037 |
try |
| 1038 |
{ |
| 1039 |
resource.save(Collections.EMPTY_MAP); |
| 1040 |
} |
| 1041 |
catch (IOException e) {}</pre> |
| 1042 |
<p> |
| 1043 |
Notice that a resource set (interface ResourceSet) is used to create the EMF |
| 1044 |
resource. A resource set is used by the EMF framework to manage resources that |
| 1045 |
may have cross document references. Using a registry (interface |
| 1046 |
Resource.Factory.Registry), it creates the right type of resource for a given |
| 1047 |
URI based on its scheme, file extension, or other possible criteria. Here, |
| 1048 |
we register the XMI resource implementation as the default for this resource |
| 1049 |
set<sup><a class="footnote" href="#fn18">[18]</a><a name="ref18"> </a></sup>. |
| 1050 |
During load, the resource set also manages the demand-loading of cross document |
| 1051 |
references. |
| 1052 |
</p> |
| 1053 |
<p> |
| 1054 |
Running this program will produce the file mylibrary.xmi with contents something |
| 1055 |
like this: |
| 1056 |
</p> |
| 1057 |
<pre> |
| 1058 |
<xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" |
| 1059 |
xmlns:library="http:///library.ecore"> |
| 1060 |
<library:Book title="King Lear" author="/1"/> |
| 1061 |
<library:Writer name="William Shakespeare" books="/0"/> |
| 1062 |
</xmi:XMI></pre> |
| 1063 |
<p> |
| 1064 |
To load the document mylibrary.xmi, as saved above, we set up a resource set, |
| 1065 |
and then simply demand-load the resource into it, as follows: |
| 1066 |
</p> |
| 1067 |
<pre> |
| 1068 |
// Create a resource set. |
| 1069 |
ResourceSet resourceSet = new ResourceSetImpl(); |
| 1070 |
|
| 1071 |
// Register the default resource factory -- only needed for stand-alone! |
| 1072 |
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( |
| 1073 |
Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl()); |
| 1074 |
|
| 1075 |
// Register the package -- only needed for stand-alone! |
| 1076 |
LibraryPackage libraryPackage = LibraryPackage.eINSTANCE; |
| 1077 |
|
| 1078 |
// Get the URI of the model file. |
| 1079 |
URI fileURI = URI.createFileURI(new File("mylibrary.xmi").getAbsolutePath()); |
| 1080 |
|
| 1081 |
// Demand load the resource for this file. |
| 1082 |
Resource resource = resourceSet.getResource(fileURI, true); |
| 1083 |
|
| 1084 |
// Print the contents of the resource to System.out. |
| 1085 |
try |
| 1086 |
{ |
| 1087 |
resource.save(System.out, Collections.EMPTY_MAP); |
| 1088 |
} |
| 1089 |
catch (IOException e) {}</pre> |
| 1090 |
|
| 1091 |
<p> |
| 1092 |
Again, we create a resource set and, for the stand-alone case, register a |
| 1093 |
default resource implementation. Also, we need to ensure that our package is |
| 1094 |
registered in the package registry, which the resource uses to obtain the |
| 1095 |
appropriate metadata and factory for the model it is loading. Simply |
| 1096 |
accessing the eINSTANCE field of a generated package interface is sufficient |
| 1097 |
to ensure that it is registered. |
| 1098 |
</p> |
| 1099 |
<p> |
| 1100 |
This example uses the second form of save(), which takes an OutputStream, to |
| 1101 |
print the serialization to the console. |
| 1102 |
</p> |
| 1103 |
<p> |
| 1104 |
Splitting a model into multiple documents, with cross references between them, |
| 1105 |
is simple. If we wanted to serialize the books and writers, in the save example |
| 1106 |
above, into separate documents, all we need to do is create a second resource: |
| 1107 |
</p> |
| 1108 |
<pre> |
| 1109 |
Resource anotherResource = resourceSet.createResource(anotherFileURI);</pre> |
| 1110 |
<p> |
| 1111 |
and add the writer to it, instead of the first: |
| 1112 |
</p> |
| 1113 |
<pre> |
| 1114 |
<strike>resource.getContents().add(writer);</strike> // (replaced) |
| 1115 |
anotherResource.getContents().add(writer);</pre> |
| 1116 |
<p> |
| 1117 |
This would produce two resources, each containing one object, with a cross |
| 1118 |
document reference to the other. |
| 1119 |
</p> |
| 1120 |
<p> |
| 1121 |
Note that a containment reference necessarily implies that the contained object |
| 1122 |
is in the same resource as its container. So, for example, suppose that we had |
| 1123 |
created an instance of Library containing our Book via the books containment |
| 1124 |
reference. That would have automatically removed the Book from the contents of |
| 1125 |
the resource, which in this sense, also behaves like a containment reference. |
| 1126 |
If we then added the Library to the resource, the book would implicitly belong |
| 1127 |
to the resource as well, and its details would again be serialized in it. |
| 1128 |
</p> |
| 1129 |
<p> |
| 1130 |
If you want to serialize your objects in a format other than XMI, that can be |
| 1131 |
arranged as well. You will need to supply your own serialization and parsing |
| 1132 |
code. Create your own resource class (as a subclass of ResourceImpl) that |
| 1133 |
implements your preferred serialization format, and then either register it |
| 1134 |
locally with your resource set, or with the global factory registry if you want |
| 1135 |
it to always be used with your model. |
| 1136 |
</p> |
| 1137 |
<h3>Observing (adapting) EMF objects</h3> |
| 1138 |
<p> |
| 1139 |
Previously, when we looked at set methods in generated EMF classes, we saw that |
| 1140 |
notifications are always sent when an attribute or reference is changed. For |
| 1141 |
example, the BookImpl.setPages() method included the following line: |
| 1142 |
</p> |
| 1143 |
<pre> |
| 1144 |
eNotify(newENotificationImpl(this, ..., oldPages, pages));</pre> |
| 1145 |
<p> |
| 1146 |
Every EObject can maintain a list of observers (also referred to as adapters), |
| 1147 |
which will be notified whenever a state change occurs. The framework eNotify() |
| 1148 |
method iterates through this list and forwards the notification to the |
| 1149 |
observers. |
| 1150 |
</p> |
| 1151 |
<p> |
| 1152 |
An observer can be attached to any EObject (for example, book) by adding to the |
| 1153 |
eAdapters list like this: |
| 1154 |
</p> |
| 1155 |
<pre> |
| 1156 |
Adapter bookObserver = ... |
| 1157 |
book.eAdapters().add(bookObserver);</pre> |
| 1158 |
<p> |
| 1159 |
More commonly, however, adapters are added to EObjects using an adapter factory. |
| 1160 |
In addition to their observer role, adapters are more generally used as a way |
| 1161 |
to extend the behavior of the object they're attached to. A client generally |
| 1162 |
attaches such extended behavior by asking an |
| 1163 |
adapter factory to adapt an object with an extension of the required type. |
| 1164 |
Typically it looks something like this: |
| 1165 |
</p> |
| 1166 |
<pre> |
| 1167 |
EObject someObject = ...; |
| 1168 |
AdapterFactory someAdapterFactory = ...; |
| 1169 |
Object requiredType = ...; |
| 1170 |
if(someAdapterFactory.isFactoryForType(requiredType)) |
| 1171 |
{ |
| 1172 |
Adapter theAdapter = someAdapterFactory.adapt(someObject, requiredType); |
| 1173 |
... |
| 1174 |
}</pre> |
| 1175 |
<p> |
| 1176 |
Usually, the requiredType represents some interface supported by the adapter. |
| 1177 |
For example, the argument might be the actual java.lang.Class for an interface |
| 1178 |
of the chosen adapter. The returned adapter could then be downcast to the |
| 1179 |
requested interface like this: |
| 1180 |
</p> |
| 1181 |
<pre> |
| 1182 |
MyAdapter theAdapter = |
| 1183 |
(MyAdapter)someAdapterFactory.adapt(someObject, MyAdapter.class);</pre> |
| 1184 |
<p> |
| 1185 |
Adapters are often used this way to extend the behavior of an object without |
| 1186 |
subclassing. |
| 1187 |
</p> |
| 1188 |
<p> |
| 1189 |
To handle notifications in an adapter we need to override the eNotifyChanged() |
| 1190 |
method, which is called on every registered adapter by eNotify(). A typical |
| 1191 |
adapter implements eNotifyChanged() to perform some action for some or all of |
| 1192 |
the notifications, based on the notification's type. |
| 1193 |
</p> |
| 1194 |
<p> |
| 1195 |
Sometimes adapters are designed to adapt a specific class (for example, Book). |
| 1196 |
In this case, the notifyChanged() method might look something like this: |
| 1197 |
</p> |
| 1198 |
<pre> |
| 1199 |
public void notifyChanged(Notification notification) |
| 1200 |
{ |
| 1201 |
Book book = (Book)notification.getNotifier(); |
| 1202 |
switch (notification.getFeatureID(Book.class)) |
| 1203 |
{ |
| 1204 |
case LibraryPackage.BOOK__TITLE: |
| 1205 |
// book title changed |
| 1206 |
doSomething(); |
| 1207 |
break; |
| 1208 |
caseLibraryPackage.BOOK__CATEGORY: |
| 1209 |
// book category changed |
| 1210 |
... |
| 1211 |
case ... |
| 1212 |
} |
| 1213 |
}</pre> |
| 1214 |
<p> |
| 1215 |
The call to notification.getFeatureID() is passed the argument Book.class to |
| 1216 |
handle the possibility that the object being adapted is not an instance of |
| 1217 |
class BookImpl, but is instead an instance of a multiple-inheritance subclass |
| 1218 |
where Book is not the primary (first) interface. In that case, the feature ID |
| 1219 |
passed in the notification will be a number relative to the other class and |
| 1220 |
therefore needs to be adjusted before we can switch using the BOOK__ constants. |
| 1221 |
In single-inheritance situations, this argument is ignored. |
| 1222 |
</p> |
| 1223 |
<p> |
| 1224 |
Another common type of adapter is not bound to any specific class, but instead |
| 1225 |
uses the reflective EMF API to perform its function. Instead of calling |
| 1226 |
getFeatureID() on the notification, it might call getFeature() instead, which |
| 1227 |
returns the actual Ecore feature (that is, the object in the metamodel that |
| 1228 |
represents the feature). |
| 1229 |
</p> |
| 1230 |
<h3>Using the reflective API</h3> |
| 1231 |
<p> |
| 1232 |
Every generated model class can also be manipulated using the reflective API |
| 1233 |
defined in interface EObject: |
| 1234 |
</p> |
| 1235 |
<pre> |
| 1236 |
public interface EObject ... |
| 1237 |
{ |
| 1238 |
.. |
| 1239 |
Object eGet(EStructuralFeature feature); |
| 1240 |
void eSet(EStructuralFeature feature, Object newValue); |
| 1241 |
|
| 1242 |
boolean eIsSet(EStructuralFeature feature); |
| 1243 |
void eUnset(EStructuralFeature feature); |
| 1244 |
}</pre> |
| 1245 |
<p> |
| 1246 |
Using the reflective API, we could set the name of an author like this: |
| 1247 |
</p> |
| 1248 |
<pre> |
| 1249 |
writer.eSet(LibraryPackage.eINSTANCE.getWriter_Name(), "William Shakespeare");</pre> |
| 1250 |
<p> |
| 1251 |
or get the name like this: |
| 1252 |
</p> |
| 1253 |
<pre> |
| 1254 |
String name = (String)writer.eGet(LibraryPackage.eINSTANCE.getWriter_Name());</pre> |
| 1255 |
<p> |
| 1256 |
Notice that the feature being accessed is identified by metadata obtained from |
| 1257 |
the singleton instance of the library package. |
| 1258 |
</p> |
| 1259 |
<p> |
| 1260 |
Using the reflective API is slightly less efficient then calling the generated |
| 1261 |
getName() and setName() methods |
| 1262 |
directly<sup><a class="footnote" href="#fn19">[19]</a><a name="ref19"> </a></sup>, |
| 1263 |
but opens up the model for completely generic access. For example, the |
| 1264 |
reflective methods are used by the EMF.Edit framework to implement a full set of |
| 1265 |
generic commands (for example, AddCommand, RemoveCommand, SetCommand) that can |
| 1266 |
be used with any model. See the <a href="../../references/overview/EMF.Edit.html">EMF.Edit |
| 1267 |
Overview</a> for details. |
| 1268 |
</p> |
| 1269 |
<p> |
| 1270 |
In addition to eGet() and eSet(), the reflective API includes two other related |
| 1271 |
methods: eIsSet() and eUnset(). The eIsSet() method can be used to find out if |
| 1272 |
an attribute is set or |
| 1273 |
not<sup><a class="footnote" href="#fn20">[20]</a><a name="ref20"> </a></sup>, |
| 1274 |
while eUnset() can be used to unset (or reset) it. The generic XMI serializer, |
| 1275 |
for example, uses eIsSet() to determine which attributes need to be serialized |
| 1276 |
during a resource save operation. |
| 1277 |
</p> |
| 1278 |
<h2><a name="advanced">Advanced Topics</a></h2> |
| 1279 |
<h3><a name="flags">Generation control flags</a></h3> |
| 1280 |
<p> |
| 1281 |
There are several flags that can be set on a model feature to control the |
| 1282 |
generated code pattern for that feature. Typically, the default settings of |
| 1283 |
these flags will be fine, so you shouldn't need to change them very often. |
| 1284 |
</p> |
| 1285 |
<ul> |
| 1286 |
<li><p><strong>Unsettable</strong> (default is false)</p> |
| 1287 |
<p> |
| 1288 |
A feature that is declared to be unsettable has a notion of an explicit unset or |
| 1289 |
no-value state. For example, a boolean attribute that is not unsettable can take |
| 1290 |
on one of two values: true or false. If, instead, the attribute is declared to |
| 1291 |
be unsettable, it can then have any of three values: true, false, or unset. |
| 1292 |
</p> |
| 1293 |
<p> |
| 1294 |
The get method on a feature that is not set will return its default value, but |
| 1295 |
for an unsettable feature, there is a distinction between this state and when |
| 1296 |
the feature has been explicitly set to the default value. Since the unset state |
| 1297 |
is outside of the set of allowed values, we need to generate additional methods |
| 1298 |
to put a feature in the unset state and to determine if it is in that state. For |
| 1299 |
example, if the pages attribute in class Book is declared to be unsettable, then |
| 1300 |
we'll get two more generated methods: |
| 1301 |
</p> |
| 1302 |
<pre> |
| 1303 |
boolean isSetPages(); |
| 1304 |
void unsetPages();</pre> |
| 1305 |
<p> |
| 1306 |
in addition to the original two: |
| 1307 |
</p> |
| 1308 |
<pre> |
| 1309 |
int getPages(); |
| 1310 |
void setPages(int value);</pre> |
| 1311 |
<p> |
| 1312 |
The isSet method returns true if the feature has been explicitly set. The unset |
| 1313 |
method changes an attribute that has been set back to its unset state. |
| 1314 |
</p> |
| 1315 |
<p> |
| 1316 |
When unsettable is false, we don't get the generated isSet or unset methods, but |
| 1317 |
we still get implementations of the reflective versions: eIsSet() and eUnset() |
| 1318 |
(which every EObject must implement). For non-unsettable attributes, eIsSet() |
| 1319 |
returns true if the current value is different from the default value, and |
| 1320 |
eUnset() sets the feature to the default value (more like a reset). |
| 1321 |
</p> |
| 1322 |
</li> |
| 1323 |
<li><p><strong>ResolveProxies</strong> (default is true)</p> |
| 1324 |
<p> |
| 1325 |
ResolveProxies only applies to non-containment references. ResolveProxies |
| 1326 |
implies that the reference may span documents, and therefore needs to |
| 1327 |
include proxy checking and resolution in the get method, as described earlier |
| 1328 |
in this paper. |
| 1329 |
</p> |
| 1330 |
<p> |
| 1331 |
You can optimize the generated get pattern for references that you know will |
| 1332 |
never be used in a cross document scenario by setting resolveProxies to false. |
| 1333 |
In that case, the generated get method will be optimally |
| 1334 |
efficient<sup><a class="footnote" href="#fn21">[21]</a><a name="ref21"> </a></sup>. |
| 1335 |
</p> |
| 1336 |
</li> |
| 1337 |
<li><p><strong>Unique</strong> (default is true)</p> |
| 1338 |
<p> |
| 1339 |
Unique only applies to multiplicity-many attributes, indicating that such an |
| 1340 |
attribute may not contain multiple equal objects. References are always treated |
| 1341 |
as unique. |
| 1342 |
</p> |
| 1343 |
</li> |
| 1344 |
<li><p><strong>Changeable</strong> (default is true)</p> |
| 1345 |
<p> |
| 1346 |
A feature that is not changeable will not include a generated set method, and |
| 1347 |
the reflective eSet() method will throw an exception if you try to set it. |
| 1348 |
Declaring one end of a bi-directional relationship to be not changeable is a |
| 1349 |
good way to force clients to always set the reference from the other end, but |
| 1350 |
still provide convenient navigation methods from either end. Declaring one-way |
| 1351 |
references or attributes to be not changeable usually implies that the feature |
| 1352 |
will be set or changed by some other (user-written) code. |
| 1353 |
</p> |
| 1354 |
</li> |
| 1355 |
<li><p><strong>Volatile</strong> (default is false)</p> |
| 1356 |
<p> |
| 1357 |
A feature that is declared volatile is generated without storage fields and with |
| 1358 |
empty implementation method bodies, which you are required to fill in. Volatile |
| 1359 |
is commonly used for a feature whose value is derived from some other feature, |
| 1360 |
or for a feature that is to be implemented by hand using a different storage |
| 1361 |
and implementation pattern. |
| 1362 |
</p> |
| 1363 |
</li> |
| 1364 |
<li><p><strong>Derived</strong> (default is false)</p> |
| 1365 |
<p> |
| 1366 |
The value of a derived feature is computed from other features, so it doesn't |
| 1367 |
represent any additional object state. Framework classes, such as |
| 1368 |
EcoreUtil.Copier, that copy model objects will not attempt to copy such |
| 1369 |
features. The generated code is unaffected by the value of the derived flag, |
| 1370 |
except for the package implementation class, which initializes the metadata for |
| 1371 |
the model. Derived features are typically also marked volatile and transient. |
| 1372 |
</p> |
| 1373 |
</li> |
| 1374 |
<li><p><strong>Transient</strong> (default is false)</p> |
| 1375 |
<p> |
| 1376 |
Transient features are used to declare (modeled) data whose lifetime never spans |
| 1377 |
application invocations and therefore doesn't need to be persisted. The (default |
| 1378 |
XMI) serializer will not save features that are declared to be transient. Like |
| 1379 |
derived, transient's only effect on the generated code is the metadata |
| 1380 |
initialization in the package implementation class. |
| 1381 |
</p> |
| 1382 |
</li> |
| 1383 |
</ul> |
| 1384 |
|
| 1385 |
<h3>Data types</h3> |
| 1386 |
<p> |
| 1387 |
As mentioned previously, all the classes defined in a model (for example, Book, |
| 1388 |
Writer) implicitly derive from the EMF base class EObject. However, all the |
| 1389 |
classes that a model uses are not necessarily EObjects. For example, assume we |
| 1390 |
want to add an attribute of type java.util.Date to our model. Before we can do |
| 1391 |
so, we need to define an EMF DataType to represent the external type. In UML, we |
| 1392 |
use a class with the datatype stereotype for this purpose: |
| 1393 |
</p> |
| 1394 |
<img src="images/EMF/image009.gif" width="183" height="75" alt="Data type definition: datatype JavaDate is of javaclass java.util.Date"/> |
| 1395 |
<p> |
| 1396 |
As shown, a data type is simply a named element in the model that acts as a |
| 1397 |
proxy for some Java class. The actual Java class is provided as an attribute |
| 1398 |
with the javaclass stereotype, whose name is the fully qualified class being |
| 1399 |
represented. With this data type defined, we can now declare attributes of type |
| 1400 |
java.util.Date like this: |
| 1401 |
</p> |
| 1402 |
<img src="images/EMF/image010.gif" width="173" height="122" alt="Attribute with data type as attribute type: Book has a publicationDate : JavaDate"/> |
| 1403 |
<p> |
| 1404 |
If we regenerate, the publicationDate attribute will now appear in the Book |
| 1405 |
interface: |
| 1406 |
</p> |
| 1407 |
<pre> |
| 1408 |
import java.util.Date; |
| 1409 |
|
| 1410 |
public interface Book extends EObject |
| 1411 |
{ |
| 1412 |
... |
| 1413 |
|
| 1414 |
Date getPublicationDate(); |
| 1415 |
void setPublicationDate(Date value); |
| 1416 |
}</pre> |
| 1417 |
<p> |
| 1418 |
As you can see, this Date-typed attribute is handled pretty much like any other. |
| 1419 |
In fact, all attributes, including ones of type String, int, and so on, have a |
| 1420 |
data type as their type. The only thing special about the standard Java types is |
| 1421 |
that their corresponding data types are predefined in the Ecore model, so they |
| 1422 |
don't need to be redefined in every model that uses them. |
| 1423 |
</p> |
| 1424 |
<p> |
| 1425 |
A data type definition has one other effect on the generated model. Since data |
| 1426 |
types represent some arbitrary class, a generic serializer and parser (for |
| 1427 |
example, the default XMI serializer) has no way of knowing how to save the state |
| 1428 |
of an attribute of that type. Should it call toString()? That's a reasonable |
| 1429 |
default, but the EMF framework doesn't want to require that, so it generates two |
| 1430 |
more methods in the factory implementation class for every data type defined in |
| 1431 |
the model: |
| 1432 |
</p> |
| 1433 |
<pre> |
| 1434 |
/** |
| 1435 |
* @generated |
| 1436 |
*/ |
| 1437 |
public Date createJavaDateFromString(EDataType eDataType, String initialValue) |
| 1438 |
{ |
| 1439 |
return (Date)super.createFromString(eDataType, initialValue); |
| 1440 |
} |
| 1441 |
|
| 1442 |
/** |
| 1443 |
* @generated |
| 1444 |
*/ |
| 1445 |
public String convertJavaDateToString(EDataType eDataType, Object instanceValue) |
| 1446 |
{ |
| 1447 |
return super.convertToString(eDataType, instanceValue); |
| 1448 |
}</pre> |
| 1449 |
<p> |
| 1450 |
By default, these methods simply invoke the superclass implementations, which |
| 1451 |
provide reasonable, but inefficient, defaults: convertToString() simply calls |
| 1452 |
toString() on the instanceValue, but createFromString() tries, using Java |
| 1453 |
reflection, to call a String constructor or, failing that, a static valueOf() |
| 1454 |
method, if one exists. Typically you should take over these methods (by |
| 1455 |
removing the @generated tags) and change them to appropriate custom |
| 1456 |
implementations: |
| 1457 |
</p> |
| 1458 |
<pre> |
| 1459 |
/** |
| 1460 |
* <strike>@generated</strike> // (removed) |
| 1461 |
*/ |
| 1462 |
public String convertJavaDateToString(EDataType eDataType, Object instanceValue) |
| 1463 |
{ |
| 1464 |
return instanceValue.toString(); |
| 1465 |
)</pre> |
| 1466 |
<h3>Ecore Model</h3> |
| 1467 |
<p> |
| 1468 |
Here is the complete class hierarchy of the Ecore model (shaded boxes are |
| 1469 |
abstract classes): |
| 1470 |
</p> |
| 1471 |
<img src="images/EMF/image011.gif" width="611" height="456" alt="Ecore class hierarchy"/> |
| 1472 |
<p> |
| 1473 |
This hierarchy includes the classes that represent the EMF model elements |
| 1474 |
described in this paper: classes (and their attributes, references and |
| 1475 |
operations) data types, enumerations, packages and factories. |
| 1476 |
</p> |
| 1477 |
<p> |
| 1478 |
EMF's implementation of Ecore is itself generated using the EMF generator and as |
| 1479 |
such has the same lightweight and efficient implementation as described in the |
| 1480 |
previous sections of this paper. |
| 1481 |
</p> |
| 1482 |
<br/><hr noshade="noshade" size="1"/> |
| 1483 |
<p> |
| 1484 |
<sup><a class="footnote" href="#ref1">[1]</a><a name="fn1"> </a></sup>Actually, |
| 1485 |
the EMF meta model is itself an EMF model, the default serialized form of which |
| 1486 |
is XMI.</p> |
| 1487 |
<p> |
| 1488 |
<sup><a class="footnote" href="#ref2">[2]</a><a name="fn2"> </a></sup>Currently, |
| 1489 |
EMF supports import from Rational Rose, but the generator architecture can |
| 1490 |
easily accommodate other modeling tools as well. |
| 1491 |
</p> |
| 1492 |
<p> |
| 1493 |
<sup><a class="footnote" href="#ref3">[3]</a><a name="fn3"> </a></sup>EMF |
| 1494 |
uses a subset of the JavaBean simple property accessor naming patterns. |
| 1495 |
</p> |
| 1496 |
<p> |
| 1497 |
<sup><a class="footnote" href="#ref4">[4]</a><a name="fn4"> </a></sup>There |
| 1498 |
are several user-specifiable options that can be used to change the generated |
| 1499 |
patterns. We'll describe some of them later (see |
| 1500 |
<a href="#flags">Generation control flags</a>, later in this document). |
| 1501 |
</p> |
| 1502 |
<p> |
| 1503 |
<sup><a class="footnote" href="#ref5">[5]</a><a name="fn5"> </a></sup>Containment |
| 1504 |
references, which we'll describe later (see <a href="#containment">Containment |
| 1505 |
references</a>), cannot span documents. There is also a flag that users can set |
| 1506 |
in a reference's meta data to indicate that resolve does not need to be called |
| 1507 |
because the reference will never be used in a cross document scenario (see |
| 1508 |
<a href="#flags">Generation control flags</a>). In these cases, the |
| 1509 |
generated get method simply returns the pointer. |
| 1510 |
</p> |
| 1511 |
<p> |
| 1512 |
<sup><a class="footnote" href="#ref6">[6]</a><a name="fn6"> </a></sup>Applications |
| 1513 |
that need to deal with and handle broken links should call eIsProxy() on the |
| 1514 |
object returned by a get method to see if it is resolved or not (for example, |
| 1515 |
book.getAuthor().eIsProxy()). |
| 1516 |
</p> |
| 1517 |
<p> |
| 1518 |
<sup><a class="footnote" href="#ref7">[7]</a><a name="fn7"> </a></sup>This |
| 1519 |
clearly fails to allow for multiple authors, but it keeps the example model |
| 1520 |
simple. |
| 1521 |
</p> |
| 1522 |
<p> |
| 1523 |
<sup><a class="footnote" href="#ref8">[8]</a><a name="fn8"> </a></sup>The |
| 1524 |
reason we bother to delegate to a basicSet() method at all is because it's also |
| 1525 |
needed by the eInverseAdd() and eInverseRemove() methods, which we'll look at a |
| 1526 |
little later. |
| 1527 |
</p> |
| 1528 |
<p> |
| 1529 |
<sup><a class="footnote" href="#ref9">[9]</a><a name="fn9"> </a></sup>Actually, |
| 1530 |
all of the concrete EList implementations are simple subclasses of one very |
| 1531 |
functional and efficient base implementation class, EcoreEList. |
| 1532 |
</p> |
| 1533 |
<p> |
| 1534 |
<sup><a class="footnote" href="#ref10">[10]</a><a name="fn10"> </a></sup>In |
| 1535 |
eInverseAdd(), instead of simply switching on the supplied feature id, it first |
| 1536 |
calls eDerivedStructuralFeatureID(featureID, baseClass). For simple single |
| 1537 |
inheritance models, this method has a default implementation that ignores the |
| 1538 |
second argument and returns the featureID passed in. For models that use |
| 1539 |
multiple inheritance, eDerivedStructuralFeatureID() may have a generated |
| 1540 |
override that adjusts a feature ID relative to a mixin class (that is, |
| 1541 |
baseClass) to a feature ID relative to the concrete derived class of the |
| 1542 |
instance. |
| 1543 |
</p> |
| 1544 |
<p> |
| 1545 |
<sup><a class="footnote" href="#ref11">[11]</a><a name="fn11"> </a></sup>EObjectImpl |
| 1546 |
also has an int-typed eContainerFeatureID instance variable to keep track of |
| 1547 |
which reference is currently used for the eContainer. |
| 1548 |
</p> |
| 1549 |
<p> |
| 1550 |
<sup><a class="footnote" href="#ref12">[12]</a><a name="fn12"> </a></sup>See |
| 1551 |
<a href="https://www.oracle.com/technical-resources/articles/java/substitutes-missing-c-constructs.html" target="_blank">Replace Enums with Classes</a>. |
| 1552 |
</p> |
| 1553 |
<p> |
| 1554 |
<sup><a class="footnote" href="#ref13">[13]</a><a name="fn13"> </a></sup>To |
| 1555 |
conform to proper Java programming style, the static constant names are |
| 1556 |
converted to upper case if the modeled enumeration's literal names are not |
| 1557 |
already upper case. |
| 1558 |
</p> |
| 1559 |
<p> |
| 1560 |
<sup><a class="footnote" href="#ref14">[14]</a><a name="fn14"> </a></sup> While |
| 1561 |
your program isn't strictly required to use the Factory or Package interfaces, |
| 1562 |
EMF does encourage clients to use the factory to create instances by generating |
| 1563 |
protected constructors on the model classes, thereby preventing you from simply |
| 1564 |
calling new to create your instances. You can, however, change the access to |
| 1565 |
public in the generated classes manually, if that's what you really want. Your |
| 1566 |
preferences will not be overwritten if you later decide to regenerate the |
| 1567 |
classes. |
| 1568 |
</p> |
| 1569 |
<p> |
| 1570 |
<sup><a class="footnote" href="#ref15">[15]</a><a name="fn15"> </a></sup>Actually, |
| 1571 |
the first base class in the Ecore model is the one used as the implementation |
| 1572 |
base class. In the UML diagram, the <<extend>> stereotype is needed to |
| 1573 |
indicate that Book should be first in the Ecore representation. |
| 1574 |
</p> |
| 1575 |
<p> |
| 1576 |
<sup><a class="footnote" href="#ref16">[16]</a><a name="fn16"> </a></sup>If |
| 1577 |
you know ahead of time that you're going to want to provide your own custom |
| 1578 |
implementation for some feature, then a better way of doing this is to model the |
| 1579 |
attribute as volatile, which instructs the generator to only generate a skeleton |
| 1580 |
method body in the first place, which you are then expected to implement. |
| 1581 |
</p> |
| 1582 |
<p> |
| 1583 |
<sup><a class="footnote" href="#ref17">[17]</a><a name="fn17"> </a></sup>EMF |
| 1584 |
includes a generic mechanism for annotating metamodel objects with additional |
| 1585 |
information. This mechanism can also be used to attach user documentation to |
| 1586 |
elements of the model, and when a model is created from XML Schema, EMF |
| 1587 |
relies on it to capture serialization details that cannot be expressed |
| 1588 |
directly using Ecore. |
| 1589 |
</p> |
| 1590 |
<p> |
| 1591 |
<sup><a class="footnote" href="#ref18">[18]</a><a name="fn18"> </a></sup>The |
| 1592 |
second line of the above code is only required when run stand-alone (that is, |
| 1593 |
directly invoked in a JVM, with the required EMF JAR files on the class path). |
| 1594 |
The same registration is automatically made in the global resource factory |
| 1595 |
registry when EMF is run within Eclipse. |
| 1596 |
</p> |
| 1597 |
<p> |
| 1598 |
<sup><a class="footnote" href="#ref19">[19]</a><a name="fn19"> </a></sup>Implementations |
| 1599 |
of the reflective methods are also generated for each model class. They |
| 1600 |
switch on the feature type, and simply call the appropriate generated typesafe |
| 1601 |
methods. |
| 1602 |
</p> |
| 1603 |
<p> |
| 1604 |
<sup><a class="footnote" href="#ref20">[20]</a><a name="fn20"> </a></sup>See |
| 1605 |
the Unsettable flag under <a href="#flags">Generation control flags</a> for |
| 1606 |
what constitutes a set attribute. |
| 1607 |
</p> |
| 1608 |
<p> |
| 1609 |
<sup><a class="footnote" href="#ref21">[21]</a><a name="fn21"> </a></sup>Think |
| 1610 |
carefully before declaring a feature to not resolve proxies. Just because you |
| 1611 |
don't need to use the reference in a cross document situation doesn't mean that |
| 1612 |
someone else who wants to use your model may not. Declaring a feature to not |
| 1613 |
resolve proxies is kind of like declaring a Java class to be final. |
| 1614 |
</p> |
| 1615 |
</body> |
| 1616 |
</html> |