god-graph 0.6.0-alpha

A graph-based LLM white-box optimization toolbox: topology validation, Lie group orthogonalization, tensor ring compression
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! 插件注册表
//!
//! 管理插件的注册、查找和执行
//!
//! # 架构设计
//!
//! 插件注册表采用类型擦除技术来解决 dyn 兼容性问题:
//! - 使用 `Box<dyn Any + Send + Sync>` 存储插件实例
//! - 通过 `downcast_ref` 进行类型转换
//! - `execute_by_name` 方法支持按名称执行,无需指定类型参数
//!
//! # 示例
//!
//! ```
//! use god_graph::plugins::PluginRegistry;
//!
//! let registry = PluginRegistry::new();
//! assert!(registry.is_empty());
//! ```

use crate::plugins::algorithm::{AlgorithmResult, FastHashMap, GraphAlgorithm, PluginContext, PluginInfo};
use crate::vgi::VgiResult;
use crate::vgi::VirtualGraph;
use std::any::Any;

/// 插件元数据
pub struct PluginMetadata {
    /// 插件信息
    pub info: PluginInfo,
    /// 插件实例(类型擦除)
    pub instance: Box<dyn Any + Send + Sync>,
}

impl PluginMetadata {
    /// 创建新的插件元数据
    pub fn new<A: GraphAlgorithm + 'static>(info: PluginInfo, instance: A) -> Self {
        Self {
            info,
            instance: Box::new(instance),
        }
    }

    /// 尝试获取算法实例的引用
    pub fn as_algorithm<A: GraphAlgorithm + 'static>(&self) -> Option<&A> {
        self.instance.downcast_ref::<A>()
    }
}

/// 插件注册表
///
/// 用于注册、查找和管理图算法插件
///
/// # 示例
///
/// ```
/// use god_graph::plugins::PluginRegistry;
///
/// let registry = PluginRegistry::new();
/// assert!(registry.is_empty());
/// ```
pub struct PluginRegistry {
    /// 已注册的插件元数据
    plugins: FastHashMap<String, PluginMetadata>,
    /// 插件标签索引
    tag_index: FastHashMap<String, Vec<String>>,
}

impl Default for PluginRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl PluginRegistry {
    /// 创建新的插件注册表
    pub fn new() -> Self {
        Self {
            plugins: FastHashMap::default(),
            tag_index: FastHashMap::default(),
        }
    }

