University of TorontoDepartment of Computer Science© 2004-5 Steve Easterbrook. This presentation is available free for non-commercial use with attribution under a creative commons license. 1Lecture 13:Modelling “State” What is State? statespace for an object concrete vs. abstract states Finite State Machines states and transitions events and actions Modularized State machine models: Statecharts superstates and substates Guidelines for drawing statechartsUniversity of TorontoDepartment of Computer Science© 2004-5 Steve Easterbrook. This presentation is available free for non-commercial use with attribution under a creative commons license. 2Getting objects to behave All objects have “state” The object either exists or it doesn’t If it exists, then it has a value for each of its attributes Each possible assignment of values to attributes is a “state” (and non-existence of the object is also a state) E.g. For a stack objectempty 1 itemPush()Pop()new()2 items 3 items 4 itemsPush() Push() Push()Pop() Pop()…Pop()Top() Top() Top() Top()University of TorontoDepartment of Computer Science© 2004-5 Steve Easterbrook. This presentation is available free for non-commercial use with attribution under a creative commons license. 3empty 1 itemPush()Pop()new()2 items 3 itemsPush() Push()Pop() Pop()Top() Top() Top()What does the model mean? Finite State Machines There are a finite number of states (all attributes have finite ranges) E.g. imagine a stack with max length = 3 The model specifies a set of traces E.g. new();Push();Push();Top();Pop();Push()… E.g. new();Push();Pop();Push();Pop()… There may be an infinite number of traces (and traces may be of infinite length) The model excludes some behaviours E.g. no trace can start with a Pop() E.g. no trace may have more Pops than Pushes E.g. no trace may have more than 3 Pushes without a Pop in betweenUniversity of TorontoDepartment of Computer Science© 2004-5 Steve Easterbrook. This presentation is available free for non-commercial use with attribution under a creative commons license. 4Abstraction The state space of most objects is enormous State space size is the product of the range of each attribute E.g. object with five boolean attributes: 25+1 states E.g. object with five integer attributes: (maxint)5+1 states E.g. object with five real-valued attributes: …? If we ignore computer representation limits, the state space is infinite Only part of that state space is “interesting” Some states are not reachable Integer and real values usually only vary within some relevant range Often, we’re only interested in certain thresholds: E.g. for Age, we may be interested in age<18; 18≤age≤ 65; and age>65 E.g. for Cost, we may want to distinguish cost≤budget, cost=0, cost>budget, andcost>(budget+10%)University of TorontoDepartment of Computer Science© 2004-5 Steve Easterbrook. This presentation is available free for non-commercial use with attribution under a creative commons license. 5Collapsing the state spaceempty 1 itemPush()Pop()new()2 items 3 items 4 itemsPush() Push() Push()Pop() Pop()…Pop()Top() Top() Top() Top()empty not emptyPush()Push()Pop() [sc=1]new()Pop() [sc>1]Top() The abstraction usually permits more traces E.g. this model does not prevent traces with more pops than pushes But it still says something usefulUniversity of TorontoDepartment of Computer Science© 2004-5 Steve Easterbrook. This presentation is available free for non-commercial use with attribution under a creative commons license. 6What are we modelling?(D) Observed states of an application domain entity?E.g. a phone can be idle, ringing, connected, … Model shows the states an entity can be in, and how events can change its state This is an indicative model(R) Required behaviour of an application domain entity?E.g. a telephone switch shall connect the phones only when the callee accepts the call Model distinguishes between traces that are desired and those that are not This is an optative model(S) Specified behaviour of a machine domain entity?E.g. when the user presses the ‘connect’ button the incoming call shall be connected Model specifies how the machine should respond to input events This is an optative model, in which all events are shared phenomenaApplication DomainMachine DomainD - domain propertiesR - requirementsC - computersP - programsUniversity of TorontoDepartment of Computer Science© 2004-5 Steve Easterbrook. This presentation is available free for non-commercial use with attribution under a creative commons license. 7Is this model indicative or optative?idle connectedringingdialtonebusyon hookon hookon hookon hookoff hookDial[calleebusy]Dial[callee idle]CalleeacceptsCallee disconnectsUniversity of TorontoDepartment of Computer Science© 2004-5 Steve Easterbrook. This presentation is available free for non-commercial use with attribution under a creative commons license. 8the world vs. the machine:personagehavebirthday()childadultseniorhavebirthday() [age = 18]havebirthday() [age = 65]havebirthday() [age < 18]havebirthday() [age < 65]havebirthday() childadultseniorwhen[thisyear-birthyear>18]when[thisyear-birthyear>65]blankdeceasedrecordBirth() /setDOB()recordDeath()/setDateofDeath():persondateOfBirthdateOfDeathrecordBirth()setDOB()recordDeath()setDateofDeath()University of TorontoDepartment of Computer Science© 2004-5 Steve Easterbrook. This presentation is available free for non-commercial use with attribution under a creative commons license. 9StateCharts Notation: States “interesting” configurations of the values of an object’s attributes may include a specification of action to be taken on entry or exit States may be nested States may be “on” or “off” at any given moment Transitions Are enabled when the state is “on”; disabled otherwise Every transition has an event that acts as a trigger A transition may also have a condition (or guard) A transitions may also cause some action to be taken When a transition is enabled, it can fire if the trigger event occurs and it guardis true Syntax: event [guard] / action Events occurrence of stimuli that can trigger an object to change its state determine when transitions can fireUniversity of TorontoDepartment of Computer Science© 2004-5 Steve
View Full Document