Skip to content

Instantly share code, notes, and snippets.

@akhundMurad
Created December 17, 2024 16:43
Show Gist options
  • Select an option

  • Save akhundMurad/c9365d59f79714de7b9cc3dffe90824c to your computer and use it in GitHub Desktop.

Select an option

Save akhundMurad/c9365d59f79714de7b9cc3dffe90824c to your computer and use it in GitHub Desktop.
Object Constraint Language (OCL) Cheatsheet

# Object Constraint Language (OCL) Cheatsheet

1. Introduction to OCL

The Object Constraint Language (OCL) is a formal language used to describe constraints, business rules, and logic that cannot be expressed using UML diagrams alone. OCL is declarative, meaning it describes what must be true, rather than how to make it true.

Common Use Cases:

  • Defining preconditions, postconditions, and invariants
  • Defining derived attributes
  • Specifying query operations

2. OCL Syntax Basics

OCL expressions operate on UML model elements such as classes, attributes, and associations.

General Syntax:

context <Classifier>
inv [<name>] : <OCL expression>
  • context: Specifies the UML element (class, operation, or attribute) being constrained.
  • inv: Declares an invariant (can also use pre for preconditions or post for postconditions).

Example:

context Person
inv: self.age >= 0

This constraint ensures that the age of a Person is always non-negative.


3. Types in OCL

OCL has its own data types, similar to programming languages.

Data Type Description Example
Boolean True or False true, false
Integer Whole numbers 0, -1, 42
Real Floating point numbers 3.14, -0.5
String Sequence of characters 'Hello'
Collection Set, Bag, Sequence, or OrderedSet Set{1, 2, 3}

4. OCL Operators

Arithmetic Operators

Operator Description Example
+ Addition 5 + 3 = 8
- Subtraction 5 - 3 = 2
* Multiplication 5 * 3 = 15
/ Division 5 / 2 = 2.5
div Integer division 5 div 2 = 2
mod Modulo 5 mod 2 = 1

Comparison Operators

Operator Description Example
= Equality 5 = 5 = true
<> Inequality 5 <> 3 = true
< Less than 3 < 5 = true
<= Less than or equal 3 <= 3 = true
> Greater than 5 > 3 = true
>= Greater or equal 5 >= 5 = true

Logical Operators

Operator Description Example
and Logical AND true and false = false
or Logical OR true or false = true
not Logical NOT not true = false
implies Logical implication true implies false = false

5. Collections in OCL

OCL supports four main collection types: Set, Bag, Sequence, and OrderedSet.

Collection Type Description
Set Unique elements, unordered
Bag Non-unique elements, unordered
Sequence Ordered, non-unique elements
OrderedSet Ordered, unique elements

Collection Operations

Operation Description Example
size() Number of elements Set{1, 2, 3}.size() = 3
includes(x) Check if x is in the collection Set{1, 2, 3}.includes(2) = true
excludes(x) Check if x is not in the collection Set{1, 2, 3}.excludes(4) = true
count(x) Number of occurrences of x Bag{1, 1, 2}.count(1) = 2
sum() Sum of elements (if numeric) Set{1, 2, 3}.sum() = 6
select(x x > 2) Filter elements based on condition
collect(x x.attr) Flattens nested collections

6. Constraints

Invariants

An invariant ensures that an attribute or relationship always satisfies a condition.

context Person
inv: self.age >= 0

This invariant ensures the age attribute of a Person is non-negative.

Preconditions and Postconditions

Preconditions and postconditions are constraints on operations.

Precondition: A constraint that must be true before an operation is executed.

context Account::withdraw(amount: Real)
pre: amount > 0

This precondition ensures the amount to withdraw is positive.

Postcondition: A constraint that must be true after an operation is executed.

context Account::withdraw(amount: Real)
post: self.balance = self.balance@pre - amount

The @pre indicates the value before execution, and the postcondition ensures the balance is reduced accordingly.


7. Defining Attributes and Operations

Derived Attributes

A derived attribute is computed from other attributes.

context Person
derive: self.fullName = self.firstName + ' ' + self.lastName

This constraint defines the fullName as a concatenation of firstName and lastName.

Query Operations

OCL can define query operations that return values.

context Person
def: getFullName() : String = self.firstName + ' ' + self.lastName

This query operation returns the fullName of a Person.


8. Advanced Features

Iterators (select, reject, forAll, exists, collect)

  • select: Filter elements from a collection.
  • reject: Remove elements from a collection.
  • forAll: Ensures a condition is true for all elements.
  • exists: Checks if at least one element satisfies the condition.
  • collect: Projects each element to another value.

Example:

context Company
inv: self.employees->forAll(e | e.age >= 18)

This invariant ensures all employees are at least 18 years old.


9. Summary of Key Concepts

  • OCL is a declarative language for expressing constraints.
  • OCL works on top of UML models and ensures logical correctness.
  • Constraints include invariants, preconditions, and postconditions.
  • It supports collections (Set, Bag, Sequence, OrderedSet) and operations like select, collect, forAll, and exists.

With this cheatsheet, you can quickly write and understand OCL constraints and logic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment