oxirs-star 0.2.4

RDF-star and SPARQL-star grammar support for quoted triples
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
//! RDF-star data model providing type-safe handling of quoted triples.
//!
//! This module implements the core RDF-star data model extending standard RDF
//! with support for quoted triples as first-class citizens.

use std::collections::HashMap;
use std::fmt;
use std::hash::Hash;

use serde::{Deserialize, Serialize};

use crate::{StarError, StarResult};

/// RDF-star term that can be an IRI, blank node, literal, or quoted triple
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum StarTerm {
    /// Named node (IRI reference)
    NamedNode(NamedNode),
    /// Blank node with identifier
    BlankNode(BlankNode),
    /// Literal value with optional language tag and datatype
    Literal(Literal),
    /// Quoted triple (RDF-star extension)
    QuotedTriple(Box<StarTriple>),
    /// Variable (used in SPARQL-star queries)
    Variable(Variable),
}

/// Named node (IRI) implementation for RDF-star
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct NamedNode {
    pub iri: String,
}

/// Blank node implementation for RDF-star
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct BlankNode {
    pub id: String,
}

/// Literal implementation for RDF-star
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Literal {
    pub value: String,
    pub language: Option<String>,
    pub datatype: Option<NamedNode>,
}

/// Variable implementation for SPARQL-star
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Variable {
    pub name: String,
}

/// RDF-star triple that may contain quoted triples in any position
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct StarTriple {
    pub subject: StarTerm,
    pub predicate: StarTerm,
    pub object: StarTerm,
}

/// RDF-star quad extending triples with optional graph context
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct StarQuad {
    pub subject: StarTerm,
    pub predicate: StarTerm,
    pub object: StarTerm,
    pub graph: Option<StarTerm>,
}

impl StarTerm {
    /// Create a new IRI term
    pub fn iri(iri: &str) -> StarResult<Self> {
        if iri.is_empty() {
            return Err(StarError::invalid_term_type("IRI cannot be empty"));
        }
        Ok(StarTerm::NamedNode(NamedNode {
            iri: iri.to_string(),
        }))
    }

    /// Create a new blank node term
    pub fn blank_node(id: &str) -> StarResult<Self> {
        if id.is_empty() {
            return Err(StarError::invalid_term_type(
                "Blank node ID cannot be empty",
            ));
        }
        Ok(StarTerm::BlankNode(BlankNode { id: id.to_string() }))
    }

    /// Create a new literal term
    pub fn literal(value: &str) -> StarResult<Self> {
        Ok(StarTerm::Literal(Literal {
            value: value.to_string(),
            language: None,
            datatype: None,
        }))
    }

    /// Create a new literal term with language tag
    pub fn literal_with_language(value: &str, language: &str) -> StarResult<Self> {
        Ok(StarTerm::Literal(Literal {
            value: value.to_string(),
            language: Some(language.to_string()),
            datatype: None,
        }))
    }

    /// Create a new literal term with datatype
    pub fn literal_with_datatype(value: &str, datatype: &str) -> StarResult<Self> {
        Ok(StarTerm::Literal(Literal {
            value: value.to_string(),
            language: None,
            datatype: Some(NamedNode {
                iri: datatype.to_string(),
            }),
        }))
    }

    /// Create a new quoted triple term
    pub fn quoted_triple(triple: StarTriple) -> Self {
        StarTerm::QuotedTriple(Box::new(triple))
    }

    /// Create a new variable term
    pub fn variable(name: &str) -> StarResult<Self> {
        if name.is_empty() {
            return Err(StarError::invalid_term_type(
                "Variable name cannot be empty",
            ));
        }
        Ok(StarTerm::Variable(Variable {
            name: name.to_string(),
        }))
    }

    /// Check if this term is a named node
    pub fn is_named_node(&self) -> bool {
        matches!(self, StarTerm::NamedNode(_))
    }

