aingle_graph 0.7.1

Native GraphDB for AIngle - Semantic triple store with SPO indexes
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
// Copyright 2019-2026 Apilium Technologies OÜ. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR Commercial

//! RDF parsers for Turtle and N-Triples formats
//!
//! This module provides parsers for standard RDF serialization formats.

use super::{NamespaceMap, RdfTerm, RdfTriple};
use crate::{Error, Result, Triple};

/// Trait for RDF parsers
pub trait RdfParser {
    /// Parse RDF content into triples
    fn parse(content: &str) -> Result<Vec<RdfTriple>>;

    /// Parse RDF content directly into aingle_graph Triples
    fn parse_to_triples(content: &str) -> Result<Vec<Triple>> {
        let rdf_triples = Self::parse(content)?;
        rdf_triples.into_iter().map(|t| t.to_triple()).collect()
    }
}

/// Parser for Turtle (.ttl) format
pub struct TurtleParser;

impl TurtleParser {
    /// Parse Turtle content
    pub fn parse(content: &str) -> Result<Vec<RdfTriple>> {
        let mut triples = Vec::new();
        let mut namespaces = NamespaceMap::new();
        let mut base_iri: Option<String> = None;
        let mut current_subject: Option<RdfTerm> = None;
        let mut current_predicate: Option<RdfTerm> = None;
        let mut blank_node_counter = 0u64;

        let mut chars = content.chars().peekable();
        let mut line_num = 1;

        while chars.peek().is_some() {
            // Skip whitespace and comments
            skip_ws_and_comments(&mut chars, &mut line_num);

            if chars.peek().is_none() {
                break;
            }

            let c = *chars.peek().unwrap();

            // Handle directives
            if c == '@' {
                chars.next();
                let directive = read_word(&mut chars);

                match directive.as_str() {
                    "prefix" => {
                        skip_ws(&mut chars);
                        let prefix = read_until(&mut chars, ':');
                        chars.next(); // skip ':'
                        skip_ws(&mut chars);
                        let iri = read_iri(&mut chars)?;
                        skip_ws(&mut chars);
                        expect_char(&mut chars, '.')?;
                        namespaces.add(&prefix, &iri);
                    }
                    "base" => {
                        skip_ws(&mut chars);
                        let iri = read_iri(&mut chars)?;
                        skip_ws(&mut chars);
                        expect_char(&mut chars, '.')?;
                        base_iri = Some(iri);
                    }
                    _ => {
                        return Err(Error::InvalidTriple(format!(
                            "Unknown directive: @{}",
                            directive
                        )))
                    }
                }
                continue;
            }

            // Handle SPARQL-style PREFIX/BASE
            if c == 'P' || c == 'B' {
                let word = peek_word(&mut chars);
                if word == "PREFIX" {
                    for _ in 0..6 {
                        chars.next();
                    }
                    skip_ws(&mut chars);
                    let prefix = read_until(&mut chars, ':');
                    chars.next(); // skip ':'
                    skip_ws(&mut chars);
                    let iri = read_iri(&mut chars)?;
                    namespaces.add(&prefix, &iri);
                    continue;
                } else if word == "BASE" {
                    for _ in 0..4 {
                        chars.next();
                    }
                    skip_ws(&mut chars);
                    let iri = read_iri(&mut chars)?;
                    base_iri = Some(iri);
                    continue;
                }
            }

            // Parse subject
            if current_subject.is_none() {
                current_subject = Some(parse_term(
                    &mut chars,
                    &namespaces,
                    &base_iri,
                    &mut blank_node_counter,
                )?);
                skip_ws(&mut chars);
            }

            // Parse predicate
            if current_predicate.is_none() {
                // Check for 'a' shorthand (rdf:type)
                if chars.peek() == Some(&'a') {
                    let word = peek_word(&mut chars);
                    if word == "a"
                        && !word
                            .chars()
                            .nth(1)
                            .map(|c| c.is_alphanumeric())
                            .unwrap_or(false)
                    {
                        chars.next();
                        current_predicate = Some(RdfTerm::iri(
                            "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
                        ));
                    } else {
                        current_predicate = Some(parse_term(
                            &mut chars,
                            &namespaces,
                            &base_iri,
                            &mut blank_node_counter,
                        )?);
                    }
                } else {
                    current_predicate = Some(parse_term(
                        &mut chars,
                        &namespaces,
                        &base_iri,
                        &mut blank_node_counter,
                    )?);
                }
                skip_ws(&mut chars);
            }

            // Parse object
            let object = parse_term(&mut chars, &namespaces, &base_iri, &mut blank_node_counter)?;

            // Add triple
            if let (Some(ref subj), Some(ref pred)) = (&current_subject, &current_predicate) {
                triples.push(RdfTriple::new(subj.clone(), pred.clone(), object));
            }

            skip_ws(&mut chars);

            // Check for punctuation
            match chars.peek() {
                Some('.') => {
                    chars.next();
                    current_subject = None;
                    current_predicate = None;
                }
                Some(';') => {
                    chars.next();
                    current_predicate = None;
                    skip_ws_and_comments(&mut chars, &mut line_num);
                    // Handle trailing semicolon before period
                    if chars.peek() == Some(&'.') {
                        chars.next();
                        current_subject = None;
                    }
                }
                Some(',') => {
                    chars.next();
                    // Keep subject and predicate, read new object
                }
                Some(_) | None => {}
            }
        }

        Ok(triples)
    }
}

