tensorlogic-oxirs-bridge 0.1.0

RDF/GraphQL/SHACL integration and provenance tracking for TensorLogic
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
//! OWL (Web Ontology Language) support for schema analysis.
//!
//! This module extends the basic RDF schema analysis with OWL constructs including:
//! - OWL class definitions (owl:Class, owl:equivalentClass)
//! - Class combinations (owl:unionOf, owl:intersectionOf, owl:complementOf)
//! - Property restrictions (owl:Restriction, owl:onProperty, etc.)
//! - Property characteristics (owl:FunctionalProperty, owl:TransitiveProperty, etc.)

use anyhow::Result;
use oxrdf::{NamedNode, NamedOrBlankNodeRef, TermRef};

use super::{ClassInfo, PropertyInfo, SchemaAnalyzer};

/// OWL namespace
pub const OWL_NS: &str = "http://www.w3.org/2002/07/owl#";

/// Extended class information with OWL-specific constructs
#[derive(Clone, Debug)]
pub struct OwlClassInfo {
    pub base: ClassInfo,
    pub equivalent_classes: Vec<String>,
    pub union_of: Vec<Vec<String>>,
    pub intersection_of: Vec<Vec<String>>,
    pub complement_of: Vec<String>,
    pub disjoint_with: Vec<String>,
}

/// OWL property characteristics
#[derive(Clone, Debug, Default)]
pub struct OwlPropertyCharacteristics {
    pub functional: bool,
    pub inverse_functional: bool,
    pub transitive: bool,
    pub symmetric: bool,
    pub asymmetric: bool,
    pub reflexive: bool,
    pub irreflexive: bool,
}

/// Extended property information with OWL-specific constructs
#[derive(Clone, Debug)]
pub struct OwlPropertyInfo {
    pub base: PropertyInfo,
    pub characteristics: OwlPropertyCharacteristics,
    pub inverse_of: Option<String>,
    pub equivalent_properties: Vec<String>,
    pub sub_property_of: Vec<String>,
}

/// OWL restriction types
#[derive(Clone, Debug)]
pub enum OwlRestriction {
    SomeValuesFrom {
        on_property: String,
        class: String,
    },
    AllValuesFrom {
        on_property: String,
        class: String,
    },
    HasValue {
        on_property: String,
        value: String,
    },
    MinCardinality {
        on_property: String,
        cardinality: u32,
    },
    MaxCardinality {
        on_property: String,
        cardinality: u32,
    },
    ExactCardinality {
        on_property: String,
        cardinality: u32,
    },
    MinQualifiedCardinality {
        on_property: String,
        cardinality: u32,
        on_class: String,
    },
    MaxQualifiedCardinality {
        on_property: String,
        cardinality: u32,
        on_class: String,
    },
    ExactQualifiedCardinality {
        on_property: String,
        cardinality: u32,
        on_class: String,
    },
}

impl SchemaAnalyzer {
    /// Extract OWL class definitions from the graph
    pub fn extract_owl_classes(&self) -> Result<Vec<OwlClassInfo>> {
        let mut owl_classes = Vec::new();

        let owl_class = NamedNode::new(format!("{}Class", OWL_NS))?;
        let rdf_type = NamedNode::new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")?;

        // Find all OWL classes
        for triple in self.graph.iter() {
            if triple.predicate == rdf_type.as_ref() {
                if let TermRef::NamedNode(obj) = triple.object {
                    if obj == owl_class.as_ref() {
                        if let NamedOrBlankNodeRef::NamedNode(subj) = triple.subject {
                            let class_iri = subj.as_str().to_string();
                            let class_node = NamedNode::new(class_iri.clone())?;

                            let owl_info = OwlClassInfo {
                                base: ClassInfo {
                                    iri: class_iri.clone(),
                                    label: self.get_label(&class_node)?,
                                    comment: self.get_comment(&class_node)?,
                                    subclass_of: self.get_subclasses(&class_node)?,
                                },
                                equivalent_classes: self.get_equivalent_classes(&class_node)?,
                                union_of: self.get_union_of(&class_node)?,
                                intersection_of: self.get_intersection_of(&class_node)?,
                                complement_of: self.get_complement_of(&class_node)?,
                                disjoint_with: self.get_disjoint_with(&class_node)?,
                            };

                            owl_classes.push(owl_info);
                        }
                    }
                }
            }
        }

        // Also check for classes defined through RDFS that may have OWL properties
        for (class_iri, class_info) in &self.classes {
            let class_node = NamedNode::new(class_iri.clone())?;

            // Check if this class has OWL-specific properties
            let has_owl_properties = self.has_owl_class_properties(&class_node)?;

            if has_owl_properties {
                let owl_info = OwlClassInfo {
                    base: class_info.clone(),
                    equivalent_classes: self.get_equivalent_classes(&class_node)?,
                    union_of: self.get_union_of(&class_node)?,
                    intersection_of: self.get_intersection_of(&class_node)?,
                    complement_of: self.get_complement_of(&class_node)?,
                    disjoint_with: self.get_disjoint_with(&class_node)?,
                };

                // Only add if not already in the list
                if !owl_classes.iter().any(|c| c.base.iri == *class_iri) {
                    owl_classes.push(owl_info);
                }
            }
        }

        Ok(owl_classes)
    }

