oxirs-wasm 0.2.3

WebAssembly bindings for OxiRS - Run RDF/SPARQL in the browser
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
//! RDFS inference for OxiRS WASM
//!
//! Implements the core RDFS entailment rules:
//! - `rdfs:subClassOf` transitivity
//! - `rdfs:subPropertyOf` transitivity
//! - `rdf:type` propagation through `rdfs:subClassOf`
//! - `rdfs:domain` inference: if `?p rdfs:domain ?C` and `?s ?p ?o`, then `?s rdf:type ?C`
//! - `rdfs:range` inference: if `?p rdfs:range ?C` and `?s ?p ?o`, then `?o rdf:type ?C`

use crate::store::OxiRSStore;
use std::collections::HashSet;

// -----------------------------------------------------------------------
// RDF/RDFS vocabulary constants
// -----------------------------------------------------------------------

const RDF_TYPE: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
const RDFS_SUB_CLASS_OF: &str = "http://www.w3.org/2000/01/rdf-schema#subClassOf";
const RDFS_SUB_PROPERTY_OF: &str = "http://www.w3.org/2000/01/rdf-schema#subPropertyOf";
const RDFS_DOMAIN: &str = "http://www.w3.org/2000/01/rdf-schema#domain";
const RDFS_RANGE: &str = "http://www.w3.org/2000/01/rdf-schema#range";

// -----------------------------------------------------------------------
// Configuration
// -----------------------------------------------------------------------

/// Configuration for the RDFS reasoner
#[derive(Debug, Clone)]
pub struct InferenceConfig {
    /// Maximum depth for transitivity closure computations.
    /// A value of 0 means unlimited depth.
    pub max_depth: usize,
    /// Whether to use forward chaining only (the only strategy currently implemented)
    pub forward_chaining_only: bool,
    /// Whether to materialise ALL inferred triples into the store immediately,
    /// or only return a count of new triples without inserting them.
    pub materialize_all: bool,
}

impl Default for InferenceConfig {
    fn default() -> Self {
        Self {
            max_depth: 0,
            forward_chaining_only: true,
            materialize_all: true,
        }
    }
}

impl InferenceConfig {
    /// Create a configuration with unlimited depth and full materialisation
    pub fn full() -> Self {
        Self::default()
    }

    /// Create a configuration limited to `depth` reasoning steps
    pub fn with_max_depth(depth: usize) -> Self {
        Self {
            max_depth: depth,
            ..Self::default()
        }
    }
}

// -----------------------------------------------------------------------
// RdfsReasoner
// -----------------------------------------------------------------------

/// Applies RDFS entailment rules to an [`OxiRSStore`], materialising inferred triples.
pub struct RdfsReasoner {
    config: InferenceConfig,
}

impl RdfsReasoner {
    /// Create a new reasoner with the given configuration
    pub fn new(config: InferenceConfig) -> Self {
        Self { config }
    }

    /// Create a reasoner with default (full) configuration
    pub fn default_config() -> Self {
        Self::new(InferenceConfig::default())
    }

    /// Apply all RDFS rules until a fixed point is reached.
    ///
    /// Returns the total number of new triples inserted.
    pub fn apply(&self, store: &mut OxiRSStore) -> u32 {
        let mut total_new = 0u32;
        let max_iterations = if self.config.max_depth == 0 {
            usize::MAX
        } else {
            self.config.max_depth
        };

        for _ in 0..max_iterations {
            let new = self.apply_one_round(store);
            if new == 0 {
                break;
            }
            total_new += new;
        }

        total_new
    }