impl RdfParser for TurtleParser {
    fn parse(content: &str) -> Result<Vec<RdfTriple>> {
        TurtleParser::parse(content)
    }
}

/// Parser for N-Triples (.nt) format
pub struct NTriplesParser;

impl NTriplesParser {
    /// Parse N-Triples content
    pub fn parse(content: &str) -> Result<Vec<RdfTriple>> {
        let mut triples = Vec::new();

        for (line_num, line) in content.lines().enumerate() {
            let line = line.trim();

            // Skip empty lines and comments
            if line.is_empty() || line.starts_with('#') {
                continue;
            }

            let triple = Self::parse_line(line)
                .map_err(|e| Error::InvalidTriple(format!("Line {}: {}", line_num + 1, e)))?;

            triples.push(triple);
        }

        Ok(triples)
    }

    fn parse_line(line: &str) -> Result<RdfTriple> {
        let mut chars = line.chars().peekable();
        let mut blank_counter = 0u64;

        // Parse subject (IRI or blank node)
        let subject = parse_nt_term(&mut chars, &mut blank_counter)?;
        skip_ws(&mut chars);

        // Parse predicate (IRI only)
        let predicate = parse_nt_term(&mut chars, &mut blank_counter)?;
        if !predicate.is_iri() {
            return Err(Error::InvalidTriple("Predicate must be an IRI".into()));
        }
        skip_ws(&mut chars);

        // Parse object (IRI, blank node, or literal)
        let object = parse_nt_term(&mut chars, &mut blank_counter)?;
        skip_ws(&mut chars);

        // Expect period
        if chars.next() != Some('.') {
            return Err(Error::InvalidTriple("Expected '.' at end of triple".into()));
        }

        Ok(RdfTriple::new(subject, predicate, object))
    }
}

impl RdfParser for NTriplesParser {
    fn parse(content: &str) -> Result<Vec<RdfTriple>> {
        NTriplesParser::parse(content)
    }
}

// Helper functions

fn skip_ws<I: Iterator<Item = char>>(chars: &mut std::iter::Peekable<I>) {
    while let Some(&c) = chars.peek() {
        if c.is_whitespace() {
            chars.next();
        } else {
            break;
        }
    }
}

fn skip_ws_and_comments<I: Iterator<Item = char>>(
    chars: &mut std::iter::Peekable<I>,
    line_num: &mut usize,
) {
    loop {
        skip_ws(chars);
        if chars.peek() == Some(&'#') {
            // Skip until end of line
            while let Some(c) = chars.next() {
                if c == '\n' {
                    *line_num += 1;
                    break;
                }
            }
        } else {
            break;
        }
    }
}

