02291: System Integration
Week 7 Hubert Baumeister
huba@dtu.dk
DTU Compute Technical University of Denmark
Spring 2018
Recap
I Last Week
I Life cycle state machines (LSM’s): Describes the life cycle of an object
I Protocol state machines (PSM’s): Describes the
communication protocol for provided/required interface of a component
I Today:
I Sequence diagrams
I Testing your model: Use case realizations
Contents
Components and Synchronous Communication Sequence Diagrams
Design Validation: Use Case Realization
Synchronous– vs Asynchronous Calls:
Asynchronous Call
public class Atm implements AtmToBank { // From the user interface
public void enterPin(String pin) {
new Thread(() -> {bank.verifyPin(pin)}).start();
}// From the bank
public void pinOk() { ... } public void pinNotOk() { ... } }
Synchronous Call
public class Atm {
// From the user interface
public void enterPin(String pin) { boolean pinOk = bank.verifyPin(pin); } }
Synchronous– vs Asynchronous Calls:
Asynchronous Call
public class Atm implements AtmToBank { // From the user interface
public void enterPin(String pin) {
new Thread(() -> {bank.verifyPin(pin)}).start();
}// From the bank
public void pinOk() { ... } public void pinNotOk() { ... } }
Synchronous Call
public class Atm {
// From the user interface
public void enterPin(String pin) { boolean pinOk = bank.verifyPin(pin);
} }
Synchronous version of the Bank
Closer View
PortBank to ATM (BA):
Provided interface I PortATM to Bank(AB):
Required interface
Protocol for interfaces BankToAtm and BankToATMSync
I Asynchronous version of the protocol state machine
withdrawing withdraw
verifying idle
sm: BankToAtm {protocol}
/ [ ^ p i n N o t O k ]
/ [ ^ w i t h d r a w O k ] /[^withdrawNoOk]
withdraw(i,a) / [ ^ p i n O k ]
verifyPin(p)
I Synchronous version of the protocol state machine
BankToATMSync. {protocol}
Idle Withdraw
withdraw(i,a)/[result = false]
withdraw(i,a)/[result = true]
verifyPin(p)/[result = true]
verifyPin(p)/[result = false]
Bank component with Implementation
Bank
Bank
Customer Account
ClearingCompanyToBank * BankToATM
«delegate»
«delegate»
BankToATMPort
*
Detailed Class Diagram for the Bank Component
Bank name: String ...
accountFor(acct): Account verifyPin(acct,pin) withdraw(acct,amount)
«interface»
ClearingCompanyToBank verifyPIN(acct,pin): bool
Customer n a m e address ...
Account number : IBAN balance : int
withdraw(amount:int): bool ...
BankToATMPort verifyPIN(acct,pin): bool withdraw(acct,amt): bool
1 cc *
1..*
1..*
c *
*
«interface»
BankToATMSync verifyPIN(acct,pin): bool
withdraw(acct,amt): bool *
1 b
Behaviour Design BankToATMPort
I Behaviour of class BanktToATMPort
I Behaviour of class Bank
Protocol conformance
Protocol for the interface BankToATMSync
BankToATMSync. {protocol}
Idle Withdraw
withdraw(i,a)/[result = false]
withdraw(i,a)/[result = true]
verifyPin(p)/[result = true]
verifyPin(p)/[result = false]
BankToATMPort lifecycle state machine
Summary: Components
I Ports in components have
I provided and required interfaces
I Each interface:Protocol state machine
I Implementing Components
1) Subcomponents delegating ports 2) Set of classes
I Classes implement provided interface of a port
I Classes use the required interface of a port
I Lifecycle state machine and protocol state machine have to conform to each other
Contents
Components and Synchronous Communication Sequence Diagrams
Design Validation: Use Case Realization
Example: Interaction Diagrams
Sequence Diagram
Communication Diagram
Example Sequence Diagram
Creation and deletion of participants
Synchronous– vs Asynchronous Calls:
Synchronous
b.m(4);
c.n(...) // Starts after m has returned
Synchronous calls
m(4)
n(...)
a:A b:B c:C
Asynchronous
// (new Thread(){ public void run() {b.m(4);}}).start();
new Thread(() -> {b.m(4);}).start(); // Using Lambdas from Java 8 c.n(...) // Starts immediately after m has been called
Synchronous– vs Asynchronous Calls:
Synchronous
b.m(4);
c.n(...) // Starts after m has returned
Synchronous calls
m(4)
n(...)
a:A b:B c:C
Asynchronous
// (new Thread(){ public void run() {b.m(4);}}).start();
new Thread(() -> {b.m(4);}).start(); // Using Lambdas from Java 8 c.n(...) // Starts immediately after m has been called
Asynchronous calls
m(4) n(...)
a:A b:B c:C
ATM to Bank: Asynchronous Version
ATM to Bank: Synchronous Version
Interaction Frames Example
Realising an algorithm using a sequence diagram public void dispatch() {
for (LineItem lineItem : lineItems) { if (lineItem.getValue() > 10000) {
careful.dispatch();
} else {
regular.dispatch();
} }
if (needsConfirmation()) { messenger.confirm();
} }
Realisation with Interaction Frames
Interaction Frame Operators I
Nested sequence diagrams
Usages of sequence diagrams
I Abstract: show the execution (i.e. exchange of messages) of a system
I Concrete
I Design (c.f. CRC cards)
I Visualize program behaviour
I Visualize model execution!use case realization
System design: Detailed Use Case Check out
I Use Case Name: Check out
I Summary: Checks out a book from the library
I Actors: User
I Preconditions: true
I Basic course of events
I 1. User scans his library card
I 2. User repeats
I 2.1 select check out
I 2.2 scan the book
I 2.3 System confirms loan I Alternative paths
. . .
I Postconditions
I Book is loaned by the user
Use Case scenarios as Sequence Diagrams
Main scenario
sd: borrow book success
scanLibraryCard(bor) true
checkOut()
scanBook(b) true loop
User
Library
System design for the main use case scenario
sd: borrow book success
scan library card(bor)
true
checkOut()
scan book(b)
canBorrow()
isOverdue() false loop
true
checkout(bor) true true
loop User
Library bor:Borrower
[b in bor.books]
b1:Book b2:Book
Contents
Components and Synchronous Communication Sequence Diagrams
Design Validation: Use Case Realization
UML Models
I Many diagrams, but onlyone model
I Different Views on the system
I Functionality: Use Case diagram, state machines, activity diagram, . . .
I Structure: Component diagram, Class diagram
I Validation: Interaction diagram
Dependence between models: Consistency conditions
1) Components are implemented by class diagrams
I Provided interfaces in ports have to be implemented by classes
I Object life cycle behaviour conforms to the the protocol statemachines
2) Use case scenarios can be realized by the system
! use sequence diagrams to show this
Use Case Diagram Library System
Detail Use Case Check out
I Use Case Name: Check out
I Summary: Checks out a book from the library
I Actors: User
I Preconditions: true
I Basic course of events
I 1. User scans his library card
I 2. User repeats
I 2.1 select check out
I 2.2 scan the book
I 2.3 System confirms loan I Alternative paths
I 3.a. User has overdue books
I System rejects loan with message ’overdue books’
I 3.b User has more then 5 books on loan
I System rejects loan with message ’too many books’
I 1.a User is not registered with the system
I . . .
I Postconditions
I Book is loaned by the user
Implementation: Component Diagram (synchronous version)
< < i n t e r f a c e > >
LibraryInterface scanLibraryCard() checkOut scanBook() checkIn ...
LibraryInterface
LibrarySystem
Implementation: Protocol State Machine (PSM):
Library Interface (synchronous version)
More functionality
...
/[result = false]
/[result = true]
check in scanBook/
[result = (false,'too many books') or result = (false,'overdue books') or
result = true]
check out scanLibraryCard
Implementation: Class Diagram
{pre: bor.canBorrow()
post: dueDate = Date.today + 3 weeks and bor.books->containing(self) }
{body: books->size <= 5 and books->forAll(b | not(b.overdue))}
< < i n t e r f a c e > >
LibraryInterface scanLibraryCard() checkOut scanBook() checkIn ...
{inv: overdue iff dueDate <> null and today > dueDate}
Book overdue dueDate register() deregister() checkout() checkin()
Borrower canBorrow()
Library scan library card() check out scan book() check in ...
* 0..5 user
*
Implementation: Object Life Cycle State Machine (LSM): Class Library
user ok
can borrow?
...
...
book scanned User scanned
Idle
[not cb] / return "book can't be borrowed"
scanBook(l)
[cb] / b.checkOut(); return "ok"
/ cb := bor.canBorrow() [users->contains(l)]
[not users->contains(l)] / return err-msg scanLibraryCard (l)
Implementation: LSM Class Book
registered
not registered
overdue borrowed available
deregister register
check in
check in
when Today > dueDate checkOut(b) / dueDate := today + 3 weeks; b.books := b.books->containing(tself)
Use Case success scenario realisation
sd: borrow book success
scan library card(bor)
true
checkOut()
scan book(b)
canBorrow()
isOverdue() false loop
true
checkout(bor) true true
loop User
Library bor:Borrower
[b in bor.books]
b1:Book b2:Book
Use Case fail realisation
sd: borrow book failure: overdue books
scanLibraryCard(bor) true
checkOut()
scanBook(b)
canBorrow()
isOverdue() true loop
false false
loop User
Library bor:Borrower
[b in bor.books]
b1:Book b2:Book
Checking that a use case realization is correct
1 The sequence diagram shows correctly the user interaction of the use case scenario
2 All messages appear in the respective interfaces of the class
3 All messages are admissiable according to the protocol state machines (PSM)
4 All message sequences are admissible according to the behaviour specification
1) User interactions
I Basic course of events
I 1. User scans his library card
I 2. User repeats
I 2.1 select check out
I 2.2 scan the book
I 2.3 System confirms loan
sd: borrow book success
scan library card(bor)
true
checkOut()
scan book(b)
canBorrow()
isOverdue() false loop
true
checkout(bor) true true
loop User
Library bor:Borrower
[b in bor.books]
b1:Book b2:Book
2) Messages belong to interfaces
{pre: bor.canBorrow() post: dueDate = Date.today + 3 weeks and bor.books->containing(self) }
{body: books->size <= 5 and books->forAll(b | not(b.overdue))}
< < i n t e r f a c e > >
LibraryInterface scanLibraryCard() checkOut scanBook() checkIn ...
{inv: overdue iff dueDate <> null and today > dueDate}
Book overdue dueDate register() deregister() checkout() checkin()
Borrower canBorrow()
Library scan library card() check out scan book() check in ...
* 0..5 user
*
sd: borrow book success
scan library card(bor)
true
checkOut()
scan book(b) canBorrow()
isOverdue() false loop
true checkout(bor)
true true
loop User
Library bor:Borrower
[b in bor.books]
b1:Book b2:Book
3) Messages conform to the PSM (LibraryInterface)
Class Library implements the LibraryInterface
More functionality ...
/[result = false]
/[result = true]
check in scanBook/
[result = (false,'too many books') or result = (false,'overdue books') or
result = true]
check out scanLibraryCard
sd: borrow book success
scan library card(bor)
true
checkOut()
scan book(b) canBorrow()
isOverdue() false loop
true checkout(bor)
true true
loop User
Library bor:Borrower
[b in bor.books]
b1:Book b2:Book
4a) Messages follow the LSM for class Library
user ok
can borrow?
...
...
book scanned User scanned
Idle
[not cb] / return "book can't be borrowed"
scanBook(l)
[cb] / b.checkOut(); return "ok"
/ cb := bor.canBorrow() [users->contains(l)]
[not users->contains(l)] / return err-msg scanLibraryCard (l)
sd: borrow book success
scan library card(bor)
true
checkOut()
scan book(b) canBorrow()
isOverdue() false loop
true checkout(bor)
true true
loop User
Library bor:Borrower
[b in bor.books]
b1:Book b2:Book
4b) Messages follow the LSM for class Book
registered
not registered
overdue borrowed available
deregister register
check in
check in
when Today > dueDate checkOut(b) / dueDate := today + 3 weeks; b.books := b.books->containing(tself)
sd: borrow book success
scan library card(bor)
true
checkOut()
scan book(b) canBorrow()
isOverdue() false loop
true checkout(bor)
true true
loop User
Library bor:Borrower
[b in bor.books]
b1:Book b2:Book
Alternative: Construct the sequence diagram from the LSM’s