rule_engine.hrl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2021 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -define(APP, emqx_rule_engine).
  17. -define(KV_TAB, '@rule_engine_db').
  18. -type maybe(T) :: T | undefined.
  19. -type rule_id() :: binary().
  20. -type rule_name() :: binary().
  21. -type mf() :: {Module::atom(), Fun::atom()}.
  22. -type hook() :: atom() | 'any'.
  23. -type topic() :: binary().
  24. -type bridge_channel_id() :: binary().
  25. -type selected_data() :: map().
  26. -type envs() :: map().
  27. -type output_type() :: bridge | builtin | func.
  28. -type output_target() :: bridge_channel_id() | atom() | output_fun().
  29. -type output_fun_args() :: map().
  30. -type output() :: #{
  31. type := output_type(),
  32. target := output_target(),
  33. args => output_fun_args()
  34. }.
  35. -type output_fun() :: fun((selected_data(), envs(), output_fun_args()) -> any()).
  36. -type rule_info() ::
  37. #{ from := list(topic())
  38. , outputs := [output()]
  39. , sql := binary()
  40. , is_foreach := boolean()
  41. , fields := list()
  42. , doeach := term()
  43. , incase := term()
  44. , conditions := tuple()
  45. , enabled := boolean()
  46. , description => binary()
  47. }.
  48. -record(rule,
  49. { id :: rule_id()
  50. , created_at :: integer() %% epoch in millisecond precision
  51. , info :: rule_info()
  52. }).
  53. %% Arithmetic operators
  54. -define(is_arith(Op), (Op =:= '+' orelse
  55. Op =:= '-' orelse
  56. Op =:= '*' orelse
  57. Op =:= '/' orelse
  58. Op =:= 'div')).
  59. %% Compare operators
  60. -define(is_comp(Op), (Op =:= '=' orelse
  61. Op =:= '=~' orelse
  62. Op =:= '>' orelse
  63. Op =:= '<' orelse
  64. Op =:= '<=' orelse
  65. Op =:= '>=' orelse
  66. Op =:= '<>' orelse
  67. Op =:= '!=')).
  68. %% Logical operators
  69. -define(is_logical(Op), (Op =:= 'and' orelse Op =:= 'or')).
  70. -define(RAISE(_EXP_, _ERROR_),
  71. ?RAISE(_EXP_, _ = do_nothing, _ERROR_)).
  72. -define(RAISE(_EXP_, _EXP_ON_FAIL_, _ERROR_),
  73. fun() ->
  74. try (_EXP_)
  75. catch _EXCLASS_:_EXCPTION_:_ST_ ->
  76. _EXP_ON_FAIL_,
  77. throw(_ERROR_)
  78. end
  79. end()).
  80. %% Tables
  81. -define(RULE_TAB, emqx_rule).