fn read_word<I: Iterator<Item = char>>(chars: &mut std::iter::Peekable<I>) -> String {
    let mut word = String::new();
    while let Some(&c) = chars.peek() {
        if c.is_alphanumeric() || c == '_' || c == '-' {
            word.push(c);
            chars.next();
        } else {
            break;
        }
    }
    word
}

fn peek_word<I: Iterator<Item = char>>(chars: &std::iter::Peekable<I>) -> String
where
    I: Clone,
{
    let mut peeker = chars.clone();
    let mut word = String::new();
    while let Some(&c) = peeker.peek() {
        if c.is_alphanumeric() || c == '_' || c == '-' {
            word.push(c);
            peeker.next();
        } else {
            break;
        }
    }
    word
}

fn read_until<I: Iterator<Item = char>>(chars: &mut std::iter::Peekable<I>, stop: char) -> String {
    let mut result = String::new();
    while let Some(&c) = chars.peek() {
        if c == stop {
            break;
        }
        result.push(c);
        chars.next();
    }
    result.trim().to_string()
}

fn expect_char<I: Iterator<Item = char>>(
    chars: &mut std::iter::Peekable<I>,
    expected: char,
) -> Result<()> {
    match chars.next() {
        Some(c) if c == expected => Ok(()),
        Some(c) => Err(Error::InvalidTriple(format!(
            "Expected '{}', found '{}'",
            expected, c
        ))),
        None => Err(Error::InvalidTriple(format!(
            "Expected '{}', found EOF",
            expected
        ))),
    }
}

fn read_iri<I: Iterator<Item = char>>(chars: &mut std::iter::Peekable<I>) -> Result<String> {
    if chars.next() != Some('<') {
        return Err(Error::InvalidTriple("Expected '<' for IRI".into()));
    }

    let mut iri = String::new();
    while let Some(c) = chars.next() {
        if c == '>' {
            return Ok(iri);
        }
        iri.push(c);
    }

    Err(Error::InvalidTriple("Unterminated IRI".into()))
}

fn parse_term<I: Iterator<Item = char> + Clone>(
    chars: &mut std::iter::Peekable<I>,
    namespaces: &NamespaceMap,
    base_iri: &Option<String>,
    blank_counter: &mut u64,
) -> Result<RdfTerm> {
    skip_ws(chars);

    match chars.peek() {
        Some('<') => {
            // Full IRI
            let iri = read_iri(chars)?;
            let resolved = if let Some(base) = base_iri {
                resolve_iri(base, &iri)
            } else {
                iri
            };
            Ok(RdfTerm::Iri(resolved))
        }
        Some('_') => {
            // Blank node
            chars.next(); // '_'
            expect_char(chars, ':')?;
            let id = read_word(chars);
            Ok(RdfTerm::BlankNode(id))
        }
        Some('[') => {
            // Anonymous blank node
            chars.next();
            skip_ws(chars);
            if chars.peek() == Some(&']') {
                chars.next();
                *blank_counter += 1;
                Ok(RdfTerm::BlankNode(format!("b{}", blank_counter)))
            } else {
                Err(Error::InvalidTriple(
                    "Blank node property lists not yet supported".into(),
                ))
            }
        }
        Some('"') => {
            // Literal
            parse_literal(chars)
        }
        Some('\'') => {
            // Single-quoted literal (Turtle)
            parse_literal_single(chars)
        }
        Some(c) if c.is_numeric() || *c == '+' || *c == '-' => {
            // Numeric literal
            parse_numeric(chars)
        }
        Some('t') | Some('f') => {
            // Possible boolean
            let word = peek_word(chars);
            if word == "true" || word == "false" {
                for _ in 0..word.len() {
                    chars.next();
                }
                Ok(RdfTerm::typed_literal(
                    word,
                    "http://www.w3.org/2001/XMLSchema#boolean",
                ))
            } else {
                // Must be a prefixed name
                parse_prefixed_name(chars, namespaces)
            }
        }
        Some(_) => {
            // Prefixed name
            parse_prefixed_name(chars, namespaces)
        }
        None => Err(Error::InvalidTriple("Unexpected end of input".into())),
    }
}

