oxirs-gql 0.2.4

GraphQL façade for OxiRS with automatic schema generation from RDF ontologies
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
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
//! GraphQL schema generation from RDF ontologies

use crate::rdf_scalars::RdfScalars;
use crate::types::*;
use anyhow::{anyhow, Result};
use oxirs_core::format::{RdfFormat, RdfParser};
use std::collections::{HashMap, HashSet};
use std::fmt::Write;

/// RDF vocabulary information extracted from an ontology
#[derive(Debug, Clone)]
pub struct RdfVocabulary {
    pub classes: HashMap<String, RdfClass>,
    pub properties: HashMap<String, RdfProperty>,
    pub namespaces: HashMap<String, String>,
}

/// RDF class information
#[derive(Debug, Clone)]
pub struct RdfClass {
    pub uri: String,
    pub label: Option<String>,
    pub comment: Option<String>,
    pub super_classes: Vec<String>,
    pub properties: Vec<String>,
}

/// RDF property information
#[derive(Debug, Clone)]
pub struct RdfProperty {
    pub uri: String,
    pub label: Option<String>,
    pub comment: Option<String>,
    pub domain: Vec<String>,
    pub range: Vec<String>,
    pub property_type: PropertyType,
    pub functional: bool,
    pub inverse_functional: bool,
}

/// Type of RDF property
#[derive(Debug, Clone, PartialEq)]
pub enum PropertyType {
    DataProperty,
    ObjectProperty,
    AnnotationProperty,
}

/// Configuration for schema generation
#[derive(Debug, Clone)]
pub struct SchemaGenerationConfig {
    pub include_deprecated: bool,
    pub max_depth: usize,
    pub custom_scalars: HashMap<String, String>,
    pub type_mappings: HashMap<String, String>,
    pub exclude_classes: HashSet<String>,
    pub exclude_properties: HashSet<String>,
    pub enable_introspection: bool,
    pub enable_mutations: bool,
    pub enable_subscriptions: bool,
}

impl Default for SchemaGenerationConfig {
    fn default() -> Self {
        Self {
            include_deprecated: false,
            max_depth: 10,
            custom_scalars: HashMap::new(),
            type_mappings: Self::default_type_mappings(),
            exclude_classes: HashSet::new(),
            exclude_properties: HashSet::new(),
            enable_introspection: true,
            enable_mutations: false,
            enable_subscriptions: false,
        }
    }
}

impl SchemaGenerationConfig {
    fn default_type_mappings() -> HashMap<String, String> {
        let mut mappings = HashMap::new();

        // XSD mappings
        mappings.insert(
            "http://www.w3.org/2001/XMLSchema#string".to_string(),
            "String".to_string(),
        );
        mappings.insert(
            "http://www.w3.org/2001/XMLSchema#int".to_string(),
            "Int".to_string(),
        );
        mappings.insert(
            "http://www.w3.org/2001/XMLSchema#integer".to_string(),
            "Int".to_string(),
        );
        mappings.insert(
            "http://www.w3.org/2001/XMLSchema#long".to_string(),
            "Int".to_string(),
        );
        mappings.insert(
            "http://www.w3.org/2001/XMLSchema#float".to_string(),
            "Float".to_string(),
        );
        mappings.insert(
            "http://www.w3.org/2001/XMLSchema#double".to_string(),
            "Float".to_string(),
        );
        mappings.insert(
            "http://www.w3.org/2001/XMLSchema#decimal".to_string(),
            "Float".to_string(),
        );
        mappings.insert(
            "http://www.w3.org/2001/XMLSchema#boolean".to_string(),
            "Boolean".to_string(),
        );
        mappings.insert(
            "http://www.w3.org/2001/XMLSchema#dateTime".to_string(),
            "DateTime".to_string(),
        );
        mappings.insert(
            "http://www.w3.org/2001/XMLSchema#date".to_string(),
            "DateTime".to_string(),
        );
        mappings.insert(
            "http://www.w3.org/2001/XMLSchema#time".to_string(),
            "DateTime".to_string(),
        );
        mappings.insert(
            "http://www.w3.org/2001/XMLSchema#duration".to_string(),
            "Duration".to_string(),
        );
        mappings.insert(
            "http://www.w3.org/2001/XMLSchema#anyURI".to_string(),
            "IRI".to_string(),
        );

        // RDF mappings
        mappings.insert(
            "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString".to_string(),
            "LangString".to_string(),
        );
        mappings.insert(
            "http://www.w3.org/2000/01/rdf-schema#Literal".to_string(),
            "Literal".to_string(),
        );
        mappings.insert(
            "http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral".to_string(),
            "String".to_string(),
        );

        // RDFS mappings
        mappings.insert(
            "http://www.w3.org/2000/01/rdf-schema#Resource".to_string(),
            "IRI".to_string(),
        );

        mappings
    }
}

/// Schema generator that converts RDF ontologies to GraphQL schemas
pub struct SchemaGenerator {
    config: SchemaGenerationConfig,
    vocabulary: Option<RdfVocabulary>,
}

impl SchemaGenerator {
    pub fn new() -> Self {
        Self {
            config: SchemaGenerationConfig::default(),
            vocabulary: None,
        }
    }

    pub fn with_config(mut self, config: SchemaGenerationConfig) -> Self {
        self.config = config;
        self
    }

    /// Set the RDF vocabulary to generate schema from
    pub fn with_vocabulary(mut self, vocabulary: RdfVocabulary) -> Self {
        self.vocabulary = Some(vocabulary);
        self
    }

