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

KFSM - A Kotlin DSL for Finite State machines

KFSM - A Kotlin DSL for Finite State machines

The presentation was first given at I Code Java - Johannesburg 2019 and more recently at KotlinEveryWhere SA.
During the presentation, I describe the need and use of finite state machines and how to create a DSL in Kotlin for configuring an FSM.
The presentation ends with a performance comparison of executing a sample program on various Kotlin runtime options.

Corneil du Plessis

July 26, 2019
Tweet

More Decks by Corneil du Plessis

Other Decks in Programming

Transcript

  1. A Kotlin DSL for finite state
    machines - A Journey
    Corneil du Plessis
    Chief Architect, JumpCO

    View full-size slide

  2. Quotable quotes
    K.I.S.S. – Keep it simple stupid!
    Graeme Mitchell
    “Everything should be a simple as possible,
    but no simpler”
    Albert Einstein

    View full-size slide

  3. What is a state machine?

    View full-size slide

  4. Examples – Packet Reader

    View full-size slide

  5. Examples – Quotation Manager

    View full-size slide

  6. A brief history of state machines

    FSM/FSA (Finite state machines / automa)

    DFA/NFA/NDFA
    (Non-deterministic state machine / automa)

    State tables / charts / maps

    Mealy / Moore machines
    (State + Input) vs (State)

    UML state machines

    SDL state machines

    View full-size slide

  7. UML State Diagram

    View full-size slide

  8. Finite State Machines - Turnstile

    View full-size slide

  9. Finite State Machines – Double Lock

    View full-size slide

  10. Finite State Machines – Secure
    Turnstile

    View full-size slide

  11. State Event Next State Action
    LOCKED
    COIN UNLOCKED unlock
    PASS alarm
    UNLOCKED
    COIN return-coin
    PASS LOCKED lock
    Transition table for Turnstile

    View full-size slide

  12. Finite State Machines – Why and How?

    View full-size slide

  13. Why and How? (cont.)

    View full-size slide

  14. FSM Patterns

    Generate code from State Map - SMC
    Class per State, method per Event
    OR
    Huge switch statement

    Executable XML – SCXML

    Language Builder – Spring Statemachine

    View full-size slide

  15. SCXML - Turnstile

    View full-size slide

  16. SMC -
    Turnstile

    View full-size slide

  17. State Machine Model

    View full-size slide

  18. State
    Machine
    Sequence
    Diagram

    View full-size slide

  19. Context
    Class

    View full-size slide

  20. States
    and
    Events

    View full-size slide

  21. State machine definition

    View full-size slide

  22. Creating a DSL – Lambda with argument

    View full-size slide

  23. Creating a DSL – Lambda as member of class

    View full-size slide

  24. Creating a DSL – Builder and Handler

    View full-size slide

  25. Creating a DSL – Using

    View full-size slide

  26. Definition, instance and context

    View full-size slide

  27. Definition, instance and context (cont.)

    View full-size slide

  28. Definition, instance and context (cont.)

    View full-size slide

  29. Definition, instance and context (cont.)

    View full-size slide

  30. Turnstile –
    Sample
    invocation

    View full-size slide

  31. Turnstile –
    Sequence
    Diagram

    View full-size slide

  32. Performance – Node.js & Kotlin Native
    3270
    4186
    2777
    515
    0 500 1000 1500 2000 2500 3000 3500 4000 4500
    Node.js v10
    Node.js v12
    Kotlin Native Release
    Kotlin Native Debug
    Transitions/ms

    View full-size slide

  33. Performance – Graal Native
    3270
    4186
    11477
    2777
    515
    0 2000 4000 6000 8000 10000 12000 14000
    Node.js v10
    Node.js v12
    Graal Native
    Kotlin Native Release
    Kotlin Native Debug
    Transitions/ms

    View full-size slide

  34. Performance - JVM
    3270
    4186
    273
    25456
    20856
    22163
    24567
    39614
    11477
    2777
    515
    0 5000 10000 15000 20000 25000 30000 35000 40000 45000
    Node.js v10
    Node.js v12
    JDK 8 Int
    JDK 8
    JDK 11
    JDK 12
    JDK 13
    Graal 19.2.0.1
    Graal Native
    Kotlin Native Release
    Kotlin Native Debug
    Transitions/ms

    View full-size slide

  35. Performance – Startup overhead
    290
    180
    180
    160
    180
    140
    130
    180
    10
    10
    10
    0 50 100 150 200 250 300 350
    Node.js v10
    Node.js v12
    JDK 8 Int
    JDK 8
    JDK 11
    JDK 12
    JDK 13
    Graal 19.2.0.1
    Graal Native
    Kotlin Native Release
    Kotlin Native Debug
    Overhead in ms

    View full-size slide

  36. Performance - Memory
    42096
    43528
    30724
    36992
    58208
    58804
    43400
    85344
    6744
    12036
    2600
    0 10000 20000 30000 40000 50000 60000 70000 80000 90000
    Node.js v10
    Node.js v12
    JDK 8 Int
    JDK 8
    JDK 11
    JDK 12
    JDK 13
    Graal 19.2.0.1
    Graal Native
    Kotlin Native Release
    Kotlin Native Debug
    Memory in Kb

    View full-size slide

  37. FSM other features

    Guard Expressions

    Entry / Exit Actions

    Event Parameters

    Automatic transitions

    Named State Maps

    Push / Pop transitions

    Typed Parameters

    Typed Return values

    View full-size slide

  38. FSM additions…

    Timeouts / Timers

    Coroutines

    Sealed Classes?

    View full-size slide

  39. Useless Box – State diagram

    View full-size slide

  40. AI Assistive technology for improved UX

    View full-size slide

  41. Resources

    Twitter - @corneil

    Slides - https://speakerdeck.com/corneil/kfsm-a-kotlin-dsl-for-
    finite-state-machines

    Project Source - https://github.com/open-jumpco/kfsm

    Samples - https://github.com/open-jumpco/kfsm-samples

    SMC - http://smc.sourceforge.net/

    Spring Statemachine - https://projects.spring.io/spring-
    statemachine/
    Questions?

    View full-size slide