Constraints Spring 2001 Kyung Hee University 1 Graphical

  • Slides: 35
Download presentation
Constraints Spring 2001 Kyung Hee University 1

Constraints Spring 2001 Kyung Hee University 1

Graphical Notations q. Graphical notations are well suited for displaying structural aspects of systems,

Graphical Notations q. Graphical notations are well suited for displaying structural aspects of systems, but less effective for documenting the fine details of a system’s specification. One disadvantage is that graphical notation can quickly become very complex. q. Graphical notations can be supplemented where necessary by textual annotations which state properties that are not implied by the diagram. Such annotations are known as constraints q. A constraint is an assertion about a model element, often a class, which states properties that must be satisfied in a legal model of the system. Kyung Hee University 2

Graphical Notations (cont’d) q Constraints are written in UML in braces ‘{‘ and ‘}’

Graphical Notations (cont’d) q Constraints are written in UML in braces ‘{‘ and ‘}’ in or near the model element that they describe. UML defines standard constraints for a handful of common situations. q More general constraints can be written either in informal English, in a more formal constraints language, or even using the notation of the target programming language. q UML defines a constraint language, called the Object Constraint Language or OCL Figure 9. 1 A constrained saving account Kyung Hee University 3

Standard Constraints q‘New’ and ‘Destroyed’ are simple examples of the standard constraints defined by

Standard Constraints q‘New’ and ‘Destroyed’ are simple examples of the standard constraints defined by UML Two other commonly used standard constraints describe properties of pairs of related associations Kyung Hee University 4

Standard Constraints (cont’d) q. The subset constraint can be applied to pairs of associations

Standard Constraints (cont’d) q. The subset constraint can be applied to pairs of associations that link the same classes, and states that the set of links which are instances of one associations with a dependency arrow to which the constraint is attached In general, this constraint means that if two objects are linked by an instance of the association at the tail of the dependency arrow, they must also be linked by an instance of the association at the head of the dependency arrow Kyung Hee University 5

Standard Constraints (cont’d) q. The xor constraint can be applied to pairs of associations

Standard Constraints (cont’d) q. The xor constraint can be applied to pairs of associations that are connected at one end to the same class. It is used to specify that an instance of the shared class can not participate in both associations at the same time Figure 9. 3 The xor constraint can be applied to pairs of associations that are connected at one end to the same class. It is used to specify that an instance of the shared class can not participate in both associations at the same time Kyung Hee University 6

Standard Constraints (cont’d) In general, this constraint means that an instance of the class

Standard Constraints (cont’d) In general, this constraint means that an instance of the class that is connected to both the constraint associations can at any given time participate in a link from one of the associations or the other, but not both. Notice that this means that the multiplicity of the constrained associations must include the zero case. Kyung Hee University 7

The Object Constraint Language q. The object constraint language The standard constraint are useful

The Object Constraint Language q. The object constraint language The standard constraint are useful in particular cases, but in order to handle the arbitrary constraints that can arise on modeling a constraint specification language is required. OCL, the Object Constraint Language, is a text-based formal language which allows completely general constraints to be written for the elements appearing in UML models, particularly in class diagrams. Kyung Hee University 8

Standard Constraints (cont’d) q OCL has to provide three essential capabilities The ability to

Standard Constraints (cont’d) q OCL has to provide three essential capabilities The ability to specify which model element is being constrained The ability to navigate through a model to identify other objects which may be relevant to the constraint being defined The ability to make assertions about the relationships between the context object and any objects retrieved by means of navigation expressions Kyung Hee University 9

The Context of A Constraint q Every OCL constraint has a context, which is

The Context of A Constraint q Every OCL constraint has a context, which is simply the model element that is being constrained q Sometimes, the context of a constraint will be a class q. If context is a class, this can be done by writing the constraint in the class symbol near the element constrained Figure 9. 5 A simple constraint Kyung Hee University 10

The Context of a Constraint (cont’d) q Constraints can also be written in textual

The Context of a Constraint (cont’d) q Constraints can also be written in textual form, instead of being shown on a diagram. In this case, the context needs to be explicitly recorded Savings. Account self. balance > 0 and self. balance < 250000 q. The keyword ‘self’ in the textual form of the constraint simply refers to the current context object q. This notation is clearly chosen to resemble the notation used in languages such as Java and C++ for accessing features of classes Kyung Hee University 11

Navigation Expressions q. The use of constraints is not limited to stating the invariants

Navigation Expressions q. The use of constraints is not limited to stating the invariants of classes considered in isolation. q. Many constraints place restriction on relationships between model elements q. Informal links to gain access to other objects Navigation expressions Kyung Hee University 12