    /// Check if this term is a blank node
    pub fn is_blank_node(&self) -> bool {
        matches!(self, StarTerm::BlankNode(_))
    }

    /// Check if this term is a literal
    pub fn is_literal(&self) -> bool {
        matches!(self, StarTerm::Literal(_))
    }

    /// Check if this term is a quoted triple
    pub fn is_quoted_triple(&self) -> bool {
        matches!(self, StarTerm::QuotedTriple(_))
    }

    /// Check if this term is a variable
    pub fn is_variable(&self) -> bool {
        matches!(self, StarTerm::Variable(_))
    }

    /// Get the IRI if this is a named node
    pub fn as_named_node(&self) -> Option<&NamedNode> {
        match self {
            StarTerm::NamedNode(node) => Some(node),
            _ => None,
        }
    }

    /// Get the blank node if this is a blank node
    pub fn as_blank_node(&self) -> Option<&BlankNode> {
        match self {
            StarTerm::BlankNode(node) => Some(node),
            _ => None,
        }
    }

    /// Get the literal if this is a literal
    pub fn as_literal(&self) -> Option<&Literal> {
        match self {
            StarTerm::Literal(literal) => Some(literal),
            _ => None,
        }
    }

    /// Get the quoted triple if this is a quoted triple
    pub fn as_quoted_triple(&self) -> Option<&StarTriple> {
        match self {
            StarTerm::QuotedTriple(triple) => Some(triple),
            _ => None,
        }
    }

    /// Get the variable if this is a variable
    pub fn as_variable(&self) -> Option<&Variable> {
        match self {
            StarTerm::Variable(var) => Some(var),
            _ => None,
        }
    }

    /// Check if this term can be used as a subject
    pub fn can_be_subject(&self) -> bool {
        matches!(
            self,
            StarTerm::NamedNode(_) | StarTerm::BlankNode(_) | StarTerm::QuotedTriple(_)
        )
    }

    /// Check if this term can be used as a predicate
    pub fn can_be_predicate(&self) -> bool {
        matches!(self, StarTerm::NamedNode(_))
    }

    /// Check if this term can be used as an object
    pub fn can_be_object(&self) -> bool {
        true // All terms can be objects in RDF-star
    }

    /// Calculate the nesting depth of quoted triples
    pub fn nesting_depth(&self) -> usize {
        match self {
            StarTerm::QuotedTriple(triple) => {
                1 + triple
                    .subject
                    .nesting_depth()
                    .max(triple.predicate.nesting_depth())
                    .max(triple.object.nesting_depth())
            }
            _ => 0,
        }
    }
}

impl StarTriple {
    /// Create a new RDF-star triple
    pub fn new(subject: StarTerm, predicate: StarTerm, object: StarTerm) -> Self {
        Self {
            subject,
            predicate,
            object,
        }
    }

    /// Validate that the triple is well-formed according to RDF-star rules
    pub fn validate(&self) -> StarResult<()> {
        if !self.subject.can_be_subject() {
            return Err(StarError::invalid_quoted_triple(format!(
                "Invalid subject term: {:?}",
                self.subject
            )));
        }

        if !self.predicate.can_be_predicate() {
            return Err(StarError::invalid_quoted_triple(format!(
                "Invalid predicate term: {:?}",
                self.predicate
            )));
        }

        if !self.object.can_be_object() {
            return Err(StarError::invalid_quoted_triple(format!(
                "Invalid object term: {:?}",
                self.object
            )));
        }

        // Check for self-containment (W3C RDF-star specification requirement)
        if self.is_self_contained() {
            return Err(StarError::invalid_quoted_triple(
                "Triple cannot contain itself (self-containment is not allowed in RDF-star)"
                    .to_string(),
            ));
        }

        Ok(())
    }

    /// Check if this triple contains a specific triple (recursively)
    ///
    /// This method traverses the quoted triple structure to determine if
    /// the target triple appears anywhere within this triple's quoted triples.
    pub fn contains_triple(&self, target: &StarTriple) -> bool {
        self.check_contains_triple(target, 0, 100)
    }

