File: EMF.Validation.html

package info (click to toggle)
eclipse-emf 2.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 50,964 kB
  • sloc: java: 407,889; xml: 7,165; sh: 200; makefile: 13
file content (340 lines) | stat: -rwxr-xr-x 13,301 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" href="images/../../../css/book.css" type="text/css"/>
<link rel="stylesheet" href="images/../../../css/emf-book.css" type="text/css"/>

<title>EMF Validation Overview</title>
</head>
<body lang="EN-US" xml:lang="EN-US">
<h1>EMF Validation Overview</h1>
<p>
Last updated: June 23, 2005
</p>
<p>
This paper presents a basic overview of EMF's validation framework and gives a few code samples to ease implementation. For 
a more complete description of all the features of EMF, refer to
<a href="http://www.informit.com/title/9780321331885">EMF: Eclipse Modeling
Framework, Second Edition</a> (Addison-Wesley Professional, 2008) or to the
Javadoc for the framework classes themselves.
</p>
<h2>Introduction</h2>

<p>Want to ensure your model's data conforms to constraints placed on that data? In three quick steps, this overview will give you an idea of how EMF's validation framework will let you do just that.</p>

<hr/>

<h2>Definitions</h2>

<table><tr valign="top"><td>

<p><strong>Invariant</strong> - Implemented as class method, defined on the model. Considered a "stronger" statement about validity than a constraint. Eg., <tt class="code">hasUSState()</tt></p>

<p><a name="fig1"></a><img src="images/EMF.Validation/image001.gif" width="131" height="51" border="0" alt="Rose Model - Invariant"/><br/>
<i>Figure 1. Rose Model - Invariant</i></p>

</td>
<td bgcolor="" width="1"><img src="http://www.eclipse.org/emf/images/c.gif" width="3" height="5" alt=""/></td>
<td bgcolor="black" width="1"><img src="http://www.eclipse.org/emf/images/c.gif" width="1" height="5" alt=""/></td>
<td bgcolor="" width="1"><img src="http://www.eclipse.org/emf/images/c.gif" width="3" height="5" alt=""/></td>
<td>

<p><strong>Named Constraint</strong> - Implemented as a method on an external validator class, not on the model itself. Considered a "weaker" statement about validity than an invariant. Eg., <tt class="code">NonNegativeQuantity</tt>, <tt class="code">ValidShipDate</tt></p>

<p><a name="fig2"></a><img src="images/EMF.Validation/image002.gif" width="242" height="300" border="0" alt="Rose Model - Constraint"/><br/>
<i>Figure 2. Rose Model - Constraint</i></p>

</td>
<td bgcolor="" width="1"><img src="http://www.eclipse.org/emf/images/c.gif" width="3" height="5" alt=""/></td>
<td bgcolor="black" width="1"><img src="http://www.eclipse.org/emf/images/c.gif" width="1" height="5" alt=""/></td>
<td bgcolor="" width="1"><img src="http://www.eclipse.org/emf/images/c.gif" width="3" height="5" alt=""/></td>
<td>

<p><strong>Schema Based Constraint</strong> - as with Named Constraint, but defined within schema. Eg., '<tt class="code">quantity</tt> must be an <tt class="code">int</tt> between 0 and 100'. Since these types of constraints have known behaviours, no additional work is required to implement them: all simple type constraints will be automatically implemented by the code generator.</p>

<pre>
  &lt;xsd:element name="quantity"&gt;
    &lt;xsd:simpleType&gt;
      &lt;xsd:restriction base="xsd:int"&gt;
        &lt;xsd:minInclusive value="0"/&gt;
        &lt;xsd:maxInclusive value="100"/&gt;
      &lt;/xsd:restriction&gt;
    &lt;/xsd:simpleType&gt;
  &lt;/xsd:element&gt;</pre>

</td></tr></table>

<hr/>

<h2>Contents<a id="contents" name="contents">&nbsp;</a></h2>

<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td valign="top">Step 1:</td>
<td valign="top"><a href="#step1">Create Constraints in the Model</a>
</td>
</tr>

<tr>
<td valign="top">Step 2:</td>
<td valign="top"><a href="#step2">Define Constraints</a>
</td>
</tr>

<tr>
<td valign="top">Step 3:</td>
<td valign="top"><a href="#step3">Generate &amp; Run</a> </td>
</tr>

<tr>
<td valign="top">Appendix:</td>
<td valign="top"><a href="#appendix">Advanced Topics</a> </td>
</tr>

</table>

<hr/>
<p style="text-align: right"><a id="step1" name="step1">&nbsp;</a><a href="#contents">contents</a></p>

<h3>Step 1: Create Constraints in the Model</h3>

