reactive_graph_behaviour_model_impl/entity/
storage.rs

1use std::sync::Arc;
2
3use dashmap::DashMap;
4use uuid::Uuid;
5
6use reactive_graph_behaviour_model_api::prelude::*;
7
8use crate::BehaviourStorage;
9use reactive_graph_reactive_model_impl::ReactiveEntity;
10
11pub struct EntityBehaviourStorage(BehaviourStorage<Uuid, ReactiveEntity>);
12
13impl EntityBehaviourStorage {
14    pub fn new() -> Self {
15        EntityBehaviourStorage(DashMap::new())
16    }
17
18    pub fn insert(
19        &self,
20        key: Uuid,
21        ty: BehaviourTypeId,
22        behaviour: Arc<dyn BehaviourFsm<Uuid, ReactiveEntity> + Send + Sync>,
23    ) -> Option<Arc<dyn BehaviourFsm<Uuid, ReactiveEntity> + Send + Sync>> {
24        if !self.0.contains_key(&key) {
25            self.0.insert(key, DashMap::new());
26        }
27        if let Some(instance_behaviours) = self.0.get(&key) {
28            return instance_behaviours.value().insert(ty, behaviour);
29        }
30        None
31    }
32
33    pub fn remove(&self, key: &Uuid, ty: &BehaviourTypeId) -> Option<(BehaviourTypeId, Arc<dyn BehaviourFsm<Uuid, ReactiveEntity> + Send + Sync>)> {
34        if let Some(instance_behaviours) = self.0.get(key) {
35            return instance_behaviours.value().remove(ty);
36        }
37        None
38    }
39
40    /// Removes all behaviours of the given behaviour type.
41    pub fn remove_by_behaviour(&self, ty: &BehaviourTypeId) {
42        for instance_behaviours in self.0.iter_mut() {
43            instance_behaviours.value().remove(ty);
44        }
45    }
46
47    pub fn remove_all(&self, key: &Uuid) {
48        self.0.remove(key);
49    }
50
51    pub fn has(&self, key: &Uuid, ty: &BehaviourTypeId) -> bool {
52        if let Some(instance_behaviours) = self.0.get(key) {
53            return instance_behaviours.value().get(ty).is_some();
54        }
55        false
56    }
57
58    pub fn get(&self, key: &Uuid, ty: &BehaviourTypeId) -> Option<Arc<dyn BehaviourFsm<Uuid, ReactiveEntity> + Send + Sync>> {
59        if let Some(instance_behaviours) = self.0.get(key) {
60            if let Some(fsm) = instance_behaviours.value().get(ty) {
61                return Some(fsm.value().clone());
62            }
63        }
64        None
65    }
66
67    pub fn get_by_behaviour(&self, ty: &BehaviourTypeId) -> Vec<Arc<dyn BehaviourFsm<Uuid, ReactiveEntity> + Send + Sync>> {
68        let mut fsms = vec![];
69        for instance_behaviours in self.0.iter() {
70            if let Some(fsm) = instance_behaviours.value().get(ty) {
71                fsms.push(fsm.value().clone());
72            }
73        }
74        fsms
75    }
76
77    pub fn get_behaviours_by_instance(&self, key: &Uuid) -> Vec<BehaviourTypeId> {
78        if let Some(instance_behaviours) = self.0.get(key) {
79            return instance_behaviours.value().iter().map(|b| b.key().clone()).collect();
80        }
81        Vec::new()
82    }
83
84    pub fn get_instances_by_behaviour(&self, ty: &BehaviourTypeId) -> Vec<ReactiveEntity> {
85        let mut instances = vec![];
86        for instance_behaviours in self.0.iter() {
87            if let Some(inner) = instance_behaviours.value().get(ty) {
88                instances.push(inner.value().get_reactive_instance().clone());
89            }
90        }
91        instances
92    }
93}
94
95impl Default for EntityBehaviourStorage {
96    fn default() -> Self {
97        Self::new()
98    }
99}