    /// Generate GraphQL schema from loaded RDF vocabulary
    pub fn generate_schema(&self) -> Result<Schema> {
        let vocabulary = self
            .vocabulary
            .as_ref()
            .ok_or_else(|| anyhow!("No vocabulary loaded"))?;

        let mut schema = Schema::new();

        // Add RDF-specific scalar types
        schema.add_type(GraphQLType::Scalar(RdfScalars::iri()));
        schema.add_type(GraphQLType::Scalar(RdfScalars::literal()));
        schema.add_type(GraphQLType::Scalar(RdfScalars::datetime()));
        schema.add_type(GraphQLType::Scalar(RdfScalars::duration()));
        schema.add_type(GraphQLType::Scalar(RdfScalars::geolocation()));
        schema.add_type(GraphQLType::Scalar(RdfScalars::lang_string()));

        // Generate object types from RDF classes
        for (class_uri, rdf_class) in &vocabulary.classes {
            if self.config.exclude_classes.contains(class_uri) {
                continue;
            }

            let object_type = self.generate_object_type_from_class(rdf_class, vocabulary)?;
            schema.add_type(GraphQLType::Object(object_type));
        }

        // Generate Query type
        let query_type = self.generate_query_type(vocabulary)?;
        schema.add_type(GraphQLType::Object(query_type));
        schema.set_query_type("Query".to_string());

        // Generate Mutation type if enabled
        if self.config.enable_mutations {
            let mutation_type = self.generate_mutation_type(vocabulary)?;
            schema.add_type(GraphQLType::Object(mutation_type));
            schema.set_mutation_type("Mutation".to_string());
        }

        // Generate Subscription type if enabled
        if self.config.enable_subscriptions {
            let subscription_type = self.generate_subscription_type(vocabulary)?;
            schema.add_type(GraphQLType::Object(subscription_type));
            schema.set_subscription_type("Subscription".to_string());
        }

        Ok(schema)
    }

    /// Generate GraphQL schema SDL from RDF ontology
    pub async fn generate_from_ontology(&self, ontology_uri: &str) -> Result<String> {
        // Load and parse real RDF ontology from URI
        let vocabulary = self.load_ontology_from_uri(ontology_uri).await?;

        let schema_with_vocab = Self::new()
            .with_config(self.config.clone())
            .with_vocabulary(vocabulary);

        let schema = schema_with_vocab.generate_schema()?;
        Ok(self.schema_to_sdl(&schema))
    }

    /// Generate GraphQL schema from RDF store containing ontology data
    pub fn generate_from_store(&self, store: &crate::RdfStore) -> Result<String> {
        let vocabulary = self.extract_vocabulary_from_store(store)?;

        let schema_with_vocab = Self::new()
            .with_config(self.config.clone())
            .with_vocabulary(vocabulary);

        let schema = schema_with_vocab.generate_schema()?;
        Ok(self.schema_to_sdl(&schema))
    }

    /// Extract RDF vocabulary from a store using SPARQL queries
    pub fn extract_vocabulary_from_store(&self, store: &crate::RdfStore) -> Result<RdfVocabulary> {
        let mut classes = HashMap::new();
        let mut properties = HashMap::new();
        let mut namespaces = HashMap::new();

        // Extract namespaces
        namespaces.insert(
            "rdf".to_string(),
            "http://www.w3.org/1999/02/22-rdf-syntax-ns#".to_string(),
        );
        namespaces.insert(
            "rdfs".to_string(),
            "http://www.w3.org/2000/01/rdf-schema#".to_string(),
        );
        namespaces.insert(
            "owl".to_string(),
            "http://www.w3.org/2002/07/owl#".to_string(),
        );

        // Extract classes using SPARQL
        let class_query = r#"
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            PREFIX owl: <http://www.w3.org/2002/07/owl#>
            
            SELECT DISTINCT ?class ?label ?comment ?superClass
            WHERE {
                {
                    ?class a rdfs:Class .
                } UNION {
                    ?class a owl:Class .
                }
                OPTIONAL { ?class rdfs:label ?label }
                OPTIONAL { ?class rdfs:comment ?comment }
                OPTIONAL { ?class rdfs:subClassOf ?superClass }
                FILTER(!isBlank(?class))
            }
        "#;

        if let Ok(results) = store.query(class_query) {
            self.process_class_results(results, &mut classes)?;
        }

        // Extract properties using SPARQL
        let property_query = r#"
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            PREFIX owl: <http://www.w3.org/2002/07/owl#>
            
            SELECT DISTINCT ?property ?label ?comment ?domain ?range ?type
            WHERE {
                {
                    ?property a rdf:Property .
                    BIND("AnnotationProperty" as ?type)
                } UNION {
                    ?property a rdfs:Property .
                    BIND("DataProperty" as ?type)
                } UNION {
                    ?property a owl:DatatypeProperty .
                    BIND("DataProperty" as ?type)
                } UNION {
                    ?property a owl:ObjectProperty .
                    BIND("ObjectProperty" as ?type)
                } UNION {
                    ?property a owl:AnnotationProperty .
                    BIND("AnnotationProperty" as ?type)
                }
                OPTIONAL { ?property rdfs:label ?label }
                OPTIONAL { ?property rdfs:comment ?comment }
                OPTIONAL { ?property rdfs:domain ?domain }
                OPTIONAL { ?property rdfs:range ?range }
                FILTER(!isBlank(?property))
            }
        "#;

        if let Ok(results) = store.query(property_query) {
            self.process_property_results(results, &mut properties)?;
        }

        // Link properties to classes
        self.link_properties_to_classes(&mut classes, &properties);

        Ok(RdfVocabulary {
            classes,
            properties,
            namespaces,
        })
    }

    fn process_class_results(
        &self,
        results: oxirs_core::query::QueryResults,
        classes: &mut HashMap<String, RdfClass>,
    ) -> Result<()> {
        use oxirs_core::query::QueryResults;

        if let QueryResults::Solutions(solutions) = results {
            for solution in solutions {
                if let Some(class_term) = solution.get(
                    &oxirs_core::model::Variable::new("class")
                        .expect("hardcoded variable name should be valid"),
                ) {
                    let class_uri = class_term.to_string();

                    let label = solution
                        .get(
                            &oxirs_core::model::Variable::new("label")
                                .expect("hardcoded variable name should be valid"),
                        )
                        .and_then(|t| self.extract_literal_value(&t.to_string()));

                    let comment = solution
                        .get(
                            &oxirs_core::model::Variable::new("comment")
                                .expect("hardcoded variable name should be valid"),
                        )
                        .and_then(|t| self.extract_literal_value(&t.to_string()));

                    let super_class = solution
                        .get(
                            &oxirs_core::model::Variable::new("superClass")
                                .expect("hardcoded variable name should be valid"),
                        )
                        .map(|t| t.to_string());

                    // Get or create class entry
                    let rdf_class = classes
                        .entry(class_uri.clone())
                        .or_insert_with(|| RdfClass {
                            uri: class_uri.clone(),
                            label: None,
                            comment: None,
                            super_classes: Vec::new(),
                            properties: Vec::new(),
                        });

                    // Update class information
                    if label.is_some() {
                        rdf_class.label = label;
                    }
                    if comment.is_some() {
                        rdf_class.comment = comment;
                    }
                    if let Some(sc) = super_class {
                        if !rdf_class.super_classes.contains(&sc) {
                            rdf_class.super_classes.push(sc);
                        }
                    }
                }
            }
        }

        Ok(())
    }

