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
//! Tests for RDFS inference engine

#[cfg(test)]
mod tests {
    use crate::schema::{inference::RdfsInferenceEngine, SchemaAnalyzer};
    use oxrdf::{NamedNode, Triple};

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

            ex:Mammal rdfs:subClassOf ex:Animal .
            ex:Dog rdfs:subClassOf ex:Mammal .
            ex:Poodle rdfs:subClassOf ex:Dog .

            ex:fluffy rdf:type ex:Poodle .
        "#;

        let mut analyzer = SchemaAnalyzer::new();
        analyzer.load_turtle(rdfs_turtle).expect("unwrap");

        let mut engine = RdfsInferenceEngine::new(analyzer.graph.clone());
        engine.materialize().expect("unwrap");

        let complete_graph = engine.get_complete_graph();

        // Check that fluffy is inferred to be an Animal (transitive closure)
        let fluffy = NamedNode::new("http://example.org/fluffy").expect("unwrap");
        let rdf_type =
            NamedNode::new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type").expect("unwrap");
        let animal = NamedNode::new("http://example.org/Animal").expect("unwrap");

        let expected = Triple::new(fluffy.clone(), rdf_type.clone(), animal.clone());
        assert!(
            complete_graph.contains(&expected),
            "Expected fluffy to be inferred as an Animal"
        );

        // Check that fluffy is also a Mammal
        let mammal = NamedNode::new("http://example.org/Mammal").expect("unwrap");
        let mammal_triple = Triple::new(fluffy.clone(), rdf_type.clone(), mammal.clone());
        assert!(
            complete_graph.contains(&mammal_triple),
            "Expected fluffy to be inferred as a Mammal"
        );

