Bab 10 state diagram 2010

40
BAB X STATE DIAGRAM

description

 

Transcript of Bab 10 state diagram 2010

Page 1: Bab 10 state diagram 2010

BAB XSTATE DIAGRAM

Page 2: Bab 10 state diagram 2010

StatechartStatechart DiagramDiagram

-- BersifatBersifat dinamisdinamis

-- MemperlihatkanMemperlihatkan statestate--state state padapada sistemsistem; ; memuatmemuat state, state, transisitransisi, event, , event, sertasertaaktivitasaktivitas..

-- MemperlihatkanMemperlihatkan sifatsifat dinamisdinamis daridariantarmukaantarmuka, , kelaskelas, , kolaborasikolaborasi dandan terutamaterutamapentingpenting padapada pemodelanpemodelan sistemsistem--sistemsistemyang yang reaktifreaktif

Page 3: Bab 10 state diagram 2010

• Setiap objek dapat dikatakan memiliki state ---nilai dari attributnya pada suatu saat tertentu.

• State diagram memodelkan daur hidup sebuahobjek tunggal. Ia menggambarkan macam-macam state yang dimiliki sebuah objek, eventyang mengakibatkan perubahan state selamawaktu tertentu dan aturan-aturan yang mengaturtransisi objek antara satu state dengan state lainnya.

• Perubahan terhadap state objek terjadi ketikaada suatu kejadian atau ketika nilai dari salahsatu atributnya berubah. Perubahan state inidipicu oleh adanya suatu event.

SYSTEMS ANALYSYS AND DESIGN METHODS 5TH Edition Whitten Bentley Dittman

Irwin/McGraw-Hill Copyrighth@2000 The Mcgraw-Hill Companies All Right reserved

Membuat model behavior dari objek-objek

Page 4: Bab 10 state diagram 2010

• Sebuah state diagram menunjukkan urut-urutan state dari sebuah objek selama masa hidupnya / lifetimenya, sekaligus dengan event-event yang menyebabkanperubahan dari state tersebut.

• Sebuah state diagram menjelaskan sebuah hypothetical machine (finite automaton) yang pada setiap waktuberada pada satu set finite state, yang terdiri dari:

• a finite, non-empty set of states;• a finite, non-empty set of events;• functions, yg menjelaskan transisi dari satu state ke

state berikutnya;• sebuah initial state;• satu set final state.

Developing Software Woth UML Booch Jacobson Rumbaugh

Addison-Wesley

Page 5: Bab 10 state diagram 2010

• On opening the flight, the initial state leads to the NoReservationstate. When the state is entered, the Reset operation is executed. If a reservation is made for this flight, the object changes to the state PartiallyReserved.

• The Reserve event is associated to the homonymous Reserveaction (implemented as an operation).

• In this operation, the actual reservation takes place, and the internal reservation counter is updated. After termination of this action, we will find the object in the PartiallyReserved state.

• Each additional reservation leads to the same action. As long as a free seats are available, the object remains in the PartiallyReservedstate. If only one seat left, it changes into FullyBooked state. Cancellation of reserved seats is carried out in a similar way. Thus, the state diagram describes which actions are triggered by whichevents and under which conditions these (and together with the call of the corresponding operations) are permitted.

Soal:

Developing Software Woth UML Booch Jacobson Rumbaugh

Addison-Wesley

Page 6: Bab 10 state diagram 2010

PartiallyReserved

FullyBooked

Closed

entry/reset()

NoReservation

cancelFlight()openFlight()

reserve()

cancel()[ReservedSeats=1]

cancel()[reservedSeats>1]

reserve()[freeSeats>1]

cancel()

reserve()[freeSeats=1]

close()

close()

Developing Software Woth UML Booch Jacobson Rumbaugh

Addison-Wesley

Page 7: Bab 10 state diagram 2010

• Sebuah state dimiliki oleh satu buah Class danmerepresentasikan abstraksi atau kombinasi darisatu set nilai atribut yang mungkin terjadi padaobjek tersebut dari class ini.

• Tidak setiap perubahan nilai atribut akan dianggapsebagai perubahan state. Hanya event tertentuyang sangat signifikan mengakibatkan perubahanbehavior dari objek.

