func_provider.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package interpreter
  11. import (
  12. "fmt"
  13. "strconv"
  14. "strings"
  15. "devt.de/krotik/ecal/engine"
  16. "devt.de/krotik/ecal/parser"
  17. "devt.de/krotik/ecal/scope"
  18. "devt.de/krotik/ecal/util"
  19. )
  20. /*
  21. inbuildFuncMap contains the mapping of inbuild functions.
  22. */
  23. var inbuildFuncMap = map[string]util.ECALFunction{
  24. "range": &rangeFunc{&inbuildBaseFunc{}},
  25. "new": &newFunc{&inbuildBaseFunc{}},
  26. "len": &lenFunc{&inbuildBaseFunc{}},
  27. "del": &delFunc{&inbuildBaseFunc{}},
  28. "add": &addFunc{&inbuildBaseFunc{}},
  29. "concat": &concatFunc{&inbuildBaseFunc{}},
  30. "dumpenv": &dumpenvFunc{&inbuildBaseFunc{}},
  31. "raise": &raise{&inbuildBaseFunc{}},
  32. "addEvent": &addevent{&inbuildBaseFunc{}},
  33. "addEventAndWait": &addeventandwait{&addevent{&inbuildBaseFunc{}}},
  34. }
  35. /*
  36. inbuildBaseFunc is the base structure for inbuild functions providing some
  37. utility functions.
  38. */
  39. type inbuildBaseFunc struct {
  40. }
  41. /*
  42. AssertNumParam converts a general interface{} parameter into a number.
  43. */
  44. func (ibf *inbuildBaseFunc) AssertNumParam(index int, val interface{}) (float64, error) {
  45. var err error
  46. resNum, ok := val.(float64)
  47. if !ok {
  48. resNum, err = strconv.ParseFloat(fmt.Sprint(val), 64)
  49. if err != nil {
  50. err = fmt.Errorf("Parameter %v should be a number", index)
  51. }
  52. }
  53. return resNum, err
  54. }
  55. /*
  56. AssertMapParam converts a general interface{} parameter into a map.
  57. */
  58. func (ibf *inbuildBaseFunc) AssertMapParam(index int, val interface{}) (map[interface{}]interface{}, error) {
  59. valMap, ok := val.(map[interface{}]interface{})
  60. if ok {
  61. return valMap, nil
  62. }
  63. return nil, fmt.Errorf("Parameter %v should be a map", index)
  64. }
  65. /*
  66. AssertListParam converts a general interface{} parameter into a list.
  67. */
  68. func (ibf *inbuildBaseFunc) AssertListParam(index int, val interface{}) ([]interface{}, error) {
  69. valList, ok := val.([]interface{})
  70. if ok {
  71. return valList, nil
  72. }
  73. return nil, fmt.Errorf("Parameter %v should be a list", index)
  74. }
  75. // Range
  76. // =====
  77. /*
  78. rangeFunc is an interator function which returns a range of numbers.
  79. */
  80. type rangeFunc struct {
  81. *inbuildBaseFunc
  82. }
  83. /*
  84. Run executes this function.
  85. */
  86. func (rf *rangeFunc) Run(instanceID string, vs parser.Scope, is map[string]interface{}, args []interface{}) (interface{}, error) {
  87. var currVal, to float64
  88. var err error
  89. lenargs := len(args)
  90. from := 0.
  91. step := 1.
  92. if lenargs == 0 {
  93. err = fmt.Errorf("Need at least an end range as first parameter")
  94. }
  95. if err == nil {
  96. if stepVal, ok := is[instanceID+"step"]; ok {
  97. step = stepVal.(float64)
  98. from = is[instanceID+"from"].(float64)
  99. to = is[instanceID+"to"].(float64)
  100. currVal = is[instanceID+"currVal"].(float64)
  101. is[instanceID+"currVal"] = currVal + step
  102. // Check for end of iteration
  103. if (from < to && currVal > to) || (from > to && currVal < to) || from == to {
  104. err = util.ErrEndOfIteration
  105. }
  106. } else {
  107. if lenargs == 1 {
  108. to, err = rf.AssertNumParam(1, args[0])
  109. } else {
  110. from, err = rf.AssertNumParam(1, args[0])
  111. if err == nil {
  112. to, err = rf.AssertNumParam(2, args[1])
  113. }
  114. if err == nil && lenargs > 2 {
  115. step, err = rf.AssertNumParam(3, args[2])
  116. }
  117. }
  118. if err == nil {
  119. is[instanceID+"from"] = from
  120. is[instanceID+"to"] = to
  121. is[instanceID+"step"] = step
  122. is[instanceID+"currVal"] = from
  123. currVal = from
  124. }
  125. }
  126. }
  127. if err == nil {
  128. err = util.ErrIsIterator // Identify as iterator
  129. }
  130. return currVal, err
  131. }
  132. /*
  133. DocString returns a descriptive string.
  134. */
  135. func (rf *rangeFunc) DocString() (string, error) {
  136. return "Range function which can be used to iterate over number ranges. Parameters are start, end and step.", nil
  137. }
  138. // New
  139. // ===
  140. /*
  141. newFunc instantiates a new object.
  142. */
  143. type newFunc struct {
  144. *inbuildBaseFunc
  145. }
  146. /*
  147. Run executes this function.
  148. */
  149. func (rf *newFunc) Run(instanceID string, vs parser.Scope, is map[string]interface{}, args []interface{}) (interface{}, error) {
  150. var res interface{}
  151. err := fmt.Errorf("Need a map as first parameter")
  152. if len(args) > 0 {
  153. var argMap map[interface{}]interface{}
  154. if argMap, err = rf.AssertMapParam(1, args[0]); err == nil {
  155. obj := make(map[interface{}]interface{})
  156. res = obj
  157. _, err = rf.addSuperClasses(vs, is, obj, argMap)
  158. if initObj, ok := obj["init"]; ok {
  159. if initFunc, ok := initObj.(*function); ok {
  160. initvs := scope.NewScope(fmt.Sprintf("newfunc: %v", instanceID))
  161. initis := make(map[string]interface{})
  162. _, err = initFunc.Run(instanceID, initvs, initis, args[1:])
  163. }
  164. }
  165. }
  166. }
  167. return res, err
  168. }
  169. /*
  170. addSuperClasses adds super class functions to a given object.
  171. */
  172. func (rf *newFunc) addSuperClasses(vs parser.Scope, is map[string]interface{},
  173. obj map[interface{}]interface{}, template map[interface{}]interface{}) (interface{}, error) {
  174. var err error
  175. var initFunc interface{}
  176. var initSuperList []interface{}
  177. // First loop into the base classes (i.e. top-most classes)
  178. if super, ok := template["super"]; ok {
  179. if superList, ok := super.([]interface{}); ok {
  180. for _, superObj := range superList {
  181. var superInit interface{}
  182. if superTemplate, ok := superObj.(map[interface{}]interface{}); ok {
  183. superInit, err = rf.addSuperClasses(vs, is, obj, superTemplate)
  184. initSuperList = append(initSuperList, superInit) // Build up the list of super functions
  185. }
  186. }
  187. } else {
  188. err = fmt.Errorf("Property _super must be a list of super classes")
  189. }
  190. }
  191. // Copy all properties from template to obj
  192. for k, v := range template {
  193. // Save previous init function
  194. if funcVal, ok := v.(*function); ok {
  195. newFunction := &function{funcVal.name, nil, obj, funcVal.declaration}
  196. if k == "init" {
  197. newFunction.super = initSuperList
  198. initFunc = newFunction
  199. }
  200. obj[k] = newFunction
  201. } else {
  202. obj[k] = v
  203. }
  204. }
  205. return initFunc, err
  206. }
  207. /*
  208. DocString returns a descriptive string.
  209. */
  210. func (rf *newFunc) DocString() (string, error) {
  211. return "New creates a new object instance.", nil
  212. }
  213. // Len
  214. // ===
  215. /*
  216. lenFunc returns the size of a list or map.
  217. */
  218. type lenFunc struct {
  219. *inbuildBaseFunc
  220. }
  221. /*
  222. Run executes this function.
  223. */
  224. func (rf *lenFunc) Run(instanceID string, vs parser.Scope, is map[string]interface{}, args []interface{}) (interface{}, error) {
  225. var res float64
  226. err := fmt.Errorf("Need a list or a map as first parameter")
  227. if len(args) > 0 {
  228. argList, ok1 := args[0].([]interface{})
  229. argMap, ok2 := args[0].(map[interface{}]interface{})
  230. if ok1 {
  231. res = float64(len(argList))
  232. err = nil
  233. } else if ok2 {
  234. res = float64(len(argMap))
  235. err = nil
  236. }
  237. }
  238. return res, err
  239. }
  240. /*
  241. DocString returns a descriptive string.
  242. */
  243. func (rf *lenFunc) DocString() (string, error) {
  244. return "Len returns the size of a list or map.", nil
  245. }
  246. // Del
  247. // ===
  248. /*
  249. delFunc removes an element from a list or map.
  250. */
  251. type delFunc struct {
  252. *inbuildBaseFunc
  253. }
  254. /*
  255. Run executes this function.
  256. */
  257. func (rf *delFunc) Run(instanceID string, vs parser.Scope, is map[string]interface{}, args []interface{}) (interface{}, error) {
  258. var res interface{}
  259. err := fmt.Errorf("Need a list or a map as first parameter and an index or key as second parameter")
  260. if len(args) == 2 {
  261. if argList, ok1 := args[0].([]interface{}); ok1 {
  262. var index float64
  263. index, err = rf.AssertNumParam(2, args[1])
  264. if err == nil {
  265. res = append(argList[:int(index)], argList[int(index+1):]...)
  266. }
  267. }
  268. if argMap, ok2 := args[0].(map[interface{}]interface{}); ok2 {
  269. key := fmt.Sprint(args[1])
  270. delete(argMap, key)
  271. res = argMap
  272. err = nil
  273. }
  274. }
  275. return res, err
  276. }
  277. /*
  278. DocString returns a descriptive string.
  279. */
  280. func (rf *delFunc) DocString() (string, error) {
  281. return "Del removes an item from a list or map.", nil
  282. }
  283. // Add
  284. // ===
  285. /*
  286. addFunc adds an element to a list.
  287. */
  288. type addFunc struct {
  289. *inbuildBaseFunc
  290. }
  291. /*
  292. Run executes this function.
  293. */
  294. func (rf *addFunc) Run(instanceID string, vs parser.Scope, is map[string]interface{}, args []interface{}) (interface{}, error) {
  295. var res interface{}
  296. err := fmt.Errorf("Need a list as first parameter and a value as second parameter")
  297. if len(args) > 1 {
  298. var argList []interface{}
  299. if argList, err = rf.AssertListParam(1, args[0]); err == nil {
  300. if len(args) == 3 {
  301. var index float64
  302. if index, err = rf.AssertNumParam(3, args[2]); err == nil {
  303. argList = append(argList, 0)
  304. copy(argList[int(index+1):], argList[int(index):])
  305. argList[int(index)] = args[1]
  306. res = argList
  307. }
  308. } else {
  309. res = append(argList, args[1])
  310. }
  311. }
  312. }
  313. return res, err
  314. }
  315. /*
  316. DocString returns a descriptive string.
  317. */
  318. func (rf *addFunc) DocString() (string, error) {
  319. return "Add adds an item to a list. The item is added at the optionally given index or at the end if no index is specified.", nil
  320. }
  321. // Concat
  322. // ======
  323. /*
  324. concatFunc joins one or more lists together.
  325. */
  326. type concatFunc struct {
  327. *inbuildBaseFunc
  328. }
  329. /*
  330. Run executes this function.
  331. */
  332. func (rf *concatFunc) Run(instanceID string, vs parser.Scope, is map[string]interface{}, args []interface{}) (interface{}, error) {
  333. var res interface{}
  334. err := fmt.Errorf("Need at least two lists as parameters")
  335. if len(args) > 1 {
  336. var argList []interface{}
  337. resList := make([]interface{}, 0)
  338. err = nil
  339. for _, a := range args {
  340. if err == nil {
  341. if argList, err = rf.AssertListParam(1, a); err == nil {
  342. resList = append(resList, argList...)
  343. }
  344. }
  345. }
  346. if err == nil {
  347. res = resList
  348. }
  349. }
  350. return res, err
  351. }
  352. /*
  353. DocString returns a descriptive string.
  354. */
  355. func (rf *concatFunc) DocString() (string, error) {
  356. return "Concat joins one or more lists together. The result is a new list.", nil
  357. }
  358. // dumpenv
  359. // =======
  360. /*
  361. dumpenvFunc returns the current variable environment as a string.
  362. */
  363. type dumpenvFunc struct {
  364. *inbuildBaseFunc
  365. }
  366. /*
  367. Run executes this function.
  368. */
  369. func (rf *dumpenvFunc) Run(instanceID string, vs parser.Scope, is map[string]interface{}, args []interface{}) (interface{}, error) {
  370. return vs.String(), nil
  371. }
  372. /*
  373. DocString returns a descriptive string.
  374. */
  375. func (rf *dumpenvFunc) DocString() (string, error) {
  376. return "Dumpenv returns the current variable environment as a string.", nil
  377. }
  378. // raise
  379. // =====
  380. /*
  381. raise returns an error. Outside of sinks this will stop the code execution
  382. if the error is not handled by try / except. Inside a sink only the specific sink
  383. will fail. This error can be used to break trigger sequences of sinks if
  384. FailOnFirstErrorInTriggerSequence is set.
  385. */
  386. type raise struct {
  387. *inbuildBaseFunc
  388. }
  389. /*
  390. Run executes this function.
  391. */
  392. func (rf *raise) Run(instanceID string, vs parser.Scope, is map[string]interface{}, args []interface{}) (interface{}, error) {
  393. var err error
  394. var detailMsg string
  395. var detail interface{}
  396. if len(args) > 0 {
  397. err = fmt.Errorf("%v", args[0])
  398. if len(args) > 1 {
  399. if args[1] != nil {
  400. detailMsg = fmt.Sprint(args[1])
  401. }
  402. if len(args) > 2 {
  403. detail = args[2]
  404. }
  405. }
  406. }
  407. erp := is["erp"].(*ECALRuntimeProvider)
  408. node := is["astnode"].(*parser.ASTNode)
  409. return nil, &util.RuntimeErrorWithDetail{
  410. RuntimeError: erp.NewRuntimeError(err, detailMsg, node).(*util.RuntimeError),
  411. Environment: vs,
  412. Data: detail,
  413. }
  414. }
  415. /*
  416. DocString returns a descriptive string.
  417. */
  418. func (rf *raise) DocString() (string, error) {
  419. return "Raise returns an error object.", nil
  420. }
  421. // addEvent
  422. // ========
  423. /*
  424. addevent adds an event to trigger sinks. This function will return immediately
  425. and not wait for the event cascade to finish. Use this function for event cascades.
  426. */
  427. type addevent struct {
  428. *inbuildBaseFunc
  429. }
  430. /*
  431. Run executes this function.
  432. */
  433. func (rf *addevent) Run(instanceID string, vs parser.Scope, is map[string]interface{}, args []interface{}) (interface{}, error) {
  434. return rf.addEvent(func(proc engine.Processor, event *engine.Event, scope *engine.RuleScope) (interface{}, error) {
  435. var monitor engine.Monitor
  436. parentMonitor, ok := is["monitor"]
  437. if scope != nil || !ok {
  438. monitor = proc.NewRootMonitor(nil, scope)
  439. } else {
  440. monitor = parentMonitor.(engine.Monitor).NewChildMonitor(0)
  441. }
  442. _, err := proc.AddEvent(event, monitor)
  443. return nil, err
  444. }, is, args)
  445. }
  446. func (rf *addevent) addEvent(addFunc func(engine.Processor, *engine.Event, *engine.RuleScope) (interface{}, error),
  447. is map[string]interface{}, args []interface{}) (interface{}, error) {
  448. var res interface{}
  449. var stateMap map[interface{}]interface{}
  450. erp := is["erp"].(*ECALRuntimeProvider)
  451. proc := erp.Processor
  452. if proc.Stopped() {
  453. proc.Start()
  454. }
  455. err := fmt.Errorf("Need at least three parameters: name, kind and state")
  456. if len(args) > 2 {
  457. if stateMap, err = rf.AssertMapParam(3, args[2]); err == nil {
  458. var scope *engine.RuleScope
  459. event := engine.NewEvent(
  460. fmt.Sprint(args[0]),
  461. strings.Split(fmt.Sprint(args[1]), "."),
  462. stateMap,
  463. )
  464. if len(args) > 3 {
  465. var scopeMap map[interface{}]interface{}
  466. // Add optional scope - if not specified it is { "": true }
  467. if scopeMap, err = rf.AssertMapParam(4, args[3]); err == nil {
  468. var scopeData = map[string]bool{}
  469. for k, v := range scopeMap {
  470. b, _ := strconv.ParseBool(fmt.Sprint(v))
  471. scopeData[fmt.Sprint(k)] = b
  472. }
  473. scope = engine.NewRuleScope(scopeData)
  474. }
  475. }
  476. if err == nil {
  477. res, err = addFunc(proc, event, scope)
  478. }
  479. }
  480. }
  481. return res, err
  482. }
  483. /*
  484. DocString returns a descriptive string.
  485. */
  486. func (rf *addevent) DocString() (string, error) {
  487. return "AddEvent adds an event to trigger sinks. This function will return " +
  488. "immediately and not wait for the event cascade to finish.", nil
  489. }
  490. // addEventAndWait
  491. // ===============
  492. /*
  493. addeventandwait adds an event to trigger sinks. This function will return once
  494. the event cascade has finished and return all errors.
  495. */
  496. type addeventandwait struct {
  497. *addevent
  498. }
  499. /*
  500. Run executes this function.
  501. */
  502. func (rf *addeventandwait) Run(instanceID string, vs parser.Scope, is map[string]interface{}, args []interface{}) (interface{}, error) {
  503. return rf.addEvent(func(proc engine.Processor, event *engine.Event, scope *engine.RuleScope) (interface{}, error) {
  504. var res []interface{}
  505. rm := proc.NewRootMonitor(nil, scope)
  506. m, err := proc.AddEventAndWait(event, rm)
  507. if m != nil {
  508. allErrors := m.(*engine.RootMonitor).AllErrors()
  509. for _, e := range allErrors {
  510. errors := map[interface{}]interface{}{}
  511. for k, v := range e.ErrorMap {
  512. se := v.(*util.RuntimeErrorWithDetail)
  513. // Note: The variable scope of the sink (se.environment)
  514. // was also captured - for now it is not exposed to the
  515. // language environment
  516. errors[k] = map[interface{}]interface{}{
  517. "error": se.Error(),
  518. "type": se.Type.Error(),
  519. "detail": se.Detail,
  520. "data": se.Data,
  521. }
  522. }
  523. item := map[interface{}]interface{}{
  524. "event": map[interface{}]interface{}{
  525. "name": e.Event.Name(),
  526. "kind": strings.Join(e.Event.Kind(), "."),
  527. "state": e.Event.State(),
  528. },
  529. "errors": errors,
  530. }
  531. res = append(res, item)
  532. }
  533. }
  534. return res, err
  535. }, is, args)
  536. }
  537. /*
  538. DocString returns a descriptive string.
  539. */
  540. func (rf *addeventandwait) DocString() (string, error) {
  541. return "AddEventAndWait adds an event to trigger sinks. This function will " +
  542. "return once the event cascade has finished.", nil
  543. }