athene-owlapi 0.0.1

This package provides a Rust API for OWL that adheres as closely as possible to the OWL 2 Web Ontology Language Structural Specification.
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
//!
//! This module provides the types to support OWL 2 Data Ranges.
//!
//! ![**Figure 6**. Data Ranges in OWL 2](https://www.w3.org/TR/owl2-syntax/C_datarange.gif)
//!

use crate::{
    entities::Datatype,
    error::ApiError,
    fmt::DisplayPretty,
    literals::Literal,
    values::{CardinalityConstraintViolation, UnlimitedNatural},
};
use core::str::FromStr;
use rdftk_iri::Iri;
use strum::{EnumIs, EnumTryAs};

#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, vec::Vec};

// ------------------------------------------------------------------------------------------------
// Public Types
// ------------------------------------------------------------------------------------------------

///
/// Datatypes, such as *xsd:string* or *xsd:integer*, and literals such as *"1"^^xsd:integer*,
/// can be used to express data ranges — sets of tuples of literals, where tuples consisting of
/// only one literal are identified with the literal itself. Each data range is associated with
/// a positive arity, which determines the size of the tuples in the data range. All datatypes
/// have arity one. This specification currently does not define data ranges of arity more than
/// one; however, by allowing for n-ary data ranges, the syntax of OWL 2 provides a "hook"
/// allowing implementations to introduce extensions such as comparisons and arithmetic.
///
/// Data ranges can be used in restrictions on data properties, as discussed in Sections 8.4 and
/// 8.5. The structure of data ranges in OWL 2 is shown in Figure 6. The simplest data ranges
/// are datatypes. The **DataIntersectionOf**, **DataUnionOf**, and **DataComplementOf** data
/// ranges provide for the standard set-theoretic operations on data ranges; in logical languages
/// these are usually called conjunction, disjunction, and negation, respectively. The
/// **DataOneOf** data range consists of exactly the specified set of literals. Finally, the
/// **DatatypeRestriction** data range restricts the value space of a datatype by a constraining
/// facet.
///
/// ## Specification (Section §7 -- Data Ranges)
///
/// ```bnf
/// DataRange :=
///     Datatype | DataIntersectionOf |
///     DataUnionOf | DataComplementOf |
///     DataOneOf | DatatypeRestriction
/// ```
///
#[derive(Clone, Debug, PartialEq, EnumIs, EnumTryAs)]
pub enum DataRange {
    DataIntersectionOf(DataIntersectionOf),
    DataUnionOf(DataUnionOf),
    DataComplementOf(DataComplementOf),
    DataOneOf(DataOneOf),
    Datatype(Datatype),
    DatatypeRestriction(DatatypeRestriction),
}

///
/// An intersection data range $DataIntersectionOf( DR_1 \cdots DR_n )$ contains all tuples of
/// literals that are contained in each data range $DR_i$ for $1 \leq i \leq n$. All data ranges
/// $DR_i$ must be of the same arity, and the resulting data range is of that arity as well.
///
/// ## Specification (Section §7.1 -- Intersection of Data Ranges)
///
/// ```bnf
/// DataIntersectionOf :=
///     'DataIntersectionOf' '('
///         DataRange DataRange { DataRange }
///     ')'
/// ```
///
/// ## Example
///
/// The following data range contains exactly the integer 0:
///
/// ```owl
/// DataIntersectionOf( xsd:nonNegativeInteger xsd:nonPositiveInteger )
/// ```
///
#[derive(Clone, Debug, PartialEq)]
pub struct DataIntersectionOf {
    arity: UnlimitedNatural,
    data_ranges: Vec<DataRange>, // 2..*
}