fn parse_nt_term<I: Iterator<Item = char>>(
    chars: &mut std::iter::Peekable<I>,
    _blank_counter: &mut u64,
) -> Result<RdfTerm> {
    skip_ws(chars);

    match chars.peek() {
        Some('<') => {
            let iri = read_iri(chars)?;
            Ok(RdfTerm::Iri(iri))
        }
        Some('_') => {
            chars.next();
            expect_char(chars, ':')?;
            let id = read_word(chars);
            Ok(RdfTerm::BlankNode(id))
        }
        Some('"') => parse_literal(chars),
        _ => Err(Error::InvalidTriple("Invalid N-Triples term".into())),
    }
}

fn parse_literal<I: Iterator<Item = char>>(chars: &mut std::iter::Peekable<I>) -> Result<RdfTerm> {
    chars.next(); // opening quote

    let mut value = String::new();
    let mut escaped = false;

    while let Some(c) = chars.next() {
        if escaped {
            match c {
                'n' => value.push('\n'),
                'r' => value.push('\r'),
                't' => value.push('\t'),
                '\\' => value.push('\\'),
                '"' => value.push('"'),
                _ => {
                    value.push('\\');
                    value.push(c);
                }
            }
            escaped = false;
        } else if c == '\\' {
            escaped = true;
        } else if c == '"' {
            break;
        } else {
            value.push(c);
        }
    }

    // Check for datatype or language tag
    match chars.peek() {
        Some('^') => {
            chars.next();
            expect_char(chars, '^')?;
            let datatype = if chars.peek() == Some(&'<') {
                read_iri(chars)?
            } else {
                // Prefixed datatype - read as-is for now
                let mut dt = String::new();
                while let Some(&c) = chars.peek() {
                    if c.is_whitespace() || c == '.' || c == ';' || c == ',' {
                        break;
                    }
                    dt.push(c);
                    chars.next();
                }
                dt
            };
            Ok(RdfTerm::typed_literal(value, datatype))
        }
        Some('@') => {
            chars.next();
            let lang = read_word(chars);
            Ok(RdfTerm::lang_literal(value, lang))
        }
        _ => Ok(RdfTerm::literal(value)),
    }
}

fn parse_literal_single<I: Iterator<Item = char>>(
    chars: &mut std::iter::Peekable<I>,
) -> Result<RdfTerm> {
    chars.next(); // opening quote

    let mut value = String::new();

    while let Some(c) = chars.next() {
        if c == '\'' {
            break;
        }
        value.push(c);
    }

    Ok(RdfTerm::literal(value))
}

fn parse_numeric<I: Iterator<Item = char>>(chars: &mut std::iter::Peekable<I>) -> Result<RdfTerm> {
    let mut num = String::new();
    let mut has_dot = false;
    let mut has_exp = false;

    while let Some(&c) = chars.peek() {
        match c {
            '0'..='9' | '+' | '-' => {
                num.push(c);
                chars.next();
            }
            '.' if !has_dot => {
                has_dot = true;
                num.push(c);
                chars.next();
            }
            'e' | 'E' if !has_exp => {
                has_exp = true;
                num.push(c);
                chars.next();
            }
            _ => break,
        }
    }

    let datatype = if has_dot || has_exp {
        "http://www.w3.org/2001/XMLSchema#double"
    } else {
        "http://www.w3.org/2001/XMLSchema#integer"
    };

    Ok(RdfTerm::typed_literal(num, datatype))
}

fn parse_prefixed_name<I: Iterator<Item = char>>(
    chars: &mut std::iter::Peekable<I>,
    namespaces: &NamespaceMap,
) -> Result<RdfTerm> {
    let mut prefix = String::new();
    let mut local = String::new();
    let mut found_colon = false;

    while let Some(&c) = chars.peek() {
        if c == ':' && !found_colon {
            found_colon = true;
            chars.next();
        } else if c.is_whitespace() || c == '.' || c == ';' || c == ',' || c == ')' || c == ']' {
            break;
        } else if found_colon {
            local.push(c);
            chars.next();
        } else {
            prefix.push(c);
            chars.next();
        }
    }

    if !found_colon {
        return Err(Error::InvalidTriple(format!(
            "Invalid prefixed name: {}",
            prefix
        )));
    }

    let expanded = namespaces.expand(&format!("{}:{}", prefix, local));
    Ok(RdfTerm::Iri(expanded))
}

