github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/graph/node.go (about) 1 package graph 2 3 import ( 4 "github.com/15mga/kiwi/ds" 5 "github.com/15mga/kiwi/util" 6 ) 7 8 type INode interface { 9 IGraphElem 10 HasIn(name string) bool 11 HasOut(name string) bool 12 AddIn(t TPoint, name string) *util.Err 13 AddOut(t TPoint, name string) *util.Err 14 GetIn(name string) (IIn, *util.Err) 15 GetOut(name string) (IOut, *util.Err) 16 Out(name string, m any) *util.Err 17 ProcessData(msg IMsg) *util.Err 18 BindFn(point string, fn MsgToErr) 19 } 20 21 func NewNode(g IGraph, name string) *node { 22 n := &node{ 23 IGraphElem: newGraphElem(g, name), 24 typeToIn: make(map[string]IIn), 25 typeToOut: make(map[string]IOut), 26 processor: make(map[string]*ds.FnErrLink1[IMsg]), 27 } 28 return n 29 } 30 31 type node struct { 32 IGraphElem 33 typeToIn map[string]IIn 34 typeToOut map[string]IOut 35 processor map[string]*ds.FnErrLink1[IMsg] 36 } 37 38 func (n *node) HasIn(name string) bool { 39 _, ok := n.typeToIn[name] 40 return ok 41 } 42 43 func (n *node) HasOut(name string) bool { 44 _, ok := n.typeToOut[name] 45 return ok 46 } 47 48 func (n *node) AddIn(t TPoint, name string) *util.Err { 49 if _, ok := n.typeToIn[name]; ok { 50 return util.NewErr(util.EcExist, util.M{ 51 "error": "exist in node", 52 "path": n.Path(), 53 "name": name, 54 }) 55 } 56 in := newPIn(n, t, name) 57 n.typeToIn[name] = in 58 plugin := n.Graph().Plugin() 59 if plugin != nil { 60 plugin.OnAddIn(in) 61 } 62 return nil 63 } 64 65 func (n *node) AddOut(t TPoint, name string) *util.Err { 66 if _, ok := n.typeToOut[name]; ok { 67 return util.NewErr(util.EcExist, util.M{ 68 "error": "exist out node", 69 "path": n.Path(), 70 "name": name, 71 }) 72 } 73 out := newOut(n, t, name) 74 n.typeToOut[name] = out 75 plugin := n.Graph().Plugin() 76 if plugin != nil { 77 plugin.OnAddOut(out) 78 } 79 return nil 80 } 81 82 func (n *node) GetIn(name string) (IIn, *util.Err) { 83 in, ok := n.typeToIn[name] 84 if !ok { 85 return nil, util.NewErr(util.EcNotExist, util.M{ 86 "path": n.Path(), 87 "out node": name, 88 }) 89 } 90 return in, nil 91 } 92 93 func (n *node) GetOut(name string) (IOut, *util.Err) { 94 out, ok := n.typeToOut[name] 95 if !ok { 96 return nil, util.NewErr(util.EcNotExist, util.M{ 97 "path": n.Path(), 98 "out node": name, 99 }) 100 } 101 return out, nil 102 } 103 104 func (n *node) Out(name string, data any) *util.Err { 105 out, ok := n.typeToOut[name] 106 if !ok { 107 return util.NewErr(util.EcNotExist, util.M{ 108 "path": n.Path(), 109 "out node": name, 110 }) 111 } 112 return out.Send(&Msg{ 113 typ: out.Type(), 114 outNode: n.Name(), 115 outPoint: name, 116 data: data, 117 }) 118 } 119 120 func (n *node) ProcessData(msg IMsg) *util.Err { 121 if !n.Enabled() { 122 return nil 123 } 124 p, ok := n.processor[msg.InPoint()] 125 if !ok { 126 return util.NewErr(util.EcNotExist, util.M{ 127 "path": n.Path(), 128 "processor": msg.InPoint(), 129 }) 130 } 131 msg.SetInNode(n) 132 err := p.Invoke(msg) 133 if err != nil { 134 err.AddParam("node", n.Path()) 135 return err 136 } 137 return nil 138 } 139 140 func (n *node) BindFn(point string, fn MsgToErr) { 141 lnk, ok := n.processor[point] 142 if !ok { 143 lnk = ds.NewFnErrLink1[IMsg]() 144 n.processor[point] = lnk 145 } 146 lnk.Push(fn) 147 }