Exercise 4. Validation Framework

What This Exercise Is About

Using your the Purchase Order model and instance data from Exercise 2, you will now validate the data - and correct any errors found - using EMF's Validation Framework. Optionally, you will add a named constraint to the XML Schema from which the model code was generated, and implement the validation code required to activate the constraint in your PurchaseOrder model editor.

Primer PO model


What You Should Be Able To Do

At the end of the lab, you should be able to:

Required Materials

Exercise Instructions

This exercise is carried out entirely using the Eclipse Software Development Kit (SDK) version 3.2 with the Eclipse Modeling Framework (EMF) 2.2 installed into it. The exercise instructions refer to this product as either Eclipse or as "the workbench."

In your workspace, there should be a EMF_Workshop/Exercise4_Validation folder containing Exercise4.java. This is the class you will modify to perform the work noted above. There is also a new version of PurchaseOrder.xsd, which will be used for the optional part of this exercise.


Directions

Step A: Verify workbench configuration

  1. See Exercise 1, Step A.

Step B: Setup

  1. You will need to complete Exercises 1 and 2 before proceeding with this exercise. The solutions can also be copied from the Solution1 and Solution2 folders, respectively. Notice that you'll need to create the four projects -- com.example.po, com.example.po.edit, com.example.po.editor, and Exercises -- at least as simple projects. Then, you can use the Navigator view to copy the contents of the solutions' projects, including the .project and .classpath files. Exercise 3 is not required to complete Exercise 4.
  2. Copy Exercise4.java from the Exercise 4 source folder into Exercises/src/exercises.

Step C: Validate Model Instance Data

  1. Open Exercise4.java, in Exercises/src/exercises.
  2. Work your way through the exercise by filling in the missing code for the first two TODO comments. As before, if you run into trouble or are pressed for time, you can copy code from the JPages folder under the Exercise 4 source folder.
  3. Implement the method printDiagnostic(), as referenced by the second TODO.
  4. Run your code. (SHIFT-ALT-X, J).
  5. You should see output similar to the following, though the path to po.xml may differ in your output. Line breaks and emphasis have been added.

      ERROR Diagnosis of com.example.po.impl.PurchaseOrderImpl@78777877
        {file:/c:/EMF_Workshop_Root/workspace/Exercises/data/po.xml#//@order}
      -> ERROR The feature 'partNum' of 'com.example.po.impl.ItemImpl@737a737a
          {file:/c:/EMF_Workshop_Root/workspace/Exercises/data/po.xml#//@order/@items.0}' 
          contains a bad value
         -> ERROR The value 'SWH123456' must be match one of {'\d{3}-[A-Z]{2}'}
      -> ERROR The feature 'partNum' of 'com.example.po.impl.ItemImpl@78e278e2
           {file:/c:/EMF_Workshop_Root/workspace/Exercises/data/po.xml#//@order/@items.1}' 
           contains a bad value
         -> ERROR The value 'STV999876' must be match one of {'\d{3}-[A-Z]{2}'}


Step D: Fix Data, Revalidate, and Save

  1. Continue filling in the missing code for the next two TODO comments.
  2. Run again (CTRL-F11). You should now see OK in the console log, after the output above.
  3.   OK

  4. Complete the final TODO in order to save the purchase order to "po-valid.xml", in the data directory.
  5. Run again. This time you should have a new file created, data/po-valid.xml.

Summary

You validated model instance data to verify if its contents matched its schema. Discovering a problem with the data, you repaired the problem, revalidated the model data, and saved your changes. If you would like to explore implementing a named constraint, please continue with the optional part of this exercise.