    fn process_property_results(
        &self,
        results: oxirs_core::query::QueryResults,
        properties: &mut HashMap<String, RdfProperty>,
    ) -> Result<()> {
        use oxirs_core::query::QueryResults;

        if let QueryResults::Solutions(solutions) = results {
            for solution in solutions {
                if let Some(property_term) = solution.get(
                    &oxirs_core::model::Variable::new("property")
                        .expect("hardcoded variable name should be valid"),
                ) {
                    let property_uri = property_term.to_string();

                    let label = solution
                        .get(
                            &oxirs_core::model::Variable::new("label")
                                .expect("hardcoded variable name should be valid"),
                        )
                        .and_then(|t| self.extract_literal_value(&t.to_string()));

                    let comment = solution
                        .get(
                            &oxirs_core::model::Variable::new("comment")
                                .expect("hardcoded variable name should be valid"),
                        )
                        .and_then(|t| self.extract_literal_value(&t.to_string()));

                    let domain = solution
                        .get(
                            &oxirs_core::model::Variable::new("domain")
                                .expect("hardcoded variable name should be valid"),
                        )
                        .map(|t| t.to_string());

                    let range = solution
                        .get(
                            &oxirs_core::model::Variable::new("range")
                                .expect("hardcoded variable name should be valid"),
                        )
                        .map(|t| t.to_string());

                    let property_type = solution
                        .get(
                            &oxirs_core::model::Variable::new("type")
                                .expect("hardcoded variable name should be valid"),
                        )
                        .map(|t| t.to_string())
                        .and_then(|s| self.extract_literal_value(&s))
                        .unwrap_or_else(|| "AnnotationProperty".to_string());

                    let prop_type = match property_type.as_str() {
                        "DataProperty" => PropertyType::DataProperty,
                        "ObjectProperty" => PropertyType::ObjectProperty,
                        _ => PropertyType::AnnotationProperty,
                    };

                    // Get or create property entry
                    let rdf_property =
                        properties
                            .entry(property_uri.clone())
                            .or_insert_with(|| RdfProperty {
                                uri: property_uri.clone(),
                                label: None,
                                comment: None,
                                domain: Vec::new(),
                                range: Vec::new(),
                                property_type: prop_type,
                                functional: false,
                                inverse_functional: false,
                            });

                    // Update property information
                    if label.is_some() {
                        rdf_property.label = label;
                    }
                    if comment.is_some() {
                        rdf_property.comment = comment;
                    }
                    if let Some(d) = domain {
                        if !rdf_property.domain.contains(&d) {
                            rdf_property.domain.push(d);
                        }
                    }
                    if let Some(r) = range {
                        if !rdf_property.range.contains(&r) {
                            rdf_property.range.push(r);
                        }
                    }
                }
            }
        }

        Ok(())
    }

    fn extract_literal_value(&self, term_str: &str) -> Option<String> {
        // Extract literal value from RDF term string format
        if let Some(stripped) = term_str.strip_prefix('"') {
            if let Some(end_quote) = stripped.find('"') {
                return Some(stripped[..end_quote].to_string());
            }
        }
        None
    }

    fn link_properties_to_classes(
        &self,
        classes: &mut HashMap<String, RdfClass>,
        properties: &HashMap<String, RdfProperty>,
    ) {
        for (property_uri, property) in properties {
            for domain_class in &property.domain {
                if let Some(class) = classes.get_mut(domain_class) {
                    if !class.properties.contains(property_uri) {
                        class.properties.push(property_uri.clone());
                    }
                }
            }
        }
    }

    fn generate_object_type_from_class(
        &self,
        rdf_class: &RdfClass,
        vocabulary: &RdfVocabulary,
    ) -> Result<ObjectType> {
        let type_name = self.uri_to_graphql_name(&rdf_class.uri);
        let mut object_type = ObjectType::new(type_name);

        if let Some(ref comment) = rdf_class.comment {
            object_type = object_type.with_description(comment.clone());
        }

        // Add ID field
        object_type = object_type.with_field(
            "id".to_string(),
            FieldType::new(
                "id".to_string(),
                GraphQLType::NonNull(Box::new(GraphQLType::Scalar(BuiltinScalars::id()))),
            )
            .with_description("The unique identifier of this resource".to_string()),
        );

        // Add URI field
        object_type = object_type.with_field(
            "uri".to_string(),
            FieldType::new(
                "uri".to_string(),
                GraphQLType::NonNull(Box::new(GraphQLType::Scalar(RdfScalars::iri()))),
            )
            .with_description("The IRI of this resource".to_string()),
        );

        // Add fields from properties
        for property_uri in &rdf_class.properties {
            if self.config.exclude_properties.contains(property_uri) {
                continue;
            }

            if let Some(property) = vocabulary.properties.get(property_uri) {
                let field = self.generate_field_from_property(property, vocabulary)?;
                let field_name = self.uri_to_graphql_name(&property.uri);
                object_type = object_type.with_field(field_name, field);
            }
        }

        Ok(object_type)
    }

    fn generate_field_from_property(
        &self,
        property: &RdfProperty,
        vocabulary: &RdfVocabulary,
    ) -> Result<FieldType> {
        let field_name = self.uri_to_graphql_name(&property.uri);

        let field_type = match property.property_type {
            PropertyType::DataProperty => self.generate_scalar_type_from_range(&property.range)?,
            PropertyType::ObjectProperty => {
                self.generate_object_type_from_range(&property.range, vocabulary)?
            }
            PropertyType::AnnotationProperty => GraphQLType::Scalar(BuiltinScalars::string()),
        };

        // Make non-functional properties lists
        let final_type = if property.functional {
            field_type
        } else {
            GraphQLType::List(Box::new(field_type))
        };

        let mut field = FieldType::new(field_name, final_type);

        if let Some(ref comment) = property.comment {
            field = field.with_description(comment.clone());
        }

        // Add filter arguments for object properties
        if matches!(property.property_type, PropertyType::ObjectProperty) {
            field = field.with_argument(
                "where".to_string(),
                ArgumentType::new(
                    "where".to_string(),
                    GraphQLType::Scalar(BuiltinScalars::string()),
                )
                .with_description("SPARQL filter condition".to_string()),
            );

            field = field.with_argument(
                "limit".to_string(),
                ArgumentType::new(
                    "limit".to_string(),
                    GraphQLType::Scalar(BuiltinScalars::int()),
                )
                .with_default_value(crate::ast::Value::IntValue(10))
                .with_description("Maximum number of results".to_string()),
            );

            field = field.with_argument(
                "offset".to_string(),
                ArgumentType::new(
                    "offset".to_string(),
                    GraphQLType::Scalar(BuiltinScalars::int()),
                )
                .with_default_value(crate::ast::Value::IntValue(0))
                .with_description("Number of results to skip".to_string()),
            );
        }

        Ok(field)
    }