    /// Internal recursive helper for contains_triple with depth limiting
    fn check_contains_triple(&self, target: &StarTriple, depth: usize, max_depth: usize) -> bool {
        // Prevent stack overflow on pathological deeply nested structures
        if depth > max_depth {
            return false;
        }

        // Check subject position
        if let StarTerm::QuotedTriple(qt) = &self.subject {
            // Direct equality check
            if **qt == *target {
                return true;
            }
            // Recursive check
            if qt.check_contains_triple(target, depth + 1, max_depth) {
                return true;
            }
        }

        // Check predicate position (rare but allowed in RDF-star)
        if let StarTerm::QuotedTriple(qt) = &self.predicate {
            if **qt == *target || qt.check_contains_triple(target, depth + 1, max_depth) {
                return true;
            }
        }

        // Check object position
        if let StarTerm::QuotedTriple(qt) = &self.object {
            if **qt == *target || qt.check_contains_triple(target, depth + 1, max_depth) {
                return true;
            }
        }

        false
    }

    /// Check if this triple is self-contained (contains itself)
    ///
    /// A self-contained triple is one that appears within its own quoted triple
    /// structure, either directly or transitively. This is explicitly disallowed
    /// by the W3C RDF-star specification.
    ///
    /// # Examples
    ///
    /// ```
    /// # use oxirs_star::model::{StarTerm, StarTriple};
    /// // This would create a cycle (if it were possible):
    /// // << A >> p o, where A contains << A >>
    /// ```
    pub fn is_self_contained(&self) -> bool {
        self.contains_triple(self)
    }

    /// Get the maximum nesting depth in this triple
    pub fn nesting_depth(&self) -> usize {
        self.subject
            .nesting_depth()
            .max(self.predicate.nesting_depth())
            .max(self.object.nesting_depth())
    }

    /// Check if this triple contains any quoted triples
    pub fn contains_quoted_triples(&self) -> bool {
        self.subject.is_quoted_triple()
            || self.predicate.is_quoted_triple()
            || self.object.is_quoted_triple()
    }

    /// Count the number of quoted triples in this triple (recursively)
    pub fn count_quoted_triples(&self) -> usize {
        let mut count = 0;

        // Count quoted triples in each position
        if let StarTerm::QuotedTriple(inner) = &self.subject {
            count += 1 + inner.count_quoted_triples();
        }
        if let StarTerm::QuotedTriple(inner) = &self.predicate {
            count += 1 + inner.count_quoted_triples();
        }
        if let StarTerm::QuotedTriple(inner) = &self.object {
            count += 1 + inner.count_quoted_triples();
        }

        count
    }

    /// Convert to a quad with optional graph
    pub fn to_quad(self, graph: Option<StarTerm>) -> StarQuad {
        StarQuad {
            subject: self.subject,
            predicate: self.predicate,
            object: self.object,
            graph,
        }
    }
}

impl StarQuad {
    /// Create a new RDF-star quad
    pub fn new(
        subject: StarTerm,
        predicate: StarTerm,
        object: StarTerm,
        graph: Option<StarTerm>,
    ) -> Self {
        Self {
            subject,
            predicate,
            object,
            graph,
        }
    }

    /// Convert to a triple (losing graph information)
    pub fn to_triple(self) -> StarTriple {
        StarTriple {
            subject: self.subject,
            predicate: self.predicate,
            object: self.object,
        }
    }

    /// Validate that the quad is well-formed
    pub fn validate(&self) -> StarResult<()> {
        // Validate the triple part
        let triple = StarTriple {
            subject: self.subject.clone(),
            predicate: self.predicate.clone(),
            object: self.object.clone(),
        };
        triple.validate()?;

        // Validate graph name if present
        if let Some(ref graph) = self.graph {
            if !matches!(graph, StarTerm::NamedNode(_) | StarTerm::BlankNode(_)) {
                return Err(StarError::invalid_quoted_triple(
                    "Graph name must be a named node or blank node",
                ));
            }
        }

        Ok(())
    }
}