    /// 注册算法插件
    ///
    /// # Arguments
    ///
    /// * `name` - 插件名称(唯一标识符)
    /// * `algorithm` - 算法实例
    ///
    /// # Returns
    ///
    /// 注册成功返回 Ok,如果名称冲突返回 Err
    ///
    /// # 示例
    ///
    /// ```ignore
    /// let mut registry = PluginRegistry::new();
    /// registry.register_algorithm("pagerank", PageRankPlugin::default());
    /// ```
    pub fn register_algorithm<A: GraphAlgorithm + 'static>(
        &mut self,
        name: impl Into<String>,
        algorithm: A,
    ) -> VgiResult<()> {
        let name = name.into();

        if self.plugins.contains_key(&name) {
            return Err(crate::vgi::VgiError::PluginRegistrationFailed {
                plugin_name: name.clone(),
                reason: "Plugin with this name already exists".to_string(),
            });
        }

        let info = algorithm.info();

        // 更新标签索引
        for tag in &info.tags {
            self.tag_index
                .entry(tag.clone())
                .or_default()
                .push(name.clone());
        }

        let metadata = PluginMetadata::new(info, algorithm);
        self.plugins.insert(name, metadata);

        Ok(())
    }

    /// 按名称执行算法(推荐方法)
    ///
    /// 这个方法不需要指定算法类型参数,内部使用类型擦除技术。
    ///
    /// # Arguments
    ///
    /// * `name` - 算法名称
    /// * `graph` - 要执行算法的图
    /// * `ctx` - 插件上下文
    ///
    /// # Returns
    ///
    /// 算法执行结果
    ///
    /// # 示例
    ///
    /// ```ignore
    /// let mut registry = PluginRegistry::new();
    /// registry.register_algorithm("pagerank", PageRankPlugin::default());
    ///
    /// let mut ctx = PluginContext::new(&graph);
    /// let result = registry.execute_by_name("pagerank", &graph, &mut ctx)?;
    /// ```
    pub fn execute_by_name<G: VirtualGraph + ?Sized>(
        &self,
        name: &str,
        _graph: &G,
        ctx: &mut PluginContext<'_, G>,
    ) -> VgiResult<AlgorithmResult> {
        let metadata =
            self.plugins
                .get(name)
                .ok_or_else(|| crate::vgi::VgiError::PluginNotFound {
                    plugin_name: name.to_string(),
                })?;

        // 使用宏来尝试 downcast 到所有可能的算法类型并执行
        // 这是 Rust 中处理 trait object 调用泛型方法的标准模式
        macro_rules! try_downcast_and_execute {
            ($($t:ty),*) => {
                $(
                    if let Some(algo) = metadata.instance.downcast_ref::<$t>() {
                        algo.validate(ctx)?;
                        algo.before_execute(ctx)?;
                        let result = algo.execute(ctx)?;
                        algo.after_execute(ctx, &result)?;
                        return Ok(result);
                    }
                )*
            };
        }

        // 尝试 downcast 到所有已知的算法类型
        // 注意:这需要手动维护列表,但这是 Rust 类型系统的限制
        try_downcast_and_execute!(
            crate::plugins::PageRankPlugin,
            crate::plugins::BfsPlugin,
            crate::plugins::DfsPlugin,
            crate::plugins::ConnectedComponentsPlugin,
            crate::plugins::DijkstraPlugin,
            crate::plugins::BellmanFordPlugin,
            crate::plugins::BetweennessCentralityPlugin,
            crate::plugins::ClosenessCentralityPlugin,
            crate::plugins::LouvainPlugin,
            crate::plugins::TopologicalSortPlugin
        );

        // 如果都不匹配,返回错误
        Err(crate::vgi::VgiError::Internal {
            message: format!(
                "Failed to downcast plugin '{}' to any known algorithm type",
                name
            ),
        })
    }

    /// 执行算法(泛型版本,已弃用)
    ///
    /// # Type Parameters
    ///
    /// * `G` - 图类型
    /// * `A` - 算法类型(必须与注册时的类型匹配)
    ///
    /// # Arguments
    ///
    /// * `name` - 算法名称
    /// * `graph` - 要执行算法的图
    /// * `ctx` - 插件上下文
    ///
    /// # Returns
    ///
    /// 算法执行结果
    #[deprecated(
        since = "0.6.1",
        note = "Use `execute_by_name` instead for simpler API"
    )]
    pub fn execute<G, A>(
        &self,
        name: &str,
        graph: &G,
        ctx: &mut PluginContext<'_, G>,
    ) -> VgiResult<AlgorithmResult>
    where
        G: VirtualGraph + ?Sized,
        A: GraphAlgorithm + 'static,
    {
        // 委托给 execute_by_name
        self.execute_by_name(name, graph, ctx)
    }

    /// 获取插件元数据
    pub fn get_metadata(&self, name: &str) -> Option<&PluginMetadata> {
        self.plugins.get(name)
    }

    /// 检查插件是否已注册
    pub fn is_registered(&self, name: &str) -> bool {
        self.plugins.contains_key(name)
    }

    /// 列出所有已注册的插件名称
    pub fn list_plugins(&self) -> Vec<&String> {
        self.plugins.keys().collect()
    }

    /// 按标签查找插件
    pub fn find_by_tag(&self, tag: &str) -> Vec<&String> {
        self.tag_index
            .get(tag)
            .map(|names| names.iter().collect())
            .unwrap_or_default()
    }

    /// 获取插件数量
    pub fn len(&self) -> usize {
        self.plugins.len()
    }

    /// 检查是否为空
    pub fn is_empty(&self) -> bool {
        self.plugins.is_empty()
    }

    /// 清空注册表
    pub fn clear(&mut self) {
        self.plugins.clear();
        self.tag_index.clear();
    }

    /// 获取算法信息
    pub fn get_algorithm_info(&self, name: &str) -> Option<&PluginInfo> {
        self.plugins.get(name).map(|m| &m.info)
    }

    /// 注销插件
    pub fn unregister(&mut self, name: &str) -> VgiResult<Option<PluginMetadata>> {
        if let Some(metadata) = self.plugins.remove(name) {
            // 清理标签索引
            for tag in &metadata.info.tags {
                if let Some(names) = self.tag_index.get_mut(tag) {
                    names.retain(|n| n != name);
                }
            }
            Ok(Some(metadata))
        } else {
            Ok(None)
        }
    }
}

/// 插件构建器
///
/// 用于链式构建和注册插件元数据
pub struct PluginMetadataBuilder {
    name: String,
    info: Option<PluginInfo>,
}