    fn generate_scalar_type_from_range(&self, range: &[String]) -> Result<GraphQLType> {
        if range.is_empty() {
            return Ok(GraphQLType::Scalar(BuiltinScalars::string()));
        }

        for range_uri in range {
            if let Some(mapped_type) = self.config.type_mappings.get(range_uri) {
                return match mapped_type.as_str() {
                    "String" => Ok(GraphQLType::Scalar(BuiltinScalars::string())),
                    "Int" => Ok(GraphQLType::Scalar(BuiltinScalars::int())),
                    "Float" => Ok(GraphQLType::Scalar(BuiltinScalars::float())),
                    "Boolean" => Ok(GraphQLType::Scalar(BuiltinScalars::boolean())),
                    "ID" => Ok(GraphQLType::Scalar(BuiltinScalars::id())),
                    "IRI" => Ok(GraphQLType::Scalar(RdfScalars::iri())),
                    "Literal" => Ok(GraphQLType::Scalar(RdfScalars::literal())),
                    "DateTime" => Ok(GraphQLType::Scalar(RdfScalars::datetime())),
                    "Duration" => Ok(GraphQLType::Scalar(RdfScalars::duration())),
                    "GeoLocation" => Ok(GraphQLType::Scalar(RdfScalars::geolocation())),
                    "LangString" => Ok(GraphQLType::Scalar(RdfScalars::lang_string())),
                    _ => Ok(GraphQLType::Scalar(BuiltinScalars::string())),
                };
            }
        }

        // Default to Literal for unknown ranges
        Ok(GraphQLType::Scalar(RdfScalars::literal()))
    }

    fn generate_object_type_from_range(
        &self,
        range: &[String],
        vocabulary: &RdfVocabulary,
    ) -> Result<GraphQLType> {
        if range.is_empty() {
            return Ok(GraphQLType::Scalar(RdfScalars::iri()));
        }

        // For now, return the first valid class in range
        for range_uri in range {
            if vocabulary.classes.contains_key(range_uri) {
                let type_name = self.uri_to_graphql_name(range_uri);
                // We assume the type will be generated elsewhere
                return Ok(GraphQLType::Object(ObjectType::new(type_name)));
            }
        }

        // Default to IRI if no class found
        Ok(GraphQLType::Scalar(RdfScalars::iri()))
    }

    fn generate_query_type(&self, vocabulary: &RdfVocabulary) -> Result<ObjectType> {
        let mut query_type = ObjectType::new("Query".to_string())
            .with_description("The root query type for accessing RDF data".to_string());

        // Add root field for each class
        for (class_uri, rdf_class) in &vocabulary.classes {
            if self.config.exclude_classes.contains(class_uri) {
                continue;
            }

            let type_name = self.uri_to_graphql_name(&rdf_class.uri);
            let field_name = self.pluralize(&self.to_camel_case(&type_name));

            // Collection query
            query_type = query_type.with_field(
                field_name.clone(),
                FieldType::new(
                    field_name.clone(),
                    GraphQLType::List(Box::new(GraphQLType::Object(ObjectType::new(
                        type_name.clone(),
                    )))),
                )
                .with_description(format!("Query all instances of {type_name}"))
                .with_argument(
                    "where".to_string(),
                    ArgumentType::new(
                        "where".to_string(),
                        GraphQLType::Scalar(BuiltinScalars::string()),
                    )
                    .with_description("SPARQL filter condition".to_string()),
                )
                .with_argument(
                    "limit".to_string(),
                    ArgumentType::new(
                        "limit".to_string(),
                        GraphQLType::Scalar(BuiltinScalars::int()),
                    )
                    .with_default_value(crate::ast::Value::IntValue(10))
                    .with_description("Maximum number of results".to_string()),
                )
                .with_argument(
                    "offset".to_string(),
                    ArgumentType::new(
                        "offset".to_string(),
                        GraphQLType::Scalar(BuiltinScalars::int()),
                    )
                    .with_default_value(crate::ast::Value::IntValue(0))
                    .with_description("Number of results to skip".to_string()),
                ),
            );

            // Single item query
            let singular_field = self.to_camel_case(&type_name);
            query_type = query_type.with_field(
                singular_field.clone(),
                FieldType::new(
                    singular_field.clone(),
                    GraphQLType::Object(ObjectType::new(type_name.clone())),
                )
                .with_description(format!("Query a single {type_name} by ID"))
                .with_argument(
                    "id".to_string(),
                    ArgumentType::new(
                        "id".to_string(),
                        GraphQLType::NonNull(Box::new(GraphQLType::Scalar(BuiltinScalars::id()))),
                    )
                    .with_description("The ID of the resource".to_string()),
                ),
            );
        }

        // Add SPARQL query field
        query_type = query_type.with_field(
            "sparql".to_string(),
            FieldType::new(
                "sparql".to_string(),
                GraphQLType::Scalar(BuiltinScalars::string()),
            )
            .with_description("Execute a raw SPARQL query".to_string())
            .with_argument(
                "query".to_string(),
                ArgumentType::new(
                    "query".to_string(),
                    GraphQLType::NonNull(Box::new(GraphQLType::Scalar(BuiltinScalars::string()))),
                )
                .with_description("The SPARQL query to execute".to_string()),
            ),
        );

        Ok(query_type)
    }

