Controllers - craterdog/go-component-framework GitHub Wiki

Overview

A controller is an agent that implements a state machine that can then be used to control the processing of events.

State Machine

This example shows a state machine consisting of three (valid) states with three event types, showing the possible state transitions caused by each event.

UML Diagram

A Quick Example

To whet your appetite, here is some short example code that demonstrates the use of controller agents.

package main

import (
	fmt "fmt"
	fra "github.com/craterdog/go-component-framework/v7"
)

const (
	invalid fra.State = iota
	available
	processing
	completed
)

const (
	none fra.Event = iota
	message
	result
	reset
)

func main() {
	// Define the set of possible events.
	var events = []fra.Event{message, result, reset}

	// Define the possible state transitions.
	var transitions = map[fra.State]fra.Transitions{
		available:  fra.Transitions{processing, invalid, available},
		processing: fra.Transitions{invalid, completed, available},
		completed:  fra.Transitions{invalid, invalid, available},
	}

	// Create a new controller using the events and transitions.
	var controller = fra.Controller(events, transitions)

	// Process various events.
	fmt.Println("Initial state:", controller.GetState())
	fmt.Println("Processing 'message' event transitions to state:", controller.ProcessEvent(message))
	fmt.Println("Processing 'result' event transitions to state:", controller.ProcessEvent(result))
	fmt.Println("Processing 'reset' event transitions to state:", controller.ProcessEvent(reset))
}

Try it out in the Go Playground...