This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
"strconv" | |
"gonum.org/v1/gonum/graph" | |
"gonum.org/v1/gonum/graph/multi" | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main() { | |
stateMachine := New() | |
initState := stateMachine.Init("locked") | |
unlockedSate := stateMachine.NewState("unlocked") | |
coinRule := NewRule(Operator("eq"), Event("coin")) | |
pushRule := NewRule(Operator("eq"), Event("push")) | |
stateMachine.LinkStates(initState, unlockedSate, coinRule) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (s *StateMachine) Compute(events []string, printState bool) State { | |
for _, e := range events { | |
s.FireEvent(Event(e)) | |
if printState { | |
fmt.Printf("%s\n", s.PresentState.String()) | |
} | |
} | |
return s.PresentState | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (s *StateMachine) FireEvent(e Event) error { | |
presentNode := s.PresentState | |
it := s.g.From(presentNode.Id) | |
for it.Next() { | |
n := s.g.Node(it.Node().ID()).(State) | |
line := graph.LinesOf(s.g.Lines(presentNode.Id, n.Id))[0].(Link) // There can be one defined path between two distinct states | |
for key, val := range line.Rules { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func NewRule(triggerConditionOperator Operator, comparisonValue Event) map[Operator]Event { | |
return map[Operator]Event{triggerConditionOperator: comparisonValue} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (s *StateMachine) Init(initStateValue interface{}) State { | |
s.PresentState = State{Id: int64(NodeIDCntr), Value: initStateValue} | |
s.g.AddNode(s.PresentState) | |
NodeIDCntr++ | |
return s.PresentState | |
} | |
func (s *StateMachine) NewState(stateValue interface{}) State { | |
state := State{Id: int64(NodeIDCntr), Value: stateValue} | |
s.g.AddNode(state) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func New() *StateMachine { | |
s := &StateMachine{} | |
s.g = multi.NewDirectedGraph() | |
return s | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Event string | |
type Operator string | |
var NodeIDCntr = 0 | |
var LineIdCntr = 1 | |
type StateMachine struct { | |
PresentState State | |
g *multi.DirectedGraph | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type State struct { | |
Id int64 | |
Value interface{} | |
} | |
type Link struct { | |
Id int64 | |
T, F graph.Node | |
Rules map[Operator]Event | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
"strconv" | |
"gonum.org/v1/gonum/graph" | |
"gonum.org/v1/gonum/graph/multi" | |
) |
NewerOlder