<<

State

In UML, we specify the behaviour of the individual objects of a class through State Diagrams. They specify:

how an object reacts when one of its methods (operations) is called.

UML State Diagrams Central the notions of state and state transformation: ITNP090 - Object Oriented Software Design

Dr Andrea Bracciali A system may assume different states at given times, and change its state

Module Co-ordinator according to occurrences (e.g., methods calls in an object oriented 4B86 [email protected] world).

Dept. of Computing Science & Mathematics 153 Dept. of Computing Science & Mathematics 154 University of Stirling University of Stirling

State Diagrams State Diagrams

A state describes a system that, at any given time, is in a certain Another "dynamical" diagram. state and can take part in an event, such as having one of its methods called.

State diagrams support event-driven modelling: how a system responds to As a consequence of that event, the object can: internal and external events (based on Statecharts [Harel 87, 88]). – move to a new state,

– perform actions, such as Not really detailed in terms of data-flow. • calling the methods of other objects, or

• change the values of attributes. They are based on extended finite state machines.

We say that the event triggers a transition.

What a finite state machine (or finite state automaton) is ? We label the transition with the name of the event.

Dept. of Computing Science & Mathematics 155 Dept. of Computing Science & Mathematics 156 University of Stirling University of Stirling Example State diagrams

A State Diagram for a computer monitor: A transition may be labelled with

• Triggering events, • Guards, i.e. boolean conditions that may enable/block the transition • Behaviour, i.e. actions to be performed before entering into the new state.

Pseudostates direct transition flow and include:

• Initial

• Final [Condition 1] • Choice [Condition 2] • Fork

• ... Dept. of Computing Science & Mathematics 157 Dept. of Computing Science & Mathematics 158 University of Stirling University of Stirling

Example: an Account object Example: an Account object

Let us look at this through the simple example of our Account class. The states that an Account object can go through can be shown in a The state of an Account object can change when we deposit or withdraw state diagram as follows. money.

?

Dept. of Computing Science & Mathematics 159 Dept. of Computing Science & Mathematics 160 University of Stirling University of Stirling Example: an Account object Example: an Account object

The filled circle is the start marker (initial state). It means that when The state diagram shows that when an object is in the nofunds state, a new object of class Account is created, it starts in the nofunds the only method that can be called is deposit. state. When in the state nofunds, we cannot withdraw money.

Objects of class Account can accept calls of methods deposit and When the object receives a call of deposit, the object moves to the withdraw. funds state.

Dept. of Computing Science & Mathematics 161 Dept. of Computing Science & Mathematics 162 University of Stirling University of Stirling

Example: an Account object Example: an Account object

There are three situations: When in the funds state, the object can receive more calls of deposit. These cause us to stay in the funds state. 1. sufficient funds: stay in funds state and decrement balance attribute;

The effect of withdraw depends on the current state of the object. It 2. exactly correct funds: move to nofunds state and make balance can only be called in the state funds and its effect depends on whether attribute zero. there are sufficient funds to satisfy the request. 3. insufficient funds: we can regard this as an error, i.e. we should never receive a withdraw message in this circumstance.

Dept. of Computing Science & Mathematics 163 Dept. of Computing Science & Mathematics 164 University of Stirling University of Stirling State diagrams State diagrams

To keep things simple, we are only modelling correct situations, i.e. we Accordingly to the Design by Contract view, the pre-condition: are not showing error transitions in the diagram.

amount<=balance They may greatly complicate the diagram.

We distinguish between different correct behaviours by putting on the withdraw operation, does not require the Account class to guards on events, e.g. amount < balance specify behaviour when the pre-condition is not met.

Note the connection with pre- and post-conditions. It is the responsibility of the client/caller to ensure that the pre- condition is satisfied.

Dept. of Computing Science & Mathematics 165 Dept. of Computing Science & Mathematics 166 University of Stirling University of Stirling

State diagrams Example: Library Case Study

We can associate actions with each event. Let us now devise a state diagram for the Copy object that we had in

the Library case study. An action can involve:

• calling a method, • changing an attribute such as balance, that is we can have assignments .

However, in the early stages, we do not want to put too much detail into a state diagram; the emphasis is on getting the main structure right.

?

Dept. of Computing Science & Mathematics 167 Dept. of Computing Science & Mathematics 168 University of Stirling University of Stirling Example: Library Case Study Example: Library Case Study

When a Copy object is initially created, it is in state onShelf and may The action of calling inform and borrow is shown by putting these calls in receive a borrow message. the label of the transition after a '/' and separating them by a semi-colon.

As a result of receiving this message, the Copy object will (according to the ) The call of borrow has a reference to the borrowing Member object as the – call the inform method of its associated Publication object, and parameter m. – call the borrow method of the Member object that is doing the borrowing.

Dept. of Computing Science & Mathematics 169 Dept. of Computing Science & Mathematics 170 University of Stirling University of Stirling

Example: Library Case Study Example: Library Case Study

When the Copy object was created, the association with a Publication Instead of writing the actions on the transitions, we can write the actions object was set up. inside the state,

BUT this means that actions are not specific of a transition, e.g., you cannot distinguish how you reach a state (by means of which transition)

Rather, there is an implicit entry event when a state is entered and an exit event when it is left (actions can be associated to these events).

That is shown in an implementation by the Copy object having a reference to UML does not indicate what the syntax of the actions should be. a Publication object as the attribute pub. States with associated actions are sometimes called active states (and, those We then use m and pub in the actions of calling other object’s methods. without actions, passive states).

