Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

Traffic Light Example

A simple finite state machine (FSM) demonstrating the basics of Statekit.

Features Demonstrated

  • Basic state transitions
  • Entry actions
  • Context management
  • Event handling

State Diagram

   ┌─────────┐
   │  green  │◄──────────────┐
   └────┬────┘               │
        │ TIMER              │
        ▼                    │
   ┌─────────┐               │
   │ yellow  │               │
   └────┬────┘               │
        │ TIMER              │ TIMER (incrementCycle)
        ▼                    │
   ┌─────────┐               │
   │   red   │───────────────┘
   └─────────┘

Usage

package main

import (
    "fmt"
    trafficlight "go.klarlabs.de/statekit/examples/traffic_light"
    "go.klarlabs.de/statekit"
)

func main() {
    machine, _ := trafficlight.NewTrafficLight()
    interp := statekit.NewInterpreter(machine)
    interp.Start()

    fmt.Println(interp.State().Value) // "green"

    interp.Send(statekit.Event{Type: "TIMER"})
    fmt.Println(interp.State().Value) // "yellow"

    interp.Send(statekit.Event{Type: "TIMER"})
    fmt.Println(interp.State().Value) // "red"

    interp.Send(statekit.Event{Type: "TIMER"})
    fmt.Println(interp.State().Value) // "green"

    ctx := interp.State().Context
    fmt.Println(ctx.CycleCount) // 1
}

Run Tests

go test -v ./examples/traffic_light/...

Key Concepts

Context

The Context struct tracks state across transitions:

type Context struct {
    CycleCount int    // Tracks complete cycles
    Log        []string // Entry action logs
}

Entry Actions

Each state logs its entry:

WithAction("logGreen", func(ctx *Context, e statekit.Event) {
    ctx.Log = append(ctx.Log, "Entered GREEN")
})

Transition Actions

The incrementCycle action fires when transitioning from red back to green:

State(StateRed).
    On(EventTimer).Target(StateGreen).Do("incrementCycle")