440 likes | 553 Vues
Dynamic Modelling (1). Prof. T.H. Tse Department of Computer Science Email: thtse@cs.hku.hk Web: http://hku.hk/thtse. Dynamic Model. Specify the behaviour of objects by describing the sequentiality control of their operations Disregard what the operations do how they are implemented
E N D
Dynamic Modelling (1) Prof. T.H. Tse Department of Computer Science Email:thtse@cs.hku.hk Web:http://hku.hk/thtse
Dynamic Model • Specify the behaviour of objects by describing the sequentiality control of their operations • Disregard • what the operations do • how they are implemented • Represented graphically using • sequence diagrams • state machines Can also be regarded as a use case model but never as a program Can also be regarded as a design model.
Event • An event occurs whenever information is exchanged between an object and an outside agent, or between two objects • Examples: Caller hangs up • Phones are disconnected • The information values exchanged are the parameters of the event • An event happens at a point in time and has noduration.
ExamplesEvents with Classes and Attributes • airplane flight departs (airline, flight number, city) • mouse button pushed (button, location) • input string entered (text) • phone receiver lifted • digit dialed (digit) • engine speed enters danger zone.
caller lifts receiver dial tone begins caller dials digit (2) dial tone ends caller dials digit (8) caller dials digit (5) caller dials digit (9) caller dials digit (2) caller dials digit (1) caller dials digit (8) caller dials digit (0) … called phone begins ringing ringing tone appears in calling phone called party answers called phone stops ringing ringing tone disappears in calling phone phones are connected called party hangs up phones are disconnected caller hangs up. ExampleScenario for Phone Call
Sequence Diagram • A sequence diagram is a graphical representation of related scenarios • It shows: • objects as vertical dotted lines • events as horizontal arrows from the sender object to the receiver object.
Sequence DiagramMain Syntax sd borrowBook :Book r1:Reader r2:Reader Time dateDue reminder time return available [mayBorrow] borrow Frame Object Life line Message Execution occurrence Condition .
Asynchronous messsage • Synchronous messsage (method calls) • Reply message • Object creation message • Lost message • Found message Message Types
Synchronous Message doYouUnderstand ( ) :Professor :Student
Synchronous Message Combined with execution occurrence :Professor :Student Execution occurrence Caller blocked. doYouUnderstand( ) yes • Typically implemented as operation call • The routine that handles the message is completed before the caller resumes execution
Return Value • Optionally indicated using a dashed arrow with a label indicating the return value • Don’t model a return value when it is obvious what is being returned, e.g., getTotal( ) • Model a return value only when you need to refer to it elsewhere, e.g., as a parameter passed in another message.
Asynchronous Message • Caller does not wait for the message to be handled before it continues to execute.
Example 1 Synchronous Message and Synchronous Call • Word Processor object sends a synchronous message to Printer object asking it to print a file • Word Processor object does not do anything until it receives a (software) acknowlegement from Printer object saying that the printing is complete • This is a synchronous call.
:Word Processor :Printer print printComplete Caller blocked Example 1 Synchronous Message and Synchronous Call
Example 2 Asynchronous Message • Word Processor object sends an asynchronous message to Printer object asking it to print a file • Printer object can print the file at any time when it is free • Word Processor object can do anything after it has sent off the asynchronous message.
:Word Processor :Printer print callHelp Example 2 Asynchronous Message No execution occurrence
:Order Object Creation Message • An object may create another object via an object creation message :Customer Constructor .
:Professor :Student doYouUnderstand( ) no Lost Message
:Professor :Student doYouUnderstand( ) no Found Message
DB is queried and the result is returned as an object : ViolationsDialogue : ViolationsController : ViolationsDBProxy Clerk lookup viewButton() id=getID() getViolation(id) v: TrafficViolation v display(v) More (Syntax) ExampleLookup Traffic Violation
[amount < balance]debit(amount) Condition • A condition is a Boolean function relating attributes and/or values • Example: • Conditions are used as guards on transitions • A guarded transition fires whenits event occurs and satisfies the guard condition.
:Mouse :Mouse Click 1 s Highlight Open {0..1 s} Click Click Duration Constraint • Example 1: Single click • Example 2: Double click
Example 1: Single click :Mouse :Mouse Click t = now Highlight Open t + 1 s • Example 2: Double click t = now Click Click t + {0..1 s} Time Constraint
Control InformationCombinedFragment • CombinedFragments are used to describe control information in sequence diagrams • They are only for modeling simple combinations of scenarios.
Control InformationCombinedFragments • Alternatives (alt) • Option (opt) • Parallel (par) • Critical Region (critical) • Iteration (loop) • Continuation.
CombinedFragmentAlternatives (alt) • Represents a choice of behavior • At most one of the operands will be chosen • The chosen operand have an explicit or implicitguard expression that evaluates to true • An operand guarded by elsedesignates a guard that is the negation of the disjunction of all other guards in the enclosing CombinedFragment ...
CombinedFragmentAlternatives (alt) • If none of the operands has a guard that evaluates to true, none of the operands are executed and the remainder of the enclosing InteractionFragment is executed.
Alt Open {0..1 s} Click Click AlternativesSingle and Double Clicks No explicit guard condition in this case. :Mouse Click 1 s Highlight
CombinedFragmentOption (opt) • Designates that the CombinedFragment represents a choice of behavior where either the (sole) operand happens or nothing happens • An option is semantically equivalent to an alternative CombinedFragment where there is one operand with non-empty content and the second operand is empty.
CombinedFragmentOption (opt) No explicit guard condition in this case. 31
CombinedFragmentParallel (par) • Designates that the CombinedFragment represents a parallel merge between the behaviors of the operands • The EventOccurrences of the different operands can be interleaved in any way as long as the ordering imposed by each operand as such is preserved.
CombinedFragmentCritical Region (critical) • The sequence of events in a critical region cannot be interleaved by events outside the region • Even though CombinedFragments may imply that some events may interleave, such as with the par-operator, this is prevented by defining a critical region.
CombinedFragmentCritical Region (critical) • A 911-call must be contiguously handled • The operator must make sure to forward the 911-call before doing anything else. • Normal calls can be freely interleaved
loop loop (3, 5) loop (3) loop (3, *) CombinedFragmentIteration (loop) [condition] Minimum of 3 iterations The same, but easier to understand
CombinedFragmentContinuation • Equivalent to:
CombinedFragmentWarning • Remember:
CombinedFragmentWarning • The modelling of most real-life applications are usually much simpler than the examples that I have shown • Consider drawing several diagrams for modeling complex combinations • Do not use sequence diagrams for detailed modelling of algorithms • Better done using state machines, activity diagrams or pseudo-code.
InteractionUse (ref ) An InteractionUse refers to an Interaction The InteractionUse is a shorthand for copying the contents of the referred Interaction where the InteractionUse is To be accurate the copying must take into account substituting parameters with arguments and connect the formal gates with the actual ones. 41
InteractionUse (ref ) An InteractionUsereferring to an Interaction :EstablishAccesswith an (input) argument “Illegal PIN” An InteractionUsereferring to an Interaction :EstablishAccesswith an (input) argument “Illegal PIN” An InteractionUse without arguments referring to an Interaction :OpenDoor. 42
InteractionUse (ref ) Interaction that models a behavior returning a value of Verdict Interaction that models a behavior returning a value of Verdict An InteractionUse refers to a_util_bwith value return to the attribute xcof :xxwith the value 9, and with in/out parameter where the argument is w with returning out-value 12 An InteractionUse refers to a_util_bwith value return to the attribute xcof :xxwith the value 9, and with in/out parameter where the argument is w with returning out-value 12 The return value from the Interaction is shown as a separate Lifeline a_op_b. 43
InteractionUseWarning • Do you find this example difficult? • Your users will find it even more so • Avoid drawing complex sequence diagrams all together • Avoid combining many scenarios together.