• Sebuah state oleh karena itu dapat dilihat jugasebagai rentang waktu (time span) antara duaevent.

• Dua type khusus state yang ada adalah initial state dan final state.

State

Developing Software Woth UML Booch Jacobson Rumbaugh

Addison-Wesley

Page 8: Bab 10 state diagram 2010

• Tidak ada transisi yang menuju initial state dan tidak adaevent yang diperbolehkan meninggalkan final state.

• Transisi dari satu state ke state berikutnya di trigger / dipicu oleh event. Sebuah event terdiri dari nama event dan sejumlah argument.

• Event dapat memicu aksi didalam state yang dilakukanmelalui operasi tertentu.

Ada tiga trigger yang sudah predefine:• entry, fires automatically when entering a state• exit, fires automatically when leaving a state• do, fires repeatedly as long as the state is active, that is,

not left.

Developing Software Woth UML Booch Jacobson Rumbaugh

Addison-Wesley

Page 9: Bab 10 state diagram 2010

• Sebuah state dapat diuraikan lebih lanjut, baiksekuensial ataupun paralel substate.

• Gambar dibawah ini menunjukkan sebuah sequential nesting. Ketika kontrak asuransi ditetapkan, sebuahproduk dipilih (sebagai contoh, content-nya), yang terdiridari beberapa produk element (Pel), seperti furniture, gelas, dsb. Untuk setiap produk elemen, sebuah cover (istilah asuransi) harus dibuat. Ilustrasi dibawah inimenunjukkan Create covers state dari context ini.

Substate

PElQuantityReset

Create Coverscovers:set=Empty set

DefinecoveraddPEl

addPEl

[isComplete]

Developing Software Woth UML Booch Jacobson Rumbaugh

Addison-Wesley

Page 10: Bab 10 state diagram 2010

S1 S2

substates

event1

event2 event3

event4

S4

S3

S5

Gambar dibawah ini menunjukkan notasidari paralel substates.

Developing Software Woth UML Booch Jacobson Rumbaugh

Addison-Wesley

Page 11: Bab 10 state diagram 2010

• Sebuah event adalah sebuah kejadian yang memiliki pengaruh khusus dalam suatu context yang dibicarakan, karena ia men-trigger / memicuperubahan state.

• Kebalikan dari state, event bukan milik satu kelastertentu.

• State transition (perubahan state) biasanya dipicuoleh adanya event yang digambarkan dengan anakpanah yang menghubungkan state satu denganstate lainnya.

Event dan transition

Developing Software Woth UML Booch Jacobson Rumbaugh

Addison-Wesley

Page 12: Bab 10 state diagram 2010

Copyright © 1997 by Rational Software Corporation

State Transition Diagram

InitializationOpen

entry: Register studentexit: Increment count

Closed

Canceled

do: Initialize course

do: Finalize course

do: Notify registered students

Add Student / Set count = 0

Add student[ count < 10 ]

[ count = 10 ]

Cancel

Cancel

Cancel

Page 13: Bab 10 state diagram 2010

Transitionsxx yy

event [guard] | action

The event that triggers the transition

Conditions that must be met for the transition to take place

The action that takes place when the transition is taken

Page 14: Bab 10 state diagram 2010

Transitionsxx yy

event [guard] | action

The event that triggers the transition

Conditions that must be met for the transition to take place

The action that takes place when the transition is taken

Page 15: Bab 10 state diagram 2010

Events • General Events

– Method calls: received(customer)– Event signals: inventoryReady

• Time event– interval expiry;

tm(3 days), tm(1000 ms)– calendar/clock time; when(11:50)

• Change Event:– Change in value of some entity;

false(hasProblems)true(hasProblems)

createdcreated

sentsent

in gatheringin gathering

is received

inventoryReady

tm(3 days)

false(hasProblems)

received()

in problem

Page 16: Bab 10 state diagram 2010

Guards (Conditions)

• Boolean expressions.• Evaluated when the transition is triggered• Types of guards:

– Simple predicate: [hasProblems], [x > 0]– Combined predicates:

