reactive_graph_std_comparison/behaviour/entity/gate/
function.rs1use std::sync::Arc;
2use std::sync::LazyLock;
3
4use reactive_graph_behaviour_model_impl::entity::EntityBehaviourFactoryCreator;
5use reactive_graph_behaviour_model_impl::entity::EntityBehaviourFunctions;
6use reactive_graph_behaviour_model_impl::entity::EntityBehaviourFunctionsStorage;
7use serde_json::Value;
8use serde_json::json;
9
10use reactive_graph_std_comparison_model::NAMESPACE_COMPARISON;
11
12use crate::behaviour::entity::gate::behaviour::ComparisonGateFactory;
13
14pub type ComparisonGateFunction = fn(&Value, &Value) -> Value;
15
16pub const FN_EQUALS: ComparisonGateFunction = |lhs, rhs| json!(lhs == rhs);
17pub const FN_NOT_EQUALS: ComparisonGateFunction = |lhs, rhs| json!(lhs != rhs);
18pub const FN_GREATER_THAN: ComparisonGateFunction = |lhs, rhs| {
19 if let Some(lhs) = lhs.as_f64() {
20 if let Some(rhs) = rhs.as_f64() {
21 return json!(lhs > rhs);
22 }
23 }
24 if let Some(lhs) = lhs.as_i64() {
25 if let Some(rhs) = rhs.as_i64() {
26 return json!(lhs > rhs);
27 }
28 }
29 if let Some(lhs) = lhs.as_u64() {
30 if let Some(rhs) = rhs.as_u64() {
31 return json!(lhs > rhs);
32 }
33 }
34 Value::Bool(false)
35};
36pub const FN_GREATER_THAN_OR_EQUALS: ComparisonGateFunction = |lhs, rhs| {
37 if let Some(lhs) = lhs.as_f64() {
38 if let Some(rhs) = rhs.as_f64() {
39 return json!(lhs >= rhs);
40 }
41 }
42 if let Some(lhs) = lhs.as_i64() {
43 if let Some(rhs) = rhs.as_i64() {
44 return json!(lhs >= rhs);
45 }
46 }
47 if let Some(lhs) = lhs.as_u64() {
48 if let Some(rhs) = rhs.as_u64() {
49 return json!(lhs >= rhs);
50 }
51 }
52 Value::Bool(false)
53};
54pub const FN_LOWER_THAN: ComparisonGateFunction = |lhs, rhs| {
55 if let Some(lhs) = lhs.as_f64() {
56 if let Some(rhs) = rhs.as_f64() {
57 return json!(lhs < rhs);
58 }
59 }
60 if let Some(lhs) = lhs.as_i64() {
61 if let Some(rhs) = rhs.as_i64() {
62 return json!(lhs < rhs);
63 }
64 }
65 if let Some(lhs) = lhs.as_u64() {
66 if let Some(rhs) = rhs.as_u64() {
67 return json!(lhs < rhs);
68 }
69 }
70 Value::Bool(false)
71};
72pub const FN_LOWER_THAN_OR_EQUALS: ComparisonGateFunction = |lhs, rhs| {
73 if let Some(lhs) = lhs.as_f64() {
74 if let Some(rhs) = rhs.as_f64() {
75 return json!(lhs <= rhs);
76 }
77 }
78 if let Some(lhs) = lhs.as_i64() {
79 if let Some(rhs) = rhs.as_i64() {
80 return json!(lhs <= rhs);
81 }
82 }
83 if let Some(lhs) = lhs.as_u64() {
84 if let Some(rhs) = rhs.as_u64() {
85 return json!(lhs <= rhs);
86 }
87 }
88 Value::Bool(false)
89};
90
91const FACTORY_CREATOR: EntityBehaviourFactoryCreator<ComparisonGateFunction> = |ty, f| Arc::new(ComparisonGateFactory::new(ty.clone(), f));
92
93pub static COMPARISON_GATES: EntityBehaviourFunctionsStorage<ComparisonGateFunction> = LazyLock::new(|| {
94 EntityBehaviourFunctions::<ComparisonGateFunction>::with_namespace(NAMESPACE_COMPARISON, FACTORY_CREATOR)
95 .behaviour("equals", FN_EQUALS)
96 .behaviour("not_equals", FN_NOT_EQUALS)
97 .behaviour("greater_than", FN_GREATER_THAN)
98 .behaviour("greater_than_or_equals", FN_GREATER_THAN_OR_EQUALS)
99 .behaviour("lower_than", FN_LOWER_THAN)
100 .behaviour("lower_than_or_equals", FN_LOWER_THAN_OR_EQUALS)
101 .get()
102});