    /// Run a single round of RDFS rule applications.
    /// Returns the number of new triples added in this round.
    fn apply_one_round(&self, store: &mut OxiRSStore) -> u32 {
        let mut inferred: Vec<(String, String, String)> = Vec::new();

        // ------------------------------------------------------------------
        // Rule 1: rdfs:subClassOf transitivity
        //   IF (?A rdfs:subClassOf ?B) AND (?B rdfs:subClassOf ?C)
        //   THEN (?A rdfs:subClassOf ?C)
        // ------------------------------------------------------------------
        {
            let sub_class_pairs = collect_pairs(store, RDFS_SUB_CLASS_OF);
            for (a, b) in &sub_class_pairs {
                for (b2, c) in &sub_class_pairs {
                    if b == b2 && a != c {
                        inferred.push((a.clone(), RDFS_SUB_CLASS_OF.to_string(), c.clone()));
                    }
                }
            }
        }

        // ------------------------------------------------------------------
        // Rule 2: rdfs:subPropertyOf transitivity
        //   IF (?p rdfs:subPropertyOf ?q) AND (?q rdfs:subPropertyOf ?r)
        //   THEN (?p rdfs:subPropertyOf ?r)
        // ------------------------------------------------------------------
        {
            let sub_prop_pairs = collect_pairs(store, RDFS_SUB_PROPERTY_OF);
            for (p, q) in &sub_prop_pairs {
                for (q2, r) in &sub_prop_pairs {
                    if q == q2 && p != r {
                        inferred.push((p.clone(), RDFS_SUB_PROPERTY_OF.to_string(), r.clone()));
                    }
                }
            }
        }

        // ------------------------------------------------------------------
        // Rule 3: rdf:type propagation through rdfs:subClassOf
        //   IF (?x rdf:type ?C) AND (?C rdfs:subClassOf ?D)
        //   THEN (?x rdf:type ?D)
        // ------------------------------------------------------------------
        {
            let sub_class_pairs = collect_pairs(store, RDFS_SUB_CLASS_OF);
            let type_pairs = collect_pairs(store, RDF_TYPE);
            for (x, c) in &type_pairs {
                for (c2, d) in &sub_class_pairs {
                    if c == c2 {
                        inferred.push((x.clone(), RDF_TYPE.to_string(), d.clone()));
                    }
                }
            }
        }

        // ------------------------------------------------------------------
        // Rule 4: rdfs:subPropertyOf propagation
        //   IF (?p rdfs:subPropertyOf ?q) AND (?s ?p ?o)
        //   THEN (?s ?q ?o)
        // ------------------------------------------------------------------
        {
            let sub_prop_pairs = collect_pairs(store, RDFS_SUB_PROPERTY_OF);
            // Snapshot current triples to avoid modifying while iterating
            let all: Vec<(String, String, String)> = store
                .all_triples()
                .map(|t| (t.subject.clone(), t.predicate.clone(), t.object.clone()))
                .collect();
            for (p, q) in &sub_prop_pairs {
                for (s, pred, o) in &all {
                    if pred == p {
                        inferred.push((s.clone(), q.clone(), o.clone()));
                    }
                }
            }
        }

        // ------------------------------------------------------------------
        // Rule 5: rdfs:domain inference
        //   IF (?p rdfs:domain ?C) AND (?s ?p ?o)
        //   THEN (?s rdf:type ?C)
        // ------------------------------------------------------------------
        {
            let domain_pairs = collect_pairs(store, RDFS_DOMAIN);
            let all: Vec<(String, String, String)> = store
                .all_triples()
                .map(|t| (t.subject.clone(), t.predicate.clone(), t.object.clone()))
                .collect();
            for (p, c) in &domain_pairs {
                for (s, pred, _o) in &all {
                    if pred == p {
                        inferred.push((s.clone(), RDF_TYPE.to_string(), c.clone()));
                    }
                }
            }
        }

        // ------------------------------------------------------------------
        // Rule 6: rdfs:range inference
        //   IF (?p rdfs:range ?C) AND (?s ?p ?o)
        //   THEN (?o rdf:type ?C)
        // ------------------------------------------------------------------
        {
            let range_pairs = collect_pairs(store, RDFS_RANGE);
            let all: Vec<(String, String, String)> = store
                .all_triples()
                .map(|t| (t.subject.clone(), t.predicate.clone(), t.object.clone()))
                .collect();
            for (p, c) in &range_pairs {
                for (_s, pred, o) in &all {
                    if pred == p {
                        inferred.push((_s.clone(), RDF_TYPE.to_string(), c.clone()));
                        // rdfs:range says the *object* has the type
                        inferred.push((o.clone(), RDF_TYPE.to_string(), c.clone()));
                    }
                }
            }
        }

        // ------------------------------------------------------------------
        // Insert new triples (only novel ones)
        // ------------------------------------------------------------------
        if !self.config.materialize_all {
            // Just count what would be new
            let new_count = inferred
                .iter()
                .filter(|(s, p, o)| !store.contains(s, p, o))
                .count() as u32;
            return new_count;
        }

        let mut inserted = 0u32;
        // Deduplicate within the inferred set before inserting
        let unique: HashSet<_> = inferred.into_iter().collect();
        for (s, p, o) in unique {
            if store.insert(&s, &p, &o) {
                inserted += 1;
            }
        }

        inserted
    }
}