<p>As with all EMF development, you can start from annotated Java, XML Schema, or Rose models. The method for defining constraints depends on your model source.</p>

<p>For Rose, the <tt class="code">&lt;&lt;inv&gt;&gt;</tt> stereotype on an operation represents an invariant. For an example of this, see <a href="#fig1">Figure 1</a> above. </p>

<p>To add a constraint using Rose 98, open your model, then select a class (such as "Item" in the example discussed here). Right-click and select "Open Specification" (or double-click "Item"). Then select the "Ecore" tab and click the constraints field to add them, as in <a href="#fig2">Figure 2</a> above.</p>

<p>For Schema, you can define a <tt class="code">&lt;xsd:restriction/&gt;</tt> (as above) or named constraint:</p>

<pre>
  &lt;xsd:complexType name="Item"&gt;
    &lt;xsd:annotation&gt;
       &lt;xsd:appinfo source="http://www.eclipse.org/emf/2002/Ecore" 
       ecore:key="constraints"&gt;
         NonNegativeQuantity ValidShipDate
       &lt;/xsd:appinfo&gt;
    &lt;/xsd:annotation&gt;
  ...
  &lt;/xsd:complexType&gt;</pre>

<p>For annotated Java, you can define invariants and constraints like this: </p>

<table><tr valign="top"><td>

<p><strong>Invariant</strong></p>

<pre>
  /**
    * @model
    */
    public interface USAddress
    {
      ...
      /**
       * @model
       */
      boolean hasUSState(
        DiagnosticChain diagnostics, 
        Map context);
      ...
    }</pre>

</td>

<td bgcolor="" width="1"><img src="http://www.eclipse.org/emf/images/c.gif" width="3" height="5" alt=""/></td>
<td bgcolor="black" width="1"><img src="http://www.eclipse.org/emf/images/c.gif" width="1" height="5" alt=""/></td>
<td bgcolor="" width="1"><img src="http://www.eclipse.org/emf/images/c.gif" width="3" height="5" alt=""/></td>
<td>

<p><strong>Constraint</strong></p>

<pre>
  /**
   * @model 
  annotation="http://www.eclipse.org/emf/2002/Ecore 
  constraints='NonNegativeQuantity ValidShipDate'"
   */
    public interface Item
    {
       ...
    }</pre>

</td></tr></table>

<hr/>
<p style="text-align: right"><a id="step2" name="step2">&nbsp;</a><a href="#contents">contents</a></p>

<h3>Step 2: Define Constraints</h3>

<p>Import your model into EMF, then generate code as in <a href="../../tutorials/clibmod/clibmod.html#step2">Steps 2 - 3 of the Generating an EMF Model tutorial</a></p>

<p>Generated code will look something like this:</p>

<table><tr valign="top"><td>

<p><strong>Invariant</strong>, com.example.ppo.impl.USAddressImpl</p>

<pre>  public boolean hasUSState(
    DiagnosticChain diagnostics, 
    Map context)
  {
    // TODO: implement this method
    // -> specify the condition that violates 
    //    the invariant
    // -> verify the details of the diagnostic, 
    //    including severity and message
    // Ensure that you remove @generated or 
    // mark it @generated NOT
    <tt class="code" style="background-color:yellow">if (false)</tt>
    {
      if (diagnostics != null)
      {
        diagnostics.add(
          new BasicDiagnostic(
             Diagnostic.ERROR,
             PPOValidator.DIAGNOSTIC_SOURCE,
             PPOValidator.US_ADDRESS__HAS_US_STATE,
             EcorePlugin.INSTANCE.getString(
               "_UI_GenericInvariant_diagnostic", 
               new Object[] {
                 "hasUSState",
                 EObjectValidator.getObjectLabel(this, 
                   context)}),
             new Object [] { this }));
      }
      return false;
    }
    return true;
  }</pre>

</td>

<td bgcolor="" width="1"><img src="http://www.eclipse.org/emf/images/c.gif" width="3" height="5" alt=""/></td>
<td bgcolor="black" width="1"><img src="http://www.eclipse.org/emf/images/c.gif" width="1" height="5" alt=""/></td>
<td bgcolor="" width="1"><img src="http://www.eclipse.org/emf/images/c.gif" width="3" height="5" alt=""/></td>
<td>

<p><strong>Constraint</strong>, com.example.ppo.util.PPOValidator</p>

