0% found this document useful (0 votes)
15 views50 pages

Lecture 9 SW

The document discusses Object Constraint Language (OCL) and how it is used to formally specify constraints in UML models. It then provides examples of using OCL constraints in class diagrams and state diagrams. Finally, it discusses best practices for modeling interactions and behavior using UML diagrams like sequence diagrams, communication diagrams, and state diagrams.

Uploaded by

MOHANAD MOhamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views50 pages

Lecture 9 SW

The document discusses Object Constraint Language (OCL) and how it is used to formally specify constraints in UML models. It then provides examples of using OCL constraints in class diagrams and state diagrams. Finally, it discusses best practices for modeling interactions and behavior using UML diagrams like sequence diagrams, communication diagrams, and state diagrams.

Uploaded by

MOHANAD MOhamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Design Models

Getting Ready to Move on to Implementation

Lecture 9
Object Constraint Language ( O C L )
 OCL is a specification language designed to
formally specify constraints in software modules
 An O C L expression simply specifies a logical fact (a
constraint) about the system that must remain
true
 A constraint cannot have any side-effects
 it cannot compute a non-Boolean result nor modify any data.
 OCL statements in class diagrams can specify what
the values of attributes and associations must be
2
O C L statements
 OCL statements can be built from:
 References to role names, association names,
attributes and the results of operations
 The logical values true and false
 Logical operators such as and, or, =, >, < or <> (not
equals)
 String values such as: ‘a string’
 Integers and real numbers
 Arithmetic operations *, /, +, -

3
An example: constraints on Polygons

4
Detailed Example: A C l a s s Diagram for
Genealogy

 Problems
 A person must have two parents
 Marriages not properly accounted for

5
Genealogy example: Possible solutions

6
C l a s s Diagrams with Interfaces
 Two ways to show association of a class with
an interface.

7
Sequence diagrams – a n example

8
Sequence diagrams –
same example, more details
altis used to described alternative scenarios of a workflow. Only one of
the options will be executed.
opt is used to describe optional step in workflow.

9
Sequence diagrams –
a n example with replicated messages
 An iteration over objects is indicated by an asterisk preceding
the message name

10
Sequence diagrams –
a n example with object deletion
 If an object’s life ends, this is shown with an X at the end of
the lifeline

11
Communication diagrams – a n example

12
Communication diagrams –
same example, more details
13
State diagrams – an example of transitions
with time-outs and conditions

14
State diagrams – a n example with
conditional transitions

15
Activities i n state diagrams
 An activity is something that takes place while the system is
in a state.

 It takes a period of time.

 The system may take a transition out of the state in response


to completion of the activity,

 Some other outgoing transition may result


in:  The interruption of the activity, and
 An early exit from the state.

16
State diagram – a n example with activity

17
Actions i n state diagrams
 An action is something that takes place
effectively instantaneously
 When a particular transition is
taken,  Upon entry into a particular
state, or  Upon exit from a particular
state

 An action should consume no noticeable amount of time


18
State diagram – a n example with actions

19
State diagrams – another example

20
Nested substates and guard conditions
A state diagram can be nested inside a state.
 The states of the inner diagram are called substates.

21
State diagram – a n example with substates

22
State machine for a Phone Line

23
Umple for the Phone Line example
• Umple is a language for both object-oriented programming
class phone
{ and modelling with class diagrams and state diagrams.
 state {

 onHook {  dialing {
 startDialing -> dialling;  completeNumber -> waitingForConnection;
 incomingCall ->  hangUp -> onHook;
ringing;  }  }
 ringing {  waitingForConnection {
 pickUp -> communicating;  otherPartyPickUp -> communicating;
 otherPartyHangUp -> onHook;  hangUp -> onHook;
 }  timeOut -> onHook;
 communicating {  }
 hangUp -> onHook;  waitForHook {
 otherPartyHangUp -> waitForHook;  hangUp -> onHook;
 putOnHold -> onHold;  }
 }  }
 onHold { }
 hangUp -> onHook;
 otherPartyHangUp -> waitForHook;
 takeOffHold -> communicating;
24  }
Activity Diagrams - Swimlanes
Activitydiagrams are most often associated
with several classes.
 The partition of activities among the existing classes can