// -----------------------------------------------------------------------
// Top-level convenience function
// -----------------------------------------------------------------------

/// Apply RDFS inference to the store with default configuration.
///
/// Returns the total number of new triples added.
pub fn apply_rdfs_inference(store: &mut OxiRSStore) -> u32 {
    RdfsReasoner::default_config().apply(store)
}

// -----------------------------------------------------------------------
// Internal helpers
// -----------------------------------------------------------------------

/// Collect all (subject, object) pairs from triples with the given predicate
fn collect_pairs(store: &OxiRSStore, predicate: &str) -> Vec<(String, String)> {
    store
        .all_triples()
        .filter(|t| t.predicate == predicate)
        .map(|t| (t.subject.clone(), t.object.clone()))
        .collect()
}

// -----------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------

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

    fn make_store() -> OxiRSStore {
        OxiRSStore::new()
    }

    // ---- subClassOf transitivity ----

    #[test]
    fn test_sub_class_of_transitivity() {
        let mut store = make_store();
        // Dog rdfs:subClassOf Animal
        // Animal rdfs:subClassOf LivingThing
        store.insert("http://Dog", RDFS_SUB_CLASS_OF, "http://Animal");
        store.insert("http://Animal", RDFS_SUB_CLASS_OF, "http://LivingThing");

        let added = apply_rdfs_inference(&mut store);
        assert!(added > 0);
        // Dog should be inferred as subClassOf LivingThing
        assert!(store.contains("http://Dog", RDFS_SUB_CLASS_OF, "http://LivingThing"));
    }

    #[test]
    fn test_sub_class_of_chain_of_three() {
        let mut store = make_store();
        store.insert("http://Poodle", RDFS_SUB_CLASS_OF, "http://Dog");
        store.insert("http://Dog", RDFS_SUB_CLASS_OF, "http://Animal");
        store.insert("http://Animal", RDFS_SUB_CLASS_OF, "http://LivingThing");

        apply_rdfs_inference(&mut store);
        assert!(store.contains("http://Poodle", RDFS_SUB_CLASS_OF, "http://Animal"));
        assert!(store.contains("http://Poodle", RDFS_SUB_CLASS_OF, "http://LivingThing"));
        assert!(store.contains("http://Dog", RDFS_SUB_CLASS_OF, "http://LivingThing"));
    }

    // ---- subPropertyOf transitivity ----

    #[test]
    fn test_sub_property_of_transitivity() {
        let mut store = make_store();
        store.insert("http://hasMother", RDFS_SUB_PROPERTY_OF, "http://hasParent");
        store.insert(
            "http://hasParent",
            RDFS_SUB_PROPERTY_OF,
            "http://hasAncestor",
        );

        apply_rdfs_inference(&mut store);
        assert!(store.contains(
            "http://hasMother",
            RDFS_SUB_PROPERTY_OF,
            "http://hasAncestor"
        ));
    }

    // ---- rdf:type propagation ----

    #[test]
    fn test_type_propagation_through_sub_class() {
        let mut store = make_store();
        store.insert("http://fido", RDF_TYPE, "http://Dog");
        store.insert("http://Dog", RDFS_SUB_CLASS_OF, "http://Animal");

        apply_rdfs_inference(&mut store);
        assert!(store.contains("http://fido", RDF_TYPE, "http://Animal"));
    }

    #[test]
    fn test_type_propagation_chain() {
        let mut store = make_store();
        store.insert("http://fido", RDF_TYPE, "http://Dog");
        store.insert("http://Dog", RDFS_SUB_CLASS_OF, "http://Animal");
        store.insert("http://Animal", RDFS_SUB_CLASS_OF, "http://LivingThing");

        apply_rdfs_inference(&mut store);
        assert!(store.contains("http://fido", RDF_TYPE, "http://Animal"));
        assert!(store.contains("http://fido", RDF_TYPE, "http://LivingThing"));
    }

    // ---- rdfs:domain inference ----

    #[test]
    fn test_domain_inference() {
        let mut store = make_store();
        // hasFather rdfs:domain http://Person
        store.insert("http://hasFather", RDFS_DOMAIN, "http://Person");
        // :alice http://hasFather :bob
        store.insert("http://alice", "http://hasFather", "http://bob");

        apply_rdfs_inference(&mut store);
        // alice should now be typed as Person
        assert!(store.contains("http://alice", RDF_TYPE, "http://Person"));
    }

    // ---- rdfs:range inference ----

    #[test]
    fn test_range_inference() {
        let mut store = make_store();
        // hasName rdfs:range xsd:string
        store.insert(
            "http://hasName",
            RDFS_RANGE,
            "http://www.w3.org/2001/XMLSchema#string",
        );
        store.insert("http://alice", "http://hasName", "http://NameNode");

        apply_rdfs_inference(&mut store);
        // The object (NameNode) should be typed as xsd:string
        assert!(store.contains(
            "http://NameNode",
            RDF_TYPE,
            "http://www.w3.org/2001/XMLSchema#string"
        ));
    }

    // ---- subPropertyOf propagation ----

    #[test]
    fn test_sub_property_of_propagation() {
        let mut store = make_store();
        // hasMother rdfs:subPropertyOf hasParent
        store.insert("http://hasMother", RDFS_SUB_PROPERTY_OF, "http://hasParent");
        // :alice hasMother :carol
        store.insert("http://alice", "http://hasMother", "http://carol");

        apply_rdfs_inference(&mut store);
        // alice hasParent carol should be inferred
        assert!(store.contains("http://alice", "http://hasParent", "http://carol"));
    }

    // ---- InferenceConfig ----

    #[test]
    fn test_max_depth_config() {
        let mut store = make_store();
        // Three-level chain: only 2 levels should be resolved with max_depth=1
        store.insert("http://C", RDFS_SUB_CLASS_OF, "http://B");
        store.insert("http://B", RDFS_SUB_CLASS_OF, "http://A");

        let config = InferenceConfig::with_max_depth(1);
        let reasoner = RdfsReasoner::new(config);
        reasoner.apply(&mut store);

        // One transitive step should be inferred (C subClassOf A)
        assert!(store.contains("http://C", RDFS_SUB_CLASS_OF, "http://A"));
    }

    #[test]
    fn test_no_self_loops_from_transitivity() {
        let mut store = make_store();
        store.insert("http://Dog", RDFS_SUB_CLASS_OF, "http://Animal");

        apply_rdfs_inference(&mut store);
        // Dog should NOT be inferred as subClassOf itself
        assert!(!store.contains("http://Dog", RDFS_SUB_CLASS_OF, "http://Dog"));
    }

    #[test]
    fn test_idempotent() {
        let mut store = make_store();
        store.insert("http://Dog", RDFS_SUB_CLASS_OF, "http://Animal");
        store.insert("http://fido", RDF_TYPE, "http://Dog");

        let added_first = apply_rdfs_inference(&mut store);
        let added_second = apply_rdfs_inference(&mut store);
        assert!(added_first > 0);
        // Second run should add nothing new
        assert_eq!(added_second, 0);
    }
}