Docsity

Object Constraint Language - Lecture Slides | CS 6310, Study notes of Computer Science

Partial preview of the text

Download Object Constraint Language - Lecture Slides | CS 6310 and more Study notes Computer Science in PDF only on Docsity!

OCL

  • Object Constraint Language
  • Official part of UML
  • Strongly typed, declarative specification of

system properties

  • Assertions + collection classes + UML

diagram navigation

  • Supported by Rational Rose, ArgoUML,

Poseidon/Octopus, Enterprise Architect

Why?

  • UML diagrams, as a graphical

language, are limited in what they can

express

  • Structural relationships, behavioral descriptions
  • The language needs a mechanism for specifying precise semantics
  • OCL extends UML with
  • Class invariants
  • Operation pre and post conditions
  • Guards on state-machine transitions

Uses of OCL

  • As a query language
  • To specify invariants on classes and types in the class model
  • To specify type invariant for Stereotypes
  • To describe pre- and post conditions on Operations and Methods
  • To describe Guards
  • To specify target (sets) for messages and actions
  • To specify constraints on operations
  • To specify derivation rules for attributes for any expression over a UML model

Syntax

  • context is a keyword indicating the start of a new constraint
  • is a class or operation name
    • The current position from which relative path names are derived
  • is inv, pre, or post
  • is usually an equation asserting a property context :

Pre and Post Conditions

  • UML operation semantics can be expressed

using pre and post condition constraints

  • The pre condition says what must be true for the operation to meaningfully take place
  • The post condition expresses what is guaranteed to be true after the operation completes - About the return value - About any state changes (e.g. instance variables) - @pre can be used to distinguish the value of an instance variable before an operation from the value afterwards

Pre and Post Conditions - 2

  • The argument to the square root routine

must be non-negative

  • The square of the computed result must

equal the argument

context Person::income(d : Date) :

Integer post : result = 5000

OCL Keywords

keyword description inv, pre, post Introduces constraints if-then-else-endif Conditional expression not, or, and, xor, implies Boolean operators package, endpackage Packages context Modifies use of names def Global definition let, in Local definition derive Attribute derivation init Initial value description attr? oper? result Result value from an operation

let Clause

  • A let clause is a way of introducing an abbreviation
  • It has two parts, both expressions
    • The first part between indicates one or more names and binds values to them
    • The second part is like a local block; it limits the scope of where the bindings apply
  • let income : Integer = self.job.salary->sum() in if isUnemployed then income < 100 else income >= 100 endif

Multiplicity

  • When an association has multiplicity greater than one, the result of a traversal is a collection (set, bag or sequence)
  • If an operation is performed on that association, the -> symbol is used, rather than the period, to separate the collection from the operation

Collections

  • OCL features four kinds of collections that support associations with many-to-many and one-to-many multiplicities
  • The parent class Collection provides features that hold true of all collections - size, includes, count, includesAll, isEmpty, notEmpty, sum, exists, forAll and iterate
  • Set, Bag, (multiset) and Sequence (ordered multiset) provide specialized operations