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