-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathhorned_triples.rs
More file actions
126 lines (109 loc) · 3.48 KB
/
Copy pathhorned_triples.rs
File metadata and controls
126 lines (109 loc) · 3.48 KB
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
use clap::App;
use clap::Arg;
use clap::ArgMatches;
use horned_owl::error::HornedError;
use horned_pretty_rdf::{PTriple, RdfFormatter};
use oxrdfio::RdfParser;
use std::io::BufReader;
use std::{fs::File, io::stdout};
#[allow(dead_code)]
fn main() -> Result<(), HornedError> {
let matches = app("horned-triples").get_matches();
matcher(&matches)
}
pub(crate) fn app(name: &str) -> App<'static> {
App::new(name)
.version(horned_bin::version_string())
.about("Parse RDF and dump the triples")
.author("Phillip Lord")
.arg(
Arg::with_name("INPUT")
.help("Sets the input file to use")
.required(true)
.index(1),
)
.arg(
Arg::with_name("filter")
.long("filter")
.takes_value(true)
.help("Only triples which match given string")
.required(false),
)
.arg(
Arg::with_name("round")
.long("round")
.help("Dump the triples")
.required(false),
)
}
pub(crate) fn matcher(matches: &ArgMatches) -> Result<(), HornedError> {
let input = matches
.value_of("INPUT")
.ok_or_else(|| HornedError::CommandError("A file name must be specified".to_string()))?;
let filter = matches.value_of("filter");
let file = File::open(input)?;
let bufreader = BufReader::new(file);
let v: Vec<String> = RdfParser::from_format(oxrdfio::RdfFormat::RdfXml)
.for_reader(bufreader)
.map(Result::unwrap)
.map(|quad| {
(
format!("{}", quad.subject),
format!("{}", quad.predicate),
format!("{}", quad.object),
// ignore graph name
)
})
.filter(|t| {
if let Some(f) = filter {
t.0.contains(f) || t.1.contains(f) || t.2.contains(f)
} else {
true
}
})
.map(|t| format!("{}\n\t{}\n\t{}", t.0, t.1, t.2))
.collect();
for t in v {
println!("{t}");
}
if matches.is_present("round") {
println!("\nRoundTripping:\n");
let b = stdout();
//let b = std::io::sink();
let mut p = indexmap::IndexMap::new();
p.insert(
"http://www.w3.org/2002/07/owl#".to_string(),
"owl".to_string(),
);
p.insert(
"http://www.w3.org/XML/1998/namespace".to_string(),
"xml".to_string(),
);
p.insert(
"http://www.w3.org/2001/XMLSchema#".to_string(),
"xsd".to_string(),
);
p.insert(
"http://www.w3.org/2000/01/rdf-schema#".to_string(),
"rdfs".to_string(),
);
let mut f: horned_pretty_rdf::PrettyRdfXmlFormatter<String, _> =
horned_pretty_rdf::PrettyRdfXmlFormatter::new(
b,
horned_pretty_rdf::ChunkedRdfXmlFormatterConfig::all(),
)?;
//let mut f = rio_xml::RdfXmlFormatter::with_indentation(&b, 4)?;
let file = File::open(input)?;
let bufreader = BufReader::new(file);
let triples: Vec<PTriple<String>> = RdfParser::from_format(oxrdfio::RdfFormat::RdfXml)
.for_reader(bufreader)
.map(Result::unwrap)
.map(Into::into)
.collect();
for t in triples {
f.format(t)?;
}
f.finish()?;
}
Ok(())
}