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
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
//! SHACL Validator - RDF data validation using SHACL shapes
//!
//! Validates RDF data against SHACL (Shapes Constraint Language) shapes.

use super::{utils, ToolResult, ToolStats};
use std::path::{Path, PathBuf};

/// Run SHACL validation
pub async fn run(
    data: Option<PathBuf>,
    dataset: Option<PathBuf>,
    shapes: PathBuf,
    format: String,
    output: Option<PathBuf>,
) -> ToolResult {
    let mut stats = ToolStats::new();

    println!("SHACL Validator");
    println!("Shapes file: {}", shapes.display());

    // Validate output format
    let supported_formats = ["text", "turtle", "json", "xml"];
    if !supported_formats.contains(&format.as_str()) {
        return Err(format!(
            "Unsupported output format '{}'. Supported: {}",
            format,
            supported_formats.join(", ")
        )
        .into());
    }

    // Check shapes file
    utils::check_file_readable(&shapes)?;
    let shapes_format = utils::detect_rdf_format(&shapes);
    println!("Shapes format: {shapes_format}");

    // Determine data source
    let data_source = match (data, dataset) {
        (Some(data_file), None) => {
            utils::check_file_readable(&data_file)?;
            let data_format = utils::detect_rdf_format(&data_file);
            println!("Data file: {} ({})", data_file.display(), data_format);
            DataSource::File(data_file, data_format)
        }
        (None, Some(dataset_path)) => {
            if !dataset_path.exists() {
                return Err(format!("Dataset not found: {}", dataset_path.display()).into());
            }
            println!("Dataset: {}", dataset_path.display());
            DataSource::Dataset(dataset_path)
        }
        (Some(_), Some(_)) => {
            return Err("Cannot specify both --data and --dataset".into());
        }
        (None, None) => {
            return Err("Must specify either --data or --dataset".into());
        }
    };

    // Load shapes
    println!("Loading SHACL shapes...");
    let shapes_graph = load_shapes_file(&shapes, &shapes_format)?;
    println!("Loaded {} shape(s)", shapes_graph.shapes.len());

    // Load data to validate
    println!("Loading data to validate...");
    let data_graph = load_data_source(&data_source)?;
    println!("Loaded {} triple(s)", data_graph.triples.len());

    // Run validation
    println!("Running SHACL validation...");
    let validation_start = std::time::Instant::now();
    let validation_report = validate_data_against_shapes(&data_graph, &shapes_graph)?;
    let validation_duration = validation_start.elapsed();

    println!(
        "Validation completed in {}",
        utils::format_duration(validation_duration)
    );

    // Generate output
    let output_content = format_validation_report(&validation_report, &format)?;

    // Write output
    if let Some(output_file) = output {
        utils::write_output(&output_content, Some(&output_file))?;
        println!("Validation report written to: {}", output_file.display());
    } else {
        println!("\n{output_content}");
    }

    // Summary
    println!("\n=== Validation Summary ===");
    println!("Shapes validated: {}", shapes_graph.shapes.len());
    println!("Triples validated: {}", data_graph.triples.len());
    println!("Validation results: {}", validation_report.results.len());
    println!("Conforms: {}", validation_report.conforms);

    if !validation_report.conforms {
        println!("Violations: {}", validation_report.results.len());

        // Group violations by severity
        let mut error_count = 0;
        let mut warning_count = 0;
        let mut info_count = 0;

        for result in &validation_report.results {
            match result.severity {
                Severity::Violation => error_count += 1,
                Severity::Warning => warning_count += 1,
                Severity::Info => info_count += 1,
            }
        }

        if error_count > 0 {
            println!("  Violations: {error_count}");
        }
        if warning_count > 0 {
            println!("  Warnings: {warning_count}");
        }
        if info_count > 0 {
            println!("  Info: {info_count}");
        }
    }

    stats.items_processed = data_graph.triples.len();
    stats.errors = if validation_report.conforms {
        0
    } else {
        validation_report.results.len()
    };
    stats.finish();
    stats.print_summary("SHACL Validator");

    // Exit with error code if validation failed
    if !validation_report.conforms {
        return Err("SHACL validation failed".into());
    }

    Ok(())
}

/// Data source types
#[derive(Debug)]
enum DataSource {
    File(PathBuf, String), // path, format
    Dataset(PathBuf),
}

/// Simple RDF graph representation
#[derive(Debug)]
struct RdfGraph {
    triples: Vec<RdfTriple>,
}

#[derive(Debug, Clone)]
struct RdfTriple {
    subject: String,
    predicate: String,
    object: String,
}

/// SHACL shapes graph
#[derive(Debug)]
struct ShaclShapesGraph {
    shapes: Vec<ShaclShape>,
}

