fsm

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2023 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package fsm provides UserStateMachine for a Questionnaire.

Index

Constants

View Source
const (
	DefaultMetricCounterLabel = "counter"
	Name                      = "fsm"
	Type                      = "FiniteStateMachine"
)

Type is the type of the entity regarding the framework. It is used to for example, to identify the entity in the logs, metrics, and for tracing.

Variables

This section is empty.

Functions

func Forward added in v0.0.3

func Forward[T shared.N](ctx context.Context, fsm *FiniteStateMachine, opt option.Option[T]) error

Forward the current question with the given option.

Types

type Callback added in v0.0.3

type Callback func(e event.Event, journal []event.Event)

Callback is a function that is called every time the state of the questionnaire changes.

type FiniteStateMachine

type FiniteStateMachine struct {
	// CurrentQuestion is the current question.
	CurrentQuestion question.Question `json:"currentQuestion" bson:"currentQuestion"`

	// CurrentQuestionID is the ID of the current question.
	CurrentQuestionID string `json:"currentQuestionID" bson:"currentQuestionID"`

	// CurrentQuestionIndex is the current question index.
	CurrentQuestionIndex int `json:"currentQuestionIndex" bson:"currentQuestionIndex"`

	// PreviousQuestionID is the ID of the previous question.
	PreviousQuestionID string `json:"previousQuestionID" bson:"previousQuestionID"`

	// TotalAnswers is the total number of answers.
	TotalAnswers int `json:"totalAnswers" bson:"totalAnswers"`

	// TotalQuestions is the total number of questions.
	TotalQuestions int `json:"totalQuestions" bson:"totalQuestions"`

	// Journal is the list of events.
	Journal []event.Event `json:"journal" bson:"journal"`

	// Logger.
	Logger sypl.ISypl `json:"-" bson:"-" validate:"required"`

	// Answers is the list of answers.
	Answers *safeorderedmap.SafeOrderedMap[answer.Answer] `json:"answers" bson:"answers"`

	// Questionnaire is the questionnaire.
	Questionnaire questionnaire.Questionnaire `json:"Questionnaire" bson:"Questionnaire" validate:"required"`

	// PreviousQuestion is the previous question.
	PreviousQuestion question.Question `json:"previousQuestion" bson:"previousQuestion"`

	// CurrentAnswer is the current answer.
	CurrentAnswer answer.Answer `json:"currentAnswer" bson:"currentAnswer"`

	// State is the current state of the questionnaire.
	State status.Status `json:"state" validate:"required" bson:"state"`

	// UserID is the ID of the user.
	UserID string `json:"userID" validate:"required" bson:"userID"`
	// contains filtered or unexported fields
}

FiniteStateMachine is the Finite State Machine for the Questionnaire.

func Load added in v0.0.3

Load the FSM up to state of the event.

func New

New creates a new machine status.

TODO: Also supports Channel.

func (*FiniteStateMachine) AddToJournal

func (fsm *FiniteStateMachine) AddToJournal(e event.Event)

AddToJournal adds an entry to the journal.

func (*FiniteStateMachine) Backward

func (fsm *FiniteStateMachine) Backward() *FiniteStateMachine

Backward goes back to the previous question.

func (*FiniteStateMachine) Done

Done the FSM setting the state to `Done`.

func (*FiniteStateMachine) Dump

func (fsm *FiniteStateMachine) Dump() event.Event

Dump returns the current state of the machine.

func (*FiniteStateMachine) Emit

func (fsm *FiniteStateMachine) Emit(prevQst, currentQst question.Question) event.Event

Emit emits the state of the machine, also returning it. Do whatever you want with it :) (e.g. persist it). It includes the a dump of the journal, so you can use it to restore the state of the machine.

func (*FiniteStateMachine) GetJournal

func (fsm *FiniteStateMachine) GetJournal() []event.Event

GetJournal returns the journal.

func (*FiniteStateMachine) GetLogger

func (fsm *FiniteStateMachine) GetLogger() sypl.ISypl

GetLogger returns the logger.

func (*FiniteStateMachine) GetState

func (fsm *FiniteStateMachine) GetState() status.Status

GetState returns the current state of the questionnaire.

func (*FiniteStateMachine) Jump

Jump to the specified question.

func (*FiniteStateMachine) SetCallback added in v0.0.3

func (fsm *FiniteStateMachine) SetCallback(cb Callback)

SetCallback sets the callback to be called when the state of the machine changes.

func (*FiniteStateMachine) Start

func (fsm *FiniteStateMachine) Start() *FiniteStateMachine

Start the state machine.

Jump to

Keyboard shortcuts

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