oxirs 0.2.4

Command-line interface for OxiRS - import, export, migration, and benchmarking tools
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
//! Graph decomposition and structural analysis
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)

use anyhow::Context;
use anyhow::Result;
use colored::Colorize;
use scirs2_graph::{center_nodes, diameter, k_core_decomposition, radius};
use std::collections::HashMap;

use super::types::{AnalyticsConfig, RdfGraph};

/// Execute K-core decomposition
pub fn execute_kcore_decomposition(graph: &RdfGraph, config: &AnalyticsConfig) -> Result<()> {
    println!(
        "{}",
        "K-Core Decomposition (Dense Subgraph Discovery)"
            .green()
            .bold()
    );
    println!();

    let scirs2_graph = graph
        .to_scirs2_graph()
        .context("Failed to convert RDF graph to scirs2_graph::Graph")?;

    println!("Computing k-core decomposition...");
    println!();

    let k_cores = k_core_decomposition(&scirs2_graph);
    let max_core = k_cores.values().copied().max().unwrap_or(0);

    println!("{}", "Core Statistics:".yellow().bold());
    println!("  Maximum core number: {}", max_core.to_string().green());
    println!();

    let mut core_counts: HashMap<usize, usize> = HashMap::new();
    for &core_num in k_cores.values() {
        *core_counts.entry(core_num).or_insert(0) += 1;
    }

    println!("{}", "Core Distribution:".yellow().bold());
    println!("{}", "─".repeat(60).cyan());
    println!("{:>10} | {:>15} | Density", "Core", "Node Count");
    println!("{}", "─".repeat(60).cyan());

    let mut sorted_cores: Vec<_> = core_counts.iter().collect();
    sorted_cores.sort_by_key(|(k, _)| std::cmp::Reverse(**k));

    for (&core_num, &count) in sorted_cores.iter().take(config.top_k) {
        let density = count as f64 / graph.node_count() as f64 * 100.0;
        let bar_length = (density / 2.0) as usize;
        let bar = "â–ˆ".repeat(bar_length);
        println!(
            "{:>10} | {:>15} | {} {:.1}%",
            core_num.to_string().yellow(),
            count.to_string().green(),
            bar.cyan(),
            density
        );
    }

    if let Some(k) = config.k_core_value {
        println!();
        println!("{}", format!("Nodes in {}-core:", k).yellow().bold());
        println!("{}", "─".repeat(100).cyan());

        let k_core_nodes: Vec<_> = k_cores
            .iter()
            .filter(|(_, &core)| core == k)
            .map(|(&node, _)| node)
            .collect();

        println!("  Found {} nodes in {}-core", k_core_nodes.len(), k);

        for (i, &node_id) in k_core_nodes.iter().take(config.top_k).enumerate() {
            if let Some(node_name) = graph.get_node_name(node_id) {
                let truncated = if node_name.len() > 85 {
                    format!("{}...", &node_name[..82])
                } else {
                    node_name.to_string()
                };
                println!("  {} {}", (i + 1).to_string().cyan(), truncated);
            }
        }

        if k_core_nodes.len() > config.top_k {
            println!(
                "  {} more...",
                (k_core_nodes.len() - config.top_k).to_string().dimmed()
            );
        }
    } else {
        println!();
        println!(
            "{}",
            format!("Sample nodes from {}-core (maximum):", max_core)
                .yellow()
                .bold()
        );
        println!("{}", "─".repeat(100).cyan());

        let max_core_nodes: Vec<_> = k_cores
            .iter()
            .filter(|(_, &core)| core == max_core)
            .map(|(&node, _)| node)
            .collect();

        for (i, &node_id) in max_core_nodes.iter().take(config.top_k).enumerate() {
            if let Some(node_name) = graph.get_node_name(node_id) {
                let truncated = if node_name.len() > 85 {
                    format!("{}...", &node_name[..82])
                } else {
                    node_name.to_string()
                };
                println!("  {} {}", (i + 1).to_string().cyan(), truncated);
            }
        }

        if max_core_nodes.len() > config.top_k {
            println!(
                "  {} more...",
                (max_core_nodes.len() - config.top_k).to_string().dimmed()
            );
        }
    }

    println!();
    println!(
        "{}",
        "K-core decomposition identifies densely connected subgraphs.".dimmed()
    );
    println!(
        "{}",
        "Higher core numbers indicate nodes in denser parts of the graph.".dimmed()
    );

    Ok(())
}