Navigation Expressions (cont’d) q. A self-association on the employee class shows who in the

Navigation Expressions (cont’d) q. A self-association on the employee class shows who in the company is managed by whom. A qualified association allows individual employees to be accessed by a unique payroll number Kyung Hee University 13

Navigation Expressions (cont’d) q. The association between the person and company classes represents the

Navigation Expressions (cont’d) q. The association between the person and company classes represents the relationship of a person working for the company q. Following links The basic form of navigation involves following links from one object to another To show navigation, role names on the association end opposite the context object are written after the name of that object, using dot notation for attributes Department Self. staff Kyung Hee University 14

Navigation Expressions (cont’d) If an association has no role name, the name of the

Navigation Expressions (cont’d) If an association has no role name, the name of the class at the further end of the association is used instead, with a lower-case initial letter Company Self. department The class name cannot be used in this way if there is any danger of ambiguity, for example if there are two associations between a pair of classes. In this case, suitable role names must be added to the associations to allow the required navigation expressions to be formed unambiguously Kyung Hee University 15

Navigation Expressions (cont’d) q Collections A navigation expression denotes the object that are retrieved

Navigation Expressions (cont’d) q Collections A navigation expression denotes the object that are retrieved by following the specified links from the context object Depending on the multiplicities of the associations traversed, however, the number of such objects may vary Person Self. department Person Self. manager Kyung Hee University 16

Navigation Expressions (cont’d) In general, in OCL a navigation expression that can return more

Navigation Expressions (cont’d) In general, in OCL a navigation expression that can return more that one object is said to return a collection, whereas others simply return single objects q. Iterated traversal Navigation expressions are not limited to following a single association. More complicated paths through a diagram can be described by writing a sequence of role or class names, separated by dots. Company Self. department. staff Kyung Hee University 17

Navigation Expressions (cont’d) q Traversing qualified associations Qualified associations can be used in navigation

Navigation Expressions (cont’d) q Traversing qualified associations Qualified associations can be used in navigation just as well as ordinary ones, but they provide an additional capacity for locating individual objects. When navigating towards the qualifier, there is no difference between a qualified an unqualified association. Person Self. employer Kyung Hee University 18

Navigation Expressions (cont’d) When navigating in the opposite direction, however, the qualifier provides a

Navigation Expressions (cont’d) When navigating in the opposite direction, however, the qualifier provides a way of picking out a particular employee whose payroll number is known. Company Self. employee[314159] This notation can be freely combined with subsequent navigation or selection of attributes. For example, the following expression returns the manager of the employee with payroll number 314159. Company Self. employee[314159]. manager Kyung Hee University 19

Navigation Expressions (cont’d) q Using association classes Navigate from an instance of an association

Navigation Expressions (cont’d) q Using association classes Navigate from an instance of an association class to the objects at the ends of the association, using role names or class names as normal. Grade Self. contract. employee Person Self. contract. grade Kyung Hee University 20

Objects and Collections q. OCL is designed so that in many situations the distinction

Objects and Collections q. OCL is designed so that in many situations the distinction between individual objects and collections, or between different kinds of collections, can be ignored. q. Operations on objects Apart from collections, the types available in OCL consist of basic types representing boolean values, real and integer numbers and strings, and model types corresponding to classes defined in the UML model for which constraints are being written Expression Person Self. age() Kyung Hee University Self. contract. grade. salary 21

Objects and Collections (cont’d) q Different types of collection Department staff. contract. grade representing

Objects and Collections (cont’d) q Different types of collection Department staff. contract. grade representing the collection of grade objects for the employees in the department A form of collection which can contain duplicate items v bag Kyung Hee University 22

Objects and Collections (cont’d) q Operations on collections OCL defines a number of basic

Objects and Collections (cont’d) q Operations on collections OCL defines a number of basic operations on collections of all types. One of these is an operation ‘sum’, which adds together all the elements in the collection and returns the total. Using this operation, the total salary bill for a department could be defined as shown below. Notice that the symbol ‘->’ is used in OCL indicate that a collection operation is being applied Department Staff. contract. grade. salary->sum() Kyung Hee University 23

Objects and Collections (cont’d) Another useful operation (return the value) Department staff. contract. grade.

Objects and Collections (cont’d) Another useful operation (return the value) Department staff. contract. grade. as. Set()->size This particular example could be handled if it was possible to form a new collection by picking out the required employees from the larger collection returned by simple navigation. OCL provides a number of operations on collections to perform such tasks. v including the declaration of a ‘local variable’ which provides a context for navigation in the boolean expression Company employee->select(p: Person | p. contract. grade. salary > 50000) Kyung Hee University 24

