fsm

package module
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: May 23, 2018 License: MIT Imports: 2 Imported by: 17

README

FSM

FSM

Build Status codecov Go Report Card Gitter

This package contains a simple interface for a finite-state machine in Go.

What's a Finite-state Machine?

[A finite-state machine] is an abstract machine that can be in exactly one of a finite number of states at any given time. The FSM can change from one state to another in response to some external inputs; the change from one state to another is called a transition. An FSM is defined by a list of its states, its initial state, and the conditions for each transition.

Wikipedia

The FSM library was built as purely against the definition of a formal finite-state machine as possible, so the Wikipedia definition holds true for this library.

Why Use a Finite-state Machine?

FSM was specifically made to build light-weight / generic conversational interfaces (think chatbots), so if that's what your trying to do you'll find this library quite nice.

Building a conversational interface as a finite-state machine will reduce a ton of cognitive overhead, as at any given point you only have to concern yourself with the current step in the conversation.

What is most attractive about FSM is the fact that you can build robust conversational interfaces that run on any platform with a single codebase.

Getting Started

If you're looking to build a chatbot, check out the getting started repository.

This README is more high-level and won't get into the details you'll want to start building.

Supported Platforms

Currently there is support for the following platforms:

Roadmap

We're soon planning to support the following platforms.

These targets libraries are relatively easy to build, so when the next hot platform comes out, your conversational interface can be easily adapted to it!

License

MIT

Documentation

Index

Constants

View Source
const StartState = "start"

StartState is a constant for defining the slug of the start state for all StateMachines.

Variables

This section is empty.

Functions

func CleanInput

func CleanInput(input string) string

CleanInput converts the input string to only the following: - Lowercase Letters (a-z) - Numbers (0-9) - Spaces ( )

Uppercase letters are converted to lowercase letters, but any character outside of what is noted above is stripped from the string. Double (or more) spaces are converted into a single space.

func Step

func Step(platform, uuid string, input interface{}, InputTransformer InputTransformer, store Store, emitter Emitter, stateMap StateMap)

Step performs a single step through a StateMachine.

This function handles the nuance of the logic for a single step through a state machine. ALL fsm-target's should call Step directly, and not attempt to handle the process of stepping through the StateMachine, so all platforms function with the same logic.

Types

type BuildState

type BuildState func(Emitter, Traverser) *State

BuildState is a function that generates a State with access to a specific Emitter and Traverser

type Emitter

type Emitter interface {
	Emit(interface{}) error
}

Emitter is a generic interface to output arbitrary data. Emit is generally called from State.EntryAction.

type InputTransformer

type InputTransformer func(input interface{}, validIntents []*Intent) (*Intent, map[string]string)

InputTransformer converts the input of a platform to an *Intent.

type Intent

type Intent struct {
	Slug          string
	PlatformSlugs map[string]string
	Slots         map[string]*Type
	Utterances    []string
}

Intent is an event that occurs that can trigger a transition

func TextInputTransformer

func TextInputTransformer(input interface{}, validIntents []*Intent) (*Intent, map[string]string)

TextInputTransformer is an implementation of an InputTransformer which handles text as the input type.

func (*Intent) Parse

func (intent *Intent) Parse(input string) (bool, map[string]string)

Parse checks if an input string matches this intent. If the input string matches this intent, any parameters are also returned.

type State

type State struct {
	Slug         string
	Entry        func(isReentry bool) error
	ValidIntents func() []*Intent
	Transition   func(*Intent, map[string]string) *State
}

State represents an individual state in a larger state machine

type StateMachine

type StateMachine []BuildState

StateMachine is an array of all BuildState functions

type StateMap added in v0.2.1

type StateMap map[string]BuildState

StateMap is a k:v map for all BuildState functions in a StateMachine. This is exclusively utilized by the internal workings of targets.

func GetStateMap

func GetStateMap(stateMachine StateMachine) StateMap

GetStateMap converts a StateMachine into a StateMap

type Store

type Store interface {
	FetchTraverser(uuid string) (Traverser, error)
	CreateTraverser(uuid string) (Traverser, error)
}

A Store is a generic interface responsible for managing The fetching and creation of traversers

type Traverser

type Traverser interface {
	// UUID
	UUID() string
	SetUUID(string)

	// Platform
	Platform() string
	SetPlatform(string)

	// State
	CurrentState() string
	SetCurrentState(string)

	// Data
	Upsert(key string, value interface{})
	Fetch(key string) interface{}
	Delete(key string)
}

A Traverser is an individual that is traversing the StateMachine. This interface that is responsible for managing the state of that individual

type Type

type Type struct {
	Slug          string
	PlatformSlugs map[string]string
	Options       []string
	IsValid       func(string) bool
}

Type is a definition of what a Intent slot value can be

Jump to

Keyboard shortcuts

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