Object Constraint Language (OCL) - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Object Constraint Language (OCL)

Description:

Object Constraint Language (OCL) Yih-Kuen Tsay Dept. of Information Management National Taiwan University – PowerPoint PPT presentation

Number of Views:122
Avg rating:3.0/5.0
Slides: 29
Provided by: tsay150
Category:

less

Transcript and Presenter's Notes

Title: Object Constraint Language (OCL)


1
Object Constraint Language(OCL)
  • Yih-Kuen Tsay
  • Dept. of Information Management
  • National Taiwan University

2
Outline
  • Introduction
  • Relation with UML Models
  • Basic Values and Types
  • Objects and Properties
  • Collection Operations

3
About OCL
  • A formal language for writing expressions (such
    as invariants) on UML models.
  • It can also be used to specify queries.
  • OCL expressions are pure specifications without
    side effects.
  • OCL is a typed language.
  • Version 2.0 in the finalization process.

4
Why OCL
  • UML diagrams do not provide all the relevant
    aspects of a specification.
  • Additional constraints expressed by a natural
    language are ambiguous.
  • Traditional formal languages are precise, but
    hard to use.
  • OCL tries to be formal and yet easy to use.

5
Where to Use UML
  • As a query language
  • Invariants on classes and types.
  • Pre- and post-conditions on operations
  • Guards
  • Target sets for messages and actions
  • Constraints on operations
  • Derivation rules for attributes

6
Basic Form
  • context TypeName inv
  • 'this is an OCL expression with stereotype
    ltltinvariantgtgt in the
  • context of TypeName' 'another string'

7
Class Diagram Example
8
Relation with UML Models
  • Each OCL expression is written in the context of
    an instance of a specific type.
  • The reserved word self is used to refer to the
    contextual instance.
  • An explicit context declaration can be omitted if
    the OCL expression is properly placed in a
    diagram.

9
Invariants
  • Inside the class diagram example
  • self.numberOfEmployees gt 50
  • specifies that the number of employees must
    always exceed 50.
  • Alternatively,
  • context Company inv
  • self.numberOfEmployees gt 50
  • A different name can be used for self
  • context c Company inv
  • c.numberOfEmployees gt 50
  • The invariant itself can also be given a name
    (after inv).

10
Pre- and Post-Condtions
  • Basic form
  • context TypenameoperationName(param1 Type1,
    ... ) ReturnType
  • pre param1 gt ...
  • post result ...

11
Pre- and Post-Condtions (cont.)
  • Example
  • context Personincome(d Date) Integer
  • post result 5000
  • Names may be given
  • context TypenameoperationName(param1 Type1,
    ... ) ReturnType
  • pre parameterOk param1 gt ...
  • post resultOk result ...

12
Package Context
  • package PackageSubPackage
  • context X inv
  • ... some invariant ...
  • context XoperationName(..)
  • pre ... some precondition ...
  • endpackage

13
Operation Body Expression
  • context PersongetCurrentSpouse() Person
  • pre self.isMarried true
  • body self.mariages-gtselect( m m.ended false
    ).spouse

14
Initial and Derived Values
  • context Personincome Integer
  • init parents.income-gtsum() 1 -- pocket
    allowance
  • derive if underAge
  • then parents.income-gtsum() 1 -- pocket
    allowance
  • else job.salary -- income from regular job
  • endif

15
Basic Types
  • Boolean true, false
  • Integer 1, -5, 2, 34, 26524, ...
  • Real 1.5, 3.14, ...
  • String 'To be or not to be...'

16
Basic Operations
  • Integer , , -, /, abs()
  • Real , , -, /, floor()
  • Boolean and, or, xor, not, implies, if-then-else
  • String concat(), size(), substring()

17
Other Types
  • Classifiers
  • All classifiers of a UML model are types in its
    OCL expressions.
  • Enumerations

18
Sub-expressions
  • context Person inv
  • let income Integer self.job.salary-gtsum() in
  • if isUnemployed then
  • income lt 100
  • else
  • income gt 100
  • endif

19
Definition Expressions
  • context Person
  • def income Integer self.job.salary-gtsum()
  • def nickname String Little Red Rooster
  • def hasTitle(t String) Boolean
    self.job-gtexists(title t)

20
More about Types and Operations
  • Type conformance
  • Casting (re-typing)
  • Precedence rules
  • Infix operators
  • Keywords
  • Comments

21
Properties
  • A property is one of
  • An Attribute
  • context Person inv
  • self.age gt 0
  • An AssociationEnd
  • An Operation with isQuery
  • A Method with isQuery

22
Previous Values in Post-Conditions
  • context PersonbirthdayHappens()
  • post age age_at_pre 1
  • context CompanyhireEmployee(p Person)
  • post employees employees_at_pre-gtincluding(p)
    and
  • stockprice() stockprice_at_pre() 10

23
Previous Values in Post-Conditions (cont.)
  • a.b_at_pre.c -- takes the old value of property b of
    a, say x
  • -- and then the new value of c of x.
  • a.b_at_pre.c_at_pre -- takes the old value of property
    b of a, say x
  • -- and then the old value of c of x.

24
Collections
  • Set
  • Bag
  • OrderedSet
  • Sequence

25
Collection Operations
  • Select
  • context Company inv
  • self.employee-gtselect(age gt 50)-gtnotEmpty()
  • Reject
  • context Company inv
  • self.employee-gtreject( isMarried )-gtisEmpty()

26
ForAll Operation
  • context Company
  • inv self.employee-gtforAll( age lt 65 )
  • inv self.employee-gtforAll( p p.age lt 65 )
  • inv self.employee-gtforAll( p Person p.age lt
    65 )
  • context Company inv
  • self.employee-gtforAll( e1, e2 Person
  • e1 ltgt e2 implies e1.forename ltgt e2.forename)

27
Exists Operation
  • context Company inv
  • self.employee-gtexists( forename 'Jack' )
  • UML OCL 2.0 Adopted Specification 29
  • context Company inv
  • self.employee-gtexists( p p.forename 'Jack' )
  • context Company inv
  • self.employee-gtexists( p Person p.forename
    'Jack' )

28
Iterate Operation
  • Reject, Select, forAll, Exists, and Collect can
    all be described in terms of interate.
  • Example
  • collection-gtcollect(x T x.property)
  • -- is identical to
  • collection-gtiterate(x T acc T2 Bag
  • acc-gtincluding(x.property))
Write a Comment
User Comments (0)
About PowerShow.com