reactive_graph_std_numeric/behaviour/entity/gate/behaviour_f64.rs
1use reactive_graph_behaviour_model_api::behaviour_validator;
2use reactive_graph_behaviour_model_api::prelude::*;
3use reactive_graph_behaviour_model_impl::entity_behaviour;
4use reactive_graph_graph::prelude::*;
5use reactive_graph_reactive_model_impl::ReactiveEntity;
6use uuid::Uuid;
7
8use reactive_graph_std_numeric_model::NumericGateProperties::LHS;
9use reactive_graph_std_numeric_model::NumericGateProperties::RHS;
10use reactive_graph_std_result_model::ResultNumberF64Properties::RESULT;
11
12use crate::behaviour::as_f64;
13use crate::behaviour::entity::gate::function::NumericGateF64Function;
14
15entity_behaviour!(
16 NumericGateF64,
17 NumericGateF64Factory,
18 NumericGateF64Fsm,
19 NumericGateF64BehaviourTransitions,
20 NumericGateF64Validator,
21 f,
22 NumericGateF64Function
23);
24
25behaviour_validator!(NumericGateF64Validator, Uuid, ReactiveEntity, LHS.as_ref(), RHS.as_ref(), RESULT.as_ref());
26
27impl BehaviourInit<Uuid, ReactiveEntity> for NumericGateF64BehaviourTransitions {
28 fn init(&self) -> Result<(), BehaviourInitializationFailed> {
29 let lhs = self.reactive_instance.get(LHS).and_then(as_f64).ok_or(BehaviourInitializationFailed {})?;
30 let rhs = self.reactive_instance.get(RHS).and_then(as_f64).ok_or(BehaviourInitializationFailed {})?;
31 let f = self.f;
32 let initial_value = f(lhs, rhs);
33 self.reactive_instance.set(RESULT, initial_value);
34 Ok(())
35 }
36}
37
38impl BehaviourConnect<Uuid, ReactiveEntity> for NumericGateF64BehaviourTransitions {
39 fn connect(&self) -> Result<(), BehaviourConnectFailed> {
40 let reactive_instance = self.reactive_instance.clone();
41 let f = self.f;
42 self.property_observers.observe_with_handle(LHS.as_ref(), move |v| {
43 if let Some(lhs) = v.as_f64() {
44 if let Some(rhs) = reactive_instance.get(RHS).and_then(as_f64) {
45 reactive_instance.set(RESULT, f(lhs, rhs));
46 }
47 }
48 });
49 let reactive_instance = self.reactive_instance.clone();
50 let f = self.f;
51 self.property_observers.observe_with_handle(RHS.as_ref(), move |v| {
52 if let Some(rhs) = v.as_f64() {
53 if let Some(lhs) = reactive_instance.get(LHS).and_then(as_f64) {
54 reactive_instance.set(RESULT, f(lhs, rhs));
55 }
56 }
57 });
58
59 Ok(())
60 }
61}
62impl BehaviourShutdown<Uuid, ReactiveEntity> for NumericGateF64BehaviourTransitions {}
63impl BehaviourTransitions<Uuid, ReactiveEntity> for NumericGateF64BehaviourTransitions {}
64
65// #[cfg(test)]
66// mod tests {
67// use reactive_graph_behaviour_api::BehaviourCreationError;
68// use serde_json::json;
69//
70// use crate::behaviour::entity::gate::function::*;
71// use reactive_graph_graph::prelude::*;
72// use reactive_graph_reactive_model_impl::ReactiveEntity;
73// use reactive_graph_std_numeric_model::NumericGateF64;
74// use reactive_graph_std_numeric_model::NumericGateProperties;
75// use reactive_graph_reactive_api::prelude::*;
76// use crate::behaviour::entity::gate::tests::numeric_gate;
77//
78// #[test]
79// fn numeric_operation_behaviour_test() {
80// let lhs: f64 = 0.5;
81// let rhs: f64 = 0.5;
82//
83// assert_eq!(lhs.atan2(rhs), test_numeric_gate_behaviour(FN_ATAN2_F64, lhs, rhs).unwrap());
84// assert_eq!(lhs.hypot(rhs), test_numeric_gate_behaviour(FN_HYPOT_F64, lhs, rhs).unwrap());
85// assert_eq!(lhs.log(rhs), test_numeric_gate_behaviour(FN_LOG_F64, lhs, rhs).unwrap());
86// assert_eq!(lhs.powf(rhs), test_numeric_gate_behaviour(FN_POW_F64, lhs, rhs).unwrap());
87// }
88//
89// fn test_numeric_gate_behaviour(f: NumericGateFunction<f64>, lhs: f64, rhs: f64) -> Option<f64> {
90// let b = create_numeric_gate_behaviour(f).unwrap();
91// b.lhs(lhs);
92// b.rhs(rhs);
93// b.result()
94// }
95//
96// fn create_numeric_gate_behaviour(f: NumericGateFunction<f64>) -> Result<NumericGateF64, BehaviourCreationError> {
97//
98// let numeric_gate = create_numeric_gate_entity();
99// NumericGateF64::from(numeric_gate("numeric", "abs"), f)
100// }
101//
102// fn create_numeric_gate_entity() -> ReactiveEntity {
103// ReactiveEntityInstanceBuilder::new("numeric", "abs")
104// .property(NumericGateProperties::LHS.as_ref(), json!(NumericGateProperties::LHS.default_value()))
105// .property(NumericGateProperties::RHS.as_ref(), json!(NumericGateProperties::RHS.default_value()))
106// .property(NumericGateProperties::RESULT.as_ref(), json!(NumericGateProperties::RESULT.default_value()))
107// .build()
108// }
109// }