1 / 28

Object Constraint Language (OCL)

Object Constraint Language (OCL). Yih-Kuen Tsay Dept. of Information Management National Taiwan University. Outline. Introduction Relation with UML Models Basic Values and Types Objects and Properties Collection Operations. About OCL.

hanzila
Télécharger la présentation

Object Constraint Language (OCL)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Constraint Language(OCL) Yih-Kuen Tsay Dept. of Information Management National Taiwan University OCL 2006/12/07 -- 1

  2. Outline • Introduction • Relation with UML Models • Basic Values and Types • Objects and Properties • Collection Operations OCL 2006/12/07 -- 2

  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). OCL 2006/12/07 -- 3

  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. OCL 2006/12/07 -- 4

  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 OCL 2006/12/07 -- 5

  6. Basic Form context TypeName inv: 'this is an OCL expression with stereotype <<invariant>> in the context of TypeName' = 'another string' OCL 2006/12/07 -- 6

  7. Class Diagram Example OCL 2006/12/07 -- 7

  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. OCL 2006/12/07 -- 8

  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). OCL 2006/12/07 -- 9

  10. Pre- and Post-Condtions • Basic form: context Typename::operationName(param1 : Type1, ... ): ReturnType pre : param1 > ... post: result = ... OCL 2006/12/07 -- 10

  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 = ... OCL 2006/12/07 -- 11

  12. Package Context package Package::SubPackage context X inv: ... some invariant ... context X::operationName(..) pre: ... some precondition ... endpackage OCL 2006/12/07 -- 12

  13. Operation Body Expression context Person::getCurrentSpouse() : Person pre: self.isMarried = true body: self.mariages->select( m | m.ended = false ).spouse OCL 2006/12/07 -- 13

  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 OCL 2006/12/07 -- 14

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

  16. Basic Operations • Integer: *, +, -, /, abs() • Real: *, +, -, /, floor() • Boolean: and, or, xor, not, implies, if-then-else • String: concat(), size(), substring() OCL 2006/12/07 -- 16

  17. Other Types • Classifiers • All classifiers of a UML model are types in its OCL expressions. • Enumerations OCL 2006/12/07 -- 17

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

  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) OCL 2006/12/07 -- 19

  20. More about Types and Operations • Type conformance • Casting (re-typing) • Precedence rules • Infix operators • Keywords • Comments OCL 2006/12/07 -- 20

  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 OCL 2006/12/07 -- 21

  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 OCL 2006/12/07 -- 22

  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. OCL 2006/12/07 -- 23

  24. Collections • Set • Bag • OrderedSet • Sequence OCL 2006/12/07 -- 24

  25. Collection Operations • Select context Company inv: self.employee->select(age > 50)->notEmpty() • Reject context Company inv: self.employee->reject( isMarried )->isEmpty() OCL 2006/12/07 -- 25

  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) OCL 2006/12/07 -- 26

  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' ) OCL 2006/12/07 -- 27

  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)) OCL 2006/12/07 -- 28

More Related