28
IM IM NTU NTU OCL OCL 2006/12/07 -- 2006/12/07 -- 1 Software Development Methods, Software Development Methods, Fall Fall 2006 2006 Object Constraint Language (OCL) Yih-Kuen Tsay Dept. of Information Management National Taiwan University

IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

Embed Size (px)

Citation preview

Page 1: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 11Software Development Methods, FallSoftware Development Methods, Fall 20062006

Object Constraint Language(OCL)

Yih-Kuen Tsay

Dept. of Information Management

National Taiwan University

Page 2: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 22Software Development Methods, FallSoftware Development Methods, Fall 20062006

Outline

• Introduction

• Relation with UML Models

• Basic Values and Types

• Objects and Properties

• Collection Operations

Page 3: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 33Software Development Methods, FallSoftware Development Methods, Fall 20062006

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 still in the finalization process

(though main part of UML 2.0 has been finalized).

Page 4: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 44Software Development Methods, FallSoftware Development Methods, Fall 20062006

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.

Page 5: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 55Software Development Methods, FallSoftware Development Methods, Fall 20062006

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

Page 6: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 66Software Development Methods, FallSoftware Development Methods, Fall 20062006

Basic Form

context TypeName inv:

'this is an OCL expression with stereotype <<invariant>> in the

context of TypeName' = 'another string'

Page 7: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 77Software Development Methods, FallSoftware Development Methods, Fall 20062006

Class Diagram Example

Page 8: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 88Software Development Methods, FallSoftware Development Methods, Fall 20062006

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.

Page 9: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 99Software Development Methods, FallSoftware Development Methods, Fall 20062006

Invariants

• Inside the class diagram example:self.numberOfEmployees > 50specifies that the number of employees must always exceed 50.

• Alternatively,context Company inv:self.numberOfEmployees > 50

• A different name can be used for self :context c : Company inv:c.numberOfEmployees > 50

• The invariant itself can also be given a name (after inv).

Page 10: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 1010Software Development Methods, FallSoftware Development Methods, Fall 20062006

Pre- and Post-Condtions

• Basic form:

context Typename::operationName(param1 : Type1, ... ): ReturnType

pre : param1 > ...

post: result = ...

Page 11: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 1111Software Development Methods, FallSoftware Development Methods, Fall 20062006

Pre- and Post-Condtions (cont.)

• Example:

context Person::income(d : Date) : Integer

post: result = 5000• Names may be given:

context Typename::operationName(param1 : Type1, ... ): ReturnType

pre parameterOk: param1 > ...

post resultOk : result = ...

Page 12: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 1212Software Development Methods, FallSoftware Development Methods, Fall 20062006

Package Context

package Package::SubPackage

context X inv:

... some invariant ...

context X::operationName(..)

pre: ... some precondition ...

endpackage

Page 13: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 1313Software Development Methods, FallSoftware Development Methods, Fall 20062006

Operation Body Expression

context Person::getCurrentSpouse() : Person

pre: self.isMarried = true

body: self.mariages->select( m | m.ended = false ).spouse

Page 14: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 1414Software Development Methods, FallSoftware Development Methods, Fall 20062006

Initial and Derived Values

context Person::income : Integer

init: parents.income->sum() * 1% -- pocket allowance

derive: if underAge

then parents.income->sum() * 1% -- pocket allowance

else job.salary -- income from regular job

endif

Page 15: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 1515Software Development Methods, FallSoftware Development Methods, Fall 20062006

Basic Types

• Boolean: true, false

• Integer: 1, -5, 2, 34, 26524, ...

• Real: 1.5, 3.14, ...

• String: 'To be or not to be...'

Page 16: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 1616Software Development Methods, FallSoftware Development Methods, Fall 20062006

Basic Operations

• Integer: *, +, -, /, abs()

• Real: *, +, -, /, floor()

• Boolean: and, or, xor, not, implies, if-then-else

• String: concat(), size(), substring()

Page 17: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 1717Software Development Methods, FallSoftware Development Methods, Fall 20062006

Other Types

• Classifiers– All classifiers of a UML model are types in its

OCL expressions.

• Enumerations

Page 18: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 1818Software Development Methods, FallSoftware Development Methods, Fall 20062006

Sub-expressions

context Person inv:let income : Integer = self.job.salary->sum() inif isUnemployed thenincome < 100elseincome >= 100endif

Page 19: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 1919Software Development Methods, FallSoftware Development Methods, Fall 20062006

Definition Expressions

context Person

def: income : Integer = self.job.salary->sum()

def: nickname : String = ’Little Red Rooster’

def: hasTitle(t : String) : Boolean = self.job->exists(title = t)

Page 20: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 2020Software Development Methods, FallSoftware Development Methods, Fall 20062006

More about Types and Operations

• Type conformance

• Casting (re-typing)

• Precedence rules

• Infix operators

• Keywords

• Comments

Page 21: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 2121Software Development Methods, FallSoftware Development Methods, Fall 20062006

Properties

A property is one of

• An Attributecontext Person inv:

self.age > 0

• An AssociationEnd

• An Operation with isQuery

• A Method with isQuery

Page 22: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 2222Software Development Methods, FallSoftware Development Methods, Fall 20062006

Previous Values in Post-Conditions

• context Person::birthdayHappens()

post: age = age@pre + 1• context Company::hireEmployee(p : Person)

post: employees = employees@pre->including(p) and

stockprice() = stockprice@pre() + 10

Page 23: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 2323Software Development Methods, FallSoftware Development Methods, Fall 20062006

Previous Values in Post-Conditions (cont.)

[email protected] -- takes the old value of property b of a, say x

-- and then the new value of c of x.

[email protected]@pre -- takes the old value of property b of a, say x

-- and then the old value of c of x.

Page 24: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 2424Software Development Methods, FallSoftware Development Methods, Fall 20062006

Collections

• Set

• Bag

• OrderedSet

• Sequence

Page 25: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 2525Software Development Methods, FallSoftware Development Methods, Fall 20062006

Collection Operations

• Select

context Company inv:

self.employee->select(age > 50)->notEmpty()

• Rejectcontext Company inv:

self.employee->reject( isMarried )->isEmpty()

Page 26: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 2626Software Development Methods, FallSoftware Development Methods, Fall 20062006

ForAll Operation

context Company

inv: self.employee->forAll( age <= 65 )

inv: self.employee->forAll( p | p.age <= 65 )

inv: self.employee->forAll( p : Person | p.age <= 65 )

context Company inv:

self.employee->forAll( e1, e2 : Person |

e1 <> e2 implies e1.forename <> e2.forename)

Page 27: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 2727Software Development Methods, FallSoftware Development Methods, Fall 20062006

Exists Operation

context Company inv:

self.employee->exists( forename = 'Jack' )

context Company inv:

self.employee->exists( p | p.forename = 'Jack' )

context Company inv:

self.employee->exists( p : Person | p.forename = 'Jack' )

Page 28: IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay

IM NTUIM NTU

OCLOCL 2006/12/07 -- 2006/12/07 -- 2828Software Development Methods, FallSoftware Development Methods, Fall 20062006

Iterate Operation

• Reject, Select, forAll, Exists, and Collect can all be described in terms of interate.

• Example:

collection->collect(x : T | x.property)

-- is identical to:

collection->iterate(x : T; acc : T2 = Bag{} |

acc->including(x.property))