Advanced OCL Modeling

Like any other language, OCL has an order of precedence for operators, variable declarations, and logical constructs (only for evaluating your expressions, not for program flow). The following sections describe constructs that you can use in any OCL expression.

Conditionals

OCL supports basic boolean expression evaluation using the if-then-else-endif keywords. The conditions are used only to determine which expression is evaluated; they can't be used to influence the underlying system or to affect program flow. The following invariant enforces that a student's year of graduation is valid only if she has paid her tuition:

    context Student inv:
    if tuitionPaid = true then
      yearOfGraduation = 2005
    else
      yearOfGraduation = 0000
    endif

OCL's logic rules are slightly different from typical programming language logic rules. The boolean evaluation rules are:

  1. True OR-ed with anything is true.

  2. False AND-ed with anything is false.

  3. False IMPLIES anything is true.

The implies keyword evaluate the first half of an expression, and, if that first half is true, the result is taken from the second half. For example, the following expression enforces that if a student's GPA is less than 1.0, their year of graduation is set to 0. If the GPA is higher than 1.0, Rule #3 applies, and the entire expression is evaluated as true (meaning the invariant is valid).

    context Student inv:
    self.GPA < 1.0 IMPLIES self.yearOfGraduation = 0000

OCL's boolean expressions are valid regardless of the order of ...

Get UML 2.0 in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.