Documentation
¶
Overview ¶
Package cfsm is a library for working with Communicating Finite State Machines.
Index ¶
Examples ¶
Constants ¶
const (
// FreeStateID is the ID used to identify a State unattached to any CFSM.
FreeStateID = -1
)
Variables ¶
var ( // ErrStateUndef is an error when referencing a state that does not exist. ErrStateUndef = errors.New("Undefined state") // ErrStateAlias is an error when reusing a non-free CFSM state in a // different CFSM (aliasing). ErrStateAlias = errors.New("State is already attached to a CFSM") )
Functions ¶
This section is empty.
Types ¶
type CFSM ¶
type CFSM struct { ID int // Unique identifier. Start *State // Starting state of the CFSM. Comment string // Comments on the CFSM. // contains filtered or unexported fields }
CFSM is a single Communicating Finite State Machine.
func (*CFSM) NewFreeState ¶
NewFreeState creates a new free State for this CFSM.
type Recv ¶
type Recv struct {
// contains filtered or unexported fields
}
Recv is a receive transition (input).
type Send ¶
type Send struct {
// contains filtered or unexported fields
}
Send is a send transition (output).
type State ¶
type State struct { ID int // Unique identifier. Label string // Free form text label. // contains filtered or unexported fields }
State is a state.
func (*State) AddTransition ¶
func (s *State) AddTransition(t Transition)
AddTransition adds a transition to the current State.
func (*State) Transitions ¶
func (s *State) Transitions() []Transition
Transitions returns a list of transitions.
type System ¶
type System struct { sync.Mutex CFSMs []*CFSM // Individual CFSMs in the communicating system. Comment string // Comments on the System. }
System is a set of CFSMs.
Example ¶
To use the CFSM library, first create a system of CFSMs. From the system, create machines (i.e. CFSMs) for the system. Add states to the machines, and attach transitions to the states. Finally set initial state of each machine.
// Create a new system of CFSMs. sys := NewSystem() alice := sys.NewMachine() // CFSM Alice alice.Comment = "Alice" bob := sys.NewMachine() // CFSM Bob bob.Comment = "Bob" a0 := alice.NewState() a1 := alice.NewState() a01 := NewSend(bob, "int") a01.SetNext(a1) a0.AddTransition(a01) // Add a transition from a0 --> a1. b0 := bob.NewState() b1 := bob.NewState() b01 := NewRecv(alice, "int") b01.SetNext(b1) b0.AddTransition(a01) // Add a transition from b0 --> b1. // Set initial states of alice and bob. alice.Start = a0 bob.Start = b0
Output:
func (*System) NewMachine ¶
NewMachine creates a new CFSM in the communicating system and returns it.
func (*System) RemoveMachine ¶
RemoveMachine removes a CFSM with the given id from System.
type Transition ¶
type Transition interface { Label() string // Label is the marking on the transition. State() *State // State after transition. }
Transition is a transition from a State to another State.