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
//! Integration tests for RDF parsers
//!
//! Tests comprehensive parser functionality for all supported formats

use oxirs_core::format::{RdfFormat, RdfParser};
use oxirs_core::model::{Object, Subject};
use oxirs_core::RdfTerm;
use std::io::Cursor;

#[test]
fn test_turtle_parser_basic() {
    let turtle_data = r#"
        @prefix ex: <http://example.org/> .
        @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

        ex:subject1 ex:predicate1 "literal value" .
        ex:subject2 rdf:type ex:Type1 .
        ex:subject3 ex:predicate2 ex:object1 .
    "#;

    let parser = RdfParser::new(RdfFormat::Turtle);
    let cursor = Cursor::new(turtle_data.as_bytes());
    let quads: Vec<_> = parser
        .for_reader(cursor)
        .collect::<Result<Vec<_>, _>>()
        .expect("Failed to parse Turtle");

    assert_eq!(quads.len(), 3, "Should parse 3 triples");

    // Check first triple
    let quad1 = &quads[0];
    assert!(matches!(
        quad1.subject(),
        Subject::NamedNode(n) if n.as_str() == "http://example.org/subject1"
    ));
    assert_eq!(quad1.predicate().as_str(), "http://example.org/predicate1");
    assert!(matches!(quad1.object(), Object::Literal(l) if l.value() == "literal value"));
}

#[test]
fn test_turtle_parser_prefixes() {
    let turtle_data = r#"
        @prefix ex: <http://example.org/> .
        @prefix foaf: <http://xmlns.com/foaf/0.1/> .
        @base <http://base.example.org/> .

        ex:alice foaf:name "Alice" ;
                 foaf:knows ex:bob .

        ex:bob foaf:name "Bob" .
    "#;

    let parser = RdfParser::new(RdfFormat::Turtle);
    let cursor = Cursor::new(turtle_data.as_bytes());
    let quads: Vec<_> = parser
        .for_reader(cursor)
        .collect::<Result<Vec<_>, _>>()
        .expect("Failed to parse Turtle with prefixes");

    assert!(quads.len() >= 3, "Should parse at least 3 triples");

    // Check that prefixes were expanded correctly
    for quad in &quads {
        if let Subject::NamedNode(n) = quad.subject() {
            assert!(
                n.as_str().starts_with("http://"),
                "Subject should be expanded: {}",
                n.as_str()
            );
        }
        assert!(
            quad.predicate().as_str().starts_with("http://"),
            "Predicate should be expanded: {}",
            quad.predicate().as_str()
        );
    }
}

#[test]
fn test_turtle_parser_literals() {
    let turtle_data = r#"
        @prefix ex: <http://example.org/> .
        @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

        ex:entity1 ex:name "Alice"@en .
        ex:entity2 ex:age "25"^^xsd:integer .
        ex:entity3 ex:description """Multi-line
        literal value""" .
    "#;

    let parser = RdfParser::new(RdfFormat::Turtle);
    let cursor = Cursor::new(turtle_data.as_bytes());
    let quads: Vec<_> = parser
        .for_reader(cursor)
        .collect::<Result<Vec<_>, _>>()
        .expect("Failed to parse Turtle literals");

    assert_eq!(
        quads.len(),
        3,
        "Should parse 3 triples with different literal types"
    );

    // Check language-tagged literal
    let quad1 = &quads[0];
    if let Object::Literal(lit) = quad1.object() {
        assert_eq!(lit.value(), "Alice");
        assert_eq!(lit.language(), Some("en"));
    } else {
        panic!("Expected language-tagged literal");
    }
}

#[test]
fn test_rdfxml_parser_basic() {
    let rdfxml_data = r#"<?xml version="1.0"?>
        <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                 xmlns:ex="http://example.org/">
            <rdf:Description rdf:about="http://example.org/subject1">
                <ex:predicate1>literal value</ex:predicate1>
            </rdf:Description>
            <rdf:Description rdf:about="http://example.org/subject2">
                <rdf:type rdf:resource="http://example.org/Type1"/>
            </rdf:Description>
        </rdf:RDF>
    "#;

    let parser = RdfParser::new(RdfFormat::RdfXml);
    let cursor = Cursor::new(rdfxml_data.as_bytes());
    let quads: Vec<_> = parser
        .for_reader(cursor)
        .collect::<Result<Vec<_>, _>>()
        .expect("Failed to parse RDF/XML");

    assert!(quads.len() >= 2, "Should parse at least 2 triples");

    // Check that subjects are properly parsed
    for quad in &quads {
        if let Subject::NamedNode(n) = quad.subject() {
            assert!(
                n.as_str().starts_with("http://example.org/subject"),
                "Subject should be parsed correctly: {}",
                n.as_str()
            );
        }
    }
}