///
/// A union data range $DataUnionOf( DR_1 \cdots DR_n )$ contains all tuples of literals that are
/// contained in the at least one data range $DR_i$ for $1 \leq i \leq n$. All data ranges $DR_i$
/// must be of the same arity, and the resulting data range is of that arity as well.
///
/// ## Specification (Section §7.2 -- Union of Data Ranges)
///
/// ```bnf
/// DataUnionOf :=
///     'DataUnionOf' '('
///         DataRange DataRange { DataRange }
///     ')'
/// ```
///
/// ## Example
///
/// The following data range contains all strings and all integers:
///
/// ```owl
/// DataUnionOf( xsd:string xsd:integer )
/// ```
///
#[derive(Clone, Debug, PartialEq)]
pub struct DataUnionOf {
    arity: UnlimitedNatural,
    data_ranges: Vec<DataRange>, // 2..*
}

///
/// A complement data range $DataComplementOf( DR )$ contains all tuples of literals that are not
/// contained in the data range $DR$. The resulting data range has the arity equal to the arity
/// of $DR$.
///
/// ## Specification (Section §7.3 -- Complement of Data Ranges)
///
/// ```bnf
/// DataComplementOf :=
///     'DataComplementOf' '(' DataRange ')'
/// ```
///
/// ## Example
///
/// The following complement data range contains literals that are not positive integers. In
/// particular, this data range contains the integer zero and all negative integers; however,
/// it also contains all strings (since strings are not positive integers).
///
/// ```owl
/// DataComplementOf( xsd:positiveInteger )
/// ```
///
#[derive(Clone, Debug, PartialEq)]
pub struct DataComplementOf {
    arity: UnlimitedNatural,
    data_range: Box<DataRange>,
}

///
/// An enumeration of literals $DataOneOf( lt_1 \cdots lt_n )$ contains exactly the explicitly
/// specified literals $lt_i$ with $1 \leq i \leq n$. The resulting data range has arity one.
///
/// ## Specification (Section §7.4 -- Enumeration of Literals)
///
/// ```bnf
/// DataOneOf :=
///     'DataOneOf' '('
///         Literal { Literal }
///     ')'
/// ```
///
/// ## Example
///
/// The following data range contains exactly two literals: the string `"Peter"` and the integer one.
///
/// ```owl
/// DataOneOf( "Peter" "1"^^xsd:integer )
/// ```
///
///
#[derive(Clone, Debug, PartialEq)]
pub struct DataOneOf {
    arity: UnlimitedNatural,
    literals: Vec<Literal>, // 1..*
}

///
/// A datatype restriction $DatatypeRestriction( DT F_1 lt_1 \cdots F_n lt_n )$ consists of a unary
/// datatype $DT$ and $n$ pairs $( F_i , lt_i )$. The resulting data range is unary and is obtained
/// by restricting the value space of $DT$ according to the semantics of all $( F_i , v_i )$
/// (multiple pairs are interpreted conjunctively), where $v_i$ are the data values of the literals
/// $lt_i$.
///
/// In an OWL 2 DL ontology, each pair $( F_i , v_i )$ must be contained in the facet space of $DT$
/// (see Section 4).
///
/// ## Specification (Section §7.5 -- Datatype Restrictions)
///
/// ```bnf
/// DatatypeRestriction := 'DatatypeRestriction' '(' Datatype constrainingFacet restrictionValue { constrainingFacet restrictionValue } ')'
///
/// constrainingFacet := IRI
///
/// restrictionValue := Literal
/// ```
///
/// ## Example
///
/// The following data range contains exactly the integers 5, 6, 7, 8, and 9:
///
/// ```owl
/// DatatypeRestriction(
///     xsd:integer
///     xsd:minInclusive "5"^^xsd:integer
///     xsd:maxExclusive "10"^^xsd:integer
/// )
/// ```
///
#[derive(Clone, Debug, PartialEq)]
pub struct DatatypeRestriction {
    arity: UnlimitedNatural,
    datatype: Datatype,
    restrictions: Vec<FacetRestriction>, // 1..*
}

