Presentation is loading. Please wait.

Presentation is loading. Please wait.

14-Jul-15 State Machines Abbreviated lecture. 2 What is a state machine? A state machine is a different way of thinking about computation A state machine.

Similar presentations


Presentation on theme: "14-Jul-15 State Machines Abbreviated lecture. 2 What is a state machine? A state machine is a different way of thinking about computation A state machine."— Presentation transcript:

1 14-Jul-15 State Machines Abbreviated lecture

2 2 What is a state machine? A state machine is a different way of thinking about computation A state machine has some number of states, and transitions between those states Transitions occur because of inputs State machines are useful when you need to respond to a sequence of inputs A “pure” state machine only knows which state it is in—it has no other memory or knowledge This is the kind of state machine you learn about in your math classes When you program a state machine, you don’t have that restriction A state machine may produce output as it goes along Alternatively, the “output” may be what state it ends up in

3 3 Example I: Even or odd The following machine determines whether the number of A s in a string is even or odd Circles represent states; arrows represent transitions Inputs are the characters of a string The “output” is the resultant state A evenodd start A anything but A

4 4 Simplifying drawings I Some state machines may have a error state with the following characteristics: An illegal input will cause a transition to the error state All subsequent inputs cause the state machine to remain in the error state We can simplify the drawing by leaving out the error state The error state is still part of the machine Any input without a transition on our drawing is assumed to go to the error state Another simplification: Use * to indicate “all other inputs” This is a convention when drawing the machine—it does not mean we look for an asterisk in the input

5 5 Example II: Nested parenthesis The following example tests whether parentheses are properly nested (up to 3 deep) How can we extend this machine to handle arbitrarily deep nesting? start ) ( ) ( ) ( OK Error ) **** ( *

6 6 Nested parentheses II Question: How can we use a state machine to check parenthesis nesting to any depth? Answer: We can’t (with a finite number of states) We need to count how deep we are into a parenthesis nest: 1, 2, 3,..., 821,... The only memory a state machine has is which state it is in However, if we aren’t required to use a pure state machine, we can add memory (such as a counter) and other features

7 7 Nested parentheses III This machine is based on a state machine, but it obviously is not just a state machine OK ( do count=1 ) and count==1 do count=0 ( do count++ ) and count>1 do count-- start

8 8 Example: Making numbers bold In HTML, you indicate boldface by surrounding the characters with... Suppose we want to make all the integers bold in an HTML page—we can write a state machine to do this NORMALNUMBER digit output digit nondigit output nondigit *: output * end of input output start digit output digit end

9 9 State machines in Java In a state machine, you can have transitions from any state to any other state This is difficult to implement with Java’s loops and if statements The trick is to make the “state” a variable, and to embed a switch ( state ) statement inside a loop Each case is responsible for resetting the “state” variable as needed to represent transitions

10 10 Outline of the bold program void run() { int state = NORMAL; for (int i = 0; i < testString.length(); i++) { char ch = testString.charAt(i); switch (state) { case NORMAL: { not inside a number } case NUMBER: { inside a number } } if (state == NUMBER) result.append(" ");

11 11 The two states case NORMAL: if (Character.isDigit(ch)) { result.append(" " + ch); state = NUMBER; break; } else { result.append(ch); } break; case NUMBER: if (!Character.isDigit(ch)) { result.append(" " + ch); state = NORMAL; break; } else { result.append(ch); } break;

12 12 Conclusions A state machine is a good model for a number of problems You can think of the problem in terms of a state machine but not actually do it that way (e.g. German vocabulary) You can implement the problem as a state machine (e.g. making integers bold) Best done as a switch inside some kind of loop Pure state machines have some severe limitations Java lets you do all kinds of additional tests and actions; you can ignore these limitations

13 13 The End Eclipse Hints ● To see older versions of a class or method, right-click its name and choose Local History  Compare with... ● To go back to an older version of a class or method, right-click its name and choose Local History  Replace with...


Download ppt "14-Jul-15 State Machines Abbreviated lecture. 2 What is a state machine? A state machine is a different way of thinking about computation A state machine."

Similar presentations


Ads by Google