Upgrade to Pro — share decks privately, control downloads, hide ads and more …

FSM DSL in Kotlin

FSM DSL in Kotlin

Tomoya Miwa

June 27, 2018
Tweet

More Decks by Tomoya Miwa

Other Decks in Programming

Transcript

  1. DSL Example val sm = stateMachine(initial = MyState.NotLoaned) { state(MyState.NotLoaned)

    { edge(MyEvent.PressRental, next = MyState.Lock) } state(MyState.OnLoan, entry = { println("turnOnRentalLed") }, exit = { println("turnOffRentalLed") }) { state(MyState.Lock) { edge(MyEvent.PressReturn, next = MyState.NotLoaned) edge(MyEvent.PressUnLock, next = MyState.UnLock) } state(MyState.UnLock) { edge(MyEvent.PressLock, next = MyState.Lock) } } }
  2. DSL internal fun stateMachine( initial: BaseState, init: StateMachine.() -> Unit

    ): StateMachine = StateMachine(initial = initial) .apply(init)
  3. StateMachine.Builder fun stateMachine( initial: BaseState, init: StateMachine.Builder.() -> Unit ):

    StateMachine = StateMachine.Builder(initial = initial) .apply(init).build()
  4. Generate transition map in build() state event actions next NotLoaned

    PressRental [OnLoan.entry] Lock Lock PressReturn [OnLoan.exit] NotLoaned Lock PressUnLock [] UnLock UnLock PressLock [] Lock
  5. Any Idea? Tree search algorithm when creating transition map, if

    you have any good idea, please let me know!