    fn generate_mutation_type(&self, vocabulary: &RdfVocabulary) -> Result<ObjectType> {
        let mut mutation_type = ObjectType::new("Mutation".to_string())
            .with_description("The root mutation type for modifying RDF data".to_string());

        // Add CRUD operations for each class
        for (class_uri, rdf_class) in &vocabulary.classes {
            if self.config.exclude_classes.contains(class_uri) {
                continue;
            }

            let type_name = self.uri_to_graphql_name(&rdf_class.uri);
            let input_type_name = format!("{type_name}Input");
            let update_input_type_name = format!("{type_name}UpdateInput");

            // Create mutation for adding new instances
            mutation_type = mutation_type.with_field(
                format!("create{type_name}"),
                FieldType::new(
                    format!("create{type_name}"),
                    GraphQLType::Object(ObjectType::new(type_name.clone())),
                )
                .with_description(format!("Create a new {type_name}"))
                .with_argument(
                    "input".to_string(),
                    ArgumentType::new(
                        "input".to_string(),
                        GraphQLType::NonNull(Box::new(GraphQLType::InputObject(
                            InputObjectType::new(input_type_name.clone()),
                        ))),
                    )
                    .with_description(format!("Input data for creating a new {type_name}")),
                ),
            );

            // Update mutation
            mutation_type = mutation_type.with_field(
                format!("update{type_name}"),
                FieldType::new(
                    format!("update{type_name}"),
                    GraphQLType::Object(ObjectType::new(type_name.clone())),
                )
                .with_description(format!("Update an existing {type_name}"))
                .with_argument(
                    "id".to_string(),
                    ArgumentType::new(
                        "id".to_string(),
                        GraphQLType::NonNull(Box::new(GraphQLType::Scalar(BuiltinScalars::id()))),
                    )
                    .with_description("The ID of the resource to update".to_string()),
                )
                .with_argument(
                    "input".to_string(),
                    ArgumentType::new(
                        "input".to_string(),
                        GraphQLType::NonNull(Box::new(GraphQLType::InputObject(
                            InputObjectType::new(update_input_type_name),
                        ))),
                    )
                    .with_description(format!("Input data for updating the {type_name}")),
                ),
            );

            // Delete mutation
            mutation_type = mutation_type.with_field(
                format!("delete{type_name}"),
                FieldType::new(
                    format!("delete{type_name}"),
                    GraphQLType::Scalar(BuiltinScalars::boolean()),
                )
                .with_description(format!("Delete a {type_name}"))
                .with_argument(
                    "id".to_string(),
                    ArgumentType::new(
                        "id".to_string(),
                        GraphQLType::NonNull(Box::new(GraphQLType::Scalar(BuiltinScalars::id()))),
                    )
                    .with_description("The ID of the resource to delete".to_string()),
                ),
            );
        }

        // Add bulk operations
        mutation_type = mutation_type.with_field(
            "executeSparqlUpdate".to_string(),
            FieldType::new(
                "executeSparqlUpdate".to_string(),
                GraphQLType::Scalar(BuiltinScalars::boolean()),
            )
            .with_description("Execute a raw SPARQL UPDATE query".to_string())
            .with_argument(
                "update".to_string(),
                ArgumentType::new(
                    "update".to_string(),
                    GraphQLType::NonNull(Box::new(GraphQLType::Scalar(BuiltinScalars::string()))),
                )
                .with_description("The SPARQL UPDATE query to execute".to_string()),
            ),
        );

        // Add transaction support
        mutation_type = mutation_type.with_field(
            "executeTransaction".to_string(),
            FieldType::new(
                "executeTransaction".to_string(),
                GraphQLType::Scalar(BuiltinScalars::boolean()),
            )
            .with_description("Execute multiple SPARQL UPDATE queries in a transaction".to_string())
            .with_argument(
                "updates".to_string(),
                ArgumentType::new(
                    "updates".to_string(),
                    GraphQLType::NonNull(Box::new(GraphQLType::List(Box::new(
                        GraphQLType::Scalar(BuiltinScalars::string()),
                    )))),
                )
                .with_description("List of SPARQL UPDATE queries to execute".to_string()),
            ),
        );

        Ok(mutation_type)
    }

    fn generate_subscription_type(&self, vocabulary: &RdfVocabulary) -> Result<ObjectType> {
        let mut subscription_type = ObjectType::new("Subscription".to_string()).with_description(
            "The root subscription type for real-time RDF data updates".to_string(),
        );

        // Add change subscriptions for each class
        for (class_uri, rdf_class) in &vocabulary.classes {
            if self.config.exclude_classes.contains(class_uri) {
                continue;
            }

            let type_name = self.uri_to_graphql_name(&rdf_class.uri);
            let field_name = format!("{}Changed", self.to_camel_case(&type_name));

            // Resource change subscription
            subscription_type = subscription_type.with_field(
                field_name.clone(),
                FieldType::new(
                    field_name.clone(),
                    GraphQLType::Object(ObjectType::new(format!("{type_name}ChangeEvent"))),
                )
                .with_description(format!("Subscribe to changes for {type_name} instances"))
                .with_argument(
                    "id".to_string(),
                    ArgumentType::new("id".to_string(), GraphQLType::Scalar(BuiltinScalars::id()))
                        .with_description(
                            "Subscribe to changes for a specific resource ID".to_string(),
                        ),
                )
                .with_argument(
                    "changeType".to_string(),
                    ArgumentType::new(
                        "changeType".to_string(),
                        GraphQLType::Enum(EnumType::new("ChangeType".to_string())),
                    )
                    .with_description(
                        "Filter by change type (CREATED, UPDATED, DELETED)".to_string(),
                    ),
                ),
            );

            // Collection changes subscription
            let collection_field = format!("{}CollectionChanged", self.to_camel_case(&type_name));
            subscription_type = subscription_type.with_field(
                collection_field.clone(),
                FieldType::new(
                    collection_field.clone(),
                    GraphQLType::Object(ObjectType::new("CollectionChangeEvent".to_string())),
                )
                .with_description(format!("Subscribe to collection changes for {type_name}"))
                .with_argument(
                    "filter".to_string(),
                    ArgumentType::new(
                        "filter".to_string(),
                        GraphQLType::Scalar(BuiltinScalars::string()),
                    )
                    .with_description("SPARQL filter condition for subscription".to_string()),
                ),
            );
        }

        // Add property-specific change subscriptions
        for (property_uri, property) in &vocabulary.properties {
            if self.config.exclude_properties.contains(property_uri) {
                continue;
            }

            let property_name = self.uri_to_graphql_name(&property.uri);
            let field_name = format!("{}PropertyChanged", self.to_camel_case(&property_name));

            subscription_type = subscription_type.with_field(
                field_name.clone(),
                FieldType::new(
                    field_name.clone(),
                    GraphQLType::Object(ObjectType::new("PropertyChangeEvent".to_string())),
                )
                .with_description(format!(
                    "Subscribe to changes for the {property_name} property"
                ))
                .with_argument(
                    "subject".to_string(),
                    ArgumentType::new(
                        "subject".to_string(),
                        GraphQLType::Scalar(RdfScalars::iri()),
                    )
                    .with_description("The subject resource to monitor".to_string()),
                ),
            );
        }

        // Add query-based subscription
        subscription_type = subscription_type.with_field(
            "queryResultChanged".to_string(),
            FieldType::new(
                "queryResultChanged".to_string(),
                GraphQLType::Object(ObjectType::new("QueryResultChangeEvent".to_string())),
            )
            .with_description("Subscribe to changes in SPARQL query results".to_string())
            .with_argument(
                "query".to_string(),
                ArgumentType::new(
                    "query".to_string(),
                    GraphQLType::NonNull(Box::new(GraphQLType::Scalar(BuiltinScalars::string()))),
                )
                .with_description("The SPARQL query to monitor".to_string()),
            )
            .with_argument(
                "pollInterval".to_string(),
                ArgumentType::new(
                    "pollInterval".to_string(),
                    GraphQLType::Scalar(BuiltinScalars::int()),
                )
                .with_default_value(crate::ast::Value::IntValue(5000))
                .with_description("Polling interval in milliseconds".to_string()),
            ),
        );

        // Add graph-level subscription
        subscription_type = subscription_type.with_field(
            "graphChanged".to_string(),
            FieldType::new(
                "graphChanged".to_string(),
                GraphQLType::Object(ObjectType::new("GraphChangeEvent".to_string())),
            )
            .with_description("Subscribe to any changes in the RDF graph".to_string())
            .with_argument(
                "graph".to_string(),
                ArgumentType::new("graph".to_string(), GraphQLType::Scalar(RdfScalars::iri()))
                    .with_description(
                        "The named graph to monitor (default graph if not specified)".to_string(),
                    ),
            ),
        );

        // Add transaction subscription
        subscription_type = subscription_type.with_field(
            "transactionCompleted".to_string(),
            FieldType::new(
                "transactionCompleted".to_string(),
                GraphQLType::Object(ObjectType::new("TransactionEvent".to_string())),
            )
            .with_description("Subscribe to transaction completion events".to_string()),
        );

        Ok(subscription_type)
    }

