oxirs 0.3.1

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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//! ASCII Art Diagram Generator for RDF Graphs
//!
//! Provides visual representation of RDF triples using ASCII art for terminal display.
//! Supports different layout styles and handles large graphs with intelligent summarization.

use std::collections::{HashMap, HashSet};
use std::io::Write;

use crate::cli::error::CliResult as Result;

/// Represents an RDF triple for diagram generation
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct DiagramTriple {
    pub subject: String,
    pub predicate: String,
    pub object: String,
}

/// Layout style for ASCII diagrams
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LayoutStyle {
    /// Tree layout with hierarchical structure
    Tree,
    /// Graph layout with connections
    Graph,
    /// Compact layout for dense graphs
    Compact,
    /// List layout showing triples as a list
    List,
}

/// Configuration for ASCII diagram generation
#[derive(Debug, Clone)]
pub struct DiagramConfig {
    /// Layout style
    pub style: LayoutStyle,
    /// Maximum width of the diagram
    pub max_width: usize,
    /// Maximum nodes to display (0 = unlimited)
    pub max_nodes: usize,
    /// Maximum edges to display (0 = unlimited)
    pub max_edges: usize,
    /// Whether to show full URIs or abbreviated
    pub abbreviate_uris: bool,
    /// Whether to use Unicode box drawing characters
    pub use_unicode: bool,
}

impl Default for DiagramConfig {
    fn default() -> Self {
        Self {
            style: LayoutStyle::Tree,
            max_width: 120,
            max_nodes: 50,
            max_edges: 100,
            abbreviate_uris: true,
            use_unicode: true,
        }
    }
}

/// ASCII diagram generator
pub struct AsciiDiagramGenerator {
    config: DiagramConfig,
}

impl AsciiDiagramGenerator {
    /// Create a new ASCII diagram generator
    pub fn new(config: DiagramConfig) -> Self {
        Self { config }
    }

    /// Generate ASCII diagram from triples
    pub fn generate(&self, triples: &[DiagramTriple], writer: &mut dyn Write) -> Result<()> {
        if triples.is_empty() {
            writeln!(writer, "(no triples to display)")?;
            return Ok(());
        }

        match self.config.style {
            LayoutStyle::Tree => self.generate_tree(triples, writer),
            LayoutStyle::Graph => self.generate_graph(triples, writer),
            LayoutStyle::Compact => self.generate_compact(triples, writer),
            LayoutStyle::List => self.generate_list(triples, writer),
        }
    }

    /// Generate tree layout
    fn generate_tree(&self, triples: &[DiagramTriple], writer: &mut dyn Write) -> Result<()> {
        // Build node hierarchy
        let mut subjects: HashSet<String> = HashSet::new();
        let mut objects: HashSet<String> = HashSet::new();
        let mut edges: HashMap<String, Vec<(String, String)>> = HashMap::new();

        for triple in triples.iter().take(self.config.max_edges) {
            subjects.insert(triple.subject.clone());
            objects.insert(triple.object.clone());
            edges
                .entry(triple.subject.clone())
                .or_default()
                .push((triple.predicate.clone(), triple.object.clone()));
        }

        // Find root nodes (subjects that are not objects)
        let roots: Vec<String> = subjects
            .iter()
            .filter(|s| !objects.contains(*s))
            .cloned()
            .collect();

        if roots.is_empty() {
            // No clear roots, use first subject
            if let Some(triple) = triples.first() {
                self.render_tree_node(
                    &triple.subject,
                    &edges,
                    writer,
                    "",
                    true,
                    &mut HashSet::new(),
                )?;
            }
        } else {
            // Render each root
            for (idx, root) in roots.iter().enumerate() {
                let is_last = idx == roots.len() - 1;
                self.render_tree_node(root, &edges, writer, "", is_last, &mut HashSet::new())?;
            }
        }

        Ok(())
    }

