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
use crate::import_kg_turtle;
use anyhow::{Context, Result};
use clap::{Parser, ValueEnum};
use std::fs::read_to_string;
use std::path::PathBuf;
#[cfg(feature = "shacl")]
use crate::kg_import::ImportError;
#[derive(Parser)]
pub struct ImportArgs {
#[arg(long, value_enum)]
pub format: ImportFormat,
pub file: PathBuf,
}
#[derive(ValueEnum, Clone, Debug, Copy)]
pub enum ImportFormat {
Sbvr,
Kg,
}
/// Helper function to print import success message with graph statistics
fn print_import_success(graph: &crate::Graph, format_desc: &str) {
println!(
"Imported {} to Graph: entities={} resources={} flows={}",
format_desc,
graph.entity_count(),
graph.resource_count(),
graph.flow_count()
);
}
pub fn run(args: ImportArgs) -> Result<()> {
let source = read_to_string(&args.file)
.with_context(|| format!("Failed to read file {}", args.file.display()))?;
match args.format {
ImportFormat::Sbvr => match crate::SbvrModel::from_xmi(&source) {
Ok(model) => match model.to_graph() {
Ok(graph) => {
print_import_success(&graph, "SBVR");
Ok(())
}
Err(e) => Err(anyhow::anyhow!("Failed to convert SBVR to Graph: {}", e)),
},
Err(e) => Err(anyhow::anyhow!("Failed to parse SBVR XMI: {}", e)),
},
ImportFormat::Kg => {
let src_trim = source.trim_start();
// More specific RDF/XML detection: check for XML declaration or RDF namespace
let is_likely_rdfxml = src_trim.starts_with("<?xml")
|| src_trim.starts_with("<rdf:")
|| src_trim.starts_with("<rdf ")
|| src_trim.contains("xmlns:rdf");
if is_likely_rdfxml {
#[cfg(feature = "shacl")]
{
match crate::import_kg_rdfxml(&source) {
Ok(graph) => {
print_import_success(&graph, "KG (RDF/XML)");
Ok(())
}
Err(err_rdf) => match err_rdf {
ImportError::ShaclValidation(msg) => Err(anyhow::anyhow!("{}", msg)),
other => {
let err_rdf_msg = other.to_string();
match import_kg_turtle(&source) {
Ok(graph) => {
print_import_success(&graph, "KG (Turtle)");
Ok(())
}
Err(err_turtle) => Err(anyhow::anyhow!(
"Failed to import KG as RDF/XML: {}; as Turtle: {}",
err_rdf_msg,
err_turtle
)),
}
}
},
}
}
#[cfg(not(feature = "shacl"))]
{
match import_kg_turtle(&source) {
Ok(graph) => {
print_import_success(&graph, "KG (Turtle)");
Ok(())
}
Err(err) => {
Err(anyhow::anyhow!("Failed to import KG: {}. Consider enabling the 'shacl' feature for RDF/XML support.", err))
}
}
}
} else {
match import_kg_turtle(&source) {
Ok(graph) => {
print_import_success(&graph, "KG (Turtle)");
Ok(())
}
Err(err_turtle) => {
#[cfg(feature = "shacl")]
{
match crate::import_kg_rdfxml(&source) {
Ok(graph) => {
print_import_success(&graph, "KG (RDF/XML)");
Ok(())
}
Err(err_rdf) => {
match err_rdf {
ImportError::ShaclValidation(msg) => {
Err(anyhow::anyhow!("{}", msg))
}
other => {
let err_rdf_msg = other.to_string();
Err(anyhow::anyhow!("Failed to import KG as Turtle: {}; and as RDF/XML: {}", err_turtle, err_rdf_msg))
}
}
}
}
}
#[cfg(not(feature = "shacl"))]
{
Err(anyhow::anyhow!("Failed to import KG (Turtle): {}. To try RDF/XML, enable feature 'shacl' in the build.", err_turtle))
}
}
}
}
}
}
}