///
/// This corresponds to the repeating pair `(constrainingFacet, restrictionValue)` in the rule
/// [`DatatypeRestriction`]. Note that this is not a production in the source grammar.
///
/// ## Specification (Section §7.5 -- Datatype Restrictions)
///
/// ```bnf
/// __FacetRestriction := constrainingFacet restrictionValue
/// ```
///
#[derive(Clone, Debug, PartialEq)]
pub struct FacetRestriction {
    constraining_facet: Iri,
    restriction_value: Literal,
}

// ------------------------------------------------------------------------------------------------
// Public Types ❯ Traits
// ------------------------------------------------------------------------------------------------

///
/// Trait for all `DataRange` types that have an `arity` field, see Section §7 -- Data Ranges.
///
pub trait HasArity {
    fn arity(&self) -> UnlimitedNatural;
}

///
/// Trait for all `DataRange` types that have a `dataRange` field (with a cardinality
/// of `1`), see Section §7 -- Data Ranges.
///
pub trait HasDataRange {
    fn data_range(&self) -> &DataRange;
    fn data_range_mut(&mut self) -> &mut DataRange;
}

///
/// Trait for all `DataRange` types that have a `dataRanges` field (with a cardinality
/// of `1..*`), see Section §7 -- Data Ranges.
///
pub trait HasDataRanges {
    fn has_data_ranges(&self) -> bool;
    fn data_ranges(&self) -> impl Iterator<Item = &DataRange>;
    fn data_ranges_mut(&mut self) -> impl Iterator<Item = &mut DataRange>;
}

// ------------------------------------------------------------------------------------------------
// Implementation ❯ DataRange
// ------------------------------------------------------------------------------------------------

impl_display_pretty!(DataRange enum DataComplementOf,
    DataIntersectionOf,
    DataUnionOf,
    DataOneOf,
    Datatype,
    DatatypeRestriction
);

impl_from_for_variant!(DataRange, DataComplementOf);
impl_from_for_variant!(DataRange, DataIntersectionOf);
impl_from_for_variant!(DataRange, DataUnionOf);
impl_from_for_variant!(DataRange, DataOneOf);
impl_from_for_variant!(DataRange, Datatype);
impl_from_for_variant!(DataRange, DatatypeRestriction);

impl HasArity for DataRange {
    fn arity(&self) -> UnlimitedNatural {
        match self {
            Self::DataComplementOf(v) => v.arity(),
            Self::DataIntersectionOf(v) => v.arity(),
            Self::DataUnionOf(v) => v.arity(),
            Self::DataOneOf(v) => v.arity(),
            Self::Datatype(v) => v.arity(),
            Self::DatatypeRestriction(v) => v.arity(),
        }
    }
}

// ------------------------------------------------------------------------------------------------
// Implementation ❯ DataComplementOf
// ------------------------------------------------------------------------------------------------

impl_display_pretty!(DataComplementOf(data_range));

impl HasArity for DataComplementOf {
    fn arity(&self) -> UnlimitedNatural {
        self.arity
    }
}

impl HasDataRange for DataComplementOf {
    fn data_range(&self) -> &DataRange {
        &self.data_range
    }
    fn data_range_mut(&mut self) -> &mut DataRange {
        &mut self.data_range
    }
}

impl DataComplementOf {
    pub fn new(data_range: DataRange) -> Self {
        Self {
            arity: Default::default(),
            data_range: Box::new(data_range),
        }
    }
}

// ------------------------------------------------------------------------------------------------
// Implementation ❯ DataIntersectionOf
// ------------------------------------------------------------------------------------------------

impl_display_pretty!(DataIntersectionOf( @list data_ranges ));

impl HasArity for DataIntersectionOf {
    fn arity(&self) -> UnlimitedNatural {
        self.arity
    }
}

impl HasDataRanges for DataIntersectionOf {
    fn has_data_ranges(&self) -> bool {
        !self.data_ranges.is_empty()
    }
    fn data_ranges(&self) -> impl Iterator<Item = &DataRange> {
        self.data_ranges.iter()
    }
    fn data_ranges_mut(&mut self) -> impl Iterator<Item = &mut DataRange> {
        self.data_ranges.iter_mut()
    }
}

