asidko / one-file-fsm

Simple Final State Machine implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Final State Machine example in Java

This is one of the simplest and good looking FSM written in Java.
All implementation contained in the same file which takes only ~100 lines of code (with examples).

Example

This example shows the transitions within the media player at the touch of a button.

Each transition contains a simple demo action that prints a text to the console.

    Set<Transition> musicTransitions = new HashSet<>(asList(
        new Transition(State.MUSIC_STOPPED /* ⏩ */, State.MUSIC_PLAYING,/* πŸ”₯ */ Event.PRESSED_PLAY_BUTTON, /* πŸš€ */ Example.playAction),
        new Transition(State.MUSIC_PLAYING /* ⏩ */, State.MUSIC_PAUSED, /* πŸ”₯ */ Event.PRESSED_PAUSE_BUTTON,/* πŸš€ */ Example.pauseAction),
        new Transition(State.MUSIC_PLAYING /* ⏩ */, State.MUSIC_STOPPED,/* πŸ”₯ */ Event.PRESSED_STOP_BUTTON, /* πŸš€ */ Example.stopAction),
        new Transition(State.MUSIC_PAUSED  /* ⏩ */, State.MUSIC_STOPPED,/* πŸ”₯ */ Event.PRESSED_STOP_BUTTON, /* πŸš€ */ Example.stopAction),
        new Transition(State.MUSIC_PAUSED  /* ⏩ */, State.MUSIC_PLAYING,/* πŸ”₯ */ Event.PRESSED_PLAY_BUTTON, /* πŸš€ */ Example.playAction)
    ));
    IState initialState = State.MUSIC_STOPPED;
    
    FSM musicFSM = new FSM(musicTransitions, initialState);
    
    // Let's create some music events and then run them
    List<IEvent> musicPlayerAutoScript = asList(
        Event.PRESSED_PLAY_BUTTON,
        Event.PRESSED_PAUSE_BUTTON,
        // Example of Event with data
        DataEvent.of(Event.PRESSED_PLAY_BUTTON, "(Bob Marley - Bad Boys)"),
        Event.PRESSED_STOP_BUTTON
    );
    
    for (IEvent playerEvent : musicPlayerAutoScript)
        musicFSM.fireTransitionEvent(playerEvent);

The result would be the following:

image

Real usage improvements

  • Add null checks (at least in fireTransitionEvent() method of the FSM class).
  • Pass fsm as the second argument to run() method in IAction. This feature will allow you, for example, to call fsm.fireTransitionEvent() in afterTransitionAction and your FSM will be able to switch to the next state automatically.

About

Simple Final State Machine implementation


Languages

Language:Java 100.0%