<pre>  public boolean validateItem_ValidShipDate(
    Item item, DiagnosticChain diagnostics, 
    Map context)
  {
    // TODO implement the constraint
    // -> specify the condition that violates 
    //    the constraint
    // -> verify the diagnostic details, 
    //    including severity, code, and message
    // Ensure that you remove @generated or 
    // mark it @generated NOT
    <tt class="code" style="background-color:yellow">if (false)</tt>
    {
      if (diagnostics != null)
      {
        diagnostics.add(
          new BasicDiagnostic(
            Diagnostic.ERROR,
            DIAGNOSTIC_SOURCE,
            0,
            EcorePlugin.INSTANCE.getString(
              "_UI_GenericConstraint_diagnostic",
              new Object[] {
                "ValidShipDate", 
                getObjectLabel(item, context) }),
            new Object[] { item }));
      }
      return false;
    }
    return true;
  }</pre>

</td></tr></table>

<p>Code is also generated in the validator, eg., in com.example.ppo.util.PPOValidator, for each defined invariant, but these method(s) simply delegate to the invariant method(s) on the objects themselves, eg.:</p>

<pre>  public boolean validateUSAddress_hasUSState(USAddress usAddress, 
    DiagnosticChain diagnostics, Map context)
  {
    return usAddress.hasUSState(diagnostics, context);
  }</pre>

<p>In both cases, generated code must be modified by hand in order to explain to EMF how to implement the invariant or constraint. For the first case, change the first line from:</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;<tt class="code" style="background-color:yellow">if (false)</tt></p>

<p>to</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;<tt class="code" style="background-color:yellow">if ("US".equals(getCountry()) &amp;&amp; getState() == null)</tt>.</p>

<hr/>
<p style="text-align: right"><a id="step3" name="step3">&nbsp;</a><a href="#contents">contents</a></p>

<h3>Step 3: Generate &amp; Run</h3>

<p>Once you're done defining constraints, launch a new workspace. For details, see <a href="../../tutorials/clibmod/clibmod.html#step4">Step 4 of the Generating an EMF Model tutorial</a>.</p>

<p>Your model will now have a 'Validate' item on its editor menu.</p>

<p><img src="images/EMF.Validation/image003.gif" width="397" height="227" border="0" alt="Start Validation from UI"/><br/>
<i>Figure 3. Start Validation from UI</i></p>

<p>With invalid data in the model, validation fails with a dialog like that in Figure 4.</p>

<p><img src="images/EMF.Validation/image004.gif" width="349" height="246" border="0" alt="Validation Problems Dialog"/><br/>
<i>Figure 4. Validation Problems Dialog</i></p>

<p>Selecting one of the diagnostics in the dialog before clicking the OK button will cause the object that caused the violation to be selected in the editor.  Markers for these diagnostics will also appear in Eclipse's Problems view.</p>

<hr/>
<p style="text-align: right"><a id="appendix" name="appendix">&nbsp;</a><a href="#contents">contents</a></p>

<h3>Advanced Topics</h3>

<p>To implement validation in another way, such as when saving or opening a file, you need only do something like:</p>

<pre>  public static boolean validateObject(EObject eObject)
  {
    Diagnostic diagnostic = Diagnostician.INSTANCE.validate(eObject);
    return diagnostic.getSeverity() == Diagnostic.OK;
  }</pre>

<p>Using a <tt class="code">Diagnostician</tt>, the result of the evaluation is calculated based on a <tt class="code">Diagnostic</tt>, not a simple boolean return.  This allows you to decide which severity represents a failure, and to expose the information about the constraints and invariants that were not satisfied.  As an example, if you were interested only in errors and warnings, you could do something like this:</p>

<pre>  public static boolean validateObject(EObject eObject)
  {
    Diagnostic diagnostic = Diagnostician.INSTANCE.validate(eObject);
    if (diagnostic.getSeverity() == Diagnostic.ERROR || 
      diagnostic.getSeverity() == Diagnostic.WARNING)
    {
      System.err.println(diagnostic.getMessage());
      for (Iterator i=diagnostic.getChildren().iterator(); i.hasNext();)
      {
        Diagnostic childDiagnostic = (Diagnostic)i.next();
        switch (childDiagnostic.getSeverity())
        {
          case Diagnostic.ERROR:
          case Diagnostic.WARNING:
            System.err.println("\t" + childDiagnostic.getMessage());
        }
      }
      return false;
    }
    return true;
  }</pre>


<p>Note that under the covers using <tt class="code">Diagnostician.INSTANCE.validate()</tt>, you get some intrinsic <tt class="code">Ecore</tt> constraint validation thanks to <tt class="code">EObjectValidator</tt>, the base for all generated package validator classes. These include:</p>

<ul>
<li>The actual multiplicities of the attributes and references match the bounds defined in the model.</li>
<li>The defined data type of the attributes is respected.</li>
<li>Any cross referenced objects are contained in resources.</li>
<li>Every proxy is properly resolved.</li>
</ul>

<p>&nbsp;</p>

</body>
</html>