Object Modeling with OMG UML Tutorial Series Advanced

Object Modeling with OMG UML Tutorial Series Advanced Modeling with UML Karin Palmkvist, Bran Selic, Jos Warmer and Nathan Dykman UML Revision Task Force © 1999 -2001 OMG and Contributors: Crossmeta, EDS, IBM, Enea Data, Hewlett-Packard, Intelli. Corp, Kabira Technologies, Klasse Objecten, Rational Software, Telelogic, Unisys Advanced Modeling with UML

Overview n n Introduction Advanced Modeling n Part 1: Model Management n n Part 2: Extension Mechanisms and Profiles n n n Karin Palmkvist, Enea Data Bran Selic, Rational Software Nathan Dykman, Hewlett Packard Part 3: Object Constraint Language (OCL) n n Jos Warmer, Klasse Objecten Bran Selic, Rational Software Advanced Modeling with UML 2

Tutorial Series n n n Lecture 1: Introduction to UML: Structural and Use Case Modeling Lecture 2: Behavioral Modeling with UML Lecture 3: Advanced Modeling with UML [Note: This version of the tutorial series is based on OMG UML Specification v. 1. 4, UML Revision Task Force recommended final draft, OMG doc# ad/01 -02 -13. ] Advanced Modeling with UML 3

Tutorial Focus: the Language n language = syntax + semantics n n syntax = language elements (e. g. words) are assembled into expressions (e. g. phrases, clauses) semantics = the meanings of the syntactic expressions UML Notation Guide – defines UML’s graphic syntax UML Semantics – defines UML’s semantics Advanced Modeling with UML 4

UML Overview «metamodel» UML Model Management Behavioral Elements dependency Foundation Advanced Modeling with UML package 5

UML Overview Behavioral Elements Model Management Foundation Extension Mechanisms Core Data Types Advanced Modeling with UML 6

UML Overview Behavioral Elements Use Cases Collaborations State Machines Activity Graphs Model Management Common Behavior Foundation Advanced Modeling with UML 7

Advanced Modeling with UML n n n Part 1: Model Management Part 2: Extension Mechanisms and Profiles Part 3: Object Constraint Language (OCL) Advanced Modeling with UML 8

UML Overview «metamodel» UML Model Management Behavioral Elements Foundation Advanced Modeling with UML 9

Model Management Overview n Main UML constructs used for model management: n n n Package Subsystem Model Advanced Modeling with UML 10

Unifying Concepts n Packages, Subsystems, and Models n n n Group other model elements together Each groups elements for a different reason (providing different semantics) Other grouping elements in UML include: n n Classes Components Advanced Modeling with UML 11

Package n n n What are Packages? Core Concepts Diagram Tour When to Use Packages Modeling Tips Advanced Modeling with UML 12

Package A package is a grouping of model elements Advanced Modeling with UML 13

Package – Example Sales Customer Order Warehouse Location Item Stock Item Order Item Advanced Modeling with UML 14

Package n A package can contain model elements of different kinds n n n Including other packages to create hierarchies A package defines a namespace for its contents Packages can be used for various purposes Advanced Modeling with UML 15

Core Concepts Construct Package Description Syntax A grouping of model elements. Name Import A dependency indicating that the public contents of the target package are added to the namespace of the source package. Access A dependency indicating that the public contents of the target package are available in the namespace of the source package. Advanced Modeling with UML «import» «access» 16

Visibility n Each contained element has a visibility relative to the containing package n n A public element is visible to elements outside the package, denoted by ‘+’ A protected element is visible only to elements within inheriting packages, denoted by ‘#’ A private element is not visible at all to elements outside the package, denoted by ‘-’ Same syntax for visibility of attributes and operations in classes Advanced Modeling with UML 17

Import Y X +C B «import» A -D +E Y X B Y: : C +C -D «import» A Y: : E +E The associations are owned by package X Advanced Modeling with UML 18

Import – Alias Y X +B B «import» A -D +E same class Y X B A +B +Y: : B -C «import» +Y: : E -D +E An imported element can be given a local alias and a local visibility Advanced Modeling with UML 19

Access Y X +C B «access» A X «access» -D +E Y +C B -D +E A The associations are owned by package X Advanced Modeling with UML 20

Import vs. Access Y X Y: : C B Y: : F A Z +C «import» -D +E Y: : E +F +Z: : F «import» +G -Z: : G Y X Z +C B «access» A -H -D +E Advanced Modeling with UML +F «access» -H +G 21

Package Inheritance n A package with a generalization to another package inherits public and protected elements that are n n owned or imported by the inherited package Advanced Modeling with UML 22

Diagram Tour n n Packages are shown in static diagrams Two equivalent ways to show containment: Advanced Modeling with UML 23

When to Use Packages n n To create an overview of a large set of model elements To organize a large model To group related elements To separate namespaces Advanced Modeling with UML 24

Modeling Tips – Package n n Gather model elements with strong cohesion in one package Keep model elements with low coupling in different packages Minimize relationships, especially associations, between model elements in different packages Namespace implication: an element imported into a package does not “know” how it is used in the imported package Advanced Modeling with UML 25

Subsystem n n n What are Subsystems? Core Concepts Diagram Tour When to Use Subsystems Modeling Tips Advanced Modeling with UML 26

Subsystems are used for system decomposition Advanced Modeling with UML 27

Subsystem – Example Traffic Control Trunk Communicating subsystems constitute a system Subscription Advanced Modeling with UML 28

Core Concepts Construct Subsystem Description A grouping of model elements that represents a behavioral unit in a physical system. Advanced Modeling with UML Syntax Name 29

Subsystem Aspects n A subsystem has two aspects: n n n An external view, showing the services provided by the subsystem An internal view, showing the realization of the subsystem There is a mapping between the two aspects Advanced Modeling with UML 30

Subsystem Aspects Specification elements Realization elements A subsystem has a specification and a realization to represent the two views Advanced Modeling with UML 31

Subsystem Realization Specification elements Realization elements ? n n The subsystem realization defines the actual contents of the subsystem The subsystem realization typically consists of classes and their relationships, or a contained hierarchy of subsystems with classes as leaves Advanced Modeling with UML 32

Subsystem Specification elements Realization elements ? The subsystem specification defines the external view of the subsystem Advanced Modeling with UML 33

Subsystem Specification n The subsystem specification n n describes the services offered by the subsystem describes the externally experienced behavior of the subsystem does not reveal the internal structure of the subsystem describes the interface of the subsystem Advanced Modeling with UML 34

Specification Techniques n n The The Use Case approach State Machine approach Logical Class approach Operation approach …and combinations of these. Advanced Modeling with UML 35

Use Case Approach Specification elements n n Realization elements For subsystem services used in certain sequences When the specification is to be understood by nontechnical people Advanced Modeling with UML 36

Use Case Approach – Example Traffic Control Specification elements Operator Realization elements Change Digit Analysis Information Initiate Call Trunk Subscription Receive Digit and Connect Hook Signal and Disconnect Advanced Modeling with UML 37

State Machine Approach Traffic Control Specification elements Stopped Running Maintenance Error n n Exhausted For subsystems with state dependent behavior Focuses on the states of the subsystem and the transitions between them Advanced Modeling with UML 38

Logical Class Approach Traffic Control Specification elements Analyzer Number Dictionary Network Manager n n When usage of the subsystem is perceived as manipulation of objects When the requirements are guided by a particular standard Advanced Modeling with UML 39

Operation Approach Traffic Control Operations initiate. Connection (…) dialled. Digit (…) through. Connect (…) b. Answer (…) b. On. Hook (…) a. On. Hook (…) n n For subsystems providing simple, “atomic” services When the operations are invoked independently Advanced Modeling with UML 40

Mixing Techniques Traffic Control Specification elements Operations change. Digit. Analysis. Information (. . . ) Specification elements Initiate Call Trunk Subscription Receive Digit and Connect Hook Signal and Disconnect Advanced Modeling with UML 41

Complete Subsystem Notation Operations Realization elements Specification elements n n The complete subsystem symbol has three pre-defined compartments Each of the compartments may be optionally omitted from the diagram Advanced Modeling with UML 42

Subsystem Interfaces Trunk Traffic Control Subscription Advanced Modeling with UML 43

Operations and Interfaces «Interface» operation 1( ) operation 2( ) «realize» operation 4( ) Operations operation 1( ) : Type 1 operation 2( ) : Type 2 «Interface» operation 2( ) «realize» operation 3( ) operation 5( ) operation 3( ) : Type 3 operation 4( ) : Type 4 operation 5( ) : Type 5 The subsystem must support all operations in the offered interfaces Advanced Modeling with UML 44

Subsystem Interfaces «Interface» Specification elements «realize» An interface operation may alternatively be supported by a specification element of the subsystem Advanced Modeling with UML 45

Specification – Realization n n The specification and the realization must be consistent The mapping between the specification and the realization can be expressed by: n n realization relationships collaborations Advanced Modeling with UML 46

Realize Relationship Operations operation 1( ) : Type 1 operation 2( ) : Type 2 Realization Elements «realize» operation 1( ) operation 3( ) : Type 3 operation 4( ) : Type 4 operation 5( ) : Type 5 Realization is particularly useful in simple mappings Advanced Modeling with UML 47

Realize – Example Traffic Control Realization elements Operations change. Digit. Analysis. Information ( ) Specification elements Initiate Call Trunk Subscription «realize» change. Digit. Analysis. Information ( ) : : Receive Digit and Connect Hook Signal and Disconnect Advanced Modeling with UML 48

Collaboration n n A collaboration defines the roles to be played when a task is performed The roles are played by interacting instances Sequence Diagram : Trunk : Traffic Control Collaboration Diagram : Subscription 2: dialled. Digit 3: dialled. Digit 6: b. Answer mark. Busy dialled. Digit : Traffic Control 4: through. Connect dialled. Digit : Trunk through. Connect mark. Busy : Subscription b. Answer 1: mark. Busy 5: mark. Busy Advanced Modeling with UML 49

Collaboration – Notation Collaboration Role role name Class role name A collaboration and its participants Advanced Modeling with UML 50

Collaboration – Example Specification elements Realization elements Initiate Call Network Interface Receive Digit and Connect Coordinator Analysis Database Hook Signal and Disconnect Collaborations are useful in more complex situations Advanced Modeling with UML 51

Subsystem Interaction 4: receive 1: transmit 2: send 3: ack 1. 4 Advanced Modeling with UML 52

Communicating with Subsystems n Two approaches: n n 1. 4 Open subsystem - public elements are accessed directly Closed subsystem - access via the subsystem itself Advanced Modeling with UML 53

Open Subsystems Traffic Control n -B +A «import» n Trunk +TC: : A -C A subsystem is called open if its realization is used directly by the environment The specification acts n n +B 1. 4 Advanced Modeling with UML as an overview of the subsystem as a requirements specification 54

Closed Subsystems Traffic Control n +A -B n A subsystem is called closed if its realization is not directly used by the environment The specification acts n Trunk n -C +B 1. 4 n as an overview of the subsystem as a requirements specification as a specification of how to use the subsystem Advanced Modeling with UML 55

Subsystem Instance : Traffic Control A subsystem instance is the runtime representation of a subsystem. Its task is to handle incoming stimuli. 1. 4 Advanced Modeling with UML 56

Subsystem Inheritance n A subsystem with a generalization to another subsystem inherits public and protected elements that are n n owned or imported by the inherited subsystem Both specification elements and realization elements are inherited Operations are also inherited Advanced Modeling with UML 57

Diagram Tour n n Subsystems can be shown in static diagrams and interaction diagrams “Fork” notation alternative for showing contents: Specification elements Realization elements Advanced Modeling with UML 58

Diagram Tour – continued n Subsystems can be shown in interaction diagrams n n collaboration diagrams sequence diagrams Sequence Diagram Advanced Modeling with UML 59

When to Use Subsystems n n To express how a large system is decomposed into smaller parts Distributed development To express how a set of modules are composed into a large system For component based development Advanced Modeling with UML 60

Component Based Development Shopping. Cart Specification Elements Realization Elements Shopping. Cart Home «Interface» Shopping. Cart. Home «call» Art. Store Client «call» create(…) «auxiliary. Class» Home. Object «call» Shopping. Cart find. By. Primary. Key(…). . . Shopping. Cart Impl «auxiliary. Class» «call» «Interface» Context Shopping. Cart get. Item. Count(…) set. Item. Count(…) get. Total(…) set. Total(…). . . «focal. Class» «auxiliary. Class» Remote. Object Context. Object Shopping. Cart DBbroker An EJB component modeled using a subsystem and classes stereotyped «focal. Class» or «auxiliary. Class» 1. 4 Advanced Modeling with UML 61

Modeling Tips – Subsystem n n n Define a subsystem for each separate part of a large system Choose specification technique depending on factors like kind of system and kind of subsystem Realize each subsystem independently, using the specification as a requirements specification Advanced Modeling with UML 62

Model n n n What are Models? Core Concepts Diagram Tour When to Use Models Modeling Tips Advanced Modeling with UML 63

Model A model captures a view of a system, with a certain purpose. It is a complete description of those aspects of the system relevant to the purpose of the model, at the appropriate level of detail. Advanced Modeling with UML 64

Model – Example Use Case Model Design Model Advanced Modeling with UML 65

Core Concepts Construct Model Trace Description A view of a system, with a certain purpose determining what aspects of the system are described and at what level of detail. A dependency connecting model elements that represent the same concept within different models. Traces are usually non-directed. Advanced Modeling with UML Syntax Name «trace» 66

Trace Analysis Design «trace» Advanced Modeling with UML 67

Model Inheritance n A model with a generalization to another model inherits public and protected elements that are n n owned or imported by the inherited model Advanced Modeling with UML 68

Diagram Tour n n Models as such are seldom shown in diagrams Two equivalent ways to show containment: Advanced Modeling with UML 69

Model vs. Diagram Use Case Model Diagrams make up the documentation of a model Design Model Advanced Modeling with UML 70

When to Use Models n n n To give different views of a system to different stakeholders To focus on a certain aspect of a system at a time To express the results of different stages in a software development process Advanced Modeling with UML 71

Modeling Tips – Model n n n Define the purpose for each model A model must give a complete picture of the system, within its purpose Focus on the purpose of the model; omit irrelevant information Advanced Modeling with UML 72

Models and Subsystems Models and subsystems can be combined in hierarchies: Advanced Modeling with UML 73

Wrap Up Model Management n Packages are used to organize a large set of model elements n n Subsystems are used to represent parts of a system n n n Visibility Import Access Specification Realization Models are used to capture different views of a system n Trace Advanced Modeling with UML 74

Advanced Modeling with UML n n n Part 1: Model Management Part 2: Extension Mechanisms and Profiles Part 3: Object Constraint Language (OCL) Advanced Modeling with UML 75

Semantic Variations in UML n n n UML contains semantic aspects that are: n undefined (e. g. , scheduling discipline), or n ambiguous (multiple choices/interpretations) Why is the case? n UML can’t address every domain perfectly n Different domains require different specializations But, if UML can be extended in arbitrary ways, what is the value of having a standard? Advanced Modeling with UML 76

UML as a “Family of Languages” n The standard can be specialized for different domains n in effect: refinements of the standard UML 1. 4 Standard UML for Real Time UML for CORBA Advanced Modeling with UML Using extensibility mechanisms etc. 77

Extensibility Mechanisms n n Allow Modelers to refine the semantics of UML for a specific domain Extensions cannot violate the standard UML semantics n n Enforces a consistent core of concepts and semantics for every variation Prevents meta-model explosion (Using UML to model everything and anything) Advanced Modeling with UML 78

How UML Extensibility Works n The standard UML semantics can be viewed as defining a space of possible interpretations refined semantics (valid) different semantics (NOT valid) Standard UML semantics Advanced Modeling with UML 79

Models n A model is a description of something n “a pattern for something to be made” (Merriam. Webster) blueprint (model) building n building model thing that is modeled n The Map is Not The Territory Advanced Modeling with UML 80

Meta-Models n Are Simply Models of Models <Ben&Jerry’s> Customer <sawdust> <2 tons> <lard> <5 tons> Customer. Order item quantity id Class Association Advanced Modeling with UML Objects (M 0) Model (M 1) Meta-Model (M 2) 81

The UML Meta-Model n Is a UML Model of UML Generalizable. Element is. Root : Boolean is. Leaf : Boolean is. Abstract : Boolean Meta-Class Feature visibility : {public, private, protected} * Classifier Well-formedness constraint (OCL) Class is. Active : Boolean not self. is. Abstract implies self. all. Operations->for. All(op | self. all. Methods->exists(m | m. specification includes (op))) Advanced Modeling with UML 82

The UML Meta-Model n Put another way, UML uses (a part of) itself as a Meta-language n n Other examples: Scheme, Lisp, Smalltalk This small subset of UML is used to describe all of UML n n The subset contains classes, associations, operations, constraints, generalization, etc. This core subset is related to MOF (more on MOF in Part 4) Advanced Modeling with UML 83

The Basic Extensibility Mechanisms n Stereotypes n n Constraints n n n used to refine meta-classes (or other stereotypes) by defining supplemental semantics predicates (e. g. , OCL expressions) that reduce semantic variation can be attached to any meta-class or stereotype Tagged Values n n individual modifiers with user-defined semantics can be attached to any meta-class or stereotype Advanced Modeling with UML 84

Stereotypes n n Used to define specialized model elements based on a core UML model element Defined by: n Base metaclasses (or stereotype) n n Constraints: n n 1. 4 What values does this stereotype need to know? icon n n What is special about this stereotype? required tags (0. . *) n n What element is specialized? How should I appear in a model? A model element can be stereotyped in multiple different ways Advanced Modeling with UML 85

Example n n Capsule: A special type of concurrent object used in modeling certain realtime systems By definition, all classes of this type: n n are active (concurrent) have only features (attributes and operations) with protected visibility have a special “language” characteristic used for code generation purposes In essence, a constrained form of the general UML Class concept Advanced Modeling with UML 86

Example: Stereotype Definition n Using a tabular form: Stereotype Base Class Tags Constraints is. Active = true; «capsule» 1. 4 Class language self. feature->select(f | f. ocl. Is. Kind. Of(Operation))-> for. All(o | o. element. Ownership. visibility = #protected) Tag Stereotype Type Multiplicity language «capsule» String 0. . 1 Advanced Modeling with UML 87

Stereotype Notation n Several choices «capsule» a. Capsule. Class (a) with guillemets (“gwee-mays”) Stereotype icon a. Capsule. Class (b) with icon (c) iconified form Advanced Modeling with UML 88

Extensibility Method n Refinements are specified at the Model (M 1) level but apply to the Meta-Model level (M 2) n n avoids need for “meta-modeling” CASE tools can be exchanged with models «capsule» a. Capsule. Class Customer. Order «Capsule» item quantity id Class Association Advanced Modeling with UML (M 1) (M 2) 89

Graphical Definition n Alternative to the tabular form n defined in a user (M 1) model «metaclass» Class «stereotype» capsule Tags language : String 1. 4 Advanced Modeling with UML 90

Heuristic: Combining Stereotypes n Through multiple inheritance: «stereotype» capsule «metaclass» Package Tags language : String «stereotype» Package. Capsule 1. 4 Advanced Modeling with UML 91

When to Use Stereotypes? n n Why not use normal subclassing instead? Use stereotypes when: n additional semantic constraints cannot be specified through standard M 1 -level modeling facilities n n e. g. “all features have protected visibility” the additional semantics have significance outside the scope of UML n e. g. instructions to a code generator “debug. On = true” Advanced Modeling with UML 92

Tagged Values n n n Consist of a tag and value pair Typed with a standard data type or M 1 class name Typically used to model stereotype attributes n n May also be used independently of stereotypes n 1. 4 Additional information that is useful/required to implement/use the model e. g. , project management data (“status = unit_tested”) Advanced Modeling with UML 93

Constrains n Constraints n n formal or informal expressions must not contradict inherited base semantics ATM_Withdrawal Account customer : id amount : Money {amount is multiple of $20} customer : id balance : Money «constraint» {ATM_Withdrawal. customer = Account. customer} Advanced Modeling with UML 94

UML Profiles n A package of related extensibility elements that capture domain-specific variations and usage patterns n n A domain-specific interpretation of UML Profiles currently being defined by the OMG: n n EDOC Real-Time CORBA. . . Advanced Modeling with UML 95

Advanced Modeling with UML n n n Part 1: Model Management Part 2: Extension Mechanisms and Profiles Part 3: Object Constraint Language (OCL) Advanced Modeling with UML 96

Overview n n What are constraints? Core OCL Concepts Advanced OCL Concepts Wrap up Advanced Modeling with UML 97

Why use OCL ? What’s in it for me? Use the Object Constraint Language Advanced Modeling with UML 98

Can we make this more precise? Flight Passenger. Flight flights 0. . * 1 Cargo. Flight Airplane 0. . * 1 1 Passenger. Plane Cargo. Plane 99

Diagram with added invariants Flight type : enum of cargo, passenger 0. . * flights 1 Airplane type : enum of cargo, passenger {context Flight inv: type = #cargo implies airplane. type = #cargo inv: type = #passenger implies airplane. type = #passenger} 100

Definition of constraint n “A constraint is a restriction on one or more values of (part of) an objectoriented model or system. ” Advanced Modeling with UML 101

Different kinds of constraints n Class invariant n n Precondition of an operation n n a constraint that must always be met by all instances of the class a constraint that must always be true BEFORE the execution of the operation Postcondition of an operation n a constraint that must always be true AFTER the execution of the operation Advanced Modeling with UML 102

Constraint stereotypes n UML defines three standard stereotypes for constraints: n n n invariant precondition postcondition Advanced Modeling with UML 103

What is OCL? n OCL is n n a textual language to describe constraints the constraint language used in UML models n n As well as the UML meta-model Formal but easy to use n n unambiguous no side effects Advanced Modeling with UML 104

Constraints and the UML model n OCL expressions are always bound to a UML model n OCL expressions can be bound to any model element in UML Advanced Modeling with UML 105

Core OCL Concepts n n What are constraints Core OCL Concepts Advanced OCL Concepts Wrap up Advanced Modeling with UML 106

Example model Flight Airport origin name: String destination departing Flights depart. Time: Time * /arrival. Time: Time duration : Interval * max. Nr. Passengers: Integer flights airline * arriving Flights Airline passengers * {ordered} name: String Passenger $min. Age: Integer age: Integer needs. Assistance: Boolean 0. . 1 book(f : Flight) CEO Advanced Modeling with UML airline 0. . 1 107

Constraint context and self n Every OCL expression is bound to a specific context. n n The context is often the element that the constraint is attached to The context may be denoted within the expression using the keyword ‘self’. n n ‘self’ is implicit in all OCL expressions Similar to`this’ in C++ Advanced Modeling with UML 108

Notation n Constraints may be denoted within the UML model or in a separate document. n the expression: context Flight inv: self. duration < 4 n is identical to: context Flight inv: duration < 4 n is identical to: Flight <<invariant>> duration < 4 duration: Integer Advanced Modeling with UML 109

Elements of an OCL expression n In an OCL expression these elements may be used: n n basic types: String, Boolean, Integer, Real. classifiers from the UML model and their features n n n attributes, and class attributes query operations, and class query operations (i. e. , those operations that do not have side effects) associations from the UML model Advanced Modeling with UML 110

Example: OCL basic types context Airline inv: name. to. Lower = ‘klm’ context Passenger inv: age >= ((9. 6 - 3. 5)* 3. 1). floor implies mature = true Advanced Modeling with UML 111

Model classes and attributes n “Normal” attributes context Flight inv: self. max. Nr. Passengers <= 1000 n Class attributes context Passenger inv: age >= Passenger. min. Age Advanced Modeling with UML 112

Example: query operations context Flight inv: self. depart. Time. difference(self. arrival. Time). equals(self. duration) Time $midnight: Time month : String day : Integer year : Integer hour : Integer minute : Integer difference(t: Time): Interval before(t: Time): Boolean plus(d : Interval) : Time Interval nr. Of. Days : Integer nr. Of. Hours : Integer nr. Of. Minutes : Integer equals(i: Interval): Boolean $Interval(d, h, m : Integer) : Interval Advanced Modeling with UML 113

Associations and navigations n n n Every association in the model is a navigation path. The context of the expression is the starting point. Role names are used to identify the navigated association. Advanced Modeling with UML 114

Example: navigations Flight Airport origin name: String destination departing Flights depart. Time: Time * /arrival. Time: Time duration : Interval * max. Nr. Passengers: Integer arriving Flights context Flight inv: origin <> destination inv: origin. name = ‘Amsterdam’ context Flight inv: airline. name = ‘KLM’ Advanced Modeling with UML 115

Association classes context Person inv: if employer. name = ‘Klasse Objecten’ then job. type = #trainer else job. type = #programmer endif Job type : {trainer, programmer} Person * employee 1 employer Advanced Modeling with UML Company name : String 116

The OCL Collection types n n What are constraints Core OCL Concepts n n n Collections Advanced OCL Concepts Wrap up Advanced Modeling with UML 117

Significance of Collections in OCL n Most navigations return collections rather than single elements Flight type : enum of cargo, passenger 0. . * flights 1 Airplane type : enum of cargo, passenger Advanced Modeling with UML 118

Three Subtypes of Collection n Set: n n n Bag: n n n arriving. Flights(from the context Airport) Non-ordered, unique arriving. Flights. duration (from the context Airport) Non-ordered, non-unique Sequence: n n passengers (from the context Flight) Ordered, non-unique Advanced Modeling with UML 119

Collection operations n n OCL has a great number of predefined operations on the collection types. Syntax: n collection->operation Use of the “->” (arrow) operator instead of the “. ” (dot) operator Advanced Modeling with UML 120

The collect operation n Syntax: collection->collect(elem : T | expr) collection->collect(elem | expr) collection->collect(expr) n Shorthand: collection. expr n n The collect operation results in the collection of the values resulting evaluating expr for all elements in the collection Shorthand often trips people up. Be Careful! Advanced Modeling with UML 121

Example: collect operation context Airport inv: self. arriving. Flights -> collect(air. Line) ->not. Empty airp 1 f 2 f 3 airp 2 airline 1 airline 2 f 4 airline 3 f 5 departing flights arriving flights Advanced Modeling with UML 122

The select operation n Syntax: collection->select(elem : T | expression) collection->select(elem | expression) collection->select(expression) n The select operation results in the subset of all elements for which expression is true Advanced Modeling with UML 123

Example: collect operation context Airport inv: self. departing. Flights->select(duration<4)->not. Empty f 1 duration = 2 airp 1 f 2 duration = 5 f 3 duration = 3 f 4 duration = 5 airp 2 departing flights arriving flights airline 1 airline 2 airline 3 f 5 duration = 2 Advanced Modeling with UML 124

The for. All operation n Syntax: n n collection->for. All(elem : T | expr) collection->for. All(elem | expr) collection->for. All(expr) The for. All operation results in true if expr is true for all elements of the collection Advanced Modeling with UML 125

Example: for. All operation context Airport inv: self. departing. Flights->for. All(depart. Time. hour>6) f 1 depart = 7 airp 1 f 2 depart = 5 f 3 depart = 8 f 4 depart = 9 airp 2 airline 1 airline 2 airline 3 f 5 depart = 8 departing flights arriving flights Advanced Modeling with UML 126

The exists operation n Syntax: collection->exists(elem : T | expr) collection->exists(elem | expr) collection->exists(expr) n The exists operation results in true if there is at least one element in the collection for which the expression expr is true. Advanced Modeling with UML 127

Example: exists operation context Airport inv: self. departing. Flights->exists(depart. Time. hour<6) f 1 depart = 7 airp 1 f 2 depart = 5 f 3 depart = 8 airp 2 f 4 depart = 9 airline 1 airline 2 airline 3 f 5 depart = 8 departing flights arriving flights Advanced Modeling with UML 128

Other collection operations n n n n is. Empty: true if collection has no elements not. Empty: true if collection has at least one element size: number of elements in collection count(elem): number of occurences of elem in collection includes(elem): true if elem is in collection excludes(elem): true if elem is not in collection includes. All(coll): true if all elements of coll are in collection Advanced Modeling with UML 129

Advanced OCL Concepts n n What are constraints Core OCL Concepts Advanced OCL Concepts Wrap up Advanced Modeling with UML 130

Result in postcondition n Example pre and postcondition context Airline: : served. Airports() : Set(Airport) pre : -- none post: result = flights. destination->as. Set Advanced Modeling with UML 131

Statechart: referring to states n The operation ocl. In. State returns true if the object is in the specified state. Bottle filled : enum {empty, half, full} closed open context Bottle inv: self. ocl. In. State(closed) implies filled = #full Advanced Modeling with UML 132

Local variables n The let construct defines variables local to one constraint: Let var : Type = <expression 1> in <expression 2> n Example: context Airport inv: Let supported. Airlines : Set (Airline) = self. arriving. Flights -> collect(air. Line) in (supported. Airlines ->not. Empty) and (supported. Airlines ->size < 500) Advanced Modeling with UML 133

Iterate n The iterate operation for collections is the most generic and complex building block. collection->iterate(elem : Type; answer : Type = <value> | <expression-with-elem-and-answer>) Advanced Modeling with UML 134

Iterate example n Example iterate: context Airline inv: flights->select(max. Nr. Passengers > 150)->not. Empty n Is identical to: context Airline inv: flights->iterate (f : Flight; answer : Set(Flight) = Set{ } | if f. max. Nr. Passengers > 150 then answer->including(f) else answer endif )->not. Empty Advanced Modeling with UML 135

Inheritance of constraints n Guiding principle Liskov’s Substitution Principle (LSP): n “Whenever an instance of a class is expected, one can always substitute an instance of any of its subclasses. ” Advanced Modeling with UML 136

Inheritance of constraints n Consequences of LSP for invariants: n n n An invariant is always inherited by each subclass. Subclasses may strengthen the invariant. Consequences of LSP for preconditions and postconditions: n n A precondition may be weakened (contravariance) A postcondition may be strengthened (covariance) Advanced Modeling with UML 137

Wrap Up n n What are constraints Core OCL Concepts Advanced OCL Concepts Wrap up Advanced Modeling with UML 138

Current Developments n Feedback from several OCL implementers handled in UML-RTF n n n Development of OCL metamodel n n n e. g. the grammar has some loose ends typical tool-related issues currently concrete syntax only will result in abstract syntax UML 2. 0 RFP n n meta-model as part of the UML meta-model additional capabilities (e. g. , business rules) Advanced Modeling with UML 139

OCL Tools n Cybernetics n n University of Dresden n www. boldsoft. com ICON computing n n www-st. inf. tu-dresden. de/ocl/ Boldsoft n n ww. cybernetic. org www. iconcomp. com Royal Dutch Navy Others … … Advanced Modeling with UML 140

Conclusions and Tips n OCL invariants allow you to n n n OCL pre- and postconditions allow you to n n n model more precisely remain implementation independent specify contracts (design by contract) specify interfaces of components more precisely OCL usage tips n n n keep constraints simple always combine natural language with OCL use a tool to check your OCL Advanced Modeling with UML 141

Further Resources for OCL n The Object Constraint Language n n ISBN 0 -201 -37940 -6 OCL home page n www. klasse. nl/ocl/index. htm Advanced Modeling with UML 142
![References n n [UML 1. 3] OMG UML Specification v. 1. 3, OMG doc# References n n [UML 1. 3] OMG UML Specification v. 1. 3, OMG doc#](http://slidetodoc.com/presentation_image_h/86e146a5f47125e6b06a0038a05f4563/image-143.jpg)
References n n [UML 1. 3] OMG UML Specification v. 1. 3, OMG doc# ad/06 -08 -99 [UML 1. 4] OMG UML Specification v. 1. 4, UML Revision Task Force recommended final draft, OMG doc# ad/01 -02 -13. Advanced Modeling with UML 143

Further Info n Web: n n n UML 1. 4 RTF: www. celigent. com/omg/umlrtf OMG UML Tutorials: www. celigent. com/omg/umlrtf/tutorials. htm UML 2. 0 Working Group: www. celigent. com/omg/adptf/wgs/uml 2 wg. htm OMG UML Resources: www. omg. org/uml/ Email n n uml-rtf@omg. org Contributors n n n Karin Palmkvist: karin. palmkvist@enea. se Bran Selic: bselic@rational. com Jos Warmer: j. warmer@klasse. nl Nathan Dykman: nathan_dykman@hp. com Conferences & workshops n n n UML World 2001, New York, June 11 -14, 2001 UML 2001, Toronto, Canada, Oct. 1 -5, 2001 OMG UML Workshop 2001, San Francisco, Dec. 3 -6, 2001 Advanced Modeling with UML 144
- Slides: 144