        // Check that fluffy is also a Dog
        let dog = NamedNode::new("http://example.org/Dog").expect("unwrap");
        let dog_triple = Triple::new(fluffy.clone(), rdf_type.clone(), dog.clone());
        assert!(
            complete_graph.contains(&dog_triple),
            "Expected fluffy to be inferred as a Dog"
        );
    }

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

            ex:hasFather rdfs:domain ex:Person .

            ex:john ex:hasFather ex:bob .
        "#;

        let mut analyzer = SchemaAnalyzer::new();
        analyzer.load_turtle(rdfs_turtle).expect("unwrap");

        let mut engine = RdfsInferenceEngine::new(analyzer.graph.clone());
        engine.materialize().expect("unwrap");

        let complete_graph = engine.get_complete_graph();

        // Check that john is inferred to be a Person
        let john = NamedNode::new("http://example.org/john").expect("unwrap");
        let rdf_type =
            NamedNode::new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type").expect("unwrap");
        let person = NamedNode::new("http://example.org/Person").expect("unwrap");

        let expected = Triple::new(john.clone(), rdf_type.clone(), person.clone());
        assert!(
            complete_graph.contains(&expected),
            "Expected john to be inferred as a Person via domain"
        );
    }

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

            ex:hasFather rdfs:range ex:Person .

            ex:john ex:hasFather ex:bob .
        "#;

        let mut analyzer = SchemaAnalyzer::new();
        analyzer.load_turtle(rdfs_turtle).expect("unwrap");

        let mut engine = RdfsInferenceEngine::new(analyzer.graph.clone());
        engine.materialize().expect("unwrap");

        let complete_graph = engine.get_complete_graph();

        // Check that bob is inferred to be a Person
        let bob = NamedNode::new("http://example.org/bob").expect("unwrap");
        let rdf_type =
            NamedNode::new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type").expect("unwrap");
        let person = NamedNode::new("http://example.org/Person").expect("unwrap");

        let expected = Triple::new(bob.clone(), rdf_type.clone(), person.clone());
        assert!(
            complete_graph.contains(&expected),
            "Expected bob to be inferred as a Person via range"
        );
    }

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

            ex:hasFather rdfs:subPropertyOf ex:hasParent .
            ex:hasParent rdfs:subPropertyOf ex:hasAncestor .

            ex:john ex:hasFather ex:bob .
        "#;

        let mut analyzer = SchemaAnalyzer::new();
        analyzer.load_turtle(rdfs_turtle).expect("unwrap");

        let mut engine = RdfsInferenceEngine::new(analyzer.graph.clone());
        engine.materialize().expect("unwrap");

        let complete_graph = engine.get_complete_graph();

        // Check that john hasParent bob is inferred
        let john = NamedNode::new("http://example.org/john").expect("unwrap");
        let has_parent = NamedNode::new("http://example.org/hasParent").expect("unwrap");
        let bob = NamedNode::new("http://example.org/bob").expect("unwrap");

        let parent_triple = Triple::new(john.clone(), has_parent.clone(), bob.clone());
        assert!(
            complete_graph.contains(&parent_triple),
            "Expected john hasParent bob to be inferred"
        );

        // Check that john hasAncestor bob is inferred (transitive)
        let has_ancestor = NamedNode::new("http://example.org/hasAncestor").expect("unwrap");
        let ancestor_triple = Triple::new(john.clone(), has_ancestor.clone(), bob.clone());
        assert!(
            complete_graph.contains(&ancestor_triple),
            "Expected john hasAncestor bob to be inferred transitively"
        );
    }

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

            ex:Employee rdfs:subClassOf ex:Person .
            ex:Person rdfs:subClassOf ex:LegalEntity .

            ex:worksFor rdfs:domain ex:Employee .
            ex:worksFor rdfs:range ex:Organization .

            ex:john ex:worksFor ex:acme .
        "#;

        let mut analyzer = SchemaAnalyzer::new();
        analyzer.load_turtle(rdfs_turtle).expect("unwrap");

        let mut engine = RdfsInferenceEngine::new(analyzer.graph.clone());
        engine.materialize().expect("unwrap");

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

        // Check domain inference: john is an Employee
        let john = NamedNode::new("http://example.org/john").expect("unwrap");
        let employee = NamedNode::new("http://example.org/Employee").expect("unwrap");
        let employee_triple = Triple::new(john.clone(), rdf_type.clone(), employee.clone());
        assert!(
            complete_graph.contains(&employee_triple),
            "Expected john to be inferred as an Employee"
        );

        // Check subclass inference: john is a Person
        let person = NamedNode::new("http://example.org/Person").expect("unwrap");
        let person_triple = Triple::new(john.clone(), rdf_type.clone(), person.clone());
        assert!(
            complete_graph.contains(&person_triple),
            "Expected john to be inferred as a Person"
        );

        // Check transitive subclass: john is a LegalEntity
        let legal_entity = NamedNode::new("http://example.org/LegalEntity").expect("unwrap");
        let entity_triple = Triple::new(john.clone(), rdf_type.clone(), legal_entity.clone());
        assert!(
            complete_graph.contains(&entity_triple),
            "Expected john to be inferred as a LegalEntity"
        );

        // Check range inference: acme is an Organization
        let acme = NamedNode::new("http://example.org/acme").expect("unwrap");
        let organization = NamedNode::new("http://example.org/Organization").expect("unwrap");
        let org_triple = Triple::new(acme.clone(), rdf_type.clone(), organization.clone());
        assert!(
            complete_graph.contains(&org_triple),
            "Expected acme to be inferred as an Organization"
        );
    }

    #[test]
    fn test_is_subclass_of() {
        let rdfs_turtle = r#"
            @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
            @prefix ex: <http://example.org/> .

            ex:Poodle rdfs:subClassOf ex:Dog .
            ex:Dog rdfs:subClassOf ex:Mammal .
            ex:Mammal rdfs:subClassOf ex:Animal .
        "#;

        let mut analyzer = SchemaAnalyzer::new();
        analyzer.load_turtle(rdfs_turtle).expect("unwrap");

        let mut engine = RdfsInferenceEngine::new(analyzer.graph.clone());
        engine.materialize().expect("unwrap");

        // Direct subclass
        assert!(engine.is_subclass_of("http://example.org/Poodle", "http://example.org/Dog"));

        // Transitive subclass
        assert!(engine.is_subclass_of("http://example.org/Poodle", "http://example.org/Mammal"));
        assert!(engine.is_subclass_of("http://example.org/Poodle", "http://example.org/Animal"));

        // Not a subclass
        assert!(!engine.is_subclass_of("http://example.org/Animal", "http://example.org/Poodle"));
    }

    #[test]
    fn test_is_subproperty_of() {
        let rdfs_turtle = r#"
            @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
            @prefix ex: <http://example.org/> .

            ex:hasMother rdfs:subPropertyOf ex:hasParent .
            ex:hasParent rdfs:subPropertyOf ex:hasAncestor .
            ex:hasAncestor rdfs:subPropertyOf ex:hasRelative .
        "#;

        let mut analyzer = SchemaAnalyzer::new();
        analyzer.load_turtle(rdfs_turtle).expect("unwrap");

        let mut engine = RdfsInferenceEngine::new(analyzer.graph.clone());
        engine.materialize().expect("unwrap");

        // Direct subproperty
        assert!(engine.is_subproperty_of(
            "http://example.org/hasMother",
            "http://example.org/hasParent"
        ));

        // Transitive subproperty
        assert!(engine.is_subproperty_of(
            "http://example.org/hasMother",
            "http://example.org/hasAncestor"
        ));
        assert!(engine.is_subproperty_of(
            "http://example.org/hasMother",
            "http://example.org/hasRelative"
        ));

        // Not a subproperty
        assert!(!engine.is_subproperty_of(
            "http://example.org/hasRelative",
            "http://example.org/hasMother"
        ));
    }

    #[test]
    fn test_get_all_superclasses() {
        let rdfs_turtle = r#"
            @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
            @prefix ex: <http://example.org/> .

            ex:Poodle rdfs:subClassOf ex:Dog .
            ex:Dog rdfs:subClassOf ex:Mammal .
            ex:Mammal rdfs:subClassOf ex:Animal .
        "#;

        let mut analyzer = SchemaAnalyzer::new();
        analyzer.load_turtle(rdfs_turtle).expect("unwrap");

        let mut engine = RdfsInferenceEngine::new(analyzer.graph.clone());
        engine.materialize().expect("unwrap");

        let superclasses = engine.get_all_superclasses("http://example.org/Poodle");

        assert!(superclasses.contains("http://example.org/Dog"));
        assert!(superclasses.contains("http://example.org/Mammal"));
        assert!(superclasses.contains("http://example.org/Animal"));
        assert_eq!(superclasses.len(), 3);
    }

    #[test]
    fn test_get_all_superproperties() {
        let rdfs_turtle = r#"
            @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
            @prefix ex: <http://example.org/> .

            ex:hasMother rdfs:subPropertyOf ex:hasParent .
            ex:hasParent rdfs:subPropertyOf ex:hasAncestor .
        "#;

        let mut analyzer = SchemaAnalyzer::new();
        analyzer.load_turtle(rdfs_turtle).expect("unwrap");

        let mut engine = RdfsInferenceEngine::new(analyzer.graph.clone());
        engine.materialize().expect("unwrap");

        let superprops = engine.get_all_superproperties("http://example.org/hasMother");

        assert!(superprops.contains("http://example.org/hasParent"));
        assert!(superprops.contains("http://example.org/hasAncestor"));
        assert_eq!(superprops.len(), 2);
    }

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

            ex:Dog rdfs:subClassOf ex:Mammal .
            ex:Mammal rdfs:subClassOf ex:Animal .

            ex:hasParent rdfs:subPropertyOf ex:hasAncestor .

            ex:hasFather rdfs:domain ex:Person .
            ex:john ex:hasFather ex:bob .
        "#;

        let mut analyzer = SchemaAnalyzer::new();
        analyzer.load_turtle(rdfs_turtle).expect("unwrap");

        let original_count = analyzer.graph.len();

        let mut engine = RdfsInferenceEngine::new(analyzer.graph.clone());
        engine.materialize().expect("unwrap");

        let stats = engine.get_inference_stats();

        assert_eq!(stats.original_triples, original_count);
        assert!(stats.inferred_triples > 0, "Expected some inferred triples");
        assert_eq!(
            stats.total_triples,
            stats.original_triples + stats.inferred_triples
        );
        assert!(stats.subclass_relations > 0);
        assert!(stats.subproperty_relations > 0);
    }

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

            ex:FlyingMammal rdfs:subClassOf ex:Mammal .
            ex:FlyingMammal rdfs:subClassOf ex:FlyingAnimal .
            ex:Mammal rdfs:subClassOf ex:Animal .
            ex:FlyingAnimal rdfs:subClassOf ex:Animal .
            ex:Bat rdfs:subClassOf ex:FlyingMammal .

            ex:stella rdf:type ex:Bat .
        "#;

        let mut analyzer = SchemaAnalyzer::new();
        analyzer.load_turtle(rdfs_turtle).expect("unwrap");

        let mut engine = RdfsInferenceEngine::new(analyzer.graph.clone());
        engine.materialize().expect("unwrap");

        let complete_graph = engine.get_complete_graph();
        let rdf_type =
            NamedNode::new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type").expect("unwrap");
        let stella = NamedNode::new("http://example.org/stella").expect("unwrap");

        // stella should be inferred to be all parent classes
        let types_to_check = vec!["FlyingMammal", "Mammal", "FlyingAnimal", "Animal"];

        for type_name in types_to_check {
            let type_node =
                NamedNode::new(format!("http://example.org/{}", type_name)).expect("unwrap");
            let type_triple = Triple::new(stella.clone(), rdf_type.clone(), type_node.clone());
            assert!(
                complete_graph.contains(&type_triple),
                "Expected stella to be inferred as a {}",
                type_name
            );
        }
    }

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

            ex:A rdfs:subClassOf ex:B .
            ex:B rdfs:subClassOf ex:A .

            ex:entity rdf:type ex:A .
        "#;

        let mut analyzer = SchemaAnalyzer::new();
        analyzer.load_turtle(rdfs_turtle).expect("unwrap");

        let mut engine = RdfsInferenceEngine::new(analyzer.graph.clone());

        // Should not hang or panic
        let result = engine.materialize();
        assert!(
            result.is_ok(),
            "Inference should handle circular hierarchies"
        );

        let stats = engine.get_inference_stats();
        assert!(
            stats.total_triples < 1000,
            "Should not create excessive triples"
        );
    }

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

            ex:Employee rdfs:subClassOf ex:Person .
            ex:worksFor rdfs:domain ex:Employee .

            ex:john ex:worksFor ex:acme .
        "#;

        let mut analyzer = SchemaAnalyzer::new();
        analyzer.load_turtle(rdfs_turtle).expect("unwrap");

        let materialized = analyzer.materialize_rdfs_entailments().expect("unwrap");

        // Check that john is inferred to be a Person
        let john = NamedNode::new("http://example.org/john").expect("unwrap");
        let rdf_type =
            NamedNode::new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type").expect("unwrap");
        let person = NamedNode::new("http://example.org/Person").expect("unwrap");

        let person_triple = Triple::new(john.clone(), rdf_type.clone(), person.clone());
        assert!(
            materialized.contains(&person_triple),
            "Expected john to be inferred as a Person via SchemaAnalyzer integration"
        );
    }
}