Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

What is a state machine?

Slide 4

Slide 4 text

Examples – Packet Reader

Slide 5

Slide 5 text

Examples – Quotation Manager

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

UML State Diagram

Slide 8

Slide 8 text

Finite State Machines - Turnstile

Slide 9

Slide 9 text

Finite State Machines – Double Lock

Slide 10

Slide 10 text

Finite State Machines – Secure Turnstile

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Finite State Machines – Why and How?

Slide 13

Slide 13 text

Why and How? (cont.)

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

SCXML - Turnstile

Slide 16

Slide 16 text

SMC - Turnstile

Slide 17

Slide 17 text

Ideal DSL

Slide 18

Slide 18 text

State Machine Model

Slide 19

Slide 19 text

State Machine Sequence Diagram

Slide 20

Slide 20 text

Context Class

Slide 21

Slide 21 text

States and Events

Slide 22

Slide 22 text

State machine definition

Slide 23

Slide 23 text

Creating a DSL – Lambda with argument

Slide 24

Slide 24 text

Creating a DSL – Lambda as member of class

Slide 25

Slide 25 text

Creating a DSL – Builder and Handler

Slide 26

Slide 26 text

Creating a DSL – Using

Slide 27

Slide 27 text

Definition, instance and context

Slide 28

Slide 28 text

Definition, instance and context (cont.)

Slide 29

Slide 29 text

Definition, instance and context (cont.)

Slide 30

Slide 30 text

Definition, instance and context (cont.)

Slide 31

Slide 31 text

Turnstile – Sample invocation

Slide 32

Slide 32 text

Turnstile – Sequence Diagram

Slide 33

Slide 33 text

Output

Slide 34

Slide 34 text

Performance

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

FSM other features ● Guard Expressions ● Entry / Exit Actions ● Event Parameters ● Automatic transitions ● Named State Maps ● Push / Pop transitions ● Typed Parameters ● Typed Return values

Slide 41

Slide 41 text

FSM additions… ● Timeouts / Timers ● Coroutines ● Sealed Classes?

Slide 42

Slide 42 text

Useless Box – State diagram

Slide 43

Slide 43 text

AI Assistive technology for improved UX

Slide 44

Slide 44 text

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?