reactive_graph_behaviour_model_impl/relation/
storage.rs

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