impl DataIntersectionOf {
    pub fn new<I: IntoIterator<Item = DataRange>>(data_ranges: I) -> Result<Self, ApiError> {
        let data_ranges: Vec<DataRange> = data_ranges.into_iter().collect();

        if data_ranges.len() >= 2 {
            Ok(Self {
                arity: Default::default(),
                data_ranges,
            })
        } else {
            Err(CardinalityConstraintViolation::min_fail(
                2,
                UnlimitedNatural::Limited(data_ranges.len() as u128),
            )
            .into())
        }
    }
}

// ------------------------------------------------------------------------------------------------
// Implementation ❯ DataUnionOf
// ------------------------------------------------------------------------------------------------

impl_display_pretty!(DataUnionOf( @list data_ranges ));

impl HasArity for DataUnionOf {
    fn arity(&self) -> UnlimitedNatural {
        self.arity
    }
}

impl HasDataRanges for DataUnionOf {
    fn has_data_ranges(&self) -> bool {
        !self.data_ranges.is_empty()
    }
    fn data_ranges(&self) -> impl Iterator<Item = &DataRange> {
        self.data_ranges.iter()
    }
    fn data_ranges_mut(&mut self) -> impl Iterator<Item = &mut DataRange> {
        self.data_ranges.iter_mut()
    }
}

impl DataUnionOf {
    pub fn new<I: IntoIterator<Item = DataRange>>(data_ranges: I) -> Result<Self, ApiError> {
        let data_ranges: Vec<DataRange> = data_ranges.into_iter().collect();

        if data_ranges.len() >= 2 {
            Ok(Self {
                arity: Default::default(),
                data_ranges,
            })
        } else {
            Err(CardinalityConstraintViolation::min_fail(
                2,
                UnlimitedNatural::Limited(data_ranges.len() as u128),
            )
            .into())
        }
    }
}

// ------------------------------------------------------------------------------------------------
// Implementation ❯ DataOneOf
// ------------------------------------------------------------------------------------------------

impl_display_pretty!(DataOneOf( @list literals ));

impl HasArity for DataOneOf {
    fn arity(&self) -> UnlimitedNatural {
        self.arity
    }
}

impl DataOneOf {
    pub fn new<I: IntoIterator<Item = Literal>>(literals: I) -> Result<Self, ApiError> {
        let literals: Vec<Literal> = literals.into_iter().collect();

        if literals.len() >= 2 {
            Ok(Self {
                arity: Default::default(),
                literals,
            })
        } else {
            Err(CardinalityConstraintViolation::min_fail(
                2,
                UnlimitedNatural::Limited(literals.len() as u128),
            )
            .into())
        }
    }

    pub fn has_literals(&self) -> bool {
        !self.literals.is_empty()
    }

    pub fn literals(&self) -> impl Iterator<Item = &Literal> {
        self.literals.iter()
    }

    pub fn literals_mut(&mut self) -> impl Iterator<Item = &mut Literal> {
        self.literals.iter_mut()
    }
}

// ------------------------------------------------------------------------------------------------
// Implementation ❯ DatatypeRestriction
// ------------------------------------------------------------------------------------------------

impl_display_pretty!(DatatypeRestriction( datatype, @list restrictions ));

impl HasArity for DatatypeRestriction {
    fn arity(&self) -> UnlimitedNatural {
        self.arity
    }
}

impl DatatypeRestriction {
    pub fn new<I: IntoIterator<Item = FacetRestriction>>(
        datatype: Datatype,
        restrictions: I,
    ) -> Result<Self, ApiError> {
        let restrictions: Vec<FacetRestriction> = restrictions.into_iter().collect();

        if restrictions.len() >= 1 {
            Ok(Self {
                arity: Default::default(),
                datatype,
                restrictions,
            })
        } else {
            Err(CardinalityConstraintViolation::min_fail(
                2,
                UnlimitedNatural::Limited(restrictions.len() as u128),
            )
            .into())
        }
    }