    fn uri_to_graphql_name(&self, uri: &str) -> String {
        if let Some(fragment) = uri.split('#').next_back() {
            self.to_pascal_case(fragment)
        } else if let Some(segment) = uri.split('/').next_back() {
            self.to_pascal_case(segment)
        } else {
            "Resource".to_string()
        }
    }

    fn to_pascal_case(&self, input: &str) -> String {
        let mut result = String::new();
        let mut capitalize_next = true;

        for ch in input.chars() {
            if ch.is_alphanumeric() {
                if capitalize_next {
                    result.push(ch.to_uppercase().next().unwrap_or(ch));
                    capitalize_next = false;
                } else {
                    result.push(ch);
                }
            } else {
                capitalize_next = true;
            }
        }

        result
    }

    fn to_camel_case(&self, input: &str) -> String {
        let pascal = self.to_pascal_case(input);
        if let Some(first_char) = pascal.chars().next() {
            first_char.to_lowercase().collect::<String>() + &pascal[first_char.len_utf8()..]
        } else {
            pascal
        }
    }

    fn pluralize(&self, word: &str) -> String {
        if word.ends_with('s') || word.ends_with("sh") || word.ends_with("ch") {
            format!("{word}es")
        } else if let Some(stripped) = word.strip_suffix('y') {
            format!("{stripped}ies")
        } else {
            format!("{word}s")
        }
    }

    fn schema_to_sdl(&self, schema: &Schema) -> String {
        let mut sdl = String::new();

        // Write schema definition
        writeln!(sdl, "schema {{").expect("writing to String should not fail");
        if let Some(ref query) = schema.query_type {
            writeln!(sdl, "  query: {query}").expect("writing to String should not fail");
        }
        if let Some(ref mutation) = schema.mutation_type {
            writeln!(sdl, "  mutation: {mutation}").expect("writing to String should not fail");
        }
        if let Some(ref subscription) = schema.subscription_type {
            writeln!(sdl, "  subscription: {subscription}")
                .expect("writing to String should not fail");
        }
        writeln!(sdl, "}}").expect("writing to String should not fail");
        writeln!(sdl).expect("writing to String should not fail");

        // Write type definitions
        for graphql_type in schema.types.values() {
            match graphql_type {
                GraphQLType::Object(obj) => {
                    self.write_object_type_sdl(&mut sdl, obj);
                }
                GraphQLType::Scalar(scalar)
                    if !["String", "Int", "Float", "Boolean", "ID"]
                        .contains(&scalar.name.as_str()) =>
                {
                    self.write_scalar_type_sdl(&mut sdl, scalar);
                }
                GraphQLType::Enum(enum_type) => {
                    self.write_enum_type_sdl(&mut sdl, enum_type);
                }
                GraphQLType::Interface(interface) => {
                    self.write_interface_type_sdl(&mut sdl, interface);
                }
                GraphQLType::Union(union_type) => {
                    self.write_union_type_sdl(&mut sdl, union_type);
                }
                _ => {} // Skip other types
            }
        }

        sdl
    }

    fn write_object_type_sdl(&self, sdl: &mut String, obj: &ObjectType) {
        if let Some(ref description) = obj.description {
            writeln!(sdl, "\"\"\"\n{description}\n\"\"\"")
                .expect("writing to String should not fail");
        }

        write!(sdl, "type {}", obj.name).expect("writing to String should not fail");

        if !obj.interfaces.is_empty() {
            write!(sdl, " implements {}", obj.interfaces.join(" & "))
                .expect("writing to String should not fail");
        }

        writeln!(sdl, " {{").expect("writing to String should not fail");

        for field in obj.fields.values() {
            self.write_field_sdl(sdl, field);
        }

        writeln!(sdl, "}}").expect("writing to String should not fail");
        writeln!(sdl).expect("writing to String should not fail");
    }

