Object Oriented Development with UML State and Activity
Object Oriented Development with UML State and Activity Diagrams By Assoc. Prof. Rangsit Sirirangsi
Event-Transition Notation เงอนไขกอนท ชอ ของ event ททำใหเกด State 1 do/activity 1 transition จะเกดขน transition การทำงานของ action เมอเกด event [condition] /action event State 2. . . event name guard } Event(arguments) [condition] /operation(arguments) trigger action
Elevator : State Diagram 1 ustate diagram สำหรบลฟท (elevator) go up Moving Up arrived go down Idle Moving Down arrived -time out go up On First Floor arrived Moving to First Floor u����������� u����������� time-out event
Elevator : State Diagram 3 u����� state diagram ����� event/actions go up( floor( Moving Up arrived do: moving to floor Idle entry: timer = 0 do: increase timer out[ ]timer =time - go up( floor( On First Floor go down( floor( arrived Moving to First Floor Moving Down arrived do: moving to floor
State Diagram : Order [ not all items checked ] / get next item / get first item Checking do: check item Item received[ some items not in stock ] [ All items checked && some items not in stock ] Waiting Item received[ all items available ] [ All items checked && all items available ] cancelled Dispatching do: initiate delivery Delivered cancelled Cancelled
Composite State. Chart Diagrams u. Nested statechart diagram เปนไดอาแกรมทมมากกวา 1 state region ออปเจคจะมเพยง 1 state ใน 1 ของ state diagrams ใชสำหรบ State ทเปนเงอนไขแบบ or-condition บนทก ๆ statechart diagrams ใช abstract/generalize states ในรปของ state เดยว u. Concurrent statechart diagram ออปเจคจะมเพยง 1 state ใน 1 ของ statechart diagrams ใชสำหรบ state ทเปนเงอนไขแบบ and-condition บนทก ๆ statechart diagrams ใชสำหรบแสดงการทำงานแบบ multi-threading
Superstate / Substates Active [ not all items checked ] / get next item / get first item Item received[ some items not in stock ] [ All items checked && some items not in stock ] Checking Waiting do: check item [ All items checked && all items available ] Dispatching do: initiate delivery Delivered Item received[ all items available ] Delivered cancelled Cancelled
Concurrency Among State. Chart Diagrams guard references Transmission statechart diagram Ignition Off [Transmission in Neutral] turn key to start Starting release key On turn key off Transmission select R Neutral Reverse select N select F select N Forward stop First upshift downshift Second upshift downshift Third
Class definition package Traveling; public class Car { private Location current. Location; private java. sql. Date built. Date; } public class Location { private java. lang. String city; }
Adding methods public class Car { private Location current. Location; private java. util. Calendar built. Date; public void start ( ) { return; } public void stop ( ) { return; } public void goto. Location (Location new. Loc ) { current. Location = new. Loc; } Missing: accessing methods }
State transitions public class Car { private Location current. Location; private java. util. Calendar built. Date; private String state; private boolean gas. Available; start[ gas available ] stopped started stop { public void start ( ) { if (gas. Available) {state = "started“; } return; } public void stop ( ) { state = "stopped"; return; } public void goto. Location (Location new. Loc ) current. Location = new. Loc; } }
The example we will use u. Class Course. Offering กำหนดคาเรมตน students. No = 0 จำนวนสงสด student. No = 10 จำนวนตำสด =3 เกดการ cancelled เมอ จำนวนนกศกษาตำกวา min มการ closed เมอนกศกษา = max u. Events initialize add student remove student
Identify start State and transitions uกำหนดจดเรมตนของสถานะ uแตละ state กำหนดวา events ใดทกอใหเกด transitions ไปยง states ใด รวมถงกำหนด guard conditions ทจำเปน u. Transitions แสดงรายละเอยดสงทเกดขนในการตอบสนองผลลพธของ event state transition เปนการเปลยนแปลงจาก state อน Initialize Add. Student( Student / ( set student = 0 Cancel Course Cancelled state หนงไปยง add. Student( ]student < 10[ Open ]student = 10[ Closed
Find events and send events uevent หนง ๆ อาจเปน trigger ไดโดยการสงสญญาณไปยง event อน ๆ uจำนวนนกศกษาครบ 10 คน มการสง Event ทเรยกวา compile. Report เพอสราง Report เปนตน uจาก Open ไปจนถง Close add. Student( ]student < 10[ Initialize Add. Student( Student / ( set student = 0 Cancel Course Open compile. Report ]student = 10[ Course. Report. Create report[ Cancelled Closed
Stop State uกำหนด stop state ซงเปนสถานะสดทายของออปเจค จำนวนของ stop state อาจมมากกวา หรออาจไมมเลยได 1 add. Student( ]student < 10[ Initialize Add. Student( Student / ( set student = 0 Cancel Course Cancelled Open ]student = 10[ Closed
Standard Events for a State u. Standard events ทอาจเกยวของกบแตละ state Entry - ระบ actions เมอเขาส state Exit - ระบ actions เมอออกจาก state Do - ระบ activities ทเกดขนใน state add. Student( ]student < 10[ Add. Student( / set student = 0 Initialize Open entry/ Register Student Cancel Course Cancelled ]student = 10 / [ Course. Report. Create report Closed do/ Finalize Course exit/ Set Status to Close
Detailed Course. Offering class add. Student Initialize Unassigned do: Initialize course object do: Assign lecturer to course add. Student/ num. Students = 0 cancel. Course Open entry: Register a student cancel. Course registration closed[ Canceled registration closed[ num. Students > = 3 ] num. Students < 3 ] do: Send cancellation notices [ num. Students = 10 ] cancel. Course Closed do: Report course is full Registration. Complete do: Generate class roster
How Do State Diagrams Map to the Rest of the Model? u. Events สามารถ map เปน operations u. Methods จะถก updated พรอมกบขอมลเฉพาะของ u. States ถกนำเสนอผาน attributes ใชสำหรบเปน state input ในขนตอนการกำหนดคา Attributes Courseoffering student : Integer add. Student( add. Student)Student( ]student = 10[ Open Closed
Phone Call example: The caller picks up the phone. A dial tones is heard on the phone line. The caller dials 7 digits. A dialling sound is heard on the phone line. After the seventh digit, the phone rings. The callee answers the phone.
Analysis: UML Activity Diagram Define use cases Define participating objects Define entity objects Define boundary objects Define control objects Define interactions Define nontrivial behavior Define attributes Consolidate model Review model Define associations
Getting Start Initial state Fork Select site Action State Commission architect Do site work Do trade work Join Develop plan Finish construction Bid plan [not accepted] Sequential branch [else] Activity with Sub Machine Final State : Certificate Of Occupancy [completed]
Fork & Join Fork และ Join เปนการ ระบการทำงานแบบขนาน (parallel ) u. Fork – เกดขนในกรณทม 1 incoming transition และหลาย ๆ outgoing transitions ทมการทำงานเปนแบบขนาน u. Join – เกดขนเมอมหลาย outgoing transition เพยงหนงเดยว ๆ incoming transitions และม Fork Decompress Synch mouth Stream Audio Join Clean up
Swimlanes Activity Customer Sale Warehouse Request product Process order Pull material Ship order Receive order Bill customer Pay bill Close order
Swimlanes Activity with Object Customer Sale Warehouse Request product Process order Pull material o : Order [in progress] Ship order Receive order Object Flow Bill customer Pay bill b : Bill [unpaid] Close order b : Bill [paid] o : Order [filled] Object Status
Activity Diagram : Code Details while (true) { amount = 0. 0; while (amount < price) { wait for a coin; add coin value to amount; } show all available soft drink; while (selection is not done) { wait for selection from user; if selection is “eject coins” { dispense coins; set selection to “done”; } else if selection is a valid soft drink { dispense change & soft drink concurrently; set selection to “done” } } }
Identify Use cases from Business Process Take Up Reservation Guest Take up reservation Wait for event enquiry/ Check availability customer arrives/ [else] cancel request/ [suitable amendment room] request/ Reservation Cancel reservation Amend reservation Make reservation Cancel no-show/ Maker Make Reservation System Process No-Show Confirm reservation Amend Reservation Process no-show Notify billing system
Example: Included Use Case Reservation system Make a reservation Guest <<include>> Take up a reservation Cancel a reservation Amend reservation Billing. System Process no shows <<include>> Identify reservation
- Slides: 53