    /// Extract OWL property definitions from the graph
    pub fn extract_owl_properties(&self) -> Result<Vec<OwlPropertyInfo>> {
        let mut owl_properties = Vec::new();

        // Check each property for OWL characteristics
        for (prop_iri, prop_info) in &self.properties {
            let prop_node = NamedNode::new(prop_iri.clone())?;

            let owl_info = OwlPropertyInfo {
                base: prop_info.clone(),
                characteristics: self.get_property_characteristics(&prop_node)?,
                inverse_of: self.get_inverse_of(&prop_node)?,
                equivalent_properties: self.get_equivalent_properties(&prop_node)?,
                sub_property_of: self.get_sub_property_of(&prop_node)?,
            };

            owl_properties.push(owl_info);
        }

        // Also check for properties with explicit OWL types
        let property_types = vec![
            format!("{}ObjectProperty", OWL_NS),
            format!("{}DatatypeProperty", OWL_NS),
            format!("{}FunctionalProperty", OWL_NS),
            format!("{}InverseFunctionalProperty", OWL_NS),
            format!("{}TransitiveProperty", OWL_NS),
            format!("{}SymmetricProperty", OWL_NS),
            format!("{}AsymmetricProperty", OWL_NS),
            format!("{}ReflexiveProperty", OWL_NS),
            format!("{}IrreflexiveProperty", OWL_NS),
        ];

        let rdf_type = NamedNode::new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")?;

        for type_iri in property_types {
            let prop_type = NamedNode::new(type_iri)?;

            for triple in self.graph.iter() {
                if triple.predicate == rdf_type.as_ref() {
                    if let TermRef::NamedNode(obj) = triple.object {
                        if obj == prop_type.as_ref() {
                            if let NamedOrBlankNodeRef::NamedNode(subj) = triple.subject {
                                let prop_iri = subj.as_str().to_string();

                                // Skip if already processed
                                if owl_properties.iter().any(|p| p.base.iri == prop_iri) {
                                    continue;
                                }

                                let prop_node = NamedNode::new(prop_iri.clone())?;

                                let base = if let Some(existing) = self.properties.get(&prop_iri) {
                                    existing.clone()
                                } else {
                                    PropertyInfo {
                                        iri: prop_iri.clone(),
                                        label: self.get_label(&prop_node)?,
                                        comment: self.get_comment(&prop_node)?,
                                        domain: self.get_domain(&prop_node)?,
                                        range: self.get_range(&prop_node)?,
                                    }
                                };

                                let owl_info = OwlPropertyInfo {
                                    base,
                                    characteristics: self
                                        .get_property_characteristics(&prop_node)?,
                                    inverse_of: self.get_inverse_of(&prop_node)?,
                                    equivalent_properties: self
                                        .get_equivalent_properties(&prop_node)?,
                                    sub_property_of: self.get_sub_property_of(&prop_node)?,
                                };

                                owl_properties.push(owl_info);
                            }
                        }
                    }
                }
            }
        }

        Ok(owl_properties)
    }

