Giter VIP home page Giter VIP logo

libstatemachine's Introduction

Common Google Statemachine

Illustration for test code

        init {
            addState(state1)
            addState(state2, state1)
            addState(state3, state1)
            addState(state4, state3)
            addState(state5, state3)
            setInitialState(state1)
            start()
        }

We build tree using addState, and set initial state with setInitialState and start The tree built with above code is like below

         state1 ---> initial state
         /   \
     state2  state3
             /   \
         state4 state5 

The state machine is driven by message, we could use sendMessage or sendMessageAtFrontOfQueue to send message immediately and also could use defferMessage to delay to handle this message only when the state changed. Current state will receive the message at first and the message isn't consumed it'll be sent to current state's parent state until it is consumed or the state reach to root. Let's see the sample below.

  1. For each state, MESSAGE_STATE1, MESSAGE_STATE4, MESSAGE_STATE5 will transition to each state with transitionTo ;
  2. state1,3,4 deffer MESSAGE_STATE2, state5 will handle MESSAGE_STATE2 and transition to state2
  3. all but state3 will handle MESSAGE_STATE3 and sendMessageAtFrontOfQueue
            override fun processMessage(msg: Message): Boolean {
                L.log(message = "$this process:$msg")
                when (msg.what) {
                    MESSAGE_STATE1, MESSAGE_STATE4, MESSAGE_STATE5 -> {
                        transitionTo(msg)
                    }
                    MESSAGE_STATE2 ->{
                        if(data == MESSAGE_STATE5){
                            transitionTo(msg)
                        }else if(data == MESSAGE_STATE2){
                            //ignore
                        }else{
                            deferMessage(msg)
                        }
                    }
                    MESSAGE_STATE3->{
                        if(data != MESSAGE_STATE3){
                            sendMessageAtFrontOfQueue(msg.what)
                            transitionTo(msg)
                        }
                    }
                    else -> {
                        return NOT_HANDLED
                    }
                }
                return HANDLED
            }

We send message from MESSAGE_STATE1 to MESSAGE_STATE5 then quit

        val test = Test()
        for(i in 1..5){
            test.sendMessage(i)
        }
        test.quit()

The whole progress is:

enter state1 // initial
//send message start
state1 process MESSAGE_STATE1
exit state1
enter state1
state1 process MESSAGE_STATE2
state1 deffer MESSAGE_STATE2
state1 process MESSAGE_STATE3
state1 send MESSAGE_STATE3
enter state3
state3 process MESSAGE_STATE2
state3 deffer MESSAGE_STATE2
state3 process MESSAGE_STATE3
state3 process MESSAGE_STATE4
enter state4
state4 process MESSAGE_STATE2
state4 deffer MESSAGE_STATE2
state4 process MESSAGE_STATE5
exit state4
enter state5
state5 process MESSAGE_STATE2
exit state5
exit state3
enter state2
exit state2
exit state1
enter QuitingState

libstatemachine's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.