Академический Документы
Профессиональный Документы
Культура Документы
A UML diagram (e.g., a class diagram) does not provide all relevants aspects of a specication It is necessary to describe additional constraints about the objects in the model Constraints specify invariant conditions that must hold for the system being modeled Constraints are often described in natural language and this always result in ambiguities Traditional formal languages allow to write unambiguous constraints, but they are dicult for the average system modeler OCL: Formal language used to express constraints, that remains easy to read and write
A number of operations are dened on the predened types Operations and, or , xor, not, implies, if-then-else-endif +, -, *, /, abs, div, mod, max, min +, -, *, /, abs, floor, round, max, min, <, >, <=, >= size, concat, substring, toInteger, toReal
Collections
Collection: an abstract type with four concrete collection types 3 Set: the mathematical set (without duplicate elements) Set {1 , 2 , 5} Set {apple, orange, strawberry} 3 OrderedSet: a set in which the elements are ordered by their position OrderedSet {5, 4, 3, 2, 1} 3 Bag: a set that may contain duplicate elements Bag {1, 2, 5, 2} 3 Sequence: a bag in which the elements are ordered Sequence {1, 2, 5, 10} Sequence {ape, nut} Notation .. used for a sequence of consecutive integers 3 Sequence {1..5} is the same as Sequence {1, 2, 3, 4, 5} Elements of collections may be collections themselves Set { Sequence {1, 2, 3, 4}, Sequence {5, 6} } Collections have a set of predened operations 3 They are accessed using the -> notation 5
Set Operations
S, S1 , S2 are values of type Set(t), B is a value of type type t Signature union Set(t) Set(t) Set(t) union Set(t) Bag(t) Bag(t) intersection Set(t) Set(t) Set(t) intersection Set(t) Bag(t) Set(t) Set(t) Set(t) Set(t) symmetricDifference Set(t) Set(t) Set(t) including Set(t) t Set(t) excluding Set(t) t Set(t) asSet Set(t) Set(t) asOrderedSet Set(t) OrderedSet(t) asBag Set(t) Bag(t) asSequence Set(t) Sequence(t) Bag(t), v is a value of Semantics S1 S2 SB S1 S2 SB S1 S2 (S1 S2 ) (S2 S1 ) S {v} S {v}
Operations asOrderedSet and asSequence are nondeterministic Result contains the elements of the source set in arbitrary order
Bag Operations
B, B1 , B2 are values of type Bag(t), S is a value of type Set(t), v is a value of type t union union intersection intersection including excluding asSet asOrderedSet asBag asSequence Signature Bag(t) Bag(t) Bag(t) Bag(t) Set(t) Bag(t) Bag(t) Bag(t) Bag(t) Bag(t) Set(t) Set(t) Bag(t) t Bag(t) Bag(t) t Bag(t) Bag(t) Set(t) Bag(t) OrderedSet(t) Bag(t) Bag(t) Bag(t) Sequence(t) Semantics B1 B2 BS B1 B2 BS S {{v}} S {{v}}
Sequence Operations
S, S1 , S2 are values of type Set(t), v is a value of type t, operator denotes j the concatenation of lists, i (S) projects the ith element of a sequence S, i (S) is the subsequence of S from the ith to the jth element union append prepend subSequence at first last including excluding asSet asOrderedSet asBag asSequence Signature Sequence(t) Sequence(t) Sequence(t) Sequence(t) t Sequence(t) Sequence(t) t Sequence(t) Sequence(t) Integer Integer Sequence(t) Sequence(t) Integer Sequence(t) Sequence(t) t Sequence(t) t Sequence(t) t Sequence(t) Sequence(t) t Sequence(t) Sequence(t) Set(t) Sequence(t) OrderedSet(t) Sequence(t) Bag(t) Sequence(t) Sequence(t) Semantics S1 S2 S v v S j i (S) i (S) 1 (S) |S| (S) S v S {v}
Type Conformance
OCL is a typed language, the basic value types are organized in a type hierarchy The hierarchy determines conformance of the dierent types to each other Type type1 conforms with type type2 when an instance of type1 can be substituted at each place where an instance of type2 is expected Valid expression: OCL expression in which all types conform
10
11
Person firstName: String lastName: String gender: Gender birthDate: Date age: Integer isMarried: Boolean 0..1 maidenName: String [0..1] isUnemployed: Boolean parents income(): Integer currentSpouse(): Person descendants(): Set children 0..*
name:String 0..* /noEmployees: Integer managedCompanies stockPrice(): Real hireEmployee(p: Person) Marriage place: String date: Date ended: Boolean
0..* husband
12
13
14
15
Invariants
Determine a constraint that must be true for all instances of a type Value of attribute noEmployees in instances of Company must be less than or equal to 50 context Company inv: self.noEmployees <= 50 Equivalent formulation with a c playing the role of self, and a name for the constraint context c: Company inv SME: c.noEmployees <= 50 The stock price of companies is greater than 0 context Company inv: self.stockPrice() > 0
16
17
18
Body Expression
Used to indicate the result of a query operation Income of a person is the sum of the salaries of her jobs context Person::income(): Integer body: self.job.salary->sum() Expression must conform to the result type of the operation Denition may be recursive: The right-hand side of the denition may refer to the operation being dened A method that obtains the direct and indirect descendants of a person context Person::descendants(): Set body: result = self.children->union( self.children->collect(c | c.descendants()) ) Pre-, and postconditions, and body expressions may be mixed together after one operation context context Person::income(): Integer pre: self.age >= 18 body: self.job.salary->sum() post: result < 5000 19
Let Expression
Allows to dene a variable that can be used in a constraint context Person inv: let numberJobs: Integer = self.job->count() in if isUnemployed then numberJobs = 0 else numberJobs > 0 endif A let expression is only known within its specic expression
20
Denition Expressions
Enable to reuse variables or operations over multiple expressions Must be attached to a classier and may only contain variable and/or operation denitions context Person def: name: String = self.firstName.concat( ).concat(lastName) def: hasTitle(t: String): Boolean = self.job->exists(title = t) Names of the attributes/operations in a def expression must not conict with the names of attributes/association ends/operations of the classier
21
22
Enumeration Types
Person gender: Gender isMarried: Boolean maidenName: String [0..1] ... enumeration Gender male female
Dene a number of literals that are the possible values of the enumeration An enumeration value is referred as in Gender::female Only married women can have a maiden name context Person inv: self.maidenName <> implies self.gender = Gender::female and self.isMarried = true
23
Packages
Within UML, types are organized in packages Previous examples supposed that the package in which the classier belongs is clear from the environment The package and endpackage statements can be used to explicitly specify this package Package::SubPackage context X inv: ... some invariant ... context X::operationName(...): ReturnType pre: ... some precondition ... endpackage For referring to types in other packages the following notations may be used Packagename::Typename Packagename1::Packagename2::Typename
24
Undened Values
One or more subexpressions in an OCL expression may be undened In this case, the complete expression will be undened Exceptions for Boolean operators 3 true or anything is true 3 false and anything is false 3 false implies anything is true 3 anything implies true is true The rst two rules are valid irrespective of the order of the arguments and whether or not the value of the other sub-expression is known Exception for if-then-else expression: it will be valid as long as the chosen branch is valid, irrespective of the value of the other branch
25
From an object, an association is navigated using the opposite role name context Company inv: self.manager.isUnemployed = false inv: self.employee->notEmpty() Value of expression depends on maximal multiplicity of the association end 3 1: value is an object 3 *: value is a Set of objects (an OrderedSet if association is {ordered}) If role name is missing, the name of the type at the association end starting with a lowercase character is used (provided it is not ambiguous) context Person inv: self.bank.balance >= 0
26
27
Association Classes
Person isUnemployed: Boolean age: Integer ... employee 0..* Job title: String ... employer 0..* Company noEmployees:Integer ...
For navigating to an association class: a dot and the name of the association class starting with a lowercase character is used context Person inv: self.isUnemployed = false implies self.job->size() >= 1 For navigating from an association class to the related objects: a dot and the role names at the association ends is used context Job inv: self.employer.noEmployees >= 1 inv: self.employee.age >= 18 This always results in exactly one object 28
Direction in which a recursive association is navigated is required Specied by enclosing the corresponding role names in square brackets A person is currently married to at most one person context Person inv: self.marriage[wife]->select(m | m.ended = false)->size()=1 and self.marriage[husband]->select(m | m.ended = false)->size()=1 May also be used for non-recursive associations, but it is not necessary context Person inv: self.job[employer] ...
29
Operation that selects the current spouse of a person context Person::currentSpouse() : Person pre: self.isMarried = true body: if gender = Gender::male self.marriage[wife]->select(m | m.ended = false).wife else self.marriage[husband]->select(m | m.ended = false).husband end
30
Qualied Associations
Bank customer 0..1 * Person
accountNo: Integer
Qualied associations use one or more qualier attributes to select the objects at the other end of the association A bank can use the accountNumber attribute to select a particular customer Using qualier values when navigating through qualied associations context Bank inv: self.customer[12345] ... -- results in one Person, having account number 12345 Leaving out the qualier values context Bank inv: self.customer ... -- results in a Set(Person) with all customers of the bank
31
Re-typing or Casting
Allows an object to be re-typed as another type Expression o.oclAsType(Type2) re-types an object o of type Type1 into a another type Type2 Suppose Super is a supertype of type Sub Allows one to use a property of an object dened on a subtype of the currently known type of the object context Super inv: self.oclAsType(Sub).p -- accesses the p property defined in Sub Can be used to access a property of a superclass that has been overriden context Sub inv: self.p -- accesses the p property defined in Sub self.oclAsType(Super).p -- accesses the p property defined in Super
32
33
Class Features
Features of a class, not of its instances They are either used-dened or predened Predened feature allInstances holds on all types There are at most 100 persons context Person inv: Person.allInstances()->size() <= 100 A user-dened feature averageAge of class Person context Person inv: Person.averageAge = Person.allInstances()->collect(age)->sum()/ Person.allInstances()->size()
34
35
36
37
38
39
40
41
42
43
44
45
46
47