rule_engine.hrl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020 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. -type(maybe(T) :: T | undefined).
  18. -type(rule_id() :: binary()).
  19. -type(rule_name() :: binary()).
  20. -type(resource_id() :: binary()).
  21. -type(action_instance_id() :: binary()).
  22. -type(action_name() :: atom()).
  23. -type(resource_type_name() :: atom()).
  24. -type(category() :: data_persist| data_forward | offline_msgs | debug | other).
  25. -type(descr() :: #{en := binary(), zh => binary()}).
  26. -type(mf() :: {Module::atom(), Fun::atom()}).
  27. -type(hook() :: atom() | 'any').
  28. -type(resource_status() :: #{ alive := boolean()
  29. , atom() => binary() | atom() | list(binary()|atom())
  30. }).
  31. -define(descr, #{en => <<>>, zh => <<>>}).
  32. -record(action,
  33. { name :: action_name()
  34. , category :: category()
  35. , for :: hook()
  36. , app :: atom()
  37. , types = [] :: list(resource_type_name())
  38. , module :: module()
  39. , on_create :: mf()
  40. , on_destroy :: maybe(mf())
  41. , hidden = false :: boolean()
  42. , params_spec :: #{atom() => term()} %% params specs
  43. , title = ?descr :: descr()
  44. , description = ?descr :: descr()
  45. }).
  46. -record(action_instance,
  47. { id :: action_instance_id()
  48. , name :: action_name()
  49. , fallbacks :: list(#action_instance{})
  50. , args :: #{atom() => term()} %% the args got from API for initializing action_instance
  51. }).
  52. -record(rule,
  53. { id :: rule_id()
  54. , for :: hook()
  55. , rawsql :: binary()
  56. , is_foreach :: boolean()
  57. , fields :: list()
  58. , doeach :: term()
  59. , incase :: list()
  60. , conditions :: tuple()
  61. , on_action_failed :: continue | stop
  62. , actions :: list(#action_instance{})
  63. , enabled :: boolean()
  64. , description :: binary()
  65. }).
  66. -record(resource,
  67. { id :: resource_id()
  68. , type :: resource_type_name()
  69. , config :: #{} %% the configs got from API for initializing resource
  70. , created_at :: erlang:timestamp()
  71. , description :: binary()
  72. }).
  73. -record(resource_type,
  74. { name :: resource_type_name()
  75. , provider :: atom()
  76. , params_spec :: #{atom() => term()} %% params specs
  77. , on_create :: mf()
  78. , on_status :: mf()
  79. , on_destroy :: mf()
  80. , title = ?descr :: descr()
  81. , description = ?descr :: descr()
  82. }).
  83. -record(rule_hooks,
  84. { hook :: atom()
  85. , rule_id :: rule_id()
  86. }).
  87. -record(resource_params,
  88. { id :: resource_id()
  89. , params :: #{} %% the params got after initializing the resource
  90. , status = #{is_alive => false} :: #{is_alive := boolean(), atom() => term()}
  91. }).
  92. -record(action_instance_params,
  93. { id :: action_instance_id()
  94. , params :: #{} %% the params got after initializing the action
  95. , apply :: fun((Data::map(), Envs::map()) -> any())
  96. | {M::module(), F::atom(), Args::list()} %% the func got after initializing the action
  97. }).
  98. %% Arithmetic operators
  99. -define(is_arith(Op), (Op =:= '+' orelse
  100. Op =:= '-' orelse
  101. Op =:= '*' orelse
  102. Op =:= '/' orelse
  103. Op =:= 'div')).
  104. %% Compare operators
  105. -define(is_comp(Op), (Op =:= '=' orelse
  106. Op =:= '=~' orelse
  107. Op =:= '>' orelse
  108. Op =:= '<' orelse
  109. Op =:= '<=' orelse
  110. Op =:= '>=' orelse
  111. Op =:= '<>' orelse
  112. Op =:= '!=')).
  113. %% Logical operators
  114. -define(is_logical(Op), (Op =:= 'and' orelse Op =:= 'or')).
  115. -define(RAISE(_EXP_, _ERROR_),
  116. begin
  117. fun() ->
  118. try (_EXP_) catch _:_REASON_:_ST_ -> throw(_ERROR_) end
  119. end()
  120. end).
  121. -define(THROW(_EXP_, _ERROR_),
  122. begin
  123. try (_EXP_) catch _:_ -> throw(_ERROR_) end
  124. end).
  125. %% Tables
  126. -define(RULE_TAB, emqx_rule).
  127. -define(ACTION_TAB, emqx_rule_action).
  128. -define(ACTION_INST_PARAMS_TAB, emqx_action_instance_params).
  129. -define(RES_TAB, emqx_resource).
  130. -define(RES_PARAMS_TAB, emqx_resource_params).
  131. -define(RULE_HOOKS, emqx_rule_hooks).
  132. -define(RES_TYPE_TAB, emqx_resource_type).