Section B.2. OCL Syntax


B.2. OCL Syntax

The remainder of this chapter uses examples from the class diagram shown in Figure B-1.

B.2.1. Constraints on Classifiers

Each OCL expression must have some sense of context that an expression relates to. Often the context can be determined by where the expression is written. For example, you can link a constraint to an element using a note. You can refer to an instance of the context classifier using the keyword self. For example, if you had a constraint on Student that their GPA must always be higher than 2.0, you can attach an OCL expression to Student using a note and refer to the GPA as follows:

     self.GPA > 2.0

Figure B-1. Example class diagram used in this chapter


It's important to realize that this OCL expression is an invariant, meaning the system would be in an invalid state if a student's GPA dropped to less than 2.0. If you want to allow a GPA of less than 2.0 and send out a letter to the student's parents in the event such a low GPA is achieved, you would model such behavior using a UML diagram such as an activity or interaction diagram.

You can follow associations between classifiers using the association end names as though they were attributes of the originating classifier. The following invariant on Course ensures that the instructor is being paid:

     self.instructor.salary > 0.00

If an association has a multiplicity of 0..1, you can treat the association end as a Set and check to see if the value is set by using the built-in notEmpty( ) operation. To call the notEmpty( ) operation on a set you must use an arrow (->) rather than a dot (.). See "Collections" for more information on sets. The following invariant on Course enforces that a course has an instructor:

     self.instructor->notEmpty(  )

If an association role name isn't specified, you can use the classifier name. The following invariant on School checks that each course has a room assignment:

     self.Course->forAll(roomAssignment <> 'No room!')

Comments can be inserted into an expression by prefixing each comment with two dashes (), like this:

     -- make sure this student could graduate     self.GPA > 2.0

If you can't determine the context of an expression by looking at the UML model, or if you want to be explicit with the context, use the OCL keyword context, followed by the classifier name. If you use this notation, you should say what the expression represents. In the following case, we're showing an invariant, so we use the keyword inv. Other types of expressions are explained in later sections.

     context Student     inv: self.GPA > 2.0

Instead of using the keyword self, you can assign a name to a classifier that you can use in the expression. Write the name you want to use, followed by a colon (:) and then the classifier name. For example, you can name the instance of Student as s:

     context s : Student     inv: s.GPA > 2.0

Finally, you can name an expression by placing a label after the expression type but before the colon (:). The label is purely decorative; it serves no OCL function.

     context s : Student     inv minimumGPARule: s.GPA > 2.0

B.2.2. Constraints on Operations

Beyond basic classifiers, OCL expressions can be associated with operations to capture preconditions and postconditions. Place the signature of the target operation (classifier, operation name, parameters, etc.) after the context keyword. Instead of the invariant keyword, use either pre or post for preconditions and postconditions, respectively.

The following expression ensures that any student who will be registered for a course has paid their tuition:

     context Course::registerStudent(s : Student) : boolean     pre: s.tuitionPaid = true

When writing postconditions, you can use the keyword result to refer to the value returned by an operation. The following expressions verify that a student's tuition was paid before registering for a course and that the operation registerStudent returned true:

     context Course::registerStudent(s : Student) : boolean     pre: s.tuitionPaid = true     post: result = true

As you can with invariants, you can name preconditions and postconditions by placing a label after the pre or post keywords:

     context Course::registerStudent(s : Student) : boolean     pre hasPaidTuition: s.tuitionPaid = true     post studentWasRegistered: result = true

Postconditions can use the @pre keyword to refer to the value of some element before an operation executes. The following expression ensures that a student was registered and that the number of students in the course has increased by 1. This expression uses the self keyword to reference the object that owns the registerStudent operation.

     context Course::registerStudent(s : Student) : boolean     pre: s.tuitionPaid = true     post: result = true AND self.students = self.students@pre + 1

You may specify the results of a query operation using the keyword body. Because OCL doesn't have syntax for program flow, you are limited to relatively simple expressions. The following expression indicates that honors students are students with GPAs higher than 3.5. The collection syntax used in this example is described in the "Collections" section.

     context Course::getHonorsStudents(  ) : Student     body: self.students->select(GPA > 3.5)

B.2.3. Constraints on Attributes

OCL expressions can specify the initial and subsequent values for attributes of classifiers. When using OCL expressions with attributes, you state the context of an expression using the classifier name, two colons (::), the attribute name, another colon (:), and then the type of the attribute. You specify the initial value of an attribute using the keyword init:

     context School::tuition : float     init: 2500.00

You can specify the value of attributes after their initial value using the derive keyword. The following example increases the tuition value by 10% every time you query it:

     context: School::tuition : float     derive: tution * 10%




UML 2.0 in a Nutshell
UML 2.0 in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596007957
EAN: 2147483647
Year: 2005
Pages: 132

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net