    /// Render a tree node recursively
    fn render_tree_node(
        &self,
        node: &str,
        edges: &HashMap<String, Vec<(String, String)>>,
        writer: &mut dyn Write,
        prefix: &str,
        is_last: bool,
        visited: &mut HashSet<String>,
    ) -> Result<()> {
        // Check for cycles
        if visited.contains(node) {
            writeln!(
                writer,
                "{}{}",
                prefix,
                self.format_node(&format!("{} (cyclic reference)", node))
            )?;
            return Ok(());
        }
        visited.insert(node.to_string());

        // Draw current node
        let (branch, continuation) = if self.config.use_unicode {
            if is_last {
                ("└── ", "    ")
            } else {
                ("├── ", "│   ")
            }
        } else if is_last {
            ("`-- ", "    ")
        } else {
            ("|-- ", "|   ")
        };

        writeln!(writer, "{}{}{}", prefix, branch, self.format_node(node))?;

        // Draw edges to children
        if let Some(children) = edges.get(node) {
            for (idx, (predicate, object)) in children.iter().enumerate() {
                let is_last_child = idx == children.len() - 1;
                let child_prefix = format!("{}{}", prefix, continuation);

                // Draw predicate
                let pred_branch = if self.config.use_unicode {
                    if is_last_child {
                        "└─["
                    } else {
                        "├─["
                    }
                } else if is_last_child {
                    "`-["
                } else {
                    "|-["
                };

                writeln!(
                    writer,
                    "{}{}{}]",
                    child_prefix,
                    pred_branch,
                    self.format_predicate(predicate)
                )?;

                // Draw object
                let obj_prefix = format!(
                    "{}{}",
                    child_prefix,
                    if is_last_child {
                        "  "
                    } else if self.config.use_unicode {
                        "│ "
                    } else {
                        "| "
                    }
                );
                self.render_tree_node(object, edges, writer, &obj_prefix, true, visited)?;
            }
        }

        visited.remove(node);
        Ok(())
    }

    /// Generate graph layout
    fn generate_graph(&self, triples: &[DiagramTriple], writer: &mut dyn Write) -> Result<()> {
        writeln!(writer, "RDF Graph (ASCII representation):")?;
        writeln!(writer)?;

        let connector = if self.config.use_unicode { "─" } else { "-" };
        let arrow = if self.config.use_unicode { "→" } else { "->" };

        for (idx, triple) in triples.iter().enumerate() {
            if self.config.max_edges > 0 && idx >= self.config.max_edges {
                writeln!(writer, "... ({} more triples)", triples.len() - idx)?;
                break;
            }

            let subject = self.format_node(&triple.subject);
            let predicate = self.format_predicate(&triple.predicate);
            let object = self.format_node(&triple.object);

            // Calculate available width
            let total_len = subject.len() + predicate.len() + object.len() + 10;
            let connector_count = if total_len < self.config.max_width {
                (self.config.max_width - total_len) / 2
            } else {
                2
            };

            writeln!(
                writer,
                "{}  {}{}{}  {}  {}",
                subject,
                connector.repeat(connector_count),
                predicate,
                connector.repeat(connector_count),
                arrow,
                object
            )?;
        }

        Ok(())
    }

    /// Generate compact layout
    fn generate_compact(&self, triples: &[DiagramTriple], writer: &mut dyn Write) -> Result<()> {
        writeln!(writer, "RDF Triples (Compact):")?;
        writeln!(writer)?;

        // Group by subject
        let mut grouped: HashMap<String, Vec<(String, String)>> = HashMap::new();
        for triple in triples {
            grouped
                .entry(triple.subject.clone())
                .or_default()
                .push((triple.predicate.clone(), triple.object.clone()));
        }

        for (idx, (subject, predicates)) in grouped.iter().enumerate() {
            if self.config.max_nodes > 0 && idx >= self.config.max_nodes {
                writeln!(writer, "... ({} more subjects)", grouped.len() - idx)?;
                break;
            }

            writeln!(writer, "{}", self.format_node(subject))?;
            for (pred_idx, (predicate, object)) in predicates.iter().enumerate() {
                let prefix = if pred_idx == predicates.len() - 1 {
                    if self.config.use_unicode {
                        "  └─"
                    } else {
                        "  `-"
                    }
                } else if self.config.use_unicode {
                    "  ├─"
                } else {
                    "  |-"
                };

                writeln!(
                    writer,
                    "{} {} {}",
                    prefix,
                    self.format_predicate(predicate),
                    self.format_node(object)
                )?;
            }
            writeln!(writer)?;
        }

        Ok(())
    }

