reactive_graph_behaviour_model_impl/behaviour/relation/
behaviour.rs

1#[macro_export]
2macro_rules! relation_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::ReactiveRelation,
24            pub fsm: $fsm,
25        }
26
27        impl $behaviour {
28            pub fn new(reactive_instance: reactive_graph_reactive_model_impl::ReactiveRelation, 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                // TODO: auto connect
34                behaviour
35                    .fsm
36                    .transition($crate::BehaviourState::Connected)
37                    .map_err($crate::BehaviourCreationError::BehaviourTransitionError)?;
38                Ok(std::sync::Arc::new(behaviour))
39            }
40        }
41
42        impl reactive_graph_behaviour_model_api::BehaviourFsm<reactive_graph_graph::RelationInstanceId, reactive_graph_reactive_model_impl::ReactiveRelation> for $behaviour {
43            fn ty(&self) -> &reactive_graph_behaviour_model_api::BehaviourTypeId {
44                &self.fsm.ty
45            }
46
47            fn get_state(&self) -> reactive_graph_behaviour_model_api::BehaviourState {
48                self.fsm.get_state()
49            }
50
51            fn set_state(&self, state: reactive_graph_behaviour_model_api::BehaviourState) {
52                self.fsm.set_state(state);
53            }
54
55            fn get_validator(&self) -> &dyn reactive_graph_behaviour_model_api::BehaviourValidator<reactive_graph_graph::RelationInstanceId, reactive_graph_reactive_model_impl::ReactiveRelation> {
56                &self.fsm.validator
57            }
58
59            fn get_transitions(&self) -> &dyn reactive_graph_behaviour_model_api::BehaviourTransitions<reactive_graph_graph::RelationInstanceId, reactive_graph_reactive_model_impl::ReactiveRelation> {
60                &self.fsm.transitions
61            }
62        }
63
64        impl reactive_graph_reactive_model_api::ReactiveInstanceContainer<reactive_graph_graph::RelationInstanceId, reactive_graph_reactive_model_impl::ReactiveRelation> for $behaviour {
65            fn get_reactive_instance(&self) -> &reactive_graph_reactive_model_impl::ReactiveRelation {
66                &self.reactive_instance
67            }
68
69            fn get(&self, property_name: &str) -> Option<serde_json::Value> {
70                self.reactive_instance.get(property_name)
71            }
72
73            fn set(&self, property_name: &str, value: serde_json::Value) {
74                self.reactive_instance.set(property_name, value);
75            }
76        }
77
78        impl Drop for $behaviour {
79            fn drop(&mut self) {
80                log::trace!("Drop relation behaviour {}", &self.fsm.ty);
81            }
82        }
83
84        reactive_graph_behaviour_model_api::behaviour_factory!($factory, $behaviour, reactive_graph_graph::RelationInstanceId, reactive_graph_reactive_model_impl::ReactiveRelation $(, $fn_name, $fn_ident)*);
85
86        reactive_graph_behaviour_model_api::behaviour_fsm!($fsm, $validator, $transitions, reactive_graph_graph::RelationInstanceId, reactive_graph_reactive_model_impl::ReactiveRelation);
87
88        $crate::relation_behaviour_transitions!($transitions $(, $fn_name, $fn_ident)*);
89    };
90}