    fn write_field_sdl(&self, sdl: &mut String, field: &FieldType) {
        if let Some(ref description) = field.description {
            writeln!(sdl, "  \"{description}\"").expect("writing to String should not fail");
        }

        write!(sdl, "  {}", field.name).expect("writing to String should not fail");

        if !field.arguments.is_empty() {
            write!(sdl, "(").expect("writing to String should not fail");
            let args: Vec<String> = field
                .arguments
                .values()
                .map(|arg| format!("{}: {}", arg.name, arg.argument_type))
                .collect();
            write!(sdl, "{}", args.join(", ")).expect("writing to String should not fail");
            write!(sdl, ")").expect("writing to String should not fail");
        }

        writeln!(sdl, ": {}", field.field_type).expect("writing to String should not fail");
    }

    fn write_scalar_type_sdl(&self, sdl: &mut String, scalar: &ScalarType) {
        if let Some(ref description) = scalar.description {
            writeln!(sdl, "\"\"\"\n{description}\n\"\"\"")
                .expect("writing to String should not fail");
        }
        writeln!(sdl, "scalar {}", scalar.name).expect("writing to String should not fail");
        writeln!(sdl).expect("writing to String should not fail");
    }

    fn write_enum_type_sdl(&self, sdl: &mut String, enum_type: &EnumType) {
        if let Some(ref description) = enum_type.description {
            writeln!(sdl, "\"\"\"\n{description}\n\"\"\"")
                .expect("writing to String should not fail");
        }
        writeln!(sdl, "enum {} {{", enum_type.name).expect("writing to String should not fail");

        for value in enum_type.values.values() {
            if let Some(ref description) = value.description {
                writeln!(sdl, "  \"{description}\"").expect("writing to String should not fail");
            }
            writeln!(sdl, "  {}", value.name).expect("writing to String should not fail");
        }

        writeln!(sdl, "}}").expect("writing to String should not fail");
        writeln!(sdl).expect("writing to String should not fail");
    }

    fn write_interface_type_sdl(&self, sdl: &mut String, interface: &InterfaceType) {
        if let Some(ref description) = interface.description {
            writeln!(sdl, "\"\"\"\n{description}\n\"\"\"")
                .expect("writing to String should not fail");
        }
        writeln!(sdl, "interface {} {{", interface.name)
            .expect("writing to String should not fail");

        for field in interface.fields.values() {
            self.write_field_sdl(sdl, field);
        }

        writeln!(sdl, "}}").expect("writing to String should not fail");
        writeln!(sdl).expect("writing to String should not fail");
    }

    fn write_union_type_sdl(&self, sdl: &mut String, union_type: &UnionType) {
        if let Some(ref description) = union_type.description {
            writeln!(sdl, "\"\"\"\n{description}\n\"\"\"")
                .expect("writing to String should not fail");
        }
        writeln!(
            sdl,
            "union {} = {}",
            union_type.name,
            union_type.types.join(" | ")
        )
        .expect("writing to String should not fail");
        writeln!(sdl).expect("writing to String should not fail");
    }

    /// Load and parse RDF ontology from URI
    async fn load_ontology_from_uri(&self, ontology_uri: &str) -> Result<RdfVocabulary> {
        // Create a temporary store to load the ontology
        let store = crate::RdfStore::new()?;

        // Determine format based on URI or default to RDF/XML
        let format = self.detect_rdf_format(ontology_uri);

        // Fetch ontology content from URI
        let content = self.fetch_ontology_content(ontology_uri).await?;

        // Parse the RDF content into the store using format-specific parsing
        let parser = RdfParser::new(format);

        // Insert parsed quads into the store
        for quad_result in parser.for_slice(&content) {
            match quad_result {
                Ok(quad) => {
                    store.insert(&quad)?;
                }
                Err(e) => {
                    return Err(anyhow::anyhow!(
                        "Failed to parse quad from {}: {}",
                        ontology_uri,
                        e
                    ));
                }
            }
        }

        // Extract vocabulary from the loaded ontology using existing method
        self.extract_vocabulary_from_store(&store)
    }

    /// Fetch ontology content from URI (HTTP/HTTPS or local file)
    async fn fetch_ontology_content(&self, uri: &str) -> Result<Vec<u8>> {
        if uri.starts_with("http://") || uri.starts_with("https://") {
            // Fetch from HTTP/HTTPS
            self.fetch_http_content(uri).await
        } else if uri.starts_with("file://") || !uri.contains("://") {
            // Load from local file
            let file_path = if let Some(stripped) = uri.strip_prefix("file://") {
                stripped // Remove "file://" prefix
            } else {
                uri
            };

            match std::fs::read(file_path) {
                Ok(content) => Ok(content),
                Err(e) => Err(anyhow::anyhow!(
                    "Failed to read local file {}: {}",
                    file_path,
                    e
                )),
            }
        } else {
            Err(anyhow::anyhow!("Unsupported URI scheme: {}", uri))
        }
    }

    /// Fetch content from HTTP/HTTPS URI
    async fn fetch_http_content(&self, uri: &str) -> Result<Vec<u8>> {
        use reqwest;

        let client = reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(30))
            .build()?;

        let response = client
            .get(uri)
            .header(
                "Accept",
                "application/rdf+xml, text/turtle, application/n-triples, application/ld+json",
            )
            .send()
            .await?;

        if !response.status().is_success() {
            return Err(anyhow::anyhow!(
                "HTTP error {}: Failed to fetch ontology from {}",
                response.status(),
                uri
            ));
        }

