Chapter 8: Specifying Operations
Overview
This chapter covers techniques for specifying operations in object-oriented systems analysis and design, focusing on Activity Diagrams and Object Constraint Language (OCL).
1.0 The Role of Operation Specifications
Operation specifications serve three critical purposes:
1.1 From Analysis Perspective
- Ensure users’ needs are understood - Validates that requirements are correctly captured
1.2 From Design Perspective
- Guide programmers to appropriate implementation - Provides clear direction for coding methods
1.3 From Test Perspective
- Verify method functionality - Ensures the method performs as originally intended
2.0 Key Components of Operation Specifications
Every operation specification should include:
- Intent or purpose of the operation
- Operation signature including return type
- Description of the logic
- Other operations called (in same object or other objects)
3.0 Techniques for Describing Operation Logic
3.1 Algorithmic Techniques
- Structure English (not covered)
- Pseudo code (not covered)
- Activity Diagrams ✓ (covered in this unit)
3.2 Non-algorithmic Techniques
- Pre- and Post-Condition Pairs (not covered)
- Decision Tables (not covered)
- Object Constraint Language (OCL) ✓ (covered in this unit)
4.0 Activity Diagrams
Activity diagrams show the flow of control in an operation, including:
- Sequential flow - Activities performed in order
- Selection (branching) - Different paths based on conditions
- Iteration (loops) - Repeated activities
Example: prepareBonusList() Operation
The diagram demonstrates:
- Initial calculation of bonus
- Decision points with conditions:
[bonus < £25]→ Create warning letter[bonus > £250]→ Add to ‘star’ list[bonus >= £25 AND bonus <= £250]→ Add to regular list
- Loop structure using
[more StaffMembers]condition - Final formatting of the list
5.0 Object Constraint Language (OCL)
5.1 Definition
OCL is a formal language used to express constraints (约束) on UML models that:
- Remains easy to read and write
- Specifies constraints that cannot be expressed directly in class diagrams
- Describes additional constraints about objects in the model
5.2 OCL Structure
OCL constraints consist of three main components:
5.2.1 Context
- Defines the domain within which the expression is valid
- Usually an instance of a type (e.g., object in class diagram)
- Can also be a link (association instance)
- Uses keyword
contextfollowed by the class name
5.2.2 Property
The property can be:
- An attribute of the class
- An association-end (role name)
- A query operation
- Referenced using
selfkeyword
5.2.3 Operation
OCL operations applied to properties include:
- Arithmetic operators:
*,+,-,/ - Comparison operators:
>,<,= - Set operators:
size(),isEmpty(),forAll(),select(),sum(),count()
5.3 OCL Syntax Rules
Important: When dealing with collections (sets), use the arrow operator →:
- Example:
self.employee → select(age < 60) - The
→indicates operations on collections
5.4 Common OCL Examples
Example 1: Simple Attribute Constraint
context Company
inv: self.numberOfEmployees > 50Interpretation: A company must have more than 50 employees
Example 2: Age Constraint
context Person
inv: self.age > 18Interpretation: Person’s age must be more than 18 years old
Example 3: Navigation with Association
context Company
inv: self.employee.sex = maleInterpretation: Only male employees in the company (but employer can be any gender)
Example 4: Conditional Constraint
context Person
inv: self.husband → notEmpty() implies self.husband.sex = maleInterpretation: If a person has a husband, the husband must be male
Example 5: Set Operations
context Company
inv: self.employee → select(age < 60)Interpretation: Select all employees under 60 years old
context Company
inv: self.employer → size() < 3Interpretation: A company must have fewer than 3 employers
6.0 Applications of OCL
OCL can be used for:
- Specifying invariants on classes and types in the class model
- Specifying type invariants for stereotypes
- Describing pre- and post-conditions on operations and methods
- Specifying constraints on operations
7.0 Benefits of Using OCL
- Code generation - Modeling tools can generate code based on OCL expressions
- Consistency checking - Tools can verify the consistency of UML models
8.0 Practice Exercises
Write OCL constraints for the following requirements:
-
One company has at least one employee
context Company inv: self.employee → size() >= 1 -
A person who is married must be more than 18 years old
context Person inv: self.isMarried = true implies self.age > 18 -
The salary of an employee cannot be greater than the salary of his/her supervisor
context Person inv: self.salary <= self.supervisor.salary -
If an employer’s age is above 60 years old, the minimum salary must be 5000
context Person inv: self.age > 60 implies self.salary >= 5000
Key Takeaways for Exams
- Operation specifications serve analysis, design, and testing purposes
- Activity diagrams show algorithmic flow with selection and iteration
- OCL provides formal constraint specification for UML models
- Remember the context-property-operation structure of OCL
- Use
→for set operations in OCL - The
selfkeyword refers to the current instance in context impliesis used for conditional constraints (if-then logic)