fn resolve_iri(base: &str, relative: &str) -> String {
    if relative.contains("://") {
        // Already absolute
        relative.to_string()
    } else if relative.starts_with('#') {
        format!("{}{}", base, relative)
    } else if relative.starts_with('/') {
        // Find scheme://host
        if let Some(idx) = base.find("://") {
            if let Some(slash_idx) = base[idx + 3..].find('/') {
                format!("{}{}", &base[..idx + 3 + slash_idx], relative)
            } else {
                format!("{}{}", base, relative)
            }
        } else {
            relative.to_string()
        }
    } else {
        // Relative to base directory
        if let Some(idx) = base.rfind('/') {
            format!("{}/{}", &base[..idx], relative)
        } else {
            relative.to_string()
        }
    }
}

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

    #[test]
    fn test_parse_ntriples() {
        let nt = r#"
            <http://example.org/alice> <http://example.org/name> "Alice" .
            <http://example.org/alice> <http://example.org/age> "30"^^<http://www.w3.org/2001/XMLSchema#integer> .
        "#;

        let triples = NTriplesParser::parse(nt).unwrap();
        assert_eq!(triples.len(), 2);

        assert_eq!(
            triples[0].subject.as_iri(),
            Some("http://example.org/alice")
        );
        assert!(matches!(&triples[0].object, RdfTerm::Literal { value, .. } if value == "Alice"));
    }

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

            ex:alice foaf:name "Alice" .
        "#;

        let triples = TurtleParser::parse(ttl).unwrap();
        assert_eq!(triples.len(), 1);

        assert_eq!(
            triples[0].subject.as_iri(),
            Some("http://example.org/alice")
        );
        assert_eq!(
            triples[0].predicate.as_iri(),
            Some("http://xmlns.com/foaf/0.1/name")
        );
    }

    #[test]
    fn test_parse_turtle_rdf_type() {
        let ttl = r#"
            @prefix ex: <http://example.org/> .
            ex:alice a ex:Person .
        "#;

        let triples = TurtleParser::parse(ttl).unwrap();
        assert_eq!(triples.len(), 1);
        assert_eq!(
            triples[0].predicate.as_iri(),
            Some("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
        );
    }

    #[test]
    fn test_parse_turtle_multiple_objects() {
        let ttl = r#"
            @prefix ex: <http://example.org/> .
            ex:alice ex:knows ex:bob, ex:charlie .
        "#;

        let triples = TurtleParser::parse(ttl).unwrap();
        assert_eq!(triples.len(), 2);
    }

    #[test]
    fn test_parse_turtle_multiple_predicates() {
        let ttl = r#"
            @prefix ex: <http://example.org/> .
            ex:alice ex:name "Alice" ;
                     ex:age 30 .
        "#;

        let triples = TurtleParser::parse(ttl).unwrap();
        assert_eq!(triples.len(), 2);
    }

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

            ex:test ex:string "hello" ;
                    ex:integer 42 ;
                    ex:double 3.14 ;
                    ex:bool true ;
                    ex:lang "Hola"@es .
        "#;

        let triples = TurtleParser::parse(ttl).unwrap();
        assert_eq!(triples.len(), 5);
    }

    #[test]
    fn test_parse_blank_nodes() {
        let nt = r#"
            _:b1 <http://example.org/name> "Test" .
        "#;

        let triples = NTriplesParser::parse(nt).unwrap();
        assert_eq!(triples.len(), 1);
        assert!(triples[0].subject.is_blank());
    }

    #[test]
    fn test_to_aingle_triple() {
        let rdf = RdfTriple::new(
            RdfTerm::iri("http://example.org/alice"),
            RdfTerm::iri("http://example.org/age"),
            RdfTerm::typed_literal("30", "http://www.w3.org/2001/XMLSchema#integer"),
        );

        let triple = rdf.to_triple().unwrap();
        assert_eq!(triple.object.as_integer(), Some(30));
    }
}