impl fmt::Display for StarTerm {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            StarTerm::NamedNode(node) => write!(f, "<{}>", node.iri),
            StarTerm::BlankNode(node) => write!(f, "_:{}", node.id),
            StarTerm::Literal(literal) => {
                write!(f, "\"{}\"", literal.value)?;
                if let Some(ref lang) = literal.language {
                    write!(f, "@{lang}")?;
                }
                if let Some(ref datatype) = literal.datatype {
                    write!(f, "^^<{}>", datatype.iri)?;
                }
                Ok(())
            }
            StarTerm::QuotedTriple(triple) => write!(
                f,
                "<<{} {} {}>>",
                triple.subject, triple.predicate, triple.object
            ),
            StarTerm::Variable(var) => write!(f, "?{}", var.name),
        }
    }
}

impl fmt::Display for StarTriple {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} {} {} .", self.subject, self.predicate, self.object)
    }
}

impl fmt::Display for StarQuad {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} {} {}", self.subject, self.predicate, self.object)?;
        if let Some(ref graph) = self.graph {
            write!(f, " {graph}")?;
        }
        write!(f, " .")
    }
}

/// Trait for collecting RDF-star terms from data structures
pub trait StarTermVisitor {
    /// Visit a term during traversal
    fn visit_term(&mut self, term: &StarTerm);
}

impl StarTriple {
    /// Apply a visitor to all terms in this triple
    pub fn visit_terms<V: StarTermVisitor>(&self, visitor: &mut V) {
        visitor.visit_term(&self.subject);
        visitor.visit_term(&self.predicate);
        visitor.visit_term(&self.object);

        // Recursively visit quoted triples
        if let StarTerm::QuotedTriple(triple) = &self.subject {
            triple.visit_terms(visitor);
        }
        if let StarTerm::QuotedTriple(triple) = &self.predicate {
            triple.visit_terms(visitor);
        }
        if let StarTerm::QuotedTriple(triple) = &self.object {
            triple.visit_terms(visitor);
        }
    }
}

/// Graph container for RDF-star triples and quads with named graph support
#[derive(Debug, Clone, Default)]
pub struct StarGraph {
    /// Default graph triples (no explicit graph name)
    triples: Vec<StarTriple>,
    /// Named graph quads grouped by graph name
    named_graphs: HashMap<String, Vec<StarTriple>>,
    /// All quads including both default and named graphs
    quads: Vec<StarQuad>,
    /// Statistics about the graph
    statistics: HashMap<String, usize>,
}

impl StarGraph {
    /// Create a new empty graph
    pub fn new() -> Self {
        let mut statistics = HashMap::new();
        statistics.insert("triples".to_string(), 0);

        Self {
            triples: Vec::new(),
            named_graphs: HashMap::new(),
            quads: Vec::new(),
            statistics,
        }
    }

    /// Add a triple to the default graph
    pub fn insert(&mut self, triple: StarTriple) -> StarResult<()> {
        triple.validate()?;
        self.triples.push(triple.clone());

        // Also add as a quad with no graph
        let quad = StarQuad::new(triple.subject, triple.predicate, triple.object, None);
        self.quads.push(quad);

        *self.statistics.entry("triples".to_string()).or_insert(0) += 1;
        Ok(())
    }

