Rush
Rush is a universal rules engine.
Rush provides a general computational abstraction. There can be multiple implementations and any combination.
Conditional parallel computing
Quick start
const SIMPLE_RULE: &'static str = "
rule COMPLEX_RULE
when
age > 18
then
stage = 'adult'
";
Here, regular expressions are generated directly. You can actually implement some of them yourself to integrate with your own services.
Keywords
The direct parsing rules are as follows:
The keyword cannot be repeated: when,then
rule
when
;
;
...
;
then
;
;
...
;
Operators
- Modifiers: + - / * & | ^ % >> <<
- Comparators: > >= < <= == !=
- Logical ops: || &&
- Numeric constants, as i64, if have '.' as f64
- String constants (single quotes: 'foobar')
- Boolean constants: true false
- Parenthesis to control order of evaluation ( )
- Arrays [anything separated by , within parenthesis: [1, 2, 'foo']]
- contain function example:
contain([1,2.3,'hello'],1)
- sub function: Find whether there are subsets of the two arrays
- contain function example:
- Prefixes: ! - ~
- Null coalescence: null
- Function: function_name(args)result
- Input field by digits, letters and underscores,if field not found then condition is failed
Function
You can add functions just like normal rust functions
let rh = rh
.register_function;
Lua
const LUA_RULE_SCRIPT: &'static str = r#"
rule LUA_RULE_SCRIPT _ lua
lua_script:
function handle(req)
local resp = {}
if req.source == ONLINE_CHANNEL then
resp.message = "线上渠道"
elseif req.source == OFFLINE_CHANNEL then
resp.message = "线下渠道"
else
resp.message = "未知渠道:"..req.source
end
return resp
end
return {handle_function="handle"}
"#;
WASM
Compile a wasm file with the following code, use command cargo build --target wasm32-unknown-unknown --release
extern "C"
pub extern "C"
Refer to this file in the rule and call:
const WASM_RULE: &'static str = "
rule WASM_RULE _ wasm
wasm_file: wasm_example/wasm_example_one.wasm
";
async
Abstraction and Structure
Benchmark
If you're concerned about the overhead of this library, a good range of benchmarks are built into this repo. You can run them with cargo bench -- --verbose
The library is built with an eye towards being quick, but has not been aggressively profiled and optimized. For most applications, though, it is completely fine.
Here are my test results,at MacBook Pro,CPU 2.6 GHz Intel Core i7, [lowest, average, highest]
Plan
Expressions are currently supported as cells of count. Future support for lua and wasm is also planned。
License
This project is licensed under the MIT general use license. You're free to integrate, fork, and play with this code as you feel fit without consulting the author, as long as you provide proper credit to the author in your works.