be explicitly shown using swimlanes.

25
Partitioned Activity diagrams – a n example
with swimlanes

26
Partitioned Activity diagrams – a n example
with swimlanes
 Partitioned by actor

27
Partitioned Activity diagrams – a n example
with swimlanes
 Partitioned by course of action

28
Implementing C l a s s e s Based on Interaction
and State Diagrams
 You should use these diagrams for the parts of your
system that you find most complex.
 I.e. not for every class

 Interaction, activity and state diagrams help you create


a correct implementation.

 This is particularly true when behaviour is distributed


across several use cases.
 E.g. a state diagram is useful when different conditions
cause instances to respond differently to the same event.

29
Example

30
Example: The CourseSection c l a s s
States:
 ‘Planned’:
closedOrCancelled == false && open == false

 ‘Cancelled’:
closedOrCancelled == true &&
registrationList.size() == 0

 ‘Closed’(course section is too full, or being


taught): closedOrCancelled == true &&
registrationList.size() > 0
31
Example: The CourseSection c l a s s
States:
 ‘Open’ (accepting
registrations): open == true

 ‘NotEnoughStudents’ (substate
of ‘Open’): open == true &&
registrationList.size() < course.getMinimum()

 ‘EnoughStudents’ (substate
of ‘Open’): open == true &&
registrationList.size() >= course.getMinimum()
32
Example: The CourseSection c l a s s
public CourseSection(Course course)
{
this.course = course;
registrationList = new LinkedList();
}

public void openRegistration()


{
if(!closedOrCanceled) // must be in 'Planned' state
{
open = true; // to 'OpenNotEnoughStudents' state
}
}

33
Example: The CourseSection c l a s s
public void closeRegistration()
{
 // to 'Canceled' or 'Closed' state
 open = false;
 closedOrCanceled = true;
 if (registrationList.size() < course.getMinimum())
 {
 unregisterStudents(); // to 'Canceled' state
 }
}

public void cancel()


{
 // to 'Canceled' state
 open = false;
 closedOrCanceled = true;
 unregisterStudents();
}

34
Example: The CourseSection c l a s s
public void requestToRegister(Student student)
{
if (open) // must be in one of the two 'Open' states
{
// The interaction specified in the sequence diagram of Figure 8.4
Course prereq = course.getPrerequisite();
if (student.hasPassedCourse(prereq))
{
// Indirectly calls addToRegistrationList
new Registration(this, student);
}
// Check for automatic transition to 'Closed' state
if (registrationList.size() >= course.getMaximum())
{
// to 'Closed' state
open = false;
closedOrCanceled = true;
}
}
}

35
Example: The CourseSection c l a s s
// Private method to remove all registrations
// Activity associated with 'Canceled' state.
private void unregisterStudents()
{
Iterator it = registrationList.iterator();
while (it.hasNext())
{
Registration r = (Registration)it.next();
r.unregisterStudent();
it.remove();
}
}
// Called within this package only, by the constructor of
// Registration to ensure the link is bi-directional
void addToRegistrationList(Registration newRegistration)
{
registrationList.add(newRegistration);
}
}

36
Difficulties and R i s k s i n Modelling
Interactions and Behaviour
Dynamic modelling is a difficult skill
 In a large system there are a very large number of possible paths
a system can take.
 It is hard to choose the classes to which to allocate each
behaviour:
 Ensure that skilled developers lead the process, and ensure
that all aspects of your models are properly reviewed.
 Work iteratively:
 Develop initial class diagrams, use cases,
responsibilities, interaction diagrams and state diagrams;
 Then go back and verify that all of these are consistent, modifying
them as necessary.
 Drawing different diagrams that capture related, but
distinct, information will often highlight problems.
37
Example

38
Example

39
Example

40
Example

41
U M L Interaction Overview Diagrams
 They arevariants on
UML activity
diagrams which
overview control flow.
 The nodes within the diagram
are frames instead of the
normal activates.
 Two types of frames:
 Interaction frames:
 Sequence diagram (sd)
 Communication diagram (cd)
 Interaction overview diagram
(iod)
 Interaction occurrence frames
 Activity or operation to invoke
(ref)
42

You might also like