    /// Add a quad to the graph (with optional named graph)
    pub fn insert_quad(&mut self, quad: StarQuad) -> StarResult<()> {
        quad.validate()?;

        let triple = StarTriple::new(
            quad.subject.clone(),
            quad.predicate.clone(),
            quad.object.clone(),
        );

        if let Some(ref graph_term) = quad.graph {
            // Named graph
            let graph_key = match graph_term {
                StarTerm::NamedNode(node) => node.iri.clone(),
                StarTerm::BlankNode(node) => format!("_:{}", node.id),
                _ => {
                    return Err(StarError::invalid_quoted_triple(
                        "Graph name must be a named node or blank node",
                    ))
                }
            };

            self.named_graphs
                .entry(graph_key.clone())
                .or_default()
                .push(triple);
            *self
                .statistics
                .entry(format!("graph_{graph_key}"))
                .or_insert(0) += 1;
        } else {
            // Default graph
            self.triples.push(triple);
            *self.statistics.entry("triples".to_string()).or_insert(0) += 1;
        }

        self.quads.push(quad);
        *self.statistics.entry("quads".to_string()).or_insert(0) += 1;
        Ok(())
    }

    /// Get all triples in the default graph
    pub fn triples(&self) -> &[StarTriple] {
        &self.triples
    }

    /// Get all quads in the graph (including both default and named graphs)
    pub fn quads(&self) -> &[StarQuad] {
        &self.quads
    }

    /// Get triples from a specific named graph
    pub fn named_graph_triples(&self, graph_name: &str) -> Option<&Vec<StarTriple>> {
        self.named_graphs.get(graph_name)
    }

    /// Get all named graph names
    pub fn named_graph_names(&self) -> Vec<&String> {
        self.named_graphs.keys().collect()
    }

    /// Get all triples from all graphs (default + named)
    pub fn all_triples(&self) -> Vec<StarTriple> {
        let mut all = self.triples.clone();
        for triples in self.named_graphs.values() {
            all.extend(triples.clone());
        }
        all
    }

    /// Check if the graph contains a specific triple in any graph
    pub fn contains(&self, triple: &StarTriple) -> bool {
        self.triples.contains(triple)
            || self
                .named_graphs
                .values()
                .any(|triples| triples.contains(triple))
    }

    /// Check if a specific named graph exists
    pub fn contains_named_graph(&self, graph_name: &str) -> bool {
        self.named_graphs.contains_key(graph_name)
    }

    /// Remove a triple from the default graph
    pub fn remove(&mut self, triple: &StarTriple) -> bool {
        if let Some(pos) = self.triples.iter().position(|t| t == triple) {
            self.triples.remove(pos);

            // Also remove from quads
            self.quads.retain(|q| {
                let q_triple =
                    StarTriple::new(q.subject.clone(), q.predicate.clone(), q.object.clone());
                q_triple != *triple || q.graph.is_some()
            });

            if let Some(count) = self.statistics.get_mut("triples") {
                *count = count.saturating_sub(1);
            }
            true
        } else {
            false
        }
    }

    /// Remove a quad from the graph
    pub fn remove_quad(&mut self, quad: &StarQuad) -> bool {
        if let Some(pos) = self.quads.iter().position(|q| q == quad) {
            let removed_quad = self.quads.remove(pos);

            // Remove from appropriate graph
            if let Some(ref graph_term) = removed_quad.graph {
                let graph_key = match graph_term {
                    StarTerm::NamedNode(node) => node.iri.clone(),
                    StarTerm::BlankNode(node) => format!("_:{}", node.id),
                    _ => return false,
                };

                if let Some(triples) = self.named_graphs.get_mut(&graph_key) {
                    let triple = StarTriple::new(
                        removed_quad.subject,
                        removed_quad.predicate,
                        removed_quad.object,
                    );
                    triples.retain(|t| t != &triple);

                    if triples.is_empty() {
                        self.named_graphs.remove(&graph_key);
                    }
                }
            } else {
                // Remove from default graph
                let triple = StarTriple::new(
                    removed_quad.subject,
                    removed_quad.predicate,
                    removed_quad.object,
                );
                self.triples.retain(|t| t != &triple);
            }

            if let Some(count) = self.statistics.get_mut("quads") {
                *count = count.saturating_sub(1);
            }
            true
        } else {
            false
        }
    }

    /// Get the number of triples in the default graph
    pub fn len(&self) -> usize {
        self.triples.len()
    }