    /// Extract OWL restrictions for a class
    pub fn extract_owl_restrictions(&self, class_node: &NamedNode) -> Result<Vec<OwlRestriction>> {
        let mut restrictions = Vec::new();

        let rdfs_subclass = NamedNode::new("http://www.w3.org/2000/01/rdf-schema#subClassOf")?;
        let owl_restriction = NamedNode::new(format!("{}Restriction", OWL_NS))?;
        let rdf_type = NamedNode::new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")?;

        // Find restrictions in subClassOf relationships
        for triple in self.graph.iter() {
            if let NamedOrBlankNodeRef::NamedNode(subj) = triple.subject {
                if subj == class_node.as_ref() && triple.predicate == rdfs_subclass.as_ref() {
                    if let TermRef::BlankNode(blank) = triple.object {
                        // Check if this blank node is a restriction
                        let is_restriction = self.graph.iter().any(|t| {
                            matches!(t.subject, NamedOrBlankNodeRef::BlankNode(b) if b.as_str() == blank.as_str())
                                && t.predicate == rdf_type.as_ref()
                                && matches!(t.object, TermRef::NamedNode(n) if n == owl_restriction.as_ref())
                        });

                        if is_restriction {
                            if let Some(restriction) = self.parse_restriction(blank.as_str())? {
                                restrictions.push(restriction);
                            }
                        }
                    }
                }
            }
        }

        Ok(restrictions)
    }

    /// Parse a restriction from a blank node
    fn parse_restriction(&self, blank_id: &str) -> Result<Option<OwlRestriction>> {
        let on_property = NamedNode::new(format!("{}onProperty", OWL_NS))?;
        let some_values = NamedNode::new(format!("{}someValuesFrom", OWL_NS))?;
        let all_values = NamedNode::new(format!("{}allValuesFrom", OWL_NS))?;
        let has_value = NamedNode::new(format!("{}hasValue", OWL_NS))?;
        let min_card = NamedNode::new(format!("{}minCardinality", OWL_NS))?;
        let max_card = NamedNode::new(format!("{}maxCardinality", OWL_NS))?;
        let exact_card = NamedNode::new(format!("{}cardinality", OWL_NS))?;
        let min_qualified = NamedNode::new(format!("{}minQualifiedCardinality", OWL_NS))?;
        let max_qualified = NamedNode::new(format!("{}maxQualifiedCardinality", OWL_NS))?;
        let exact_qualified = NamedNode::new(format!("{}qualifiedCardinality", OWL_NS))?;
        let on_class = NamedNode::new(format!("{}onClass", OWL_NS))?;

        let mut property = None;
        let mut some_class = None;
        let mut all_class = None;
        let mut value = None;
        let mut min_c = None;
        let mut max_c = None;
        let mut exact_c = None;
        let mut min_q = None;
        let mut max_q = None;
        let mut exact_q = None;
        let mut qualified_class = None;

        for triple in self.graph.iter() {
            if let NamedOrBlankNodeRef::BlankNode(subj) = triple.subject {
                if subj.as_str() == blank_id {
                    if triple.predicate == on_property.as_ref() {
                        if let TermRef::NamedNode(prop) = triple.object {
                            property = Some(self.extract_local_name(prop.as_str()));
                        }
                    } else if triple.predicate == some_values.as_ref() {
                        if let TermRef::NamedNode(cls) = triple.object {
                            some_class = Some(self.extract_local_name(cls.as_str()));
                        }
                    } else if triple.predicate == all_values.as_ref() {
                        if let TermRef::NamedNode(cls) = triple.object {
                            all_class = Some(self.extract_local_name(cls.as_str()));
                        }
                    } else if triple.predicate == has_value.as_ref() {
                        match triple.object {
                            TermRef::NamedNode(n) => {
                                value = Some(self.extract_local_name(n.as_str()));
                            }
                            TermRef::Literal(lit) => {
                                value = Some(lit.value().to_string());
                            }
                            _ => {}
                        }
                    } else if triple.predicate == min_card.as_ref() {
                        if let TermRef::Literal(lit) = triple.object {
                            min_c = lit.value().parse().ok();
                        }
                    } else if triple.predicate == max_card.as_ref() {
                        if let TermRef::Literal(lit) = triple.object {
                            max_c = lit.value().parse().ok();
                        }
                    } else if triple.predicate == exact_card.as_ref() {
                        if let TermRef::Literal(lit) = triple.object {
                            exact_c = lit.value().parse().ok();
                        }
                    } else if triple.predicate == min_qualified.as_ref() {
                        if let TermRef::Literal(lit) = triple.object {
                            min_q = lit.value().parse().ok();
                        }
                    } else if triple.predicate == max_qualified.as_ref() {
                        if let TermRef::Literal(lit) = triple.object {
                            max_q = lit.value().parse().ok();
                        }
                    } else if triple.predicate == exact_qualified.as_ref() {
                        if let TermRef::Literal(lit) = triple.object {
                            exact_q = lit.value().parse().ok();
                        }
                    } else if triple.predicate == on_class.as_ref() {
                        if let TermRef::NamedNode(cls) = triple.object {
                            qualified_class = Some(self.extract_local_name(cls.as_str()));
                        }
                    }
                }
            }
        }

        if let Some(prop) = property {
            if let Some(cls) = some_class {
                return Ok(Some(OwlRestriction::SomeValuesFrom {
                    on_property: prop,
                    class: cls,
                }));
            }
            if let Some(cls) = all_class {
                return Ok(Some(OwlRestriction::AllValuesFrom {
                    on_property: prop,
                    class: cls,
                }));
            }
            if let Some(val) = value {
                return Ok(Some(OwlRestriction::HasValue {
                    on_property: prop,
                    value: val,
                }));
            }
            if let Some(card) = min_c {
                return Ok(Some(OwlRestriction::MinCardinality {
                    on_property: prop,
                    cardinality: card,
                }));
            }
            if let Some(card) = max_c {
                return Ok(Some(OwlRestriction::MaxCardinality {
                    on_property: prop,
                    cardinality: card,
                }));
            }
            if let Some(card) = exact_c {
                return Ok(Some(OwlRestriction::ExactCardinality {
                    on_property: prop,
                    cardinality: card,
                }));
            }
            if let (Some(card), Some(cls)) = (min_q, qualified_class.as_ref()) {
                return Ok(Some(OwlRestriction::MinQualifiedCardinality {
                    on_property: prop,
                    cardinality: card,
                    on_class: cls.clone(),
                }));
            }
            if let (Some(card), Some(cls)) = (max_q, qualified_class.as_ref()) {
                return Ok(Some(OwlRestriction::MaxQualifiedCardinality {
                    on_property: prop,
                    cardinality: card,
                    on_class: cls.clone(),
                }));
            }
            if let (Some(card), Some(cls)) = (exact_q, qualified_class) {
                return Ok(Some(OwlRestriction::ExactQualifiedCardinality {
                    on_property: prop,
                    cardinality: card,
                    on_class: cls,
                }));
            }
        }

        Ok(None)
    }