#[test]
fn test_rdfxml_parser_typed_nodes() {
    let rdfxml_data = r#"<?xml version="1.0"?>
        <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                 xmlns:ex="http://example.org/">
            <ex:Person rdf:about="http://example.org/alice">
                <ex:name>Alice</ex:name>
                <ex:age>25</ex:age>
            </ex:Person>
        </rdf:RDF>
    "#;

    let parser = RdfParser::new(RdfFormat::RdfXml);
    let cursor = Cursor::new(rdfxml_data.as_bytes());
    let quads: Vec<_> = parser
        .for_reader(cursor)
        .collect::<Result<Vec<_>, _>>()
        .expect("Failed to parse RDF/XML with typed nodes");

    // Should have at least 3 triples: rdf:type, name, age
    assert!(quads.len() >= 3, "Should parse type and properties");

    // Check for rdf:type triple
    let has_type = quads
        .iter()
        .any(|q| q.predicate().as_str() == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
    assert!(has_type, "Should have rdf:type triple");
}

#[test]
fn test_jsonld_parser_basic() {
    let jsonld_data = r#"
    {
        "@context": {
            "ex": "http://example.org/"
        },
        "@id": "http://example.org/subject1",
        "ex:predicate1": "literal value",
        "@type": "ex:Type1"
    }
    "#;

    use oxirs_core::format::JsonLdProfileSet;
    let parser = RdfParser::new(RdfFormat::JsonLd {
        profile: JsonLdProfileSet::empty(),
    });
    let cursor = Cursor::new(jsonld_data.as_bytes());
    let quads: Vec<_> = parser
        .for_reader(cursor)
        .collect::<Result<Vec<_>, _>>()
        .expect("Failed to parse JSON-LD");

    assert!(
        quads.len() >= 2,
        "Should parse at least 2 triples (type + property)"
    );

    // Check for rdf:type triple
    let has_type = quads
        .iter()
        .any(|q| q.predicate().as_str() == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
    assert!(has_type, "Should have rdf:type triple");
}

#[test]
fn test_jsonld_parser_array() {
    let jsonld_data = r#"
    [
        {
            "@id": "http://example.org/alice",
            "@type": "http://example.org/Person",
            "http://example.org/name": "Alice"
        },
        {
            "@id": "http://example.org/bob",
            "@type": "http://example.org/Person",
            "http://example.org/name": "Bob"
        }
    ]
    "#;

    use oxirs_core::format::JsonLdProfileSet;
    let parser = RdfParser::new(RdfFormat::JsonLd {
        profile: JsonLdProfileSet::empty(),
    });
    let cursor = Cursor::new(jsonld_data.as_bytes());
    let quads: Vec<_> = parser
        .for_reader(cursor)
        .collect::<Result<Vec<_>, _>>()
        .expect("Failed to parse JSON-LD array");

    // Should have at least 4 triples (2 types + 2 names)
    assert!(quads.len() >= 4, "Should parse multiple objects from array");
}

#[test]
fn test_jsonld_parser_literals() {
    let jsonld_data = r#"
    {
        "@id": "http://example.org/entity1",
        "http://example.org/name": {
            "@value": "Alice",
            "@language": "en"
        },
        "http://example.org/age": {
            "@value": "25",
            "@type": "http://www.w3.org/2001/XMLSchema#integer"
        }
    }
    "#;

    use oxirs_core::format::JsonLdProfileSet;
    let parser = RdfParser::new(RdfFormat::JsonLd {
        profile: JsonLdProfileSet::empty(),
    });
    let cursor = Cursor::new(jsonld_data.as_bytes());
    let quads: Vec<_> = parser
        .for_reader(cursor)
        .collect::<Result<Vec<_>, _>>()
        .expect("Failed to parse JSON-LD literals");

    assert_eq!(quads.len(), 2, "Should parse 2 triples with literals");

    // Check language-tagged literal
    let has_lang = quads.iter().any(|q| {
        if let Object::Literal(lit) = q.object() {
            lit.language() == Some("en")
        } else {
            false
        }
    });
    assert!(has_lang, "Should have language-tagged literal");
}

#[test]
fn test_parser_error_resilience() {
    // Test with invalid Turtle data in lenient mode
    let invalid_turtle = r#"
        @prefix ex: <http://example.org/> .

        ex:subject1 ex:predicate1 "valid triple" .
        this is invalid turtle syntax
        ex:subject2 ex:predicate2 "another valid triple" .
    "#;

    let parser = RdfParser::new(RdfFormat::Turtle).lenient();
    let cursor = Cursor::new(invalid_turtle.as_bytes());
    let quads: Vec<_> = parser
        .for_reader(cursor)
        .collect::<Result<Vec<_>, _>>()
        .unwrap_or_default();

    // In lenient mode, should parse valid triples and skip invalid ones
    assert!(
        !quads.is_empty(),
        "Should parse at least some valid triples in lenient mode"
    );
}

#[test]
fn test_format_round_trip_turtle() {
    use oxirs_core::format::RdfSerializer;

    let original_data = r#"
        @prefix ex: <http://example.org/> .

        ex:subject1 ex:predicate1 "literal value" .
        ex:subject2 ex:predicate2 ex:object1 .
    "#;

    // Parse
    let parser = RdfParser::new(RdfFormat::Turtle);
    let cursor = Cursor::new(original_data.as_bytes());
    let quads: Vec<_> = parser
        .for_reader(cursor)
        .collect::<Result<Vec<_>, _>>()
        .expect("Failed to parse");

    // Serialize back
    let output = Vec::new();
    let mut serializer = RdfSerializer::new(RdfFormat::Turtle).for_writer(output);
    for quad in &quads {
        serializer
            .serialize_quad(quad.as_ref())
            .expect("Failed to serialize");
    }
    let output = serializer.finish().expect("Failed to finish");

    // Parse again
    let parser2 = RdfParser::new(RdfFormat::Turtle);
    let cursor2 = Cursor::new(output.clone());
    let quads2: Vec<_> = parser2
        .for_reader(cursor2)
        .collect::<Result<Vec<_>, _>>()
        .expect("Failed to re-parse");

    assert_eq!(
        quads.len(),
        quads2.len(),
        "Round-trip should preserve triple count"
    );
}

#[test]
fn test_cross_format_conversion() {
    use oxirs_core::format::RdfSerializer;

    // Start with Turtle
    let turtle_data = r#"
        @prefix ex: <http://example.org/> .
        ex:subject ex:predicate "value" .
    "#;

    let parser = RdfParser::new(RdfFormat::Turtle);
    let cursor = Cursor::new(turtle_data.as_bytes());
    let quads: Vec<_> = parser
        .for_reader(cursor)
        .collect::<Result<Vec<_>, _>>()
        .expect("Failed to parse Turtle");

    // Convert to N-Triples
    let ntriples_output = Vec::new();
    let mut serializer = RdfSerializer::new(RdfFormat::NTriples).for_writer(ntriples_output);
    for quad in &quads {
        serializer
            .serialize_quad(quad.as_ref())
            .expect("Failed to serialize to N-Triples");
    }
    let ntriples_output = serializer.finish().expect("Failed to finish");

    // Parse N-Triples
    let parser2 = RdfParser::new(RdfFormat::NTriples);
    let cursor2 = Cursor::new(ntriples_output.clone());
    let quads2: Vec<_> = parser2
        .for_reader(cursor2)
        .collect::<Result<Vec<_>, _>>()
        .expect("Failed to parse N-Triples");

    assert_eq!(
        quads.len(),
        quads2.len(),
        "Cross-format conversion should preserve triples"
    );
}

#[test]
fn test_large_turtle_file() {
    // Generate large Turtle data
    let mut turtle_data = String::from("@prefix ex: <http://example.org/> .\n\n");
    for i in 0..1000 {
        turtle_data.push_str(&format!(
            "ex:subject{} ex:predicate{} \"value{}\" .\n",
            i,
            i % 10,
            i
        ));
    }

    let parser = RdfParser::new(RdfFormat::Turtle);
    let cursor = Cursor::new(turtle_data.into_bytes());
    let quads: Vec<_> = parser
        .for_reader(cursor)
        .collect::<Result<Vec<_>, _>>()
        .expect("Failed to parse large Turtle file");

    assert_eq!(quads.len(), 1000, "Should parse all 1000 triples");

    // Verify some triples
    for (i, quad) in quads.iter().enumerate() {
        if let Subject::NamedNode(n) = quad.subject() {
            assert_eq!(n.as_str(), format!("http://example.org/subject{}", i));
        }
        if let Object::Literal(lit) = quad.object() {
            assert_eq!(lit.value(), format!("value{}", i));
        }
    }
}