Reaves.dev

v0.1.0

built using

Phoenix v1.7.20

OCL

Stephen M. Reaves

::

2023-10-03

Notes about Lesson 8 of CS-6310

Summary

Object Constraint Language

Official part of UML

Provides details that cannot be described by diagrams

  • Specifically semantics

Strongly typed, declarative specification of system properties

Constraints + collection classes + UML diagram navigation

Extends UML with:

  • Class invariants
  • Operation pre and post conditions
  • Guards on state-machine transitions

Overview

Declarative, not procedural

  • Pure expression language

No assignments or other side effects

Strongly typed

  • built-in types

Highest level mechanism is constraint

  • Formal assertion of system properties

Uses

  • Specify invariants
  • Describe pre and post conditions
  • Specify derivation rules for derived attributes
  • Describe guards on transitions in statecharts
  • Specify targets for messages and actions
  • Use as a query language

Syntax

context <identifier> <constraintType>: <Boolean expression>

The context+identifier describe where you are in a diagram, typically a class

Invariants

Statement of a property that is always true

Express key system requirement

inv keyword

context LargeCompany inv: numberOfEmployees > 50

Integrity constraints

Pre and Post conditions

context Real::squareRoot() : Real
pre: self >= 0
post: self = result * result

Changes to Attribute Values

Post conditions can also be used to describe the results of changes in the value of the attribute

context Account::deposit(Real : amount)
pre: amount > 0
post: balance = balance@pre + amount

Built-ins

TypesLiteralsOperations
Booleantrue, falseand, or, xor, not, implies, if-then-else
Integer1, -5, 10000, …*, +, -, /, abs(), …
Real1.5, 3.14, …*, =, -, /, floor(), …
String‘to be or not to be’toUpper(), concat()

KeywordDescription
inv, pre, postIntroduces constraints
if-then-else-endifConditional
not, or, and, xor, impliesBoolean operators
package, endpackagePackages
contextIntroduces constraints
defGlobal definition
let, inlocal definition
deriveAttribute derivation
initIntial value description
resultResult value from an operation
selfObject being constrained

Let clause

Introduces an abbreviation to a constraint

let incoome : Integer = self.job.salary->sum() in
  if isUnemployed then income < 100
                  else income >= 100
  endif

To refer to features of a non-context class, you must use a qualified name

Navigation is done by walking through a diagram from the context class to the other class using intermediary relationship lines and class rectangles

Customer

name

address

Order

date

status

calcTax()

calcTotal()

calcTotalWeight()

context Customer
  self.order.date

Multiplicity means a collection

Collections

Collection

size()

includes()

count()

includesAll()

isEmpty()

notEmpty()

sum()

exists()

forAll()

iterate()

Set

OrderedSet

Bag

Sequence

Other Features

  • Tuples
    • Similar to structs
  • Enumerations
  • Message expression
    • denoting the sending of a signal or the invoking of an operation
  • Access to metamodel
  • Automatic flattening
    • A set of sets is just a set