10.1 Expressions
As discussed in Chapter 9, a
property is a
characteristic
of a model element shown as part of a comma-delimited list of text
strings inside a pair of braces ({}) after or below the name of a
model element, and a constraint
is
a rule for a model element shown as a text string within the property
list. An OCL expression is a constraint that
yields a value when evaluated. An invariant is
an expression
attached to a
model element that must hold true for all instances of that model
element. That is, an expression attached to a class means that each
object of the class must satisfy the expression, and an expression
attached to an association means that each link of the association
must satisfy the expression. When an expression is evaluated for a
specific object or link, the object or link is known as the
contextual instance, and
the optional
keyword self in an expression refers to that
contextual instance.An OCL expression is evaluated from left to right. For an expression
attached to an object, the expression may reference its attributes
and tags, side effect-free operations, links and link objects, and
the objects with which it is linked through the link ends of its
links. For an expression attached to a link, the expression may
reference its attributes and tags, side effect-free operations, and
the objects it links through its link ends. A side
effect-free operation is an operation
that does not modify the attributes of an
object or link, but may calculate and return a value. As each part of
an expression is evaluated, each intermediate resulting value may be
a single object or a collection of objects called a
collection that is further used in the
evaluation of the overall expression.Expressions may involve integers and real numbers, strings, and other
objects defined in a model. For integers and real numbers, you can
use the arithmetic operators and comparison operators shown in Table 10-1. For strings, you can use the
= operator for "equal
to" and the <> operator for
"not equal to." In addition,
expressions may use the logical operators, also shown in Table 10-1. As an expression is evaluated from left to
right, the unary operators have the highest precedence, followed by
the arithmetic operators, and then followed by the logical operators.
These operations are used in the OCL much the way they are used in
Java, C++, C#, or any other similar programming language with which
you may
be
familiar.
|
Type | Operator | Description |
---|---|---|
Arithmetic | + | Addition |
- | Subtraction and unary minus | |
* | Multiplication | |
/ | Division | |
Comparison | < | Less than |
> | Greater than | |
<= | Less than or equal to | |
>= | Greater than or equal to | |
<> | Not equal to | |
= | Equal to | |
Logical operators | Not | Unary not |
And | And | |
Or | Or | |
Xor | Exclusive or |
organizations, projects, teams, people, and people's
roles on teams in the project management system. An organization
relates to zero or more projects, zero or more teams, and zero or
more people who are employees of the organization. A project relates
to a single organization and to a single team. A team relates to a
single organization and to a single project. A person relates to a
single organization that is an employer. A team relates to zero or
more people as members of the team in which a person plays a role. A
person relates to a single team in which the person plays a role.
Figure 10-1. Organizations, projects, teams, and people

Figure 10-1 also shows various details about
organizations, projects, teams, people, and people's
roles on teams in the project management system. A project has a name
that is a string, a start date that is a string, an end date that is
a string, a budget that is a real number, and an operation to ensure
that the start date and end date of the project are valid (that the
project's start date precedes its end date). Each
team and organization has a name that is a string. A person has an
identification number that is an integer, a name that is a string,
hours that they are available to work presented as an integer, an
operation to retrieve the number of hours that they are available to
work, and an operation to determine whether the number of hours that
they are available to work is within a range of a minimum and maximum
number of hours. The relationship between a person and a team defines
the title as a string of the role that the person plays on the team.
All the attributes and operations are public, but a
project's start and end date and the hours they are
available to work are private.I'll use Figure 10-1 throughout the
remainder of this chapter as the basis for showing you how to
apply the OCL.