reactive_graph_std_string/behaviour/entity/string_string_number_gate/
mod.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 serde_json::Value;
7use serde_json::json;
8use uuid::Uuid;
9
10pub use function::STRING_STRING_NUMBER_GATES;
11pub use function::StringStringNumberFunction;
12
13use reactive_graph_std_result_model::ResultNumberU64Properties::RESULT;
14use reactive_graph_std_string_model::StringStringNumberGateProperties::LHS;
15use reactive_graph_std_string_model::StringStringNumberGateProperties::RHS;
16
17pub mod function;
18
19entity_behaviour!(
20    StringStringNumberGate,
21    StringStringNumberGateFactory,
22    StringStringNumberGateFsm,
23    StringStringNumberGateBehaviourTransitions,
24    StringStringNumberGateValidator,
25    f,
26    StringStringNumberFunction
27);
28
29behaviour_validator!(StringStringNumberGateValidator, Uuid, ReactiveEntity, LHS.as_ref(), RESULT.as_ref());
30
31impl BehaviourInit<Uuid, ReactiveEntity> for StringStringNumberGateBehaviourTransitions {
32    fn init(&self) -> Result<(), BehaviourInitializationFailed> {
33        let lhs = self
34            .reactive_instance
35            .get(LHS)
36            .and_then(|lhs| lhs.as_str().map(|lhs| lhs.to_string()))
37            .ok_or(BehaviourInitializationFailed {})?;
38        let rhs = self
39            .reactive_instance
40            .get(RHS)
41            .and_then(|rhs| rhs.as_str().map(|rhs| rhs.to_string()))
42            .ok_or(BehaviourInitializationFailed {})?;
43        let f = self.f;
44        self.reactive_instance.set(RESULT, json!(f(lhs, rhs)));
45        Ok(())
46    }
47}
48
49impl BehaviourConnect<Uuid, ReactiveEntity> for StringStringNumberGateBehaviourTransitions {
50    fn connect(&self) -> Result<(), BehaviourConnectFailed> {
51        let reactive_instance = self.reactive_instance.clone();
52        let f = self.f;
53        self.property_observers.observe_with_handle(LHS.as_ref(), move |lhs: &Value| {
54            if let Some(lhs) = lhs.as_str().map(|lhs| lhs.to_string()) {
55                if let Some(rhs) = reactive_instance.get(RHS).and_then(|rhs| rhs.as_str().map(|rhs| rhs.to_string())) {
56                    reactive_instance.set(RESULT, json!(f(lhs, rhs)));
57                }
58            }
59        });
60        let reactive_instance = self.reactive_instance.clone();
61        let f = self.f;
62        self.property_observers.observe_with_handle(RHS.as_ref(), move |rhs: &Value| {
63            if let Some(rhs) = rhs.as_str().map(|rhs| rhs.to_string()) {
64                if let Some(lhs) = reactive_instance.get(LHS).and_then(|lhs| lhs.as_str().map(|lhs| lhs.to_string())) {
65                    reactive_instance.set(RESULT, json!(f(lhs, rhs)));
66                }
67            }
68        });
69        Ok(())
70    }
71}
72
73impl BehaviourShutdown<Uuid, ReactiveEntity> for StringStringNumberGateBehaviourTransitions {}
74impl BehaviourTransitions<Uuid, ReactiveEntity> for StringStringNumberGateBehaviourTransitions {}