    /// Get the total number of triples across all graphs
    pub fn total_len(&self) -> usize {
        self.triples.len() + self.named_graphs.values().map(|v| v.len()).sum::<usize>()
    }

    /// Get the total number of quads
    pub fn quad_len(&self) -> usize {
        self.quads.len()
    }

    /// Check if the default graph is empty
    pub fn is_empty(&self) -> bool {
        self.triples.is_empty()
    }

    /// Check if all graphs are empty
    pub fn is_completely_empty(&self) -> bool {
        self.triples.is_empty() && self.named_graphs.is_empty()
    }

    /// Clear all triples and quads from all graphs
    pub fn clear(&mut self) {
        self.triples.clear();
        self.named_graphs.clear();
        self.quads.clear();
        self.statistics.clear();
    }

    /// Clear a specific named graph
    pub fn clear_named_graph(&mut self, graph_name: &str) {
        if let Some(triples) = self.named_graphs.remove(graph_name) {
            // Remove corresponding quads
            self.quads.retain(|q| {
                if let Some(ref graph_term) = q.graph {
                    let key = match graph_term {
                        StarTerm::NamedNode(node) => node.iri.clone(),
                        StarTerm::BlankNode(node) => format!("_:{}", node.id),
                        _ => String::new(),
                    };
                    key != graph_name
                } else {
                    true
                }
            });

            self.statistics.remove(&format!("graph_{graph_name}"));
            if let Some(count) = self.statistics.get_mut("quads") {
                *count = count.saturating_sub(triples.len());
            }
        }
    }

    /// Get statistics about the graph
    pub fn statistics(&self) -> &HashMap<String, usize> {
        &self.statistics
    }

    /// Count quoted triples across all graphs
    pub fn count_quoted_triples(&self) -> usize {
        let mut count = 0;

        // Count in default graph
        for triple in &self.triples {
            if triple.contains_quoted_triples() {
                count += 1;
            }
        }

        // Count in named graphs
        for triples in self.named_graphs.values() {
            for triple in triples {
                if triple.contains_quoted_triples() {
                    count += 1;
                }
            }
        }

        count
    }

    /// Get maximum nesting depth across all graphs
    pub fn max_nesting_depth(&self) -> usize {
        let default_max = self
            .triples
            .iter()
            .map(|t| t.nesting_depth())
            .max()
            .unwrap_or(0);

        let named_max = self
            .named_graphs
            .values()
            .flat_map(|triples| triples.iter())
            .map(|t| t.nesting_depth())
            .max()
            .unwrap_or(0);

        default_max.max(named_max)
    }

    /// Check if the graph is valid (all triples validate)
    pub fn is_valid(&self) -> bool {
        // Check default graph triples
        for triple in &self.triples {
            if triple.validate().is_err() {
                return false;
            }
        }

        // Check named graph triples
        for triples in self.named_graphs.values() {
            for triple in triples {
                if triple.validate().is_err() {
                    return false;
                }
            }
        }

        true
    }

    /// Get an iterator over all triples in all graphs
    pub fn iter(&self) -> impl Iterator<Item = &StarTriple> {
        self.triples
            .iter()
            .chain(self.named_graphs.values().flatten())
    }

    /// Get an iterator over all subjects in the graph
    pub fn subjects(&self) -> impl Iterator<Item = &StarTerm> {
        self.iter().map(|triple| &triple.subject)
    }

    /// Get an iterator over all predicates in the graph
    pub fn predicates(&self) -> impl Iterator<Item = &StarTerm> {
        self.iter().map(|triple| &triple.predicate)
    }

    /// Get an iterator over all objects in the graph
    pub fn objects(&self) -> impl Iterator<Item = &StarTerm> {
        self.iter().map(|triple| &triple.object)
    }
}