Objects and Collections (cont’d) Retrieving the managers of highly paid employees Company Employee. select

Objects and Collections (cont’d) Retrieving the managers of highly paid employees Company Employee. select (contract. grade. salary > 5000). manager • retuning the ages of all the employees in a department Department Staff -> collect (p : Person | p. age ( )) • using a collect expression to calculate the company’s salary Company Kyung Hee Contract. grade -> collect (salary * 1. 1) -> sum ( ) University 25

Objects and Collections (cont’d) • equivalent ways of returning the names of all the

Objects and Collections (cont’d) • equivalent ways of returning the names of all the employees in a department Deparment Self. staff -> collect (name) Self. staff. name Kyung Hee University 26

Constraint q Basic constraints standard operators : ‘=‘ , ‘<>’ for equality and inequality

Constraint q Basic constraints standard operators : ‘=‘ , ‘<>’ for equality and inequality Person Self. employer = self. department. company • the assertion that all employees are aged 18 or over, and often be formalized by defining a collection Company Employee -> select (age () < 18 ) -> is. Empty Employee -> select (age () < 18 ) -> size = 0 Kyung Hee University 27

Constraint (cont’d) • every employee’s grade is one of the set of grades linked

Constraint (cont’d) • every employee’s grade is one of the set of grades linked to that employee’s company Person Employee. grade->includes(contract. grade) • the staff of department are all employees of the company the department belongs to Department Company. employee->includes. All(staff) Kyung Hee University 28

Constraint (cont’d) q. Combining constraints Constraints can be combined using the boolean operators ‘and’,

Constraint (cont’d) q. Combining constraints Constraints can be combined using the boolean operators ‘and’, ‘or’, ‘xor’ , ’not’. OCL differs from most programming languages in defining a boolean operator representing implication. Person self. age() > 50 implies self. contract. grade. salary > 2500 Kyung Hee University 29

Constraint (cont’d) q. Iterative constraints resemble iterative operators such as ‘select’ in that they

Constraint (cont’d) q. Iterative constraints resemble iterative operators such as ‘select’ in that they are defined over collections, and return a result which is determined by applying a boolean expression Rather than returning a new collection, however, they return a boolean value which depends on the results obtained for each individual element. Company self. grade->for. All (g | not g. contract->is. Empty()) Department Staff -> exists (e | e. manager -> is. Empty ()) Kyung Hee University 30

Constraint (cont’d) Grade salary > 20000 Grade. all. Instance->for. All(g | g. salary >

Constraint (cont’d) Grade salary > 20000 Grade. all. Instance->for. All(g | g. salary > 20000) Grade. all. Instance->for. All(g : Grade | g <> self implies g. salary <> self. salary) Kyung Hee University 31

Stereotyped Constraints q Constraints are commonly used to state properties of a class and

Stereotyped Constraints q Constraints are commonly used to state properties of a class and its operations that cannot be represented graphically. q Include limitations on the possible states of instances of the class, and certain aspects of the operations defined in the class. q Class invariants A class invariant is a property of a class that is intended to be true at all times for all instance of the class. Saving. Account balance > 0 and balance < 250000 Kyung Hee University Figure 9. 7 class specification using constraints 32

Time events q. Preconditions and postconditions Even if an invariant is defined for a

Time events q. Preconditions and postconditions Even if an invariant is defined for a class, there is no guarantee that the operations of the class will ensure that the invariant is maintained. Preconditions and postconditions are special constraints that can be written for operations. Precondition is something that must be true just before an operation is called. And usually expressed as a constraint relating the attributes of a class instance and the actual parameters of the operation being specified Postcondition is something that must be true just after operation has completed. And typically specify the effect of an operation by comparing the attributes values before and after execution of the operation Saving. Account : : withdraw (amt) Pre: amt < balance Post : balance = balance@pre - amt Kyung Hee University 33

Constraints and Generalization q. Generalization relationships do not give rise to any navigable relationships

Constraints and Generalization q. Generalization relationships do not give rise to any navigable relationships between object, and so they do not feature explicitly in constraints Figure 9. 8 Polymorphic account holding Kyung Hee University 34

Constraints and Generalization Customer Account->size > 0 implies Account->select(oc 1 Istyped. Of(current. Account))->size 1

Constraints and Generalization Customer Account->size > 0 implies Account->select(oc 1 Istyped. Of(current. Account))->size 1 Individual Account-> for. All (a | a. ocl. Type) = personal. Account) Kyung Hee University 35