reactive_graph_plugin_delegates/
entity_instance_manager_impl.rs

1use std::collections::HashMap;
2use std::sync::Arc;
3
4use uuid::Uuid;
5
6use reactive_graph_behaviour_model_api::BehaviourTypeId;
7use reactive_graph_graph::ComponentTypeId;
8use reactive_graph_graph::EntityInstance;
9use reactive_graph_graph::EntityTypeId;
10use reactive_graph_reactive_model_impl::ReactiveEntity;
11use reactive_graph_reactive_service_api::ReactiveEntityComponentAddError;
12use reactive_graph_reactive_service_api::ReactiveEntityCreationError;
13use reactive_graph_reactive_service_api::ReactiveEntityManager;
14use reactive_graph_reactive_service_api::ReactiveEntityRegistrationError;
15use reactive_graph_type_system_api::ComponentManager;
16use reactive_graph_type_system_api::EntityTypeManager;
17
18pub struct EntityInstanceManagerDelegate {
19    component_manager: Arc<dyn ComponentManager + Send + Sync>,
20    entity_type_manager: Arc<dyn EntityTypeManager + Send + Sync>,
21    reactive_entity_manager: Arc<dyn ReactiveEntityManager + Send + Sync>,
22}
23
24impl EntityInstanceManagerDelegate {
25    pub fn new(
26        component_manager: Arc<dyn ComponentManager + Send + Sync>,
27        entity_type_manager: Arc<dyn EntityTypeManager + Send + Sync>,
28        reactive_entity_manager: Arc<dyn ReactiveEntityManager + Send + Sync>,
29    ) -> Self {
30        Self {
31            component_manager,
32            entity_type_manager,
33            reactive_entity_manager,
34        }
35    }
36}
37
38impl reactive_graph_plugin_api::EntityInstanceManager for EntityInstanceManagerDelegate {
39    fn has(&self, id: Uuid) -> bool {
40        self.reactive_entity_manager.has(id)
41    }
42
43    fn get(&self, id: Uuid) -> Option<ReactiveEntity> {
44        self.reactive_entity_manager.get(id)
45    }
46
47    fn get_by_label(&self, label: &str) -> Option<ReactiveEntity> {
48        self.reactive_entity_manager.get_by_label(label)
49    }
50
51    fn get_by_label_with_params(&self, label: &str) -> Option<(ReactiveEntity, HashMap<String, String>)> {
52        self.reactive_entity_manager.get_by_label_with_params(label)
53    }
54
55    fn get_all(&self) -> Vec<ReactiveEntity> {
56        self.reactive_entity_manager.get_all()
57    }
58
59    fn get_by_type(&self, ty: &EntityTypeId) -> Vec<ReactiveEntity> {
60        self.reactive_entity_manager.get_by_type(ty)
61    }
62
63    fn get_ids(&self) -> Vec<Uuid> {
64        self.reactive_entity_manager.get_ids()
65    }
66
67    fn count(&self) -> usize {
68        self.reactive_entity_manager.count()
69    }
70
71    fn count_by_type(&self, ty: &EntityTypeId) -> usize {
72        self.reactive_entity_manager.count_by_type(ty)
73    }
74
75    fn count_by_component(&self, component: &ComponentTypeId) -> usize {
76        self.reactive_entity_manager.count_by_component(component)
77    }
78
79    fn count_by_behaviour(&self, behaviour_ty: &BehaviourTypeId) -> usize {
80        self.reactive_entity_manager.count_by_behaviour(behaviour_ty)
81    }
82
83    fn create(&self, entity_instance: EntityInstance) -> Result<ReactiveEntity, ReactiveEntityCreationError> {
84        match self.entity_type_manager.get(&entity_instance.ty) {
85            Some(entity_type) => {
86                let entity_instance = entity_instance;
87                // Add properties from entity type if not existing
88                for (property_name, property_type) in entity_type.properties {
89                    if !entity_instance.properties.contains_key(&property_name) {
90                        entity_instance
91                            .properties
92                            .insert(property_name.clone(), property_type.data_type.default_value());
93                    }
94                }
95                // Add properties from components if not existing
96                for component in entity_type.components.iter() {
97                    if let Some(component) = self.component_manager.get(component.key()) {
98                        for (property_name, property_type) in component.properties {
99                            if !entity_instance.properties.contains_key(&property_name) {
100                                entity_instance
101                                    .properties
102                                    .insert(property_name.clone(), property_type.data_type.default_value());
103                            }
104                        }
105                    }
106                }
107                self.reactive_entity_manager
108                    .create_with_id(&entity_instance.ty, entity_instance.id, entity_instance.properties)
109            }
110            None => Err(ReactiveEntityCreationError::ReactiveEntityRegistrationError(
111                ReactiveEntityRegistrationError::UnknownEntityType(entity_instance.ty.clone()),
112            )),
113        }
114    }
115
116    fn register(&self, reactive_entity: ReactiveEntity) -> Result<ReactiveEntity, ReactiveEntityRegistrationError> {
117        self.reactive_entity_manager.register_reactive_instance(reactive_entity)
118    }
119
120    fn add_component(&self, id: Uuid, component: &ComponentTypeId) -> Result<(), ReactiveEntityComponentAddError> {
121        self.reactive_entity_manager.add_component(id, component)
122    }
123
124    fn remove_component(&self, id: Uuid, component: &ComponentTypeId) {
125        self.reactive_entity_manager.remove_component(id, component);
126    }
127
128    fn delete(&self, id: Uuid) -> bool {
129        self.reactive_entity_manager.delete(id)
130    }
131}