    /// Extract local name from full IRI
    fn extract_local_name(&self, iri: &str) -> String {
        iri.split(['/', '#']).next_back().unwrap_or(iri).to_string()
    }

    /// Check if a class has OWL-specific properties
    fn has_owl_class_properties(&self, node: &NamedNode) -> Result<bool> {
        let owl_predicates = vec![
            format!("{}equivalentClass", OWL_NS),
            format!("{}unionOf", OWL_NS),
            format!("{}intersectionOf", OWL_NS),
            format!("{}complementOf", OWL_NS),
            format!("{}disjointWith", OWL_NS),
        ];

        for pred_iri in owl_predicates {
            let pred = NamedNode::new(pred_iri)?;
            for triple in self.graph.iter() {
                if let NamedOrBlankNodeRef::NamedNode(subj) = triple.subject {
                    if subj == node.as_ref() && triple.predicate == pred.as_ref() {
                        return Ok(true);
                    }
                }
            }
        }

        Ok(false)
    }

    /// Get equivalent classes for a node
    fn get_equivalent_classes(&self, node: &NamedNode) -> Result<Vec<String>> {
        let equivalent = NamedNode::new(format!("{}equivalentClass", OWL_NS))?;
        let mut classes = Vec::new();

        for triple in self.graph.iter() {
            if let NamedOrBlankNodeRef::NamedNode(subj) = triple.subject {
                if subj == node.as_ref() && triple.predicate == equivalent.as_ref() {
                    if let TermRef::NamedNode(obj) = triple.object {
                        classes.push(obj.as_str().to_string());
                    }
                }
            }
        }

        Ok(classes)
    }