    pub fn datatype(&self) -> &Datatype {
        &self.datatype
    }

    pub fn has_restrictions(&self) -> bool {
        !self.restrictions.is_empty()
    }

    pub fn restrictions(&self) -> impl Iterator<Item = &FacetRestriction> {
        self.restrictions.iter()
    }

    pub fn restrictions_mut(&mut self) -> impl Iterator<Item = &mut FacetRestriction> {
        self.restrictions.iter_mut()
    }
}

// ------------------------------------------------------------------------------------------------
// Implementation ❯ FacetRestriction
// ------------------------------------------------------------------------------------------------

impl_display_pretty!(FacetRestriction(constraining_facet, restriction_value));

impl FacetRestriction {
    pub fn new(constraining_facet: Iri, restriction_value: Literal) -> Self {
        Self {
            constraining_facet,
            restriction_value,
        }
    }

    pub fn new_min_inclusive(restriction_value: Literal) -> Self {
        Self {
            constraining_facet: Iri::from_str("http://www.w3.org/2001/XMLSchema#minInclusive")
                .unwrap(),
            restriction_value,
        }
    }

    // usize
    pub fn new_min_length(restriction_value: Literal) -> Self {
        Self {
            constraining_facet: Iri::from_str("http://www.w3.org/2001/XMLSchema#minLength")
                .unwrap(),
            restriction_value,
        }
    }

    // usize
    pub fn new_max_length(restriction_value: Literal) -> Self {
        Self {
            constraining_facet: Iri::from_str("http://www.w3.org/2001/XMLSchema#maxLength")
                .unwrap(),
            restriction_value,
        }
    }

    // usize
    pub fn new_total_digits(restriction_value: Literal) -> Self {
        Self {
            constraining_facet: Iri::from_str("http://www.w3.org/2001/XMLSchema#totalDigits")
                .unwrap(),
            restriction_value,
        }
    }

    // usize
    pub fn new_fraction_digits(restriction_value: Literal) -> Self {
        Self {
            constraining_facet: Iri::from_str("http://www.w3.org/2001/XMLSchema#fractionDigits")
                .unwrap(),
            restriction_value,
        }
    }

    // {required|prohibited|optional}
    pub fn new_explicit_timezone(restriction_value: Literal) -> Self {
        Self {
            constraining_facet: Iri::from_str("http://www.w3.org/2001/XMLSchema#explicitTimezone")
                .unwrap(),
            restriction_value,
        }
    }

    // array of strings?
    pub fn new_pattern(restriction_value: Literal) -> Self {
        Self {
            constraining_facet: Iri::from_str("http://www.w3.org/2001/XMLSchema#pattern").unwrap(),
            restriction_value,
        }
    }

    // usize
    pub fn new_length(restriction_value: Literal) -> Self {
        Self {
            constraining_facet: Iri::from_str("http://www.w3.org/2001/XMLSchema#length").unwrap(),
            restriction_value,
        }
    }

    pub fn new_max_inclusive(restriction_value: Literal) -> Self {
        Self {
            constraining_facet: Iri::from_str("http://www.w3.org/2001/XMLSchema#maxInclusive")
                .unwrap(),
            restriction_value,
        }
    }

    pub fn new_min_exclusive(restriction_value: Literal) -> Self {
        Self {
            constraining_facet: Iri::from_str("http://www.w3.org/2001/XMLSchema#minExclusive")
                .unwrap(),
            restriction_value,
        }
    }

    pub fn new_max_exclusive(restriction_value: Literal) -> Self {
        Self {
            constraining_facet: Iri::from_str("http://www.w3.org/2001/XMLSchema#maxExclusive")
                .unwrap(),
            restriction_value,
        }
    }

    pub fn constraining_facet(&self) -> &Iri {
        &self.constraining_facet
    }

    pub fn restriction_value(&self) -> &Literal {
        &self.restriction_value
    }
}