Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 1 Object Constraint Language (OCL) Yih-Kuen Tsay."— Presentation transcript:

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

2 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 2 Outline Introduction Relation with UML Models Basic Values and Types Objects and Properties Collection Operations

3 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 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 still in the finalization process (though main part of UML 2.0 has been finalized).

4 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 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 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 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 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 6 Basic Form context TypeName inv: 'this is an OCL expression with stereotype > in the context of TypeName' = 'another string'

7 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 7 Class Diagram Example

8 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 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 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 9 Invariants Inside the class diagram example: self.numberOfEmployees > 50 specifies 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).

10 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 10 Pre- and Post-Condtions Basic form: context Typename::operationName(param1 : Type1,... ): ReturnType pre : param1 >... post: result =...

11 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 11 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 =...

12 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 12 Package Context package Package::SubPackage context X inv:... some invariant... context X::operationName(..) pre:... some precondition... endpackage

13 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 13 Operation Body Expression context Person::getCurrentSpouse() : Person pre: self.isMarried = true body: self.mariages->select( m | m.ended = false ).spouse

14 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 14 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

15 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 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 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 16 Basic Operations Integer: *, +, -, /, abs() Real: *, +, -, /, floor() Boolean: and, or, xor, not, implies, if-then- else String: concat(), size(), substring()

17 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 17 Other Types Classifiers –All classifiers of a UML model are types in its OCL expressions. Enumerations

18 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 18 Sub-expressions context Person inv: let income : Integer = self.job.salary->sum() in if isUnemployed then income < 100 else income >= 100 endif

19 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 19 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)

20 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 20 More about Types and Operations Type conformance Casting (re-typing) Precedence rules Infix operators Keywords Comments

21 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 21 Properties A property is one of An Attribute context Person inv: self.age > 0 An AssociationEnd An Operation with isQuery A Method with isQuery

22 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 22 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

23 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 23 Previous Values in Post-Conditions (cont.) a.b@pre.c -- takes the old value of property b of a, say x -- and then the new value of c of x. a.b@pre.c@pre -- takes the old value of property b of a, say x -- and then the old value of c of x.

24 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 24 Collections Set Bag OrderedSet Sequence

25 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 25 Collection Operations Select context Company inv: self.employee->select(age > 50)->notEmpty() Reject context Company inv: self.employee->reject( isMarried )->isEmpty()

26 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 26 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)

27 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 27 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' )

28 IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/07 -- 28 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))


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

Similar presentations


Ads by Google