    /// Get unionOf classes for a node
    fn get_union_of(&self, node: &NamedNode) -> Result<Vec<Vec<String>>> {
        let union_pred = NamedNode::new(format!("{}unionOf", OWL_NS))?;
        let mut unions = Vec::new();

        for triple in self.graph.iter() {
            if let NamedOrBlankNodeRef::NamedNode(subj) = triple.subject {
                if subj == node.as_ref() && triple.predicate == union_pred.as_ref() {
                    if let TermRef::BlankNode(list) = triple.object {
                        let classes = self.extract_rdf_list_named_nodes(list.as_str());
                        if !classes.is_empty() {
                            unions.push(classes);
                        }
                    }
                }
            }
        }

        Ok(unions)
    }

    /// Get intersectionOf classes for a node
    fn get_intersection_of(&self, node: &NamedNode) -> Result<Vec<Vec<String>>> {
        let intersection_pred = NamedNode::new(format!("{}intersectionOf", OWL_NS))?;
        let mut intersections = Vec::new();

        for triple in self.graph.iter() {
            if let NamedOrBlankNodeRef::NamedNode(subj) = triple.subject {
                if subj == node.as_ref() && triple.predicate == intersection_pred.as_ref() {
                    if let TermRef::BlankNode(list) = triple.object {
                        let classes = self.extract_rdf_list_named_nodes(list.as_str());
                        if !classes.is_empty() {
                            intersections.push(classes);
                        }
                    }
                }
            }
        }

        Ok(intersections)
    }

    /// Get complementOf classes for a node
    fn get_complement_of(&self, node: &NamedNode) -> Result<Vec<String>> {
        let complement = NamedNode::new(format!("{}complementOf", OWL_NS))?;
        let mut classes = Vec::new();

        for triple in self.graph.iter() {
            if let NamedOrBlankNodeRef::NamedNode(subj) = triple.subject {
                if subj == node.as_ref() && triple.predicate == complement.as_ref() {
                    if let TermRef::NamedNode(obj) = triple.object {
                        classes.push(obj.as_str().to_string());
                    }
                }
            }
        }

        Ok(classes)
    }

    /// Get disjointWith classes for a node
    fn get_disjoint_with(&self, node: &NamedNode) -> Result<Vec<String>> {
        let disjoint = NamedNode::new(format!("{}disjointWith", OWL_NS))?;
        let mut classes = Vec::new();

        for triple in self.graph.iter() {
            if let NamedOrBlankNodeRef::NamedNode(subj) = triple.subject {
                if subj == node.as_ref() && triple.predicate == disjoint.as_ref() {
                    if let TermRef::NamedNode(obj) = triple.object {
                        classes.push(obj.as_str().to_string());
                    }
                }
            }
        }

        Ok(classes)
    }

    /// Get property characteristics
    fn get_property_characteristics(&self, node: &NamedNode) -> Result<OwlPropertyCharacteristics> {
        let rdf_type = NamedNode::new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")?;

        let mut chars = OwlPropertyCharacteristics::default();

        let char_types = vec![
            (format!("{}FunctionalProperty", OWL_NS), "functional"),
            (
                format!("{}InverseFunctionalProperty", OWL_NS),
                "inverse_functional",
            ),
            (format!("{}TransitiveProperty", OWL_NS), "transitive"),
            (format!("{}SymmetricProperty", OWL_NS), "symmetric"),
            (format!("{}AsymmetricProperty", OWL_NS), "asymmetric"),
            (format!("{}ReflexiveProperty", OWL_NS), "reflexive"),
            (format!("{}IrreflexiveProperty", OWL_NS), "irreflexive"),
        ];

        for (type_iri, char_name) in char_types {
            let type_node = NamedNode::new(type_iri)?;

            for triple in self.graph.iter() {
                if let NamedOrBlankNodeRef::NamedNode(subj) = triple.subject {
                    if subj == node.as_ref() && triple.predicate == rdf_type.as_ref() {
                        if let TermRef::NamedNode(obj) = triple.object {
                            if obj == type_node.as_ref() {
                                match char_name {
                                    "functional" => chars.functional = true,
                                    "inverse_functional" => chars.inverse_functional = true,
                                    "transitive" => chars.transitive = true,
                                    "symmetric" => chars.symmetric = true,
                                    "asymmetric" => chars.asymmetric = true,
                                    "reflexive" => chars.reflexive = true,
                                    "irreflexive" => chars.irreflexive = true,
                                    _ => {}
                                }
                            }
                        }
                    }
                }
            }
        }

        Ok(chars)
    }

