-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
67 lines (57 loc) · 1.14 KB
/
node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import "fmt"
type NodeState int
const (
Off = iota
On
Undefined
)
func (n NodeState) String() string {
switch n {
case Off:
return "off"
case On:
return "on"
case Undefined:
return "undefined"
default:
return "unknown"
}
}
// TODO: abstract rendering part
type Node struct {
ID string
State NodeState
connections []*Node
}
// TODO: determine ID automatically
func NewNode(id string) *Node {
return &Node{
ID: id,
State: Undefined,
connections: []*Node{},
}
}
func (n *Node) Change(newState NodeState) error {
if n.State != Undefined && n.State != newState {
return fmt.Errorf("conflicting values for node %s", n.ID)
}
n.State = newState
for _, node := range n.connections {
if node.State == newState {
continue
}
node.Change(newState)
}
return nil
}
func (n *Node) Debug() string {
return fmt.Sprintf("%s=<state: %s>", n.ID, n.State)
}
func (n *Node) Connect(n1 *Node) *Node {
n.connections = append(n.connections, n1)
n1.connections = append(n1.connections, n)
return n
}
var SharedSourceNode = NewNode("SharedSource")
var SharedGroundNode = NewNode("SharedGround")