#[derive(Debug)]
struct ShaclShape {
    id: String,
    target_class: Option<String>,
    target_node: Option<String>,
    property_shapes: Vec<PropertyShape>,
}

#[derive(Debug)]
#[allow(dead_code)]
struct PropertyShape {
    path: String,
    min_count: Option<usize>,
    max_count: Option<usize>,
    datatype: Option<String>,
    node_kind: Option<String>,
    value_in: Vec<String>,
}

/// SHACL validation report
#[derive(Debug)]
struct ValidationReport {
    conforms: bool,
    results: Vec<ValidationResult>,
}

#[derive(Debug)]
struct ValidationResult {
    severity: Severity,
    focus_node: String,
    result_path: Option<String>,
    value: Option<String>,
    message: String,
    source_constraint_component: String,
    source_shape: String,
}

#[derive(Debug, Clone)]
#[allow(dead_code)]
enum Severity {
    Violation,
    Warning,
    Info,
}

/// Load SHACL shapes from file
fn load_shapes_file(shapes_path: &Path, format: &str) -> ToolResult<ShaclShapesGraph> {
    let content = utils::read_input(shapes_path)?;

    // Parse shapes (simplified implementation)
    let mut shapes = Vec::new();

    match format {
        "turtle" | "ntriples" => {
            // Very basic SHACL shape parsing
            // In a real implementation, this would be much more sophisticated

            let mut current_shape: Option<ShaclShape> = None;

            for line in content.lines() {
                let line = line.trim();
                if line.is_empty() || line.starts_with('#') {
                    continue;
                }

                // Look for shape definitions
                if line.contains("sh:NodeShape") || line.contains("sh:PropertyShape") {
                    if let Some(shape) = current_shape.take() {
                        shapes.push(shape);
                    }

                    // Extract shape ID (very simplified)
                    let shape_id = if let Some(start) = line.find('<') {
                        if let Some(end) = line.find('>') {
                            line[start + 1..end].to_string()
                        } else {
                            format!("shape_{}", shapes.len())
                        }
                    } else {
                        format!("shape_{}", shapes.len())
                    };

                    current_shape = Some(ShaclShape {
                        id: shape_id,
                        target_class: None,
                        target_node: None,
                        property_shapes: Vec::new(),
                    });
                }

                // Parse shape properties (very simplified)
                if let Some(ref mut shape) = current_shape {
                    if line.contains("sh:targetClass") {
                        if let Some(class_start) = line.find('<') {
                            if let Some(class_end) = line.find('>') {
                                shape.target_class =
                                    Some(line[class_start + 1..class_end].to_string());
                            }
                        }
                    }
                }
            }

            if let Some(shape) = current_shape {
                shapes.push(shape);
            }
        }
        _ => {
            return Err(format!("Shapes format '{format}' not supported").into());
        }
    }

    // If no shapes found, create a default example shape
    if shapes.is_empty() {
        shapes.push(ShaclShape {
            id: "http://example.org/shapes#PersonShape".to_string(),
            target_class: Some("http://example.org/Person".to_string()),
            target_node: None,
            property_shapes: vec![PropertyShape {
                path: "http://example.org/name".to_string(),
                min_count: Some(1),
                max_count: None,
                datatype: Some("http://www.w3.org/2001/XMLSchema#string".to_string()),
                node_kind: None,
                value_in: Vec::new(),
            }],
        });
    }

    Ok(ShaclShapesGraph { shapes })
}

/// Load data from source
fn load_data_source(data_source: &DataSource) -> ToolResult<RdfGraph> {
    let mut triples = Vec::new();

    match data_source {
        DataSource::File(path, format) => {
            let content = utils::read_input(path)?;
            triples = parse_rdf_data(&content, format)?;
        }
        DataSource::Dataset(_path) => {
            // In a real implementation, this would load from TDB dataset
            // For now, simulate loading some data
            triples.push(RdfTriple {
                subject: "<http://example.org/person1>".to_string(),
                predicate: "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>".to_string(),
                object: "<http://example.org/Person>".to_string(),
            });
            triples.push(RdfTriple {
                subject: "<http://example.org/person1>".to_string(),
                predicate: "<http://example.org/name>".to_string(),
                object: "\"John Doe\"".to_string(),
            });
        }
    }

    Ok(RdfGraph { triples })
}

/// Parse RDF data (simplified)
fn parse_rdf_data(content: &str, format: &str) -> ToolResult<Vec<RdfTriple>> {
    let mut triples = Vec::new();

    match format {
        "ntriples" | "turtle" => {
            for line in content.lines() {
                let line = line.trim();
                if line.is_empty() || line.starts_with('#') || line.starts_with('@') {
                    continue;
                }

                if let Some(line) = line.strip_suffix(" .") {
                    let parts: Vec<&str> = line.split_whitespace().collect();

                    if parts.len() >= 3 {
                        triples.push(RdfTriple {
                            subject: parts[0].to_string(),
                            predicate: parts[1].to_string(),
                            object: parts[2..].join(" "),
                        });
                    }
                }
            }
        }
        _ => {
            return Err(format!("Data format '{format}' not supported").into());
        }
    }

    Ok(triples)
}