    /// Get inverse property
    fn get_inverse_of(&self, node: &NamedNode) -> Result<Option<String>> {
        let inverse = NamedNode::new(format!("{}inverseOf", OWL_NS))?;

        for triple in self.graph.iter() {
            if let NamedOrBlankNodeRef::NamedNode(subj) = triple.subject {
                if subj == node.as_ref() && triple.predicate == inverse.as_ref() {
                    if let TermRef::NamedNode(obj) = triple.object {
                        return Ok(Some(obj.as_str().to_string()));
                    }
                }
            }
        }

        Ok(None)
    }

    /// Get equivalent properties
    fn get_equivalent_properties(&self, node: &NamedNode) -> Result<Vec<String>> {
        let equivalent = NamedNode::new(format!("{}equivalentProperty", OWL_NS))?;
        let mut properties = Vec::new();

        for triple in self.graph.iter() {
            if let NamedOrBlankNodeRef::NamedNode(subj) = triple.subject {
                if subj == node.as_ref() && triple.predicate == equivalent.as_ref() {
                    if let TermRef::NamedNode(obj) = triple.object {
                        properties.push(obj.as_str().to_string());
                    }
                }
            }
        }

        Ok(properties)
    }

    /// Get sub-properties
    fn get_sub_property_of(&self, node: &NamedNode) -> Result<Vec<String>> {
        let sub_property = NamedNode::new("http://www.w3.org/2000/01/rdf-schema#subPropertyOf")?;
        let mut properties = Vec::new();

        for triple in self.graph.iter() {
            if let NamedOrBlankNodeRef::NamedNode(subj) = triple.subject {
                if subj == node.as_ref() && triple.predicate == sub_property.as_ref() {
                    if let TermRef::NamedNode(obj) = triple.object {
                        properties.push(obj.as_str().to_string());
                    }
                }
            }
        }

        Ok(properties)
    }

    /// Extract named nodes from an RDF list
    fn extract_rdf_list_named_nodes(&self, list_id: &str) -> Vec<String> {
        let mut values = Vec::new();
        let rdf_first = match NamedNode::new("http://www.w3.org/1999/02/22-rdf-syntax-ns#first") {
            Ok(n) => n,
            Err(_) => return values,
        };
        let rdf_rest = match NamedNode::new("http://www.w3.org/1999/02/22-rdf-syntax-ns#rest") {
            Ok(n) => n,
            Err(_) => return values,
        };
        let rdf_nil = match NamedNode::new("http://www.w3.org/1999/02/22-rdf-syntax-ns#nil") {
            Ok(n) => n,
            Err(_) => return values,
        };

        let mut current = list_id.to_string();
        loop {
            let mut found_first = false;
            let mut next_node = None;

            for triple in self.graph.iter() {
                if let NamedOrBlankNodeRef::BlankNode(subj) = triple.subject {
                    if subj.as_str() == current {
                        if triple.predicate == rdf_first.as_ref() {
                            if let TermRef::NamedNode(n) = triple.object {
                                values.push(n.as_str().to_string());
                                found_first = true;
                            }
                        } else if triple.predicate == rdf_rest.as_ref() {
                            match triple.object {
                                TermRef::BlankNode(rest) => {
                                    next_node = Some(rest.as_str().to_string());
                                }
                                TermRef::NamedNode(n) if n == rdf_nil.as_ref() => {
                                    return values;
                                }
                                _ => {}
                            }
                        }
                    }
                }
            }

            if !found_first {
                break;
            }

            if let Some(next) = next_node {
                current = next;
            } else {
                break;
            }
        }

        values
    }
}