[Tutor] What exactly is "state"?

Alan Gauld alan.gauld at btinternet.com
Mon Mar 2 19:00:30 CET 2015


On 02/03/15 16:25, Sydney Shall wrote:

> I am not clear precisely what is meant by state. In its
> common meaning I can see some relevance. But is there a technical aspect
> to the notion.

Yes, although it is related to the normal vernacular meaning.
There is a whole area of math/engineering dedicated to the study
of state and in particular finite state machines and automata.
Finite State Machine on Wikipedia will tell you more about
that than you want to know :-)

Trying to simplify it to the level of ordinary mortals, state
simply means a specific set of data attribute values, which
affect the outcome of the operations of a system.

A very simple state might be a button which can be ON or OFF.
That's not very interesting.

But suppose we have a more complex circuit model with an upstairs
and downstairs switch controlling a stairway light. Now there are
two boolean variables and whether the light is on or off depends
on the state of both switches

Top	Bottom		Light
UP	UP		OFF
UP	DOWN		ON
DOWN	UP		ON
DOWN	DOWN		OFF

So the state of the light depends on the state of the two switches.
You can't tell what effect moving the Top switch to the UP
position will have on the light unless you already know
either the current state of the light or the state of
the Bottom switch.

We can very easily model that as a class:

class StairLight:
    def __init__(self, sw1=True,sw2=True):
        self.sw1 = sw1
        self.sw2 = sw2
        self._setLight()

    def _setLight(self):
        self.light = self.sw1 != self.sw2

    def __str__(self):
        if self.light:
           return 'light is ON'
        else:
           return 'light is OFF'

    def toggle1(self):
        self.sw1 = not self.sw1
        self._setLight()

    def toggle2(self):
        self.sw2 = not self.sw2
        self._setLight()


SL = StairLight()
print(SL)  # -> OFF
SL.toggle1()
print(SL)  # -> ON
SL.toggle1()
print(SL)  # -> OFF
SL.toggle2()
print(SL)  # -> ON
SL.toggle2()
print(SL)  # -> OFF

Of course the  state values don't need to be boolean, they
can be any type. And they can have many valid values.
Consider the state of a order with several order lines.

It can be initiated, costed, approved, packaged, shipped,
delivered, paid, completed, archived.

That's the happy path, but then consider the extra states
needed for the 'unhappy paths' - awaiting stock, cancelled,
rejected, partial delivery, returned, faulty, and so on.

To simplify things we usually create an explicit state
attribute which is updated after each operation on the
order to reflect the current status.

There are many ways to address these kinds of problems,
and mathematical models that can be applied to simplify
them (in many ways similar to the techniques for simplifying
boolean algebra). In practice they boil down to either a
state model where each state is modelled as a class or
a state table where a large table contains a row for
each possible state and a column for each possible
event. The cells contain an action and the action
returns the next state.

The huge advantage of using state machines like this is
that they are entirely predictable in behaviour and can
be modelled and tested exhaustively. This means they
are ideal for safety critical systems and the like.
Most air/rail/traffic control systems, telecomms network switches,
and equipment control software (cameras, iPods, phones etc)
are built using state machines. Specialist CASE tools
are available (for many $$$$!) that will generate the
code from a state model (often graphical). They also
allow the user to run simulations etc to see the effects,
perform performance tests etc all without writing a line
of actual code. It's one of the main reasons that aircraft
don't often crash due to software faults even though your
web browser probably falls over almost every day!

One formal language for developing complex designs based
on this approach is SDL (not the graphics toolset). There
is an opensource CASE tool for SDL written in Python and
Qt, called openGeode:

http://taste.tuxfamily.org/wiki/index.php?title=Technical_topic:_OpenGEODE,_an_SDL_editor_for_TASTE

I spent many years of my life building systems using SDL,
C and Motorola 68000 assembler....

Oh, and finally. Another big win for state machines is that they
can oftewn be translated from software into hardware for
the ultimate in performance tweaking! :-)

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list