flow.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package flow
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "flow/registry"
  7. "fmt"
  8. "io"
  9. "os"
  10. "sync"
  11. )
  12. // Data interface
  13. type Data = interface{}
  14. // Flow structure
  15. // We could Create a single array of operations
  16. // refs would only mean id, types would be embed in operation
  17. type Flow struct {
  18. sync.Mutex
  19. registry *registry.R
  20. idGen func() string
  21. consts map[string]Data
  22. Data map[string]Data // Should be named, to fetch later
  23. operations sync.Map
  24. runID int
  25. // Experimental run Event
  26. hooks Hooks
  27. }
  28. // New create a new flow
  29. func New() *Flow {
  30. return &Flow{
  31. registry: registry.Global,
  32. idGen: func() string { return RandString(8) },
  33. // Data
  34. consts: map[string]Data{},
  35. Data: map[string]Data{},
  36. operations: sync.Map{},
  37. //map[string]opEntry{},
  38. }
  39. }
  40. //SetRegistry use the registry specified
  41. func (f *Flow) SetRegistry(r *registry.R) *Flow {
  42. f.registry = r
  43. // chain
  44. return f
  45. }
  46. //SetIDGen set the id generator that will generate
  47. //ID for new nodes
  48. func (f *Flow) SetIDGen(idGen func() string) {
  49. f.idGen = idGen
  50. }
  51. // Must Helper to return from operations
  52. func (f *Flow) Must(op Operation, err error) Operation {
  53. if err != nil {
  54. panic(err)
  55. }
  56. return op
  57. }
  58. // Auto ID generation
  59. // Op return an function operator
  60. // name - a previous registered function
  61. // params - the function inputs
  62. func (f *Flow) Op(name string, params ...interface{}) (Operation, error) {
  63. var op Operation
  64. var err error
  65. allocErr := f.allocID(func(id string) error {
  66. op, err = f.DefOp(id, name, params...)
  67. return err
  68. })
  69. if allocErr != nil {
  70. return nil, allocErr
  71. }
  72. return op, err
  73. }
  74. // ErrOp error operation with generated ID
  75. func (f *Flow) ErrOp(operr error) (Operation, error) {
  76. var op Operation
  77. var err error
  78. allocErr := f.allocID(func(id string) error {
  79. op, err = f.DefErrOp(id, operr)
  80. return err
  81. })
  82. if allocErr != nil {
  83. return nil, err
  84. }
  85. return op, err
  86. }
  87. // Const returns a const operation with generated ID
  88. func (f *Flow) Const(value Data) (Operation, error) {
  89. var op Operation
  90. var err error
  91. allocErr := f.allocID(func(id string) error {
  92. op, err = f.DefConst(id, value)
  93. return err
  94. })
  95. if allocErr != nil {
  96. return nil, allocErr
  97. }
  98. return op, err
  99. }
  100. // Var operation
  101. func (f *Flow) Var(name string, initial ...Data) Operation {
  102. var op Operation
  103. err := f.allocID(func(id string) error {
  104. op = f.DefVar(id, name, initial...)
  105. return nil
  106. })
  107. if err != nil {
  108. return nil
  109. }
  110. return op
  111. }
  112. // In input operation
  113. func (f *Flow) In(paramID int) (Operation, error) {
  114. var op Operation
  115. err := f.allocID(func(id string) error {
  116. op = f.DefIn(id, paramID)
  117. return nil
  118. })
  119. if err != nil {
  120. return nil, err
  121. }
  122. return op, nil
  123. }
  124. // Analyse every operations
  125. func (f *Flow) Analyse(w io.Writer, params ...Data) {
  126. if w == nil {
  127. w = os.Stdout
  128. }
  129. fmt.Fprintf(w, "Ops analysis:\n")
  130. f.operations.Range(func(pk, po interface{}) bool {
  131. k, op := pk.(string), po.(*operation)
  132. fw := bytes.NewBuffer(nil)
  133. //fmt.Fprintf(w, " [%s] (%v)", k, op.name)
  134. fmt.Fprintf(fw, " [%s] %s(", pk, op.name)
  135. for j, in := range op.inputs {
  136. //ref := in.(Op)
  137. if j != 0 {
  138. fmt.Fprintf(fw, ", ")
  139. }
  140. ires, err := in.Process(params...)
  141. if err != nil {
  142. fmt.Fprintf(w, "Operator: %s error#%s\n", op.name, err)
  143. return false
  144. }
  145. fmt.Fprintf(fw, " %s[%v](%v)", op.kind, op.id, ires)
  146. }
  147. fmt.Fprintf(fw, ") - ")
  148. // Create OpProcessor and execute
  149. //
  150. opfn := f.GetOp(k)
  151. res, err := opfn.Process(params...)
  152. if err != nil {
  153. fmt.Fprintf(fw, "ERR\n")
  154. }
  155. fmt.Fprintf(fw, "%v\n", res)
  156. fmt.Fprintf(w, "%s", fw.String())
  157. return true
  158. })
  159. }
  160. /////////////////////////////
  161. // Serializers inspectors
  162. //////////////////////
  163. func (f *Flow) String() string {
  164. ret := bytes.NewBuffer(nil)
  165. fmt.Fprintf(ret, "Flow\n")
  166. // consts
  167. fmt.Fprintf(ret, "consts:\n")
  168. for i, v := range f.consts {
  169. fmt.Fprintf(ret, " [%s] %v\n", i, v)
  170. }
  171. fmt.Fprintf(ret, "data:\n") // Or variable
  172. for k, v := range f.Data {
  173. fmt.Fprintf(ret, " [%v] %v\n", k, v)
  174. }
  175. fmt.Fprintf(ret, "operations:\n")
  176. f.operations.Range(func(pk, pv interface{}) bool {
  177. k, v := pk.(string), pv.(*operation)
  178. fmt.Fprintf(ret, " [%s] %s(", k, v.name)
  179. for j, in := range v.inputs {
  180. if j != 0 {
  181. fmt.Fprintf(ret, ", ")
  182. }
  183. fmt.Fprintf(ret, "%s[%v]", "func", in.ID())
  184. }
  185. fmt.Fprintf(ret, ")\n")
  186. return true
  187. })
  188. return ret.String()
  189. }
  190. // MarshalJSON implementation
  191. func (f *Flow) MarshalJSON() ([]byte, error) {
  192. data := map[string]interface{}{}
  193. type opMarshal struct {
  194. Name string
  195. Input []map[string]interface{}
  196. }
  197. operations := map[string]opMarshal{}
  198. f.operations.Range(func(pk, po interface{}) bool {
  199. k, o := pk.(string), po.(*operation)
  200. refs := []map[string]interface{}{}
  201. for _, in := range o.inputs { // Switch type?
  202. refs = append(refs, map[string]interface{}{
  203. "type": in.kind,
  204. "id": in.id,
  205. })
  206. }
  207. operations[k] = opMarshal{o.name, refs}
  208. return true
  209. })
  210. data["operations"] = operations
  211. data["data"] = f.Data
  212. data["consts"] = f.consts
  213. return json.Marshal(data)
  214. }
  215. func (f *Flow) allocID(fn func(id string) error) error {
  216. genID := func() (string, error) {
  217. f.Lock()
  218. defer f.Unlock()
  219. var id string
  220. // generate ID
  221. for i := 0; i < 10; i++ {
  222. id = f.idGen()
  223. if _, ok := f.operations.Load(id); !ok {
  224. f.operations.Store(id, nil) // tmp
  225. return id, nil
  226. }
  227. }
  228. return "", errors.New("ID Exausted")
  229. }
  230. // Safe generate an ID
  231. id, err := genID()
  232. if err != nil {
  233. return err
  234. }
  235. err = fn(id)
  236. if err != nil {
  237. f.operations.Delete(id)
  238. }
  239. return nil
  240. }
  241. /*func (f *Flow) addEntry(entry *operation) (string, error) {
  242. f.Lock()
  243. defer f.Unlock()
  244. // generate ID
  245. for i := 0; i < 10; i++ {
  246. id := f.idGen()
  247. if _, ok := f.operations.Load(id); ok {
  248. continue
  249. }
  250. f.operations.Store(id, entry)
  251. return id, nil
  252. }
  253. return "", errors.New("ID exausted")
  254. }*/
  255. // GetOp Return an existing operation or return notfound error
  256. func (f *Flow) GetOp(id string) *operation {
  257. op, ok := f.operations.Load(id)
  258. if !ok {
  259. return nil
  260. }
  261. return op.(*operation)
  262. }
  263. //////////////////////////////////////////////
  264. // Experimental event
  265. ////////////////
  266. // Hook attach the node event hooks
  267. func (f *Flow) Hook(hook Hook) {
  268. f.hooks.Attach(hook)
  269. }