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
  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
  3. 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
  4. State Event Next State Action LOCKED COIN UNLOCKED unlock PASS

    alarm UNLOCKED COIN return-coin PASS LOCKED lock Transition table for Turnstile
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. FSM other features • Guard Expressions • Entry / Exit

    Actions • Event Parameters • Automatic transitions • Named State Maps • Push / Pop transitions • Typed Parameters • Typed Return values
  12. 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?