[¬hasProblems ∨ (hasProblems ∧ order.sum < 100]– Guards on activities: [active(gatherItems)]– State related (we’ll get back to it later)

sentsent

in gatheringin gathering

gatheringFinished [all items were gathered]

in problemin problemgatheringFinished [items are not found]

Page 17: Bab 10 state diagram 2010

Guards - Example

SellingSelling

UnhappyUnhappy

bid [(value >= 100) AND (value < 200)]

HappyHappybid [value >= 200]

bidbid [value < 100]

Intro | Building Blocks | Advanced

Page 18: Bab 10 state diagram 2010

Static Conditional Branching

• A graphical shortcut for convenient rendering of decision trees

[(value >= 100) & (value < 200)]

[value >= 200]

[value < 100]

SellingSellingSelling

UnhappyUnhappyUnhappy

HappyHappyHappy

bid

Page 19: Bab 10 state diagram 2010

Empty Transitions

• A transition can have any combination (including none) of the events, guards and actions

• When a transition does not have an event, it is taken after all the activities were ended

Cleaningdo: put waterdo: put soap

do: wash soapdo: drain

doing nothing

Employee

rest[break]

work

Workingdo: shovel

Empty Transition

work

Page 20: Bab 10 state diagram 2010

Guards and Events

• What’s the difference between the two machines?

S1 S2E1

S3true(C)

S1 S2E1

S3[C]

What happens if C changes to True before E1?

Page 21: Bab 10 state diagram 2010

Actions• An executable atomic computation• Types of actions

– Variable assignment:

– Throwing a signal:

– Start, or stop activities (and concatenation of actions):

sentsentsent is receivedis receivedreceived() | status := received

sentsentsent is receivedis receivedreceived() | throw(InventoryUpdate)

sentsentsent is receivedis received| start(sendBill); stop(delivery); x := x+1

Page 22: Bab 10 state diagram 2010

Transitions - advanced• Self-transitions: Transitions can be directed to the same

state:

• Un-deterministic states – when two transitions are taken in the same time, one of will be taken in an un-deterministic fashion:

S2 E1 / c:=c+1

/ c=0

S1 S2E1

S3

[C1]

Page 23: Bab 10 state diagram 2010

How does a Washing Machine Works?• On / Off button. Start button• (No stop button.)• Feedback is given on the current stage

(soaking, rinsing, draining, drying)• Three plans:

– Regular– Delicate (no soaking)– Super delicate (no soaking, no drying)

• Off can be clicked only before starting, or after finishing

Page 24: Bab 10 state diagram 2010

Washing Machineoff

idle

Do: light(soak)

Do: pump(in)

soak

Do: light(rinse)

Do: stir()

rinse

Do: light(drain)

Do: pump(out)

drain

Do: light(dry)

Do: stir()

dry

click(power) click(power)

click(start) [plan=regular]

click(start) [plan=delicate

or super delicate]

tm(30 mins)

tm(30 mins)

Tm(5 mins) [plan=super delicate] | light(off)

tm(5 mins)

[plan=not super

delicate]

Tm(10 mins) | light(off)

Page 25: Bab 10 state diagram 2010

State Explosion: An Example

What is the offbutton can be clicked at any time?

What if we want to show how many minutes left to the end of the cycle?

What if we want to come back to the same state we left?

Page 26: Bab 10 state diagram 2010

Abstraction in Statechart

Finding Common Behavior

Separating Independent Regions

Composite States Parallel States

Page 27: Bab 10 state diagram 2010

The State Explosion Problem

• Let– n: Num of Classes– m: Num of variables (assume equals among classes)

• Number of possible states = 2(nm)

• And...– What if the state space of each variable > 2– What about association between objects?

Class

v1 : {t, f}v2 : {t, f}v3 : {t, f}...

Class

v1 : {t, f}v2 : {t, f}v3 : {t, f}...

Class

v1 : {t, f}v2 : {t, f}v3 : {t, f}......

Page 28: Bab 10 state diagram 2010

Composite States

Page 29: Bab 10 state diagram 2010

Composite + History

On

off

idle

Do: light(soak)

Do: pump(in)

soak

Do: light(rinse)

Do: stir()

rinse

Do: light(drain)

Do: pump(out)

drain

Do: light(dry)

Do: stir()

dry

click(start) [plan=regular]

click(start) [plan=delicate

or super delicate]

tm(30 mins)

tm(30 mins)

Tm(5 mins) [plan=super delicate] | light(off)

tm(5 mins)

[plan=not super

delicate]

Tm(10 mins) | light(off)

click(power)

click(power)

H

Page 30: Bab 10 state diagram 2010

deep and shallow

DiagnosingDiagnosingDiagnosing

Diagnostic1Diagnostic1

setupsetupsetup

Check blood pressure

Check blood Check blood pressurepressure

Diagnostic2Diagnostic2

Find a veinFind a veinFind a vein

Draw BloodDraw BloodDraw Blood

H*H*

HH

StopStopStoppause

return

return-full

Shift ended | clh()

clh() – clears the history

Page 31: Bab 10 state diagram 2010

Completion Transitions• Triggered by a completion event

– generated automatically when an immediately nested state machine terminates

CommittingCommittingCommitting

Phase1Phase1Phase1

Phase2Phase2Phase2Commit DoneCommit DoneCommit Done

completion transition (no trigger)

Page 32: Bab 10 state diagram 2010

LampFlashingLampFlashing

off/off/

FlashOffFlashOff

FlashOnFlashOn

Triggering Rules

• Two or more transitions may have the same event trigger– inner transition takes precedence– if no transition is triggered, event is discarded

on/on/

on/on/

Page 33: Bab 10 state diagram 2010

Order of Actions: Complex Case

• Same approach as for the simple caseS1exit:exS1

S1S1exit:exS1exit:exS1

S11exit:exS11

S11S11exit:exS11exit:exS11

S2entry:enS2

S2S2entry:enS2entry:enS2

S21entry:enS21

S21S21entry:enS21entry:enS21

| initS2| initS2E/E/actEactE

Actions execution sequence:

exS11� exS1 � actE�enS2 � initS2 � enS21

Page 34: Bab 10 state diagram 2010

Parallel States• Sometimes, some aspect of an entity are independent of other

aspects

single

married

divorced

Martial

StaffMember

Manager

employee

Page 35: Bab 10 state diagram 2010

Parallel States Annotation

• Combine multiple simultaneous descriptions

StaffStaffMemberMember

ManagerManager

employeeemployee

StaffStaffMemberMember

ManagerManager

employeeemployee

singlesingle

marriedmarried

divorceddivorced

MartialMartial

singlesingle

marriedmarried

divorceddivorced

Martial Martial

Intro | Building Blocks | Advanced

Page 36: Bab 10 state diagram 2010

Interactions Between Parallel States

On

idle

soak

rinse

drain

idle

Do: pump in

water

in

Regular

Delicate

click(start)

[Plan in Regular]

Entered(soak)

Tm(30)

[Plan not in Super Delicate]

Tm(30)

click(plan)

Pump PlanController

Super

Delicate

click(plan)

click(plan)

click(start)

[Plan not in Regular]

Tm(30)

Tm(30)

[Plan in

Super Delicate]

idle

Rotor

Do: spin(150rpm)

Spin Slow

Do: pump out

water

out

Entered(rinse)

Entered(drain)

Do: spin(400rpm)

Spin Fast

Exited(drain)

Exited(rinse)Tm(entered(rinse),15)

Entered(rinse)

[Plan not in Regular]

off

click(power)

click(power)

H

Page 37: Bab 10 state diagram 2010

“Flat” and Parallel Machines

Use of Orthogonal

Regions

No Use of Orthogonal

Regions

B

C

E

G

F

A D

αααα ββββ γγγγ δδδδ

µµµµ

(in G) αααα

C,F

C,E C,G

B,GB,E

B,F

αααα ββββ

γγγγ

γγγγ

δδδδ

δδδδ

µµµµ

µµµµ

αααααααα

αααα

• Every parallel machine can be transformed into a sequential machine:

Page 38: Bab 10 state diagram 2010

Transition Forks and Joins

• For transitions into/out of orthogonal regions:

Credit Card verification

Credit Card verification

chargingcharging

ProcessingProcessing SentSent ConfirmedConfirmed

shippingshipping

Receipt SentReceipt Sent

In problemIn problem

[ok]

[not ok] Joinfork

Page 39: Bab 10 state diagram 2010
Page 40: Bab 10 state diagram 2010