reactive_graph_std_string/plugin.rs
1use reactive_graph_plugin_api::EntityBehaviourRegistry;
2use reactive_graph_plugin_api::prelude::plugin::*;
3use reactive_graph_plugin_api::prelude::providers::*;
4use reactive_graph_std_string_model::BEHAVIOUR_TEMPLATING;
5use reactive_graph_std_string_model::ENTITY_BEHAVIOUR_TEMPLATING;
6
7use crate::behaviour::entity::string_bool_operation::function::STRING_BOOL_OPERATIONS;
8use crate::behaviour::entity::string_comparison::function::STRING_COMPARISONS;
9use crate::behaviour::entity::string_gate::STRING_GATES;
10use crate::behaviour::entity::string_number_operation::STRING_NUMBER_OPERATIONS;
11use crate::behaviour::entity::string_string_number_gate::STRING_STRING_NUMBER_GATES;
12use crate::behaviour::entity::templating::TemplatingFactory;
13
14export_plugin!({
15 "dependencies": [
16 { "name": "reactive-graph-std-base", "version": ">=0.10.0, <0.11.0" },
17 { "name": "reactive-graph-std-result", "version": ">=0.10.0, <0.11.0" }
18 ]
19});
20
21#[injectable]
22pub trait StringPlugin: Plugin + Send + Sync {}
23
24#[derive(Component)]
25pub struct StringPluginImpl {
26 component_provider: Arc<dyn TypeProvider<Components> + Send + Sync>,
27
28 #[component(default = "component_provider_registry")]
29 component_provider_registry: Arc<dyn ComponentProviderRegistry + Send + Sync>,
30
31 entity_types_provider: Arc<dyn TypeProvider<EntityTypes> + Send + Sync>,
32
33 #[component(default = "entity_types_provider_registry")]
34 entity_type_provider_registry: Arc<dyn EntityTypeProviderRegistry + Send + Sync>,
35
36 #[component(default = "entity_behaviour_registry")]
37 entity_behaviour_registry: Arc<dyn EntityBehaviourRegistry + Send + Sync>,
38}
39
40#[async_trait]
41#[component_alias]
42impl Plugin for StringPluginImpl {
43 async fn activate(&self) -> Result<(), PluginActivationError> {
44 self.component_provider_registry.register_provider(self.component_provider.clone()).await;
45 self.entity_type_provider_registry.register_provider(self.entity_types_provider.clone()).await;
46 self.entity_behaviour_registry.register_all(&STRING_BOOL_OPERATIONS.get_factories()).await;
47 self.entity_behaviour_registry.register_all(&STRING_COMPARISONS.get_factories()).await;
48 self.entity_behaviour_registry.register_all(&STRING_GATES.get_factories()).await;
49 self.entity_behaviour_registry.register_all(&STRING_NUMBER_OPERATIONS.get_factories()).await;
50 self.entity_behaviour_registry.register_all(&STRING_STRING_NUMBER_GATES.get_factories()).await;
51 self.entity_behaviour_registry
52 .register(ENTITY_BEHAVIOUR_TEMPLATING.clone(), Arc::new(TemplatingFactory::new(BEHAVIOUR_TEMPLATING.clone())))
53 .await;
54 Ok(())
55 }
56
57 async fn deactivate(&self) -> Result<(), PluginDeactivationError> {
58 self.entity_behaviour_registry.unregister(&ENTITY_BEHAVIOUR_TEMPLATING).await;
59 self.entity_behaviour_registry
60 .unregister_all(&STRING_STRING_NUMBER_GATES.to_entity_behaviour_tys())
61 .await;
62 self.entity_behaviour_registry
63 .unregister_all(&STRING_NUMBER_OPERATIONS.to_entity_behaviour_tys())
64 .await;
65 self.entity_behaviour_registry.unregister_all(&STRING_GATES.to_entity_behaviour_tys()).await;
66 self.entity_behaviour_registry
67 .unregister_all(&STRING_COMPARISONS.to_entity_behaviour_tys())
68 .await;
69 self.entity_behaviour_registry
70 .unregister_all(&STRING_BOOL_OPERATIONS.to_entity_behaviour_tys())
71 .await;
72 self.entity_type_provider_registry.unregister_provider(self.entity_types_provider.id()).await;
73 self.component_provider_registry.unregister_provider(self.component_provider.id()).await;
74 Ok(())
75 }
76}
77
78// use std::sync::Arc;
79// use std::sync::RwLock;
80//
81// use async_trait::async_trait;
82//
83// use crate::behaviour::entity::string_bool_operation::StringBoolOperationFactory;
84// use crate::behaviour::entity::string_bool_operation::STRING_BOOL_OPERATIONS;
85// use crate::behaviour::entity::string_comparison::function::STRING_COMPARISONS;
86// use crate::behaviour::entity::string_comparison::StringComparisonFactory;
87// use crate::behaviour::entity::string_gate::StringGateFactory;
88// use crate::behaviour::entity::string_gate::STRING_GATES;
89// use crate::behaviour::entity::string_number_operation::StringNumberOperationFactory;
90// use crate::behaviour::entity::string_number_operation::STRING_NUMBER_OPERATIONS;
91// use crate::behaviour::entity::string_operation::StringOperationFactory;
92// use crate::behaviour::entity::string_operation::STRING_OPERATIONS;
93// use crate::behaviour::entity::templating::TemplatingFactory;
94// use crate::di::*;
95// use crate::model_string::BEHAVIOUR_TEMPLATING;
96// use crate::model_string::ENTITY_BEHAVIOUR_TEMPLATING;
97// use crate::plugins::component_provider;
98// use crate::plugins::entity_type_provider;
99// use crate::plugins::plugin_context::PluginContext;
100// use crate::plugins::ComponentProvider;
101// use crate::plugins::ComponentProviderError;
102// use crate::plugins::EntityTypeProvider;
103// use crate::plugins::EntityTypeProviderError;
104// use crate::plugins::Plugin;
105// use crate::plugins::PluginActivationError;
106// use crate::plugins::PluginContextDeinitializationError;
107// use crate::plugins::PluginContextInitializationError;
108// use crate::plugins::PluginDeactivationError;
109// use crate::providers::StringComponentProviderImpl;
110// use crate::providers::StringEntityTypeProviderImpl;
111// use reactive_graph_graph::EntityBehaviourTypeId;
112//
113// #[wrapper]
114// pub struct PluginContextContainer(RwLock<Option<Arc<dyn PluginContext>>>);
115//
116// #[provides]
117// fn create_empty_plugin_context_container() -> PluginContextContainer {
118// PluginContextContainer(RwLock::new(None))
119// }
120//
121// pub trait StringPlugin: Plugin + Send + Sync {}
122//
123// #[component]
124// pub struct StringPluginImpl {
125// component_provider: Wrc<StringComponentProviderImpl>,
126// entity_type_provider: Wrc<StringEntityTypeProviderImpl>,
127//
128// context: PluginContextContainer,
129// }
130//
131// impl StringPluginImpl {}
132//
133// interfaces!(StringPluginImpl: dyn Plugin);
134//
135// #[provides]
136// impl StringPlugin for StringPluginImpl {}
137//
138// #[async_trait]
139// impl Plugin for StringPluginImpl {
140// async fn activate(&self) -> Result<(), PluginActivationError> {
141// let guard = self.context.0.read().unwrap();
142// if let Some(context) = guard.clone() {
143// let entity_behaviour_registry = context.get_entity_behaviour_registry();
144// // String Bool Operations
145// // fn(String) -> bool
146// for (behaviour_ty, f) in STRING_BOOL_OPERATIONS.iter() {
147// entity_behaviour_registry
148// .register(EntityBehaviourTypeId::from(behaviour_ty), Arc::new(StringBoolOperationFactory::new(behaviour_ty.clone(), *f)));
149// }
150// // String Comparisons
151// // fn(String, String) -> bool
152// for (behaviour_ty, f) in STRING_COMPARISONS.iter() {
153// entity_behaviour_registry.register(EntityBehaviourTypeId::from(behaviour_ty), Arc::new(StringComparisonFactory::new(behaviour_ty.clone(), *f)));
154// }
155// // String Gates
156// // fn(String, String) -> String
157// for (behaviour_ty, f) in STRING_GATES.iter() {
158// entity_behaviour_registry.register(EntityBehaviourTypeId::from(behaviour_ty), Arc::new(StringGateFactory::new(behaviour_ty.clone(), *f)));
159// }
160// // String Number Operations
161// // fn(String) -> Number
162// for (behaviour_ty, f) in STRING_NUMBER_OPERATIONS.iter() {
163// entity_behaviour_registry.register(
164// EntityBehaviourTypeId::from(behaviour_ty),
165// Arc::new(StringNumberOperationFactory::new(behaviour_ty.clone(), *f)),
166// );
167// }
168// // String Operations
169// // fn(String) -> String
170// for (behaviour_ty, f) in STRING_OPERATIONS.iter() {
171// entity_behaviour_registry.register(EntityBehaviourTypeId::from(behaviour_ty), Arc::new(StringOperationFactory::new(behaviour_ty.clone(), *f)));
172// }
173// // String String Number Gates
174// // fn(String, String) -> Number
175// for (behaviour_ty, f) in STRING_OPERATIONS.iter() {
176// entity_behaviour_registry.register(EntityBehaviourTypeId::from(behaviour_ty), Arc::new(StringOperationFactory::new(behaviour_ty.clone(), *f)));
177// }
178//
179// // Templating
180// let factory = Arc::new(TemplatingFactory::new(BEHAVIOUR_TEMPLATING.clone()));
181// entity_behaviour_registry.register(ENTITY_BEHAVIOUR_TEMPLATING.clone(), factory);
182// }
183// Ok(())
184// }
185//
186// async fn deactivate(&self) -> Result<(), PluginDeactivationError> {
187// let guard = self.context.0.read().unwrap();
188// if let Some(context) = guard.clone() {
189// let entity_behaviour_registry = context.get_entity_behaviour_registry();
190// for behaviour_ty in STRING_BOOL_OPERATIONS.keys() {
191// entity_behaviour_registry.unregister(&EntityBehaviourTypeId::from(behaviour_ty));
192// }
193// for behaviour_ty in STRING_COMPARISONS.keys() {
194// entity_behaviour_registry.unregister(&EntityBehaviourTypeId::from(behaviour_ty));
195// }
196// for behaviour_ty in STRING_GATES.keys() {
197// entity_behaviour_registry.unregister(&EntityBehaviourTypeId::from(behaviour_ty));
198// }
199// for behaviour_ty in STRING_NUMBER_OPERATIONS.keys() {
200// entity_behaviour_registry.unregister(&EntityBehaviourTypeId::from(behaviour_ty));
201// }
202// for behaviour_ty in STRING_OPERATIONS.keys() {
203// entity_behaviour_registry.unregister(&EntityBehaviourTypeId::from(behaviour_ty));
204// }
205// entity_behaviour_registry.unregister(&ENTITY_BEHAVIOUR_TEMPLATING);
206// }
207// Ok(())
208// }
209//
210// fn set_context(&self, context: Arc<dyn PluginContext>) -> Result<(), PluginContextInitializationError> {
211// self.context.0.write().unwrap().replace(context.clone());
212// Ok(())
213// }
214//
215// fn remove_context(&self) -> Result<(), PluginContextDeinitializationError> {
216// let mut writer = self.context.0.write().unwrap();
217// *writer = None;
218// Ok(())
219// }
220//
221// fn get_component_provider(&self) -> Result<Option<Arc<dyn ComponentProvider>>, ComponentProviderError> {
222// component_provider!(self.component_provider)
223// }
224//
225// fn get_entity_type_provider(&self) -> Result<Option<Arc<dyn EntityTypeProvider>>, EntityTypeProviderError> {
226// entity_type_provider!(self.entity_type_provider)
227// }
228// }