reactive_graph_behaviour_model_impl/behaviour/entity/
behaviour.rs

1#[macro_export]
2macro_rules! entity_behaviour {
3    (
4        /// The ident of the behaviour.
5        $behaviour: ident,
6        /// The ident of the factory to create instances of the behaviour.
7        $factory: ident,
8        /// The ident of the finite state machine of the behaviour.
9        $fsm: ident,
10        /// The ident of the transitions of the finite state machine.
11        $transitions: ident,
12        /// The ident of the property validator of the behaviour.
13        $validator: ty
14        $(,
15            // Function name.
16            $fn_name: ident,
17            // Function.
18            $fn_ident: ident
19        )*
20        $(,)?
21    ) => {
22        pub struct $behaviour {
23            pub reactive_instance: reactive_graph_reactive_model_impl::ReactiveEntity,
24            pub fsm: $fsm,
25        }
26
27        impl $behaviour {
28            pub fn new(reactive_instance: reactive_graph_reactive_model_impl::ReactiveEntity, ty: reactive_graph_behaviour_model_api::BehaviourTypeId, $($fn_name: $fn_ident)*) -> Result<std::sync::Arc<$behaviour>, $crate::BehaviourCreationError> {
29                let transitions = <$transitions>::new(reactive_instance.clone(), ty.clone() $(, $fn_name)*);
30                let validator = <$validator>::new(reactive_instance.clone());
31                let fsm = <$fsm>::new(reactive_instance.clone(), ty, validator, transitions);
32                let mut behaviour = $behaviour { reactive_instance, fsm };
33                behaviour
34                    .fsm
35                    .transition($crate::BehaviourState::Connected)
36                    .map_err($crate::BehaviourCreationError::BehaviourTransitionError)?;
37                Ok(std::sync::Arc::new(behaviour))
38            }
39        }
40
41        impl reactive_graph_behaviour_model_api::BehaviourFsm<uuid::Uuid, reactive_graph_reactive_model_impl::ReactiveEntity> for $behaviour {
42            fn ty(&self) -> &reactive_graph_behaviour_model_api::BehaviourTypeId {
43                &self.fsm.ty
44            }
45
46            fn get_state(&self) -> reactive_graph_behaviour_model_api::BehaviourState {
47                self.fsm.get_state()
48            }
49
50            fn set_state(&self, state: reactive_graph_behaviour_model_api::BehaviourState) {
51                self.fsm.set_state(state);
52            }
53
54            fn get_validator(&self) -> &dyn reactive_graph_behaviour_model_api::BehaviourValidator<uuid::Uuid, reactive_graph_reactive_model_impl::ReactiveEntity> {
55                &self.fsm.validator
56            }
57
58            fn get_transitions(&self) -> &dyn reactive_graph_behaviour_model_api::BehaviourTransitions<uuid::Uuid, reactive_graph_reactive_model_impl::ReactiveEntity> {
59                &self.fsm.transitions
60            }
61        }
62
63        impl reactive_graph_reactive_model_api::ReactiveInstanceContainer<uuid::Uuid, reactive_graph_reactive_model_impl::ReactiveEntity> for $behaviour {
64            fn get_reactive_instance(&self) -> &reactive_graph_reactive_model_impl::ReactiveEntity {
65                &self.reactive_instance
66            }
67
68            fn get(&self, property_name: &str) -> Option<serde_json::Value> {
69                // reactive_graph_graph::PropertyInstanceGetter::get(self, property_name)
70                self.reactive_instance.get(property_name)
71            }
72
73            fn set(&self, property_name: &str, value: serde_json::Value) {
74                // reactive_graph_graph::PropertyInstanceSetter::get(self, property_name, value)
75                self.reactive_instance.set(property_name, value);
76            }
77        }
78
79        impl Drop for $behaviour {
80            fn drop(&mut self) {
81                log::trace!("Drop entity behaviour {}", &self.fsm.ty);
82            }
83        }
84
85        reactive_graph_behaviour_model_api::behaviour_factory!($factory, $behaviour, uuid::Uuid, reactive_graph_reactive_model_impl::ReactiveEntity $(, $fn_name, $fn_ident)*);
86
87        reactive_graph_behaviour_model_api::behaviour_fsm!($fsm, $validator, $transitions, uuid::Uuid, reactive_graph_reactive_model_impl::ReactiveEntity);
88
89        $crate::entity_behaviour_transitions!($transitions $(, $fn_name, $fn_ident)*);
90    };
91}