/// Validate data against SHACL shapes
fn validate_data_against_shapes(
    data_graph: &RdfGraph,
    shapes_graph: &ShaclShapesGraph,
) -> ToolResult<ValidationReport> {
    let mut results = Vec::new();
    let mut conforms = true;

    for shape in &shapes_graph.shapes {
        // Find target nodes for this shape
        let target_nodes = find_target_nodes(data_graph, shape);

        for target_node in target_nodes {
            // Validate each property shape
            for property_shape in &shape.property_shapes {
                let validation_result =
                    validate_property_shape(data_graph, &target_node, property_shape, shape);

                if let Some(result) = validation_result {
                    if matches!(result.severity, Severity::Violation) {
                        conforms = false;
                    }
                    results.push(result);
                }
            }
        }
    }

    Ok(ValidationReport { conforms, results })
}

/// Find target nodes for a shape
fn find_target_nodes(data_graph: &RdfGraph, shape: &ShaclShape) -> Vec<String> {
    let mut targets = Vec::new();

    if let Some(ref target_class) = shape.target_class {
        // Find all instances of the target class
        for triple in &data_graph.triples {
            if triple.predicate == "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>"
                && triple.object == format!("<{target_class}>")
            {
                targets.push(triple.subject.clone());
            }
        }
    }

    if let Some(ref target_node) = shape.target_node {
        targets.push(format!("<{target_node}>"));
    }

    // If no specific targets, include all subjects (not recommended for real use)
    if targets.is_empty() {
        for triple in &data_graph.triples {
            if !targets.contains(&triple.subject) {
                targets.push(triple.subject.clone());
            }
        }
    }

    targets
}

/// Validate a property shape against a target node
fn validate_property_shape(
    data_graph: &RdfGraph,
    target_node: &str,
    property_shape: &PropertyShape,
    source_shape: &ShaclShape,
) -> Option<ValidationResult> {
    // Find all values for this property on the target node
    let mut values = Vec::new();

    for triple in &data_graph.triples {
        if triple.subject == *target_node
            && triple.predicate == format!("<{}>", property_shape.path)
        {
            values.push(triple.object.clone());
        }
    }

    // Check min count constraint
    if let Some(min_count) = property_shape.min_count {
        if values.len() < min_count {
            return Some(ValidationResult {
                severity: Severity::Violation,
                focus_node: target_node.to_string(),
                result_path: Some(property_shape.path.clone()),
                value: None,
                message: format!(
                    "Property {} has {} value(s) but minimum count is {}",
                    property_shape.path,
                    values.len(),
                    min_count
                ),
                source_constraint_component: "sh:minCount".to_string(),
                source_shape: source_shape.id.clone(),
            });
        }
    }

    // Check max count constraint
    if let Some(max_count) = property_shape.max_count {
        if values.len() > max_count {
            return Some(ValidationResult {
                severity: Severity::Violation,
                focus_node: target_node.to_string(),
                result_path: Some(property_shape.path.clone()),
                value: None,
                message: format!(
                    "Property {} has {} value(s) but maximum count is {}",
                    property_shape.path,
                    values.len(),
                    max_count
                ),
                source_constraint_component: "sh:maxCount".to_string(),
                source_shape: source_shape.id.clone(),
            });
        }
    }

    // Additional validations would go here (datatype, nodeKind, etc.)

    None // No violations found
}

/// Format validation report
fn format_validation_report(report: &ValidationReport, format: &str) -> ToolResult<String> {
    match format {
        "text" => format_text_report(report),
        "turtle" => format_turtle_report(report),
        "json" => format_json_report(report),
        "xml" => format_xml_report(report),
        _ => Err(format!("Unsupported output format: {format}").into()),
    }
}

/// Format report as plain text
fn format_text_report(report: &ValidationReport) -> ToolResult<String> {
    let mut output = String::new();

    output.push_str("SHACL Validation Report\n");
    output.push_str(&format!("Conforms: {}\n\n", report.conforms));

    if report.results.is_empty() {
        output.push_str("No validation results.\n");
    } else {
        output.push_str(&format!("Validation Results ({}):\n", report.results.len()));
        output.push_str(&"=".repeat(50));
        output.push('\n');

        for (i, result) in report.results.iter().enumerate() {
            output.push_str(&format!(
                "\n{}. {} - {}\n",
                i + 1,
                format_severity(&result.severity),
                result.message
            ));
            output.push_str(&format!("   Focus Node: {}\n", result.focus_node));
            if let Some(ref path) = result.result_path {
                output.push_str(&format!("   Property: {path}\n"));
            }
            if let Some(ref value) = result.value {
                output.push_str(&format!("   Value: {value}\n"));
            }
            output.push_str(&format!("   Shape: {}\n", result.source_shape));
            output.push_str(&format!(
                "   Constraint: {}\n",
                result.source_constraint_component
            ));
        }
    }

    Ok(output)
}