/// Iterator implementation for StarGraph to iterate over all triples
impl<'a> IntoIterator for &'a StarGraph {
    type Item = &'a StarTriple;
    type IntoIter = std::iter::Chain<
        std::slice::Iter<'a, StarTriple>,
        std::iter::Flatten<std::collections::hash_map::Values<'a, String, Vec<StarTriple>>>,
    >;

    fn into_iter(self) -> Self::IntoIter {
        self.triples
            .iter()
            .chain(self.named_graphs.values().flatten())
    }
}

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

    #[test]
    fn test_star_term_creation() {
        let iri = StarTerm::iri("http://example.org/test").unwrap();
        assert!(iri.is_named_node());
        assert!(!iri.is_literal());

        let literal = StarTerm::literal("test value").unwrap();
        assert!(literal.is_literal());
        assert!(!literal.is_named_node());

        let blank = StarTerm::blank_node("b1").unwrap();
        assert!(blank.is_blank_node());
    }

    #[test]
    fn test_quoted_triple_creation() {
        let subject = StarTerm::iri("http://example.org/alice").unwrap();
        let predicate = StarTerm::iri("http://example.org/age").unwrap();
        let object = StarTerm::literal("25").unwrap();

        let triple = StarTriple::new(subject, predicate, object);
        assert!(triple.validate().is_ok());

        let quoted = StarTerm::quoted_triple(triple);
        assert!(quoted.is_quoted_triple());
        assert_eq!(quoted.nesting_depth(), 1);
    }

    #[test]
    fn test_nested_quoted_triples() {
        // Create inner triple
        let inner = StarTriple::new(
            StarTerm::iri("http://example.org/alice").unwrap(),
            StarTerm::iri("http://example.org/knows").unwrap(),
            StarTerm::iri("http://example.org/bob").unwrap(),
        );

        // Create outer triple with quoted triple as subject
        let outer = StarTriple::new(
            StarTerm::quoted_triple(inner),
            StarTerm::iri("http://example.org/certainty").unwrap(),
            StarTerm::literal("0.9").unwrap(),
        );

        assert_eq!(outer.nesting_depth(), 1);
        assert!(outer.contains_quoted_triples());
    }

    #[test]
    fn test_star_graph_operations() {
        let mut graph = StarGraph::new();
        assert!(graph.is_empty());

        let triple = StarTriple::new(
            StarTerm::iri("http://example.org/s").unwrap(),
            StarTerm::iri("http://example.org/p").unwrap(),
            StarTerm::iri("http://example.org/o").unwrap(),
        );

        graph.insert(triple.clone()).unwrap();
        assert_eq!(graph.len(), 1);
        assert!(graph.contains(&triple));

        graph.remove(&triple);
        assert!(graph.is_empty());
    }

    #[test]
    fn test_validation() {
        // Valid triple
        let valid = StarTriple::new(
            StarTerm::iri("http://example.org/s").unwrap(),
            StarTerm::iri("http://example.org/p").unwrap(),
            StarTerm::literal("object").unwrap(),
        );
        assert!(valid.validate().is_ok());

        // Invalid predicate (literal cannot be predicate)
        let invalid = StarTriple::new(
            StarTerm::iri("http://example.org/s").unwrap(),
            StarTerm::literal("invalid_predicate").unwrap(),
            StarTerm::literal("object").unwrap(),
        );
        assert!(invalid.validate().is_err());
    }

    #[test]
    fn test_display_formatting() {
        let triple = StarTriple::new(
            StarTerm::iri("http://example.org/alice").unwrap(),
            StarTerm::iri("http://example.org/age").unwrap(),
            StarTerm::literal("25").unwrap(),
        );

        let display_str = format!("{triple}");
        assert!(display_str.contains("<http://example.org/alice>"));
        assert!(display_str.contains("\"25\""));

        let quoted = StarTerm::quoted_triple(triple.clone());
        let quoted_str = format!("{quoted}");
        assert!(quoted_str.starts_with("<<"));
        assert!(quoted_str.ends_with(">>"));
    }
}