FSM

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 19, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DebugPrintTray

func DebugPrintTray[T any](tray ut.Trayer[T]) []string

DebugPrintTray is a helper function that prints the contents of a tray.

Parameters:

  • tray: The tray to print.

Returns:

  • []string: A slice of strings that represent the contents of the tray.

Types

type ActiveFSM

type ActiveFSM[I any, S any, E uc.Enumer] struct {

	// Tray is the tray that the FSM uses to store data.
	Tray ut.Trayer[I]
	// contains filtered or unexported fields
}

ActiveFSM is a struct that represents an active finite state machine.

func (*ActiveFSM[I, S, E]) GetState

func (a *ActiveFSM[I, S, E]) GetState() S

GetState returns the current state of the FSM.

Returns:

  • S: The current state of the FSM.

func (*ActiveFSM[I, S, E]) GetValue

func (a *ActiveFSM[I, S, E]) GetValue(key E) (any, bool)

GetValue returns the value associated with key in the FSM.

Parameters:

  • key: The key of the value to retrieve.

Returns:

  • any: The value associated with key.
  • bool: A boolean indicating whether the value was found.

type DetFunc

type DetFunc[I any, S any, E uc.Enumer] func(fsm *ActiveFSM[I, S, E]) (any, error)

DetFunc is a function that determines the value of an element of the FSM.

Parameters:

  • fsm: The active FSM.

Returns:

  • any: The value of the element.
  • error: An error if the function fails.

type EndCond

type EndCond[I any, S any, E uc.Enumer] func(fsm *ActiveFSM[I, S, E]) bool

EndCond is a function that determines whether the FSM should end.

Parameters:

  • fsm: The active FSM.

Returns:

  • bool: A boolean indicating whether the FSM should end.

type EvalFunc

type EvalFunc[I any, S any, R any, E uc.Enumer] func(fsm *ActiveFSM[I, S, E]) (R, error)

EvalFunc is a function that evaluates the FSM.

Parameters:

  • fsm: The active FSM.

Returns:

  • any: The result of the evaluation.
  • error: An error if the function fails.

type FSM

type FSM[I any, S any, R any, E uc.Enumer] struct {
	// InitFn is the function that initializes the FSM.
	InitFn InitFunc[I, S]

	// ShouldEndFn is the function that determines whether the FSM should end.
	ShouldEndFn EndCond[I, S, E]

	// GetResFn is the function that retrieves the result of the FSM.
	GetResFn EvalFunc[I, S, R, E]

	// NextFn is the function that transitions the FSM to the next state.
	NextFn TransFunc[I, S, E]
	// contains filtered or unexported fields
}

FSM is a struct that represents a finite state machine.

func (*FSM[I, S, R, E]) Run

func (fsm *FSM[I, S, R, E]) Run(inputStream ut.Trayable[I]) ([]R, error)

Run runs the FSM.

Parameters:

  • inputStream: The input stream for the FSM.

Returns:

  • []R: A slice of results from the FSM.
  • error: An error if the function fails.

type FsmBuilder

type FsmBuilder[I any, S any, R any, E uc.Enumer] struct {
	// InitFn is the function that initializes the FSM.
	InitFn InitFunc[I, S]

	// ShouldEndFn is the function that determines whether the FSM should end.
	ShouldEndFn EndCond[I, S, E]

	// GetResFn is the function that retrieves the result of the FSM.
	GetResFn EvalFunc[I, S, R, E]

	// NextFn is the function that transitions the FSM to the next state.
	NextFn TransFunc[I, S, E]
	// contains filtered or unexported fields
}

FsmBuilder is a struct that represents a builder for a finite state machine.

func (*FsmBuilder[I, S, R, E]) AddDetFn

func (b *FsmBuilder[I, S, R, E]) AddDetFn(elem E, fn DetFunc[I, S, E])

AddDetFn adds a function that is executed before the FSM transitions to the next state.

Parameters:

  • elem: The element that the function determines the value of.
  • fn: The function that determines the value of the element.

func (*FsmBuilder[I, S, R, E]) Build

func (b *FsmBuilder[I, S, R, E]) Build() (*FSM[I, S, R, E], error)

Build creates a new FSM from the builder.

Returns:

  • *FSM: A pointer to the newly created FSM.
  • error: An error if the function fails.

type InitFunc

type InitFunc[I any, S any] func(tray ut.Trayer[I]) (S, error)

InitFunc is a function that initializes the FSM.

Parameters:

  • tray: The tray that the FSM uses to store data.

Returns:

  • S: The initial state of the FSM.
  • error: An error if the function fails.

type TransFunc

type TransFunc[I any, S any, E uc.Enumer] func(fsm *ActiveFSM[I, S, E]) (S, error)

TransFunc is a function that transitions the FSM to the next state.

Parameters:

  • fsm: The active FSM.

Returns:

  • S: The next state of the FSM.
  • error: An error if the function fails.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL