State Modeling New

download State Modeling New

of 48

Transcript of State Modeling New

  • 7/23/2019 State Modeling New

    1/48

    State Modeling

  • 7/23/2019 State Modeling New

    2/48

    Introduction

    The structure of objects and their relationships to eachother in a system described by its static structure i.e.the class model.

    Some objects in a system have complex temporalbehaviors, which must be carefully design.

    Temporal phenomena that occur over an interval oftime are properly modeled with a state

    The state model examine changes to the objects andtheir relationships over time

    The state model describes the sequence of operationsthat occur in response to events external stimuli!

  • 7/23/2019 State Modeling New

    3/48

    Events occurrence at a point in time

    instantaneous often corresponds to verb in past tense

    e.g., alarm set, powered on

    or onset of a condition e.g., paper tray becomes empty, temperature drops below

    free"ing

    may logically precede or follow another ormay be unrelated e.g., #light $%& must depart ''( before it can arrive in 'elhi

    causally related! e.g., #light $%& may depart before or after )ight &*+ departs

    ''( causally unrelated!

    Concurrent event: causally unrelated

    eventshave no e-ect on one another

  • 7/23/2019 State Modeling New

    4/48

    inds of events

    Signal event/ the event of sending or receiving of a signal Signal/ an explicit one0way transmission of

    information from one object to another may be parameteri"ed

    1.g., stringEntered(Foo) sending of a signal by one object is a

    distinct event from its reception by another 'i-erence between signal and signal event

    every signal transmission is a uniqueoccurrence but we group them into signalclasses to indicate common structure andbehavior. 1.g., I2 )ight $%& departs from ''( on 3an $$, %4$& is

    an instance of #light'eparture

  • 7/23/2019 State Modeling New

    5/48

    Signal class 0 5M6 notation

    77 signal 88

    FlightDeparture

    airline)ight(umcity

    date

    9eyword :signal; in 77 88

    name of signal class

    attributes

  • 7/23/2019 State Modeling New

    6/48

    inds of 1vents

    Change event 1vent caused by satisfaction of a

  • 7/23/2019 State Modeling New

    7/48

    inds of 1vents

    Time event 1vent caused by the occurrence of an absolute

    time or the elapse of a time interval

    for absolute time the 5M6 (otation/ 9eyword

    when followed by parenthesi"ed expressioninvolving time

    whendate = 3an $, %4$&!

    for time interval the 5M6 (otation/ 9eyword

    after followed by parenthesi"ed expressionthat evaluate to a time duration

    aftern time5nits!

    after$4 seconds!

  • 7/23/2019 State Modeling New

    8/48

    States an abstraction of values and lin9s of an object

    behavioral condition that persists in time according to gross behavior of objects, set of

    values and lin9s are grouped together into astate

    often corresponds to verbs with su>x of :0ing;

    e.g.,

  • 7/23/2019 State Modeling New

    9/48

    Aontd. In deBning states

    ignore attributes that do not a-ect the behavior of the object lump together in a single state all combinations of values and lin9s

    with the same response to events

    1.g., except for leading 4Cs D $Cs, the exact digits dialed do not a-ectthe control of the phone line, so we can deBne a state Dialingandtrac9 the phone number as a parameter

    Ebjects in a class have a Bnite number of possible states 1ach object can only be in one state at a time

    2t a given moment of time, the various objects for a class can existin a multitude of states

    2 state speciBes the response of an object to input events 1.g., if a digit is dialed in state DialTone, the phone line drops the

    dial tone and enters state Dialing;

    If the receiver is replaced in state DialTone, the phone line goesdead and enters state Idle.

  • 7/23/2019 State Modeling New

    10/48

    Symmetry between 1vents and

    States

    1vents represent points in time

    State represent intervals of time. 2state corresponds to the intervalbetween two events received by anobject

    @ower turnedon

    @ower turnedo-

    @ower turnedon

    @owered (ot @owered

  • 7/23/2019 State Modeling New

    11/48

    Transitions and Aonditions

    ransition/ an instantaneous change in state triggered by an event

    Transition is said to Bre upon the change from source totarget state

    Erigin and target state of a transition are di-erent states

    but may be the same e.g., when a phone line is answered, the phone line

    transitions from the Ringingstate to the Connectedstate.

    !uard Condition/ boolean expression that must be true for transition to occur

    chec9ed only once, at the time event occurs transition Bresif true

    1.g., when you go out in the morning (event), if thetemperature is below free"ing (condition), then put onyour gloves (next state).

  • 7/23/2019 State Modeling New

    12/48

    1nabling and Bring of transitions

    Transition is/ enabled when source state is active and guard

    condition satisfed

    freswhen enabled and the triggering eventoccurs

    1xample below/ enabled when current state is 1diting and the

    form is complete Bres when the user presses the :E; button

    Editing Submitted

    pressOK[form complete]

  • 7/23/2019 State Modeling New

    13/48

    State 'iagrams

    a graph whose nodes are states and whosedirected arcs are transitions between states

    speciBes state sequences caused by eventsequences

    all objects in a class execute the state diagramfor that class diagram models their commonbehavior

    (ote/ state names are unique within the scope ofstate diagram

    2 class with more than one state has importanttemporal behavior

    2 class is temporarily important if it has a single state

    with multiple responses to events

  • 7/23/2019 State Modeling New

    14/48

    State diagrams

    Fraphical state0modeling notation/ States/ labeled rounded boxTransitions/ directed arcs, labeled by triggering

    event, optional guard condition, andGor e-ects

    SpeciBes the response of an object to input

    events0 ignores events except those for which behavior isprescribed

    1xample/

    S T

    States

  • 7/23/2019 State Modeling New

    15/48

    State diagrams

    S T

    States

    Transition

    Fraphical state0modeling notation/ States/ labeled rounded boxTransitions/ directed arcs, labeled by triggering

    event, optional guard condition, andGor e-ects

    SpeciBes the response of an object to inputevents

    0 ignores events except those for which behavioris prescribed

    1xample/

  • 7/23/2019 State Modeling New

    16/48

    State diagrams

    Fraphical state0modeling notation/ States/ labeled rounded box

    Transitions/ directed arcs, labeled by triggeringevent, optional guard condition, andGor e-ects

    1xample/

    S T

    event(attribs)[condition] / effect

    States

    1ventTransition

  • 7/23/2019 State Modeling New

    17/48

    State diagrams can represent Aontinuous loops

    'o not care, how the loop is started

    Ene0shot life cycles

  • 7/23/2019 State Modeling New

    18/48

    @hone 6ine example

  • 7/23/2019 State Modeling New

    19/48

    :Ene0shot; state diagrams

    represent objects with Bnite lives have initial and Bnite states

    initial state0 entered on objectcreation

    Bnal state 0 entry implies destructionof object

  • 7/23/2019 State Modeling New

    20/48

    1xample

    Whites

    turn

    Blacks

    turn

    white

    moves

    black

    moves

    checkmate

    checkmate

    stalemate

    stalemate

    Chess game

    start state

    Deault fnal state

  • 7/23/2019 State Modeling New

    21/48

    1xample

    Whites

    turn

    Blacks

    turn

    Black

    wins

    White

    wins

    Drawwhite

    moves

    black

    moves

    checkmate

    checkmate

    stalemate

    stalemate

    Chess game

    start state

    Final

    states

  • 7/23/2019 State Modeling New

    22/48

    1xample 0 entry and exitpoints

    Whites

    turn

    Blacks

    turn

    white

    moves

    black

    moves

    checkmate

    checkmate

    stalemate

    stalemate

    Chess game

    Black

    wins

    Draw

    White

    wins

  • 7/23/2019 State Modeling New

    23/48

    State Model

    multiple state diagrams, one for eachclass with important temporalbehavior diagrams interact by passing events and

    through side e-ects of guard conditions

    events and guard conditions must

    match across diagrams in the model

  • 7/23/2019 State Modeling New

    24/48

    'etails

    if more than one transition leaves astate, then the Brst event to occurcauses the corresponding transition to

    Bre if an event occurs and no transition

    matches it, the event is ignored

    if more than one transition matches anevent, only one transition will Bre butthe choice is non0deterministic

  • 7/23/2019 State Modeling New

    25/48

    2ctivity 1-ects

    eect= reference to a behaviorexecuted in response to an event can be attached to a transition or a

    state listed after a slash :G;!

    multiple e-ects separated with a :,; and

    are performed concurrently

  • 7/23/2019 State Modeling New

    26/48

    2ctivity 1-ects

    Activity behavior that can beinvo9ed by any number of e-ects

    May be performed upon/

    a transition entry to or exit from a state

    some event within a state

    (otation/ eventG resulting0activity

  • 7/23/2019 State Modeling New

    27/48

    2ctivities

    Eften useful to specify an activitythatis performed within a given state 1.g., while in Paper"amstate, the

    warning light should be )ashing 1.g., on entry into the #peningstate,

    the motor should be switched on

    1.g., upon exit of the #peningstate,the motor should be switched o-

  • 7/23/2019 State Modeling New

    28/48

    2ctivity e-ects

    Idle

    Menu visible

    r_button_down / showPopup

    r_button_up / hidePopup

  • 7/23/2019 State Modeling New

    29/48

    'o02ctivities

    PaperJam

    do/ flash warning light

    continue for an extended time

    can occur only within a state

    can not be attached to a transition

    include

    continuous operations, such as displaying a picture on atelevision screen

    Sequential operations that terminate by themselves after an

    interval of time

    may be performed for all or part of time that an obect is in a state

    may be interrupted by event received during execution! event

    may or may not cause state transition

  • 7/23/2019 State Modeling New

    30/48

    1ntry and 1xit 2ctivities

    Opening

    entry / motor up

    exit / motor off

    can bind activities to entry to/ exit from a state

    All transitions into a state perform the same activity, in

    which case it is more concise to attach the activity to the state

  • 7/23/2019 State Modeling New

    31/48

    Erder of activities

    1. activities on incoming transition

    2. entry activities

    3. doactivities!. exit activities

    ". activities on outgoing transition

    #vents that cause transitions out of the state can interrupt

    doactivities. $f a doactivity is interrupted, the exit

    activity is still performed

  • 7/23/2019 State Modeling New

    32/48

    In general, any event can occurwithin a state and cause an activity

    to be performed.

    1ntry and exit are only two examples

    of events that can occur

    Diference between an event

    within a state and sel-transition/ only the self0transitioncauses the entry and exit activities to

    be executed but an event within a

  • 7/23/2019 State Modeling New

    33/48

    Aompletion Transition

    triggered b com!letion o activit inthe so"rce state

    Eften the sole purpose of a state is to

    perform a sequential activity. ?hen the activity is completed, a

    transition to another state Bres

    2n arrow without an event name indicatesan automatic transition that Bres

    State "

    do / blah#$

    State %

  • 7/23/2019 State Modeling New

    34/48

    Aontd.

    If a state has one or more completion transitions,but none of the guard conditions are satisBed, thenthe state remains active and may become Hstuc9C.

    The completion event does not occur a second time

    Therefore no completion transition will Bre later tochange the state

    So if a state has completion transition leaving it,normally guard condition should cover every

    possible outcome. 'o not use guard condition on a completion

    transition to model waiting for a change of value

  • 7/23/2019 State Modeling New

    35/48

    Sending signals 2 object can perform the activity of sending a signal to

    another object. 2 system of objects interact by exchanging signals

    The activity :send target!"(attributes); sends a signal S withthe given attributes to the target object.

    1.g., the phone line sends a connect(phone number) signal to

    the s#itcher #hen a complete phone number has beendialed!

    2 signal can be directed to a set of objects or a single object.

    If the target is a set of objects, each of them receives a

    separate copy of the signal conc"rrentl and independentlyprocess the signal and determines whether to Bre a transitionor not

    If an object receive signals from more than one object, theorder in which concurrent signals are received may a-ect the

    Bnal state race condition!

  • 7/23/2019 State Modeling New

    36/48

    2dvanced state modeling

    Aonventional state diagrams aresu>cient for describing simplesystems but need additional power

    to handle large problems Model complex system by using

    (ested state diagrams

    (ested states Signal generali"ation

    Aoncurrency

  • 7/23/2019 State Modeling New

    37/48

    (ested state diagram

    @roblem with )at state diagram Aonsider an object with n independent

  • 7/23/2019 State Modeling New

    38/48

    (ested state

    (est states to show their commonalityand share behavior

    Com!osite state# state that enclosesthe nested states. 6abels in the outer contour

    2 nested state receives the outgoing

    transitions of its composite states

  • 7/23/2019 State Modeling New

    39/48

    1xample/ Transmission

    ransmission

    Forward

    (eutraleverse

    $st %nd &rd

    downshift

    upshift

    downshift

    upshiftstop

    push (

    push #

    push (

    push

  • 7/23/2019 State Modeling New

    40/48

    Signal Fenerali"ation

    Ergani"e signals into generali"ation hierarchy

    with inheritance of signal attributes Jiew every actual signal as a leaf on a

    generali"ation tree of signals.

    eceived signal triggers transitions that are

    deBned for any ancestor signal type. 1.g., typing an HaC would trigger a transition on signal

    2lphanumeric as well as signal eyboardAharacter.

    2 signal hierarchy permits di-erent levels of

    abstraction to be used in a model. 1.g., some state might handle all iGp characters the

    same other states might treat control charactersdi-erently from printing characters .

  • 7/23/2019 State Modeling New

    41/48

    AE(A51(AK

    State model supports concurrencyamong objects

    Ebject can act D change stateindependent of one another.

    Sometime objects shares constraintsthat causes their state changes

    2FF1F2TIE(

  • 7/23/2019 State Modeling New

    42/48

    2FF1F2TIE(AE(A51(AK

    State 2ggregation means collection ofstate diagrams , one for each part

    :and; relationship

    2ggregate state is one state from Brstdiagram D a state from second diagramD state from each other diagram.

    Transition for one object depend onanother object that allows interactionbetween the state diagram.

  • 7/23/2019 State Modeling New

    43/48

    Aoncurrency within an

  • 7/23/2019 State Modeling New

    44/48

    Aoncurrency within anEbject

    Some objects can be partitioned into subsetsof attributes or lin9s.

    1ach of the partitioned subset has its ownsubdiagram.

    The state of the object comprises one statefrom each subdiagram.

    The sub diagrams need not be independent

    the same event can cause transitions inmore than one subdiagram

    5M6 (otation0 partition the composite stateinto regions with dotted lines.

  • 7/23/2019 State Modeling New

    45/48

    Synchroni"ation of Aoncurrent2ctivities

    Sometimes one object must performtwo or more! activities concurrently.

    The object must complete both

    activities before it can progress to itsnext state.

    or an o n

  • 7/23/2019 State Modeling New

    46/48

    or an o n#%

    &plitting control and 'erging

    control #E0 2 transition that for9s

    indicates splitting of control into

    concurrent parts.

    3EI(0 1xplicit merging of concurrent

    control by a transition.

  • 7/23/2019 State Modeling New

    47/48

    elation of class model, state model

    2 state diagram describes all or part ofthe behavior of the objects of a givenclass.

    States = classes of values D lin9 for anobject

    State model of a class is inherited by its

    subclasses. Subclass inherits both thestate D Transitions.

    It is also possible to reBne an inherited

    state diagram by expanding state into

  • 7/23/2019 State Modeling New

    48/48

    Aontd.

    State structure is related to andconstrained by class structure. 2 composite state is the aggregation of

    more than one concurrent substate.Try to ma9e the state diagrams of

    subclasses independent of the statediagrams of their superclasses.