Dept. of Computing Science & Mathematics 171 Dept. of Computing Science & Mathematics 172 University of Stirling University of Stirling Example: Library Case Study Example: a Publication object

Let us now look at a possible state diagram for Publication: The state diagram for Copy can be shown in either of the two following ways.

What is this?

Dept. of Computing Science & Mathematics 173 Dept. of Computing Science & Mathematics 174 University of Stirling University of Stirling

Generating Code Generating Code

Let us consider again an object of class Copy.

The state diagrams have been used to define the behaviour of an

object, e.g. an object of class Copy.

Then the actual objects are expected to be compliant with such a defined behaviour, that is, state diagrams impose constraints on what methods are/are not allowed to do.

Indeed, it is possible to generate the corresponding Java or C++ code. As we are following the idea of Design by Contract, we have made the decision that an unexpected message is just ignored.

Hence, if a Copy is in state onLoan and the borrow method is called, it is ignored.

Dept. of Computing Science & Mathematics 175 Dept. of Computing Science & Mathematics 176 University of Stirling University of Stirling Generating Code Generating Code

class Copy { public void borrow(Member m) { // define the possible states // borrow

private final static int onShelf = 0; if (currentState == onShelf) { private final static int onLoan = 1; pub.inform(this); private int currentState; m.borrow(this); private Publication pub; currentState = onLoan; } public Copy(Publication p) { } // constructor

pub = p; currentState = onShelf; }

Dept. of Computing Science & Mathematics 177 Dept. of Computing Science & Mathematics 178 University of Stirling University of Stirling

Generating Code Generating Code

public void returns(Member m) { We will know about the required parameters in the constructor from // returns the sequence diagram in which a Copy object was created.

if (currentState == onLoan) { Note that the information in the generated code comes from several pub.returns(this); different diagrams. m.returns(this);

currentState = onShelf; } A drawback of the code is that it is not really very readable by } humans.

} // end Copy "However, we can regard the UML diagrams as our source program and the (automatically) generated Java or C++ as machine code".

Dept. of Computing Science & Mathematics 179 Dept. of Computing Science & Mathematics 180 University of Stirling University of Stirling Generating Code State vs. Sequence Diagrams

Idea is that the UML is at a higher more abstract level and so we are It should be emphasised that a state diagram defines the different better able to reason at that level of abstraction about the overall states that an object can go through when it is being executed. design. It does show the possible method calls that are made to carry out its It should therefore be easier to prove that the system, and hence actions.

the corresponding "generated" code, satisfy the requirements. However, we are concerned with the behaviour of one typical object of a class.

Sequence diagrams, on the other hand, are concerned with the interaction between objects and how they collaborate with one another.

Dept. of Computing Science & Mathematics 181 Dept. of Computing Science & Mathematics 182 University of Stirling University of Stirling

State diagrams Time

State diagrams can also include time events.

So far, in our state diagrams, transitions have been caused by a Suppose that our Copy object became overdue after two weeks and so method of the object being called. The object can then, as an action, moved into the overdue state. Our diagram could become: call the methods of other objects.

Calling a method may result in information being returned that will be used to change the values of attributes.

Some changes of attribute values will cause a new state to be entered.

This is modelled by a transition labelled with an event of the form:

when(x == 10)

Dept. of Computing Science & Mathematics 183 Dept. of Computing Science & Mathematics 184 University of Stirling University of Stirling State diagrams ATM

Consider, for instance, our ATM example: State diagrams can become very large (with lots of states) and hence difficult to read. This is especially true when we include error handling.

There is also the case where the same transition can occur from many different states. That can cause a large number of arrows to be required.

The user may want to cancel a transaction at any time.

Dept. of Computing Science & Mathematics 185 Dept. of Computing Science & Mathematics 186 University of Stirling University of Stirling

ATM Substates

Cancelling a transaction at any time can be modelled by a having a cancel The solution is to use substates: transition from the – Validation, – Selection and The Working state – Processing has a series of substates. states, i.e. an arrow from each of these states to the Idle state. We enter Working through the insertCard transition. In each case, we want the ejectCard On entry to the state, action to be associated with the transition. Working the action readCard is executed

The initial substate is . That would greatly complicate the diagram. ? Validating

Dept. of Computing Science & Mathematics 187 Dept. of Computing Science & Mathematics 188 University of Stirling University of Stirling Substates Substates

We move from one substate We therefore only have to to another in the normal way show the cancel transition and the finish transition once. will cause us to return to the Idle state. It causes the Working state to be left and we return to Idle. When that occurs, the action ejectCard is executed. The exit action ejectCard is executed when we leave Working. Showing a transition from the It does not matter what substate Working state, means that it we are in or which transition caused can occur when we are in any us to leave. of its substates.

Dept. of Computing Science & Mathematics 189 Dept. of Computing Science & Mathematics 190 University of Stirling University of Stirling

Assignment Use of state diagrams

You have been asked for state diagrams in the assignment. As state diagrams are based on finite state automata, they are amenable to mathematical reasoning.

It is therefore possible to prove that the object has certain properties. You should take a class with significant internal behaviour and show what causes it to move from one state to the next one. As an example, one may consider SDL, a specification language which is heavily dependent on state diagrams and has been traditionally used in the telecommunications industry to specify, design and implement systems.

The latest versions of SDL are object-oriented.

General formal techniques for the verification of properties exhibited by software systems are, amongst others, Model Checking,Theorem Proving and Static Analysis.

Dept. of Computing Science & Mathematics 191 Dept. of Computing Science & Mathematics 192 University of Stirling University of Stirling