Documentation
¶
Overview ¶
The dfa package implements a deterministic finite automata to define stateful computations that are easier understood when transitions are specified explicitly. The API is more interested in using the DFA to clearly define stateful computation, rather than actually being used to recognize languages.
Importing
import "github.com/lytics/dfa"
Example
Starting = dfa.State("starting") Finishing = dfa.State("finishing") Done = dfa.Letter("done") Repeat = dfa.Letter("repeat") var errors []error starting := func() dfa.Letter { if err := do(); err != nil { errors = append(errors, err) return Repeat } else { return Done } } finishing := func() { fmt.Println("all finished") } d := dfa.New() d.SetStartState(Starting) d.SetTerminalStates(Finishing) d.SetTransition(Starting, Done, Finishing, finishing) d.SetTransition(Starting, Repeat, Starting, starting) final, accepted := d.Run(starting) ... for _, err := range errors { ... }
Index ¶
- type DFA
- func (m *DFA) Alphabet() []Letter
- func (m *DFA) GraphViz() string
- func (m *DFA) Run(init interface{}) (State, bool)
- func (m *DFA) RunSynchronous(init interface{}) (State, bool)
- func (m *DFA) SetStartState(q0 State)
- func (m *DFA) SetTerminalStates(f ...State)
- func (m *DFA) SetTransition(from State, input Letter, to State, exec interface{})
- func (m *DFA) SetTransitionLogger(logger func(State))
- func (m *DFA) States() []State
- func (m *DFA) Stop()
- type Letter
- type State
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DFA ¶
type DFA struct {
// contains filtered or unexported fields
}
func (*DFA) GraphViz ¶
GraphViz representation string which can be copy-n-pasted into any online tool like http://graphs.grevian.org/graph to get a diagram of the DFA.
func (*DFA) Run ¶
Run the DFA, blocking until Stop is called or the DFA enters a terminal state. Returns the last state and true if the last state was a terminal state.
func (*DFA) RunSynchronous ¶
RunSynchronous runs the DFA without a cancellable goroutine
func (*DFA) SetStartState ¶
SetStartState, there can be only one.
func (*DFA) SetTerminalStates ¶
SetTerminalStates, there can be more than one. Once entered the DFA will stop.
func (*DFA) SetTransition ¶
SetTransition, argument 'exec' must be a function that will supply the next letter if the 'to' state is non-terminal.