1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
#![allow(dead_code)]
//! Simple rules engine that represents requirements as a tree, with each node having one or more requirements in order to be "Met".
//!
//! A tree of rules is constructed, and then the `.check_json()` method is called.
//! `json` is a nested `field: value` that will be given to each node in the tree for testing.
//!
//! Status output can be either `Met`, `NotMet`, or `Unknown` if the tested field is not present in the json.
//!
//! To construct a tree, see the following methods.
//!
//! ## Example
//!
//! ```rust
//! extern crate json_rules_engine_fork;
//! use serde_json::json;
//!
//! let tree = json_rules_engine_fork::and(vec![
//! json_rules_engine_fork::string_equals("name", "John Doe"),
//! json_rules_engine_fork::or(vec![
//! json_rules_engine_fork::int_equals("fav_number", 5),
//! json_rules_engine_fork::int_in_range("thinking_of", 5, 10)
//! ])
//! ]);
//! let mut facts = json!({
//! "name": "John Doe",
//! "fav_number": 5
//! });
//! #[cfg(not(feature = "eval"))]
//! {
//! let result = tree.check_value(&facts);
//! println!("{:?}", result);
//! assert!(result.status == json_rules_engine_fork::Status::Met);
//! }
//! // result = ConditionResult { name: "And", status: Met, children: [ConditionResult { name: "Name is John Doe", status: Met, children: [] }, ConditionResult { name: "Or", status: Met, children: [ConditionResult { name: "Favorite number is 5", status: Met, children: [] }, ConditionResult { name: "Thinking of a number between 5 and 10", status: Unknown, children: [] }] }] }
//! ```
//!
//! This creates a tree like the following:
//!
//! ```text
//! +---------+
//! | AND |
//! +---------+
//! _____________________/\_______________
//! | |
//! V V
//! +-------------------+ +--------+
//! | Name is John Doe | | OR |
//! +-------------------+ +--------+
//! | field: "name" | ______________/\___________
//! | value: "John Doe" | | |
//! +-------------------+ V V
//! +----------------------+ +-------------------------+
//! | Favorite number is 5 | | Number between 5 and 10 |
//! +----------------------+ +-------------------------+
//! | field: "fav_number" | | field: "thinking_of" |
//! | value: 5 | | start: 5 |
//! +----------------------+ | end: 10 |
//! +-------------------------+
//! ```
//!
//! [1]: enum.Rule.html#method.check
mod condition;
mod constraint;
mod error;
mod event;
mod rule;
mod status;
pub use crate::{condition::*, constraint::*, event::*, rule::*, status::*};
#[cfg(feature = "eval")]
pub use rhai::{serde::from_dynamic, Map};
#[cfg(feature = "eval")]
use rhai::{
def_package,
packages::{
ArithmeticPackage, BasicArrayPackage, BasicMapPackage, LogicPackage,
Package,
},
Engine as RhaiEngine,
};
use serde_json::value::to_value;
use std::{collections::HashMap, time::Instant};
#[cfg(feature = "email")]
use crate::event::email_notification::EmailNotification;
#[cfg(feature = "callback")]
use crate::event::post_callback::PostCallback;
pub use crate::error::*;
use serde::Serialize;
use std::{rc::Rc, sync::RwLock};
#[cfg(feature = "eval")]
def_package!(rhai:JsonRulesEnginePackage:"Package for json-rules-engine", lib, {
ArithmeticPackage::init(lib);
LogicPackage::init(lib);
BasicArrayPackage::init(lib);
BasicMapPackage::init(lib);
});
#[derive(Default)]
pub struct Engine {
rules: Vec<Rule>,
events: HashMap<String, Rc<RwLock<dyn EventTrait>>>,
#[cfg(feature = "eval")]
rhai_engine: RhaiEngine,
coalescences: HashMap<String, (Instant, u64)>,
}
impl Engine {
pub fn new() -> Self {
#[allow(unused_mut)]
let mut events: HashMap<_, Rc<RwLock<dyn EventTrait>>> = HashMap::new();
#[cfg(feature = "callback")]
{
let event = Rc::new(RwLock::new(PostCallback::new()));
let key = event.read().unwrap().get_type().to_string();
events.insert(key, event);
}
#[cfg(feature = "email")]
{
let event = Rc::new(RwLock::new(EmailNotification::new()));
let key = event.read().unwrap().get_type().to_string();
events.insert(key, event);
}
Self {
rules: Vec::new(),
#[cfg(feature = "eval")]
rhai_engine: {
let mut engine = RhaiEngine::new_raw();
engine.register_global_module(
JsonRulesEnginePackage::new().as_shared_module(),
);
engine
},
coalescences: HashMap::new(),
events,
}
}
pub fn add_rule(&mut self, rule: Rule) {
self.rules.push(rule)
}
pub fn add_rules(&mut self, rules: Vec<Rule>) {
self.rules.extend(rules)
}
pub fn load_rules(&mut self, rules: Vec<Rule>) {
self.rules = rules;
}
pub fn clear(&mut self) {
self.rules.clear();
}
#[cfg(feature = "eval")]
pub fn add_function(&mut self, fname: &str, f: fn(Map) -> bool) {
self.rhai_engine.register_fn(fname, f);
}
pub fn add_event(&mut self, f: Rc<RwLock<dyn EventTrait>>) {
let key = f.read().unwrap().get_type().to_string();
self.events.insert(key, f);
}
pub async fn run<T: Serialize>(
&mut self,
facts: &T,
) -> Result<Vec<RuleResult>> {
let facts = to_value(facts)?;
let mut met_rule_results: Vec<RuleResult> = self
.rules
.iter()
.map(|rule| {
rule.check_value(
&facts,
#[cfg(feature = "eval")]
&self.rhai_engine,
)
})
.filter(|rule_result| {
rule_result.condition_result.status == Status::Met
})
.collect();
self.coalescences.retain(|_k, (start, expiration)| {
start.elapsed().as_secs() < *expiration
});
for rule_result in met_rule_results.iter_mut() {
// filter the events
let mut cole = self.coalescences.clone();
rule_result.events.retain(|event| {
if let (Some(coalescence_group), Some(coalescence)) =
(&event.coalescence_group, event.coalescence)
{
if cole.contains_key(coalescence_group) {
return false;
} else {
cole.insert(
coalescence_group.clone(),
(Instant::now(), coalescence),
);
}
}
true
});
self.coalescences = cole;
// TODO run all the async events in parallel
// run the events
for event in &rule_result.events {
let e =
self.events.get_mut(&event.event.ty).ok_or_else(|| {
Error::EventError(
"Event type doesn't exist".to_string(),
)
})?;
e.read()
.unwrap()
.validate(&event.event.params)
.map_err(Error::EventError)?;
e.write()
.unwrap()
.trigger(&event.event.params, &facts)
.await?;
}
}
Ok(met_rule_results)
}
}