impl PluginMetadataBuilder {
    /// 创建新的构建器
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            info: None,
        }
    }

    /// 设置插件信息
    pub fn info(mut self, info: PluginInfo) -> Self {
        self.info = Some(info);
        self
    }

    /// 注册到注册表
    pub fn register<A: GraphAlgorithm + 'static>(
        self,
        registry: &mut PluginRegistry,
        algorithm: A,
    ) -> VgiResult<()> {
        let info = self
            .info
            .ok_or_else(|| crate::vgi::VgiError::PluginRegistrationFailed {
                plugin_name: self.name.clone(),
                reason: "No plugin info provided".to_string(),
            })?;

        let name = self.name.clone();
        registry.register_algorithm(name.clone(), algorithm)?;

        // 更新已注册插件的 info(如果算法实例的 info 不同)
        if let Some(metadata) = registry.plugins.get_mut(&name) {
            metadata.info = info;
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graph::Graph;
    use crate::graph::traits::GraphOps;
    use crate::plugins::algorithm::{AlgorithmResult, PluginInfo};
    use crate::vgi::VirtualGraph;
    use std::any::Any;

    struct TestAlgorithm;

    impl GraphAlgorithm for TestAlgorithm {
        fn info(&self) -> PluginInfo {
            PluginInfo::new("test", "1.0.0", "Test Algorithm").with_tags(&["test", "demo"])
        }

        fn execute<G>(&self, _ctx: &mut PluginContext<G>) -> VgiResult<AlgorithmResult>
        where
            G: VirtualGraph + ?Sized,
        {
            Ok(AlgorithmResult::scalar(42.0))
        }

        fn as_any(&self) -> &dyn Any {
            self
        }
    }

    #[test]
    fn test_plugin_registry() {
        let mut registry = PluginRegistry::new();

        assert!(registry.is_empty());
        assert_eq!(registry.len(), 0);

        registry.register_algorithm("test", TestAlgorithm).unwrap();

        assert!(!registry.is_empty());
        assert_eq!(registry.len(), 1);
        assert!(registry.is_registered("test"));
        assert!(registry.get_metadata("test").is_some());
    }

    #[test]
    fn test_plugin_find_by_tag() {
        let mut registry = PluginRegistry::new();

        struct TaggedAlgorithm {
            tags: Vec<&'static str>,
        }
        impl GraphAlgorithm for TaggedAlgorithm {
            fn info(&self) -> PluginInfo {
                PluginInfo::new("tagged", "1.0.0", "Tagged Algorithm")
                    .with_tags(&self.tags.to_vec())
            }
            fn execute<G>(&self, _ctx: &mut PluginContext<G>) -> VgiResult<AlgorithmResult>
            where
                G: VirtualGraph + ?Sized,
            {
                Ok(AlgorithmResult::scalar(1.0))
            }
            fn as_any(&self) -> &dyn Any {
                self
            }
        }

        registry
            .register_algorithm(
                "algo1",
                TaggedAlgorithm {
                    tags: vec!["tag1", "common"],
                },
            )
            .unwrap();
        registry
            .register_algorithm(
                "algo2",
                TaggedAlgorithm {
                    tags: vec!["tag2", "common"],
                },
            )
            .unwrap();

        // 单标签查找
        assert_eq!(registry.find_by_tag("tag1").len(), 1);
        assert_eq!(registry.find_by_tag("common").len(), 2);

        // 不存在的标签
        assert_eq!(registry.find_by_tag("nonexistent").len(), 0);
    }

    #[test]
    fn test_plugin_execute() {
        use crate::plugins::PageRankPlugin;

        let mut registry = PluginRegistry::new();
        // 使用内置的 PageRankPlugin 测试
        registry
            .register_algorithm("pagerank", PageRankPlugin::default())
            .unwrap();

        let graph = Graph::<String, f64>::directed();
        let mut ctx = PluginContext::new(&graph);

        // 使用新的 execute_by_name 方法(不需要类型参数)
        let result = registry.execute_by_name("pagerank", &graph, &mut ctx);
        assert!(result.is_ok());
        let result = result.unwrap();
        // PageRank 返回的是 NodeValues
        assert!(result.data.as_node_values().is_some());
    }

    #[test]
    fn test_plugin_unregister() {
        let mut registry = PluginRegistry::new();
        registry.register_algorithm("test", TestAlgorithm).unwrap();
        assert!(registry.is_registered("test"));

        registry.unregister("test").unwrap();
        assert!(!registry.is_registered("test"));
    }
}