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