/// Format severity for display
fn format_severity(severity: &Severity) -> &'static str {
    match severity {
        Severity::Violation => "VIOLATION",
        Severity::Warning => "WARNING",
        Severity::Info => "INFO",
    }
}

/// Format report as Turtle (simplified)
fn format_turtle_report(report: &ValidationReport) -> ToolResult<String> {
    let mut output = String::new();

    output.push_str("@prefix sh: <http://www.w3.org/ns/shacl#> .\n");
    output.push_str("@prefix ex: <http://example.org/> .\n\n");

    output.push_str("ex:report a sh:ValidationReport ;\n");
    output.push_str(&format!("  sh:conforms {} ;\n", report.conforms));

    if !report.results.is_empty() {
        output.push_str("  sh:result\n");
        for (i, _result) in report.results.iter().enumerate() {
            output.push_str(&format!("    ex:result{i}"));
            if i < report.results.len() - 1 {
                output.push_str(",\n");
            } else {
                output.push_str(" .\n\n");
            }
        }

        for (i, result) in report.results.iter().enumerate() {
            output.push_str(&format!("ex:result{i} a sh:ValidationResult ;\n"));
            output.push_str(&format!("  sh:focusNode {} ;\n", result.focus_node));
            if let Some(ref path) = result.result_path {
                output.push_str(&format!("  sh:resultPath <{path}> ;\n"));
            }
            output.push_str(&format!("  sh:resultMessage \"{}\" ;\n", result.message));
            output.push_str(&format!(
                "  sh:sourceConstraintComponent sh:{} ;\n",
                result.source_constraint_component.trim_start_matches("sh:")
            ));
            output.push_str(&format!("  sh:sourceShape <{}> .\n\n", result.source_shape));
        }
    } else {
        output.push_str(" .\n");
    }

    Ok(output)
}

/// Format report as JSON
fn format_json_report(report: &ValidationReport) -> ToolResult<String> {
    // Simple JSON formatting - in practice would use serde_json
    let mut output = String::new();

    output.push_str("{\n");
    output.push_str(&format!("  \"conforms\": {},\n", report.conforms));
    output.push_str("  \"results\": [\n");

    for (i, result) in report.results.iter().enumerate() {
        output.push_str("    {\n");
        output.push_str(&format!(
            "      \"severity\": \"{}\",\n",
            format_severity(&result.severity)
        ));
        output.push_str(&format!(
            "      \"focusNode\": \"{}\",\n",
            result.focus_node
        ));
        if let Some(ref path) = result.result_path {
            output.push_str(&format!("      \"resultPath\": \"{path}\",\n"));
        }
        output.push_str(&format!("      \"message\": \"{}\",\n", result.message));
        output.push_str(&format!(
            "      \"sourceConstraintComponent\": \"{}\",\n",
            result.source_constraint_component
        ));
        output.push_str(&format!(
            "      \"sourceShape\": \"{}\"\n",
            result.source_shape
        ));
        output.push_str("    }");
        if i < report.results.len() - 1 {
            output.push(',');
        }
        output.push('\n');
    }

    output.push_str("  ]\n");
    output.push_str("}\n");

    Ok(output)
}

/// Format report as XML
fn format_xml_report(report: &ValidationReport) -> ToolResult<String> {
    let mut output = String::new();

    output.push_str("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    output.push_str("<ValidationReport xmlns=\"http://www.w3.org/ns/shacl#\">\n");
    output.push_str(&format!("  <conforms>{}</conforms>\n", report.conforms));

    for result in &report.results {
        output.push_str("  <result>\n");
        output.push_str(&format!(
            "    <severity>{}</severity>\n",
            format_severity(&result.severity)
        ));
        output.push_str(&format!(
            "    <focusNode>{}</focusNode>\n",
            result.focus_node
        ));
        if let Some(ref path) = result.result_path {
            output.push_str(&format!("    <resultPath>{path}</resultPath>\n"));
        }
        output.push_str(&format!("    <message>{}</message>\n", result.message));
        output.push_str(&format!(
            "    <sourceConstraintComponent>{}</sourceConstraintComponent>\n",
            result.source_constraint_component
        ));
        output.push_str(&format!(
            "    <sourceShape>{}</sourceShape>\n",
            result.source_shape
        ));
        output.push_str("  </result>\n");
    }

    output.push_str("</ValidationReport>\n");

    Ok(output)
}