        let content = response.bytes().await?;
        Ok(content.to_vec())
    }

    /// Detect RDF format based on URI or content-type
    fn detect_rdf_format(&self, uri: &str) -> RdfFormat {
        use oxirs_core::format::JsonLdProfileSet;

        let uri_lower = uri.to_lowercase();

        if uri_lower.ends_with(".ttl") || uri_lower.ends_with(".turtle") {
            RdfFormat::Turtle
        } else if uri_lower.ends_with(".nt") || uri_lower.ends_with(".ntriples") {
            RdfFormat::NTriples
        } else if uri_lower.ends_with(".jsonld") || uri_lower.ends_with(".json-ld") {
            RdfFormat::JsonLd {
                profile: JsonLdProfileSet::empty(),
            }
        } else if uri_lower.ends_with(".n3") {
            RdfFormat::N3
        } else {
            // Default to RDF/XML for .rdf, .owl, or unknown extensions
            RdfFormat::RdfXml
        }
    }

    #[allow(dead_code)]
    fn load_mock_vocabulary(&self, _ontology_uri: &str) -> Result<RdfVocabulary> {
        // Enhanced mock vocabulary for demonstration
        let mut classes = HashMap::new();
        let mut properties = HashMap::new();
        let mut namespaces = HashMap::new();

        // Common namespaces
        namespaces.insert("foaf".to_string(), "http://xmlns.com/foaf/0.1/".to_string());
        namespaces.insert("schema".to_string(), "http://schema.org/".to_string());
        namespaces.insert(
            "dbo".to_string(),
            "http://dbpedia.org/ontology/".to_string(),
        );
        namespaces.insert(
            "dc".to_string(),
            "http://purl.org/dc/elements/1.1/".to_string(),
        );

        // FOAF Agent (base class)
        classes.insert(
            "http://xmlns.com/foaf/0.1/Agent".to_string(),
            RdfClass {
                uri: "http://xmlns.com/foaf/0.1/Agent".to_string(),
                label: Some("Agent".to_string()),
                comment: Some(
                    "An agent (eg. person, group, software or physical artifact)".to_string(),
                ),
                super_classes: vec![],
                properties: vec!["http://xmlns.com/foaf/0.1/name".to_string()],
            },
        );

        // FOAF Person class
        classes.insert(
            "http://xmlns.com/foaf/0.1/Person".to_string(),
            RdfClass {
                uri: "http://xmlns.com/foaf/0.1/Person".to_string(),
                label: Some("Person".to_string()),
                comment: Some("A person".to_string()),
                super_classes: vec!["http://xmlns.com/foaf/0.1/Agent".to_string()],
                properties: vec![
                    "http://xmlns.com/foaf/0.1/name".to_string(),
                    "http://xmlns.com/foaf/0.1/email".to_string(),
                    "http://xmlns.com/foaf/0.1/knows".to_string(),
                    "http://xmlns.com/foaf/0.1/age".to_string(),
                    "http://xmlns.com/foaf/0.1/homepage".to_string(),
                ],
            },
        );

        // FOAF Organization class
        classes.insert(
            "http://xmlns.com/foaf/0.1/Organization".to_string(),
            RdfClass {
                uri: "http://xmlns.com/foaf/0.1/Organization".to_string(),
                label: Some("Organization".to_string()),
                comment: Some("An organization".to_string()),
                super_classes: vec!["http://xmlns.com/foaf/0.1/Agent".to_string()],
                properties: vec![
                    "http://xmlns.com/foaf/0.1/name".to_string(),
                    "http://xmlns.com/foaf/0.1/homepage".to_string(),
                ],
            },
        );

        // Schema.org Product class
        classes.insert(
            "http://schema.org/Product".to_string(),
            RdfClass {
                uri: "http://schema.org/Product".to_string(),
                label: Some("Product".to_string()),
                comment: Some("Any offered product or service".to_string()),
                super_classes: vec![],
                properties: vec![
                    "http://schema.org/name".to_string(),
                    "http://schema.org/description".to_string(),
                    "http://schema.org/price".to_string(),
                    "http://schema.org/manufacturer".to_string(),
                ],
            },
        );

        // Properties
        let property_definitions = vec![
            (
                "http://xmlns.com/foaf/0.1/name",
                "name",
                "A name for some thing",
                PropertyType::DataProperty,
                vec!["http://xmlns.com/foaf/0.1/Agent"],
                vec!["http://www.w3.org/2001/XMLSchema#string"],
            ),
            (
                "http://xmlns.com/foaf/0.1/email",
                "email",
                "An email address",
                PropertyType::DataProperty,
                vec!["http://xmlns.com/foaf/0.1/Person"],
                vec!["http://www.w3.org/2001/XMLSchema#string"],
            ),
            (
                "http://xmlns.com/foaf/0.1/age",
                "age",
                "The age in years of some agent",
                PropertyType::DataProperty,
                vec!["http://xmlns.com/foaf/0.1/Person"],
                vec!["http://www.w3.org/2001/XMLSchema#int"],
            ),
            (
                "http://xmlns.com/foaf/0.1/homepage",
                "homepage",
                "A homepage for some thing",
                PropertyType::DataProperty,
                vec!["http://xmlns.com/foaf/0.1/Agent"],
                vec!["http://www.w3.org/2001/XMLSchema#anyURI"],
            ),
            (
                "http://xmlns.com/foaf/0.1/knows",
                "knows",
                "A person known by this person",
                PropertyType::ObjectProperty,
                vec!["http://xmlns.com/foaf/0.1/Person"],
                vec!["http://xmlns.com/foaf/0.1/Person"],
            ),
            (
                "http://schema.org/name",
                "name",
                "The name of the item",
                PropertyType::DataProperty,
                vec!["http://schema.org/Product"],
                vec!["http://www.w3.org/2001/XMLSchema#string"],
            ),
            (
                "http://schema.org/description",
                "description",
                "A description of the item",
                PropertyType::DataProperty,
                vec!["http://schema.org/Product"],
                vec!["http://www.w3.org/2001/XMLSchema#string"],
            ),
            (
                "http://schema.org/price",
                "price",
                "The price of the product",
                PropertyType::DataProperty,
                vec!["http://schema.org/Product"],
                vec!["http://www.w3.org/2001/XMLSchema#decimal"],
            ),
            (
                "http://schema.org/manufacturer",
                "manufacturer",
                "The manufacturer of the product",
                PropertyType::ObjectProperty,
                vec!["http://schema.org/Product"],
                vec!["http://xmlns.com/foaf/0.1/Organization"],
            ),
        ];

        for (uri, label, comment, prop_type, domain, range) in property_definitions {
            properties.insert(
                uri.to_string(),
                RdfProperty {
                    uri: uri.to_string(),
                    label: Some(label.to_string()),
                    comment: Some(comment.to_string()),
                    domain: domain.into_iter().map(|s| s.to_string()).collect(),
                    range: range.into_iter().map(|s| s.to_string()).collect(),
                    property_type: prop_type,
                    functional: matches!(label, "email" | "age" | "homepage"),
                    inverse_functional: label == "email",
                },
            );
        }

        Ok(RdfVocabulary {
            classes,
            properties,
            namespaces,
        })
    }
}

impl Default for SchemaGenerator {
    fn default() -> Self {
        Self::new()
    }
}