reactive_graph_reactive_service_api/
reactive_entity_manager.rs

1use std::collections::HashMap;
2
3use async_trait::async_trait;
4use serde_json::Value;
5use springtime_di::injectable;
6use uuid::Uuid;
7
8use crate::ReactiveEntityComponentAddError;
9use crate::ReactiveEntityCreationError;
10use crate::ReactiveEntityPropertyAddError;
11use crate::ReactiveEntityPropertyRemoveError;
12use crate::ReactiveEntityRegistrationError;
13use reactive_graph_behaviour_model_api::BehaviourTypeId;
14use reactive_graph_behaviour_model_api::ComponentBehaviourTypeId;
15use reactive_graph_behaviour_model_api::EntityBehaviourTypeId;
16use reactive_graph_graph::ComponentTypeId;
17use reactive_graph_graph::EntityInstance;
18use reactive_graph_graph::EntityTypeId;
19use reactive_graph_graph::Mutability;
20use reactive_graph_graph::PropertyInstances;
21use reactive_graph_lifecycle::Lifecycle;
22use reactive_graph_reactive_model_impl::ReactiveEntity;
23
24#[injectable]
25#[async_trait]
26pub trait ReactiveEntityManager: Send + Sync + Lifecycle {
27    /// Returns true, if an entity instance exists with the given UUID.
28    fn has(&self, id: Uuid) -> bool;
29
30    /// Returns the reactive entity instance with the given UUID or None.
31    fn get(&self, id: Uuid) -> Option<ReactiveEntity>;
32
33    /// Returns the reactive entity instance that matches the given label or None.
34    fn get_by_label(&self, label: &str) -> Option<ReactiveEntity>;
35
36    /// Returns the reactive entity instance and the matched path parameters that matches the given label or None.
37    /// /io/reactive-graph/local/users/:user_id
38    /// /io/reactive-graph/local/users/PeterPenacka returns: (instance, {"user_id": "PeterPenacka"})
39    fn get_by_label_with_params(&self, label: &str) -> Option<(ReactiveEntity, HashMap<String, String>)>;
40
41    /// Returns all registered reactive entity instances.
42    fn get_all(&self) -> Vec<ReactiveEntity>;
43
44    /// Returns all reactive entity instances of the given type.
45    fn get_by_type(&self, ty: &EntityTypeId) -> Vec<ReactiveEntity>;
46
47    /// Returns all reactive entity instances of the given type which are of the given component..
48    fn get_by_component(&self, component_ty: &ComponentTypeId) -> Vec<ReactiveEntity>;
49
50    /// Returns all reactive entity instances of the given type which behaves as the given behaviour.
51    fn get_by_behaviour(&self, behaviour_ty: &BehaviourTypeId) -> Vec<ReactiveEntity>;
52
53    /// Returns all reactive entity instances of the given namespace.
54    fn get_by_namespace(&self, namespace: &str) -> Vec<ReactiveEntity>;
55
56    /// Returns the ids of all registered reactive entity instances.
57    fn get_ids(&self) -> Vec<Uuid>;
58
59    /// Returns the count of registered reactive entity instances.
60    fn count(&self) -> usize;
61
62    /// Returns the count of registered reactive entity instances of the given type.
63    fn count_by_type(&self, ty: &EntityTypeId) -> usize;
64
65    /// Returns the count of registered reactive entity instances which are of the given component.
66    fn count_by_component(&self, component_ty: &ComponentTypeId) -> usize;
67
68    /// Returns the count of registered reactive entity instances which behaves as the given behaviour.
69    fn count_by_behaviour(&self, behaviour_ty: &BehaviourTypeId) -> usize;
70
71    /// Creates a new reactive entity instance of the given type. The reactive instance will be
72    /// initialized with the given properties and values. A random id will be generated.
73    fn create_reactive_entity(&self, ty: &EntityTypeId, properties: PropertyInstances) -> Result<ReactiveEntity, ReactiveEntityCreationError>;
74
75    /// Creates a new reactive entity instance of the given type, with the given id and initialized
76    /// with the given properties and values.
77    fn create_with_id(&self, ty: &EntityTypeId, id: Uuid, properties: PropertyInstances) -> Result<ReactiveEntity, ReactiveEntityCreationError>;
78
79    /// Creates a reactive entity instance from the given non-reactive entity instance. The
80    /// reactive entity instance will be registered.
81    fn create_reactive_instance(&self, entity_instance: EntityInstance) -> Result<ReactiveEntity, ReactiveEntityCreationError>;
82
83    /// Registers a reactive entity instance and applies components and behaviours.
84    fn register_reactive_instance(&self, reactive_entity: ReactiveEntity) -> Result<ReactiveEntity, ReactiveEntityRegistrationError>;
85
86    /// Registers a reactive entity instance if and only if the given instance doesn't exist.
87    ///
88    /// No properties are merged if the given entity instance already exists.
89    fn register_or_merge_reactive_instance(&self, reactive_entity: ReactiveEntity) -> Result<ReactiveEntity, ReactiveEntityRegistrationError>;
90
91    /// Adds the component with the given name to the entity instance with the given id.
92    fn add_component(&self, id: Uuid, component_ty: &ComponentTypeId) -> Result<(), ReactiveEntityComponentAddError>;
93
94    /// Removes the component with the given name from the entity instance with the given id.
95    fn remove_component(&self, id: Uuid, component_ty: &ComponentTypeId);
96
97    /// Adds the property with the given name and initial value to the entity instance with the given id.
98    fn add_property(&self, id: Uuid, property_name: &str, mutability: Mutability, value: Value) -> Result<(), ReactiveEntityPropertyAddError>;
99
100    /// Removes the property with the given name from the entity instance with the given id.
101    fn remove_property(&self, id: Uuid, property_name: &str) -> Result<(), ReactiveEntityPropertyRemoveError>;
102
103    /// Adds the given behaviour to all instances of the given entity type.
104    fn add_behaviour_to_all_entity_instances(&self, entity_behaviour_ty: &EntityBehaviourTypeId);
105
106    /// Adds the given behaviour to all instances of the given entity type.
107    fn add_behaviour_to_all_entity_components(&self, component_behaviour_ty: &ComponentBehaviourTypeId);
108
109    fn delete(&self, id: Uuid) -> bool;
110
111    // TODO: fn delete_and_delete_relations(&self, id: Uuid);
112
113    /// Unregisters the reactive entity instance. Also removes all behaviours. If there are any
114    /// references to the reactive entity instance, their reactive streams still work but the
115    /// applied behaviours are gone.
116    fn unregister_reactive_instance(&self, id: Uuid) -> bool;
117
118    // TODO: rename import_from_file
119    // fn import(&self, path: &str) -> Result<ReactiveEntity, ReactiveEntityImportError>;
120
121    // TODO: import_from_json_string
122    // Goal: web-ui: upload entity instance as json file
123
124    // TODO: return result
125    // TODO: rename export_as_file
126    // fn export(&self, id: Uuid, path: &str);
127
128    // TODO: implement export_as_json_string
129    // Goal: web-ui: download entity instance as json file
130    // fn export_as_json_string(&self, id: Uuid) -> String;
131
132    fn handle_component_added_events(&self);
133
134    fn handle_component_removed_events(&self);
135
136    fn handle_property_added_events(&self);
137
138    fn handle_property_removed_events(&self);
139}