Simple Constraints

Simple constraints that involve one object, or two objects and their link, provide the foundation for working with more complex constraints that involve multiple objects and their links.

Attributes

To specify a rule about the values of an object’s attributes, you can refer to an object’s attribute from the object itself by using the name of the attribute.

Within the context of an organization, the following expression indicates that the name of an organization may not be an empty string:

Name <> ''

Using the keyword self, this rule may be described as follows:

self.Name <> ''

Notice that a literal string is enclosed in single quotes.

Figure 10-2 updates Figure 10-1 to show how the rule is captured. Look specifically at the top compartment for Organization.

Organizations with an OCL expression indicating that organizations must have names

Figure 10-2. Organizations with an OCL expression indicating that organizations must have names

Within the context of a person, the following expression indicates that a person must work more than 10 hours and less than or equal to 40 hours:

HoursAvailable > 10 and HoursAvailable <= 40

Using the keyword self, this rule may be described as follows:

self.HoursAvailable > 10 and self.HoursAvailable <= 40

Notice that the keyword self is used at the beginning of each occurrence of the HoursAvailable attribute. Also notice that I can access this attribute even though it is private, because the expression is written within the ...

Get Learning UML 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.