Changes between Version 2 and Version 3 of ControlSystems/SoftwareTeam/Training/GettingStarted/StateMachines1


Ignore:
Timestamp:
Nov 6, 2019, 12:16:10 PM (6 years ago)
Author:
David Albert
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/Training/GettingStarted/StateMachines1

    v2 v3  
    1 Consider a program that must drive a robot forward some distance and then turn the robot 90-degrees to face a target.  As the program runs, it spends time in at least two states:
     1Consider a program that must drive a robot forward some distance and then turn the robot 90-degrees to face a target.  As the program runs, it spends time in one of these states:
     2* Initial (startup)
    23* Driving Forward
    34* Turning
    4 It may remain in each state for a considerable amount of time.  A useful way to model this in software is using a '''State Machine'''.
     5* Finished
     6It may remain in a state for a considerable amount of time.  When it finishes one state, it transitions to another.  At any given time, the program is only in one state
    57
    6 As the name implies, a State Machine is a program that has stateful behavior, meaning that what it does at any given moment depends on its current state.  We could model the above sequence of states with this program:
     8We can model this behavior using a software construct called a '''State Machine'''.
     9
     10As the name implies, a State Machine is a program that has stateful behavior, meaning that what the program does at any given moment depends on its current state.  We could model the above sequence of states with this program:
    711{{{
    812public class StateMachine {
     
    4650* The case matching the current state is run each time we go through the loop
    4751* The program can stay in any state for as long as needed and explicitly advances to the next state when it's ready.
     52
     53In computer science, we often draw '''state diagrams''' that represent the states and the transitions between states.  Read about them [https://en.wikipedia.org/wiki/State_diagram here]
     54
     55Java is object oriented so its natural to model a state as an object.  Consider (and run) the following program (from [https://rosettacode.org/wiki/Finite_state_machine here]) that models a vending machine; it's a complex program that will take some time to understand; try running it in the debugger:
     56{{{
     57import java.util.*;
     58 
     59public class StateMachine {
     60 
     61    private enum State {
     62        // each state and its transitions (actions)
     63        Ready(true, "Deposit", "Quit"),
     64        Waiting(true, "Select", "Refund"),
     65        Dispensing(true, "Remove"),
     66        Refunding(false, "Refunding"),
     67        Exiting(false, "Quiting");
     68 
     69        State(boolean is_explicit, String... in) {
     70            inputs = Arrays.asList(in);
     71            explicit = is_explicit;
     72        }
     73 
     74        State nextState(String input, State current) {
     75            if (inputs.contains(input)) {
     76                return map.getOrDefault(input, current);
     77            }
     78            return current;
     79        }
     80 
     81        final List<String> inputs;
     82        final static Map<String, State> map = new HashMap<>();
     83        final boolean explicit;
     84 
     85        // Map contains transitions, next state
     86        static {
     87            map.put("Deposit", State.Waiting);
     88            map.put("Quit", State.Exiting);
     89            map.put("Select", State.Dispensing);
     90            map.put("Refund", State.Refunding);
     91            map.put("Remove", State.Ready);
     92            map.put("Refunding", State.Ready);
     93        }
     94    }
     95 
     96    public static void main(String[] args) {
     97        Scanner sc = new Scanner(System.in);
     98        State state = State.Ready;
     99 
     100        while (state != State.Exiting) {
     101            System.out.println(state.inputs);
     102            if (state.explicit){
     103                System.out.print("> ");
     104                state = state.nextState(sc.nextLine().trim(), state);
     105            } else {
     106                state = state.nextState(state.inputs.get(0), state);
     107            }
     108        }
     109        sc.close();
     110    }
     111}
     112}}}
     113
     114Read more about implementing state machines in Java in these advanced tutorials [https://www.mirkosertic.de/blog/2013/04/implementing-state-machines-with-java-enums/ here] and [https://www.baeldung.com/java-enum-simple-state-machine here]