    /// Generate list layout
    fn generate_list(&self, triples: &[DiagramTriple], writer: &mut dyn Write) -> Result<()> {
        writeln!(writer, "RDF Triples:")?;
        writeln!(writer)?;

        for (idx, triple) in triples.iter().enumerate() {
            if self.config.max_edges > 0 && idx >= self.config.max_edges {
                writeln!(writer, "... ({} more triples)", triples.len() - idx)?;
                break;
            }

            writeln!(
                writer,
                "{:4}. {} {} {}",
                idx + 1,
                self.format_node(&triple.subject),
                self.format_predicate(&triple.predicate),
                self.format_node(&triple.object)
            )?;
        }

        Ok(())
    }

    /// Format a node (subject or object) for display
    fn format_node(&self, node: &str) -> String {
        if self.config.abbreviate_uris {
            self.abbreviate_uri(node)
        } else {
            node.to_string()
        }
    }

    /// Format a predicate for display
    fn format_predicate(&self, predicate: &str) -> String {
        if self.config.abbreviate_uris {
            self.abbreviate_uri(predicate)
        } else {
            predicate.to_string()
        }
    }

    /// Abbreviate a URI by extracting the local name
    fn abbreviate_uri(&self, uri: &str) -> String {
        // Remove angle brackets if present
        let uri = uri.trim_start_matches('<').trim_end_matches('>');

        // Check for common prefixes
        let common_prefixes = [
            ("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf:"),
            ("http://www.w3.org/2000/01/rdf-schema#", "rdfs:"),
            ("http://www.w3.org/2002/07/owl#", "owl:"),
            ("http://www.w3.org/ns/shacl#", "sh:"),
            ("http://xmlns.com/foaf/0.1/", "foaf:"),
            ("http://purl.org/dc/elements/1.1/", "dc:"),
            ("http://purl.org/dc/terms/", "dct:"),
            ("http://schema.org/", "schema:"),
        ];

        for (prefix, abbrev) in &common_prefixes {
            if let Some(stripped) = uri.strip_prefix(prefix) {
                return format!("{}{}", abbrev, stripped);
            }
        }

        // Extract local name from URI
        if let Some(pos) = uri.rfind(['/', '#']) {
            uri[pos + 1..].to_string()
        } else {
            uri.to_string()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_abbreviate_uri() {
        let config = DiagramConfig::default();
        let generator = AsciiDiagramGenerator::new(config);

        assert_eq!(
            generator.abbreviate_uri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
            "rdf:type"
        );
        assert_eq!(
            generator.abbreviate_uri("http://xmlns.com/foaf/0.1/name"),
            "foaf:name"
        );
        assert_eq!(
            generator.abbreviate_uri("http://example.org/person/John"),
            "John"
        );
    }

    #[test]
    fn test_tree_layout_simple() {
        let triples = vec![
            DiagramTriple {
                subject: "ex:John".to_string(),
                predicate: "rdf:type".to_string(),
                object: "foaf:Person".to_string(),
            },
            DiagramTriple {
                subject: "ex:John".to_string(),
                predicate: "foaf:name".to_string(),
                object: "\"John Doe\"".to_string(),
            },
        ];

        let config = DiagramConfig::default();
        let generator = AsciiDiagramGenerator::new(config);

        let mut output = Vec::new();
        generator.generate(&triples, &mut output).unwrap();

        let result = String::from_utf8(output).unwrap();
        assert!(result.contains("ex:John"));
        assert!(result.contains("type"));
        assert!(result.contains("name"));
    }

    #[test]
    fn test_graph_layout() {
        let triples = vec![
            DiagramTriple {
                subject: "ex:John".to_string(),
                predicate: "foaf:knows".to_string(),
                object: "ex:Jane".to_string(),
            },
            DiagramTriple {
                subject: "ex:Jane".to_string(),
                predicate: "foaf:knows".to_string(),
                object: "ex:Bob".to_string(),
            },
        ];

        let config = DiagramConfig {
            style: LayoutStyle::Graph,
            ..Default::default()
        };
        let generator = AsciiDiagramGenerator::new(config);

        let mut output = Vec::new();
        generator.generate(&triples, &mut output).unwrap();

        let result = String::from_utf8(output).unwrap();
        assert!(result.contains("RDF Graph"));
        assert!(result.contains("ex:John"));
        assert!(result.contains("ex:Jane"));
    }

    #[test]
    fn test_compact_layout() {
        let triples = vec![
            DiagramTriple {
                subject: "ex:John".to_string(),
                predicate: "rdf:type".to_string(),
                object: "foaf:Person".to_string(),
            },
            DiagramTriple {
                subject: "ex:John".to_string(),
                predicate: "foaf:name".to_string(),
                object: "\"John Doe\"".to_string(),
            },
            DiagramTriple {
                subject: "ex:Jane".to_string(),
                predicate: "rdf:type".to_string(),
                object: "foaf:Person".to_string(),
            },
        ];

        let config = DiagramConfig {
            style: LayoutStyle::Compact,
            ..Default::default()
        };
        let generator = AsciiDiagramGenerator::new(config);

        let mut output = Vec::new();
        generator.generate(&triples, &mut output).unwrap();

        let result = String::from_utf8(output).unwrap();
        assert!(result.contains("Compact"));
        assert!(result.contains("ex:John"));
        assert!(result.contains("ex:Jane"));
    }

    #[test]
    fn test_list_layout() {
        let triples = vec![
            DiagramTriple {
                subject: "ex:John".to_string(),
                predicate: "rdf:type".to_string(),
                object: "foaf:Person".to_string(),
            },
            DiagramTriple {
                subject: "ex:John".to_string(),
                predicate: "foaf:name".to_string(),
                object: "\"John Doe\"".to_string(),
            },
        ];

        let config = DiagramConfig {
            style: LayoutStyle::List,
            ..Default::default()
        };
        let generator = AsciiDiagramGenerator::new(config);

        let mut output = Vec::new();
        generator.generate(&triples, &mut output).unwrap();

        let result = String::from_utf8(output).unwrap();
        assert!(result.contains("RDF Triples:"));
        assert!(result.contains("1."));
        assert!(result.contains("2."));
    }

    #[test]
    fn test_empty_triples() {
        let triples = vec![];

        let config = DiagramConfig::default();
        let generator = AsciiDiagramGenerator::new(config);

        let mut output = Vec::new();
        generator.generate(&triples, &mut output).unwrap();

        let result = String::from_utf8(output).unwrap();
        assert!(result.contains("no triples"));
    }

    #[test]
    fn test_max_edges_limit() {
        let triples = vec![
            DiagramTriple {
                subject: "ex:S1".to_string(),
                predicate: "ex:p".to_string(),
                object: "ex:O1".to_string(),
            },
            DiagramTriple {
                subject: "ex:S2".to_string(),
                predicate: "ex:p".to_string(),
                object: "ex:O2".to_string(),
            },
            DiagramTriple {
                subject: "ex:S3".to_string(),
                predicate: "ex:p".to_string(),
                object: "ex:O3".to_string(),
            },
        ];

        let config = DiagramConfig {
            style: LayoutStyle::List,
            max_edges: 2,
            ..Default::default()
        };
        let generator = AsciiDiagramGenerator::new(config);

        let mut output = Vec::new();
        generator.generate(&triples, &mut output).unwrap();

        let result = String::from_utf8(output).unwrap();
        assert!(result.contains("more triples"));
    }
}