/// Execute triangle counting
pub fn execute_triangle_counting(graph: &RdfGraph, config: &AnalyticsConfig) -> Result<()> {
    println!(
        "{}",
        "Triangle Counting (Clustering Coefficient Analysis)"
            .green()
            .bold()
    );
    println!();

    let scirs2_graph = graph
        .to_scirs2_graph()
        .context("Failed to convert RDF graph to scirs2_graph::Graph")?;

    println!("Finding triangles in the graph...");
    println!();

    use scirs2_graph::algorithms::motifs::{find_motifs, MotifType};
    let triangles = find_motifs(&scirs2_graph, MotifType::Triangle);
    let triangle_count = triangles.len();
    let node_count = graph.node_count();

    println!("{}", "Triangle Statistics:".yellow().bold());
    println!("  Total triangles: {}", triangle_count.to_string().green());
    println!("  Nodes: {}", node_count.to_string().yellow());
    println!();

    let total_triples = calculate_connected_triples(graph);
    let global_clustering = if total_triples > 0 {
        (3.0 * triangle_count as f64) / total_triples as f64
    } else {
        0.0
    };

    println!("{}", "Clustering Metrics:".yellow().bold());
    println!(
        "  Global clustering coefficient: {}",
        format!("{:.6}", global_clustering).green()
    );

    let mut node_triangle_count: HashMap<usize, usize> = HashMap::new();
    for triangle in &triangles {
        for node in triangle {
            if let Some(&node_id) = graph.node_to_id.get(&format!("{:?}", node)) {
                *node_triangle_count.entry(node_id).or_insert(0) += 1;
            }
        }
    }

    println!();
    println!(
        "{}",
        format!("Top {} Nodes by Triangle Participation:", config.top_k)
            .yellow()
            .bold()
    );
    println!("{}", "─".repeat(100).cyan());
    println!("{:>5} | {:<70} | {:>12}", "Rank", "Resource", "Triangles");
    println!("{}", "─".repeat(100).cyan());

    let mut sorted_nodes: Vec<_> = node_triangle_count.iter().collect();
    sorted_nodes.sort_by_key(|(_, &count)| std::cmp::Reverse(count));

    for (i, (&node_id, &count)) in sorted_nodes.iter().take(config.top_k).enumerate() {
        if let Some(node_name) = graph.get_node_name(node_id) {
            let truncated = if node_name.len() > 70 {
                format!("{}...", &node_name[..67])
            } else {
                node_name.to_string()
            };
            println!(
                "{:>5} | {:<70} | {}",
                (i + 1).to_string().yellow(),
                truncated,
                count.to_string().green()
            );
        }
    }

    if !triangles.is_empty() {
        println!();
        println!(
            "{}",
            "Sample Triangles (showing up to 5):"
                .to_string()
                .yellow()
                .bold()
        );
        println!("{}", "─".repeat(100).cyan());

        for (i, triangle) in triangles.iter().take(5).enumerate() {
            println!("  Triangle {}:", (i + 1).to_string().cyan());
            for (j, node) in triangle.iter().enumerate() {
                let node_str = format!("{:?}", node);
                if let Some(&node_id) = graph.node_to_id.get(&node_str) {
                    if let Some(node_name) = graph.get_node_name(node_id) {
                        let truncated = if node_name.len() > 85 {
                            format!("{}...", &node_name[..82])
                        } else {
                            node_name.to_string()
                        };
                        println!("    {} - {}", (j + 1), truncated);
                    }
                }
            }
        }

        if triangles.len() > 5 {
            println!(
                "  {} more triangles...",
                (triangles.len() - 5).to_string().dimmed()
            );
        }
    }

    println!();
    println!(
        "{}",
        "Triangles indicate transitive relationships in the graph.".dimmed()
    );
    println!(
        "{}",
        "High clustering coefficient suggests community structure.".dimmed()
    );

    Ok(())
}

/// Calculate the number of connected triples in the graph
fn calculate_connected_triples(graph: &RdfGraph) -> usize {
    let mut triples = 0;
    for node in 0..graph.node_count() {
        let neighbors = graph.neighbors(node);
        let degree = neighbors.len();
        if degree >= 2 {
            triples += degree * (degree - 1) / 2;
        }
    }
    triples
}

/// Execute diameter and radius calculation
pub fn execute_diameter_radius(graph: &RdfGraph, _config: &AnalyticsConfig) -> Result<()> {
    println!("{}", "Graph Diameter and Radius Analysis".green().bold());
    println!();

    let scirs2_graph = graph
        .to_scirs2_graph()
        .context("Failed to convert RDF graph to scirs2_graph::Graph")?;

    println!("Computing graph diameter and radius...");
    println!();

    let graph_diameter = diameter(&scirs2_graph);
    let graph_radius = radius(&scirs2_graph);

    println!("{}", "Graph Metrics:".yellow().bold());
    println!("{}", "─".repeat(60).cyan());

    match graph_diameter {
        Some(d) => {
            println!(
                "  Diameter (maximum eccentricity): {}",
                format!("{:.2}", d).green()
            );
        }
        None => {
            println!("  Diameter: {}", "undefined (disconnected graph)".dimmed());
        }
    }

    match graph_radius {
        Some(r) => {
            println!(
                "  Radius (minimum eccentricity):   {}",
                format!("{:.2}", r).green()
            );
        }
        None => {
            println!("  Radius: {}", "undefined (disconnected graph)".dimmed());
        }
    }

    if let (Some(d), Some(r)) = (graph_diameter, graph_radius) {
        println!();
        println!("{}", "Interpretation:".yellow().bold());
        println!(
            "  • Diameter ({:.2}) is the longest shortest path in the graph",
            d
        );
        println!(
            "  • Radius ({:.2}) is the smallest eccentricity of any node",
            r
        );
        println!("  • Center nodes have eccentricity equal to the radius");

        let ratio = d / r;
        println!();
        println!(
            "  Diameter/Radius ratio: {}",
            format!("{:.2}", ratio).cyan()
        );

        if ratio < 1.5 {
            println!("  → {}", "Compact graph structure".green());
        } else if ratio < 2.5 {
            println!("  → {}", "Moderate graph structure".yellow());
        } else {
            println!("  → {}", "Extended graph structure".red());
        }
    }

    println!();
    println!(
        "{}",
        "Diameter and radius measure graph compactness and connectivity.".dimmed()
    );
    println!(
        "{}",
        "Lower values indicate more compact, well-connected graphs.".dimmed()
    );

    Ok(())
}

/// Execute center nodes identification
pub fn execute_center_nodes(graph: &RdfGraph, config: &AnalyticsConfig) -> Result<()> {
    println!(
        "{}",
        "Graph Center Nodes (Minimum Eccentricity)".green().bold()
    );
    println!();

    let scirs2_graph = graph
        .to_scirs2_graph()
        .context("Failed to convert RDF graph to scirs2_graph::Graph")?;

    println!("Finding center nodes...");
    println!();

    let center_node_ids: Vec<usize> = center_nodes(&scirs2_graph);
    let graph_radius = radius(&scirs2_graph);

    println!("{}", "Center Nodes:".yellow().bold());
    println!("{}", "─".repeat(100).cyan());

    if center_node_ids.is_empty() {
        println!(
            "  {} No center nodes found (disconnected graph)",
            "âš ".yellow()
        );
    } else {
        if let Some(r) = graph_radius {
            println!(
                "  Found {} center nodes with eccentricity {:.2}",
                center_node_ids.len().to_string().green(),
                r.to_string().yellow()
            );
        } else {
            println!(
                "  Found {} center nodes",
                center_node_ids.len().to_string().green()
            );
        }

        println!();

        let display_count = config.top_k.min(center_node_ids.len());
        println!("{:>5} | {:<85}", "Rank", "Resource");
        println!("{}", "─".repeat(100).cyan());

        for (i, &node_id) in center_node_ids.iter().take(display_count).enumerate() {
            if let Some(node_name) = graph.get_node_name(node_id) {
                let truncated = if node_name.len() > 85 {
                    format!("{}...", &node_name[..82])
                } else {
                    node_name.to_string()
                };
                println!("{:>5} | {}", (i + 1).to_string().yellow(), truncated);
            }
        }

        if center_node_ids.len() > display_count {
            println!(
                "  {} more center nodes...",
                (center_node_ids.len() - display_count).to_string().dimmed()
            );
        }
    }

    println!();
    println!(
        "{}",
        "Center nodes have minimum eccentricity (minimum of maximum distances).".dimmed()
    );
    println!(
        "{}",
        "They are the most 'central' nodes in terms of distance to other nodes.".dimmed()
    );

    Ok(())
}