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
//!
//! This module provides certain *value type* required by the OWL 2 specification, namely
//! [`Natural`] and [`UnlimitedNatural`], and cardinality constraints defined by relationships
//! in the specification.
//!
//! The type `UnlimitedNatural` (which implies `Natural`) is described in the UML diagrams
//! as the type of the common field `arity` on data ranges. However, as it does not appear
//! in the grammar there is no representation specified and therefore no syntax for
//! *unbounded* other than the UML notation adopted by the types herein.
//!
//! Both the grammar and UML model make clear the cardinality of relationships through
//! different notation, `1`, `0..1`, `1..*`, `2..*`, etc. These are captured in
//! `CardinalityConstraint` instances which can then be used to quickly test collection
//! types and which will return a `CardinalityConstraintViolation` error if the collection
//! is out of bounds.
//!

use crate::{
    error::ApiError,
    fmt::{DisplayPretty, Indenter},
};
use core::{
    fmt::{Display, Formatter, Result as FmtResult},
    ops::{Range, RangeFrom, RangeFull, RangeTo},
    str::FromStr,
};
use num_traits::{One, Zero};
use rdftk_iri::{Iri, IriPrefixMap, IriRef, LocalName, Name, Namespace, PrefixedName};
use strum::{EnumIs, EnumTryAs};

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

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

///
/// While no upper bound is defined on the natural number in the OWL 2 specification the
/// choice to limit the number to 128 bits here seems like enough for about most any
/// reasonable usage.
///
pub type Natural = u128;

///
/// An `UnlimitedNatural` is either unlimited, i.e. can be any value without any constraint
/// whatsoever, or limited to a specific value.
///
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, EnumIs, EnumTryAs)]
pub enum UnlimitedNatural {
    ///
    /// The value is *unlimited*, or in some circumstances *unknown*.
    ///
    #[default]
    Unlimited,
    ///
    /// The value is limited to the specified `Natural`.
    ///
    Limited(Natural),
}

///
/// Represents a constraint on some element's cardinality.
///
/// Usually this is a collection type and while the collection itself has no bounds
/// checking this represents the bounds allowed by the specification for the collection
/// in the context of it's usage.
///
#[derive(Clone, Debug, PartialEq, Eq, Hash, EnumIs, EnumTryAs)]
pub enum CardinalityConstraint {
    /// Unlimited, there is no upper or lower bounds.
    Unlimited(RangeFull),
    /// The lower, or minimum, bound has been set but no upper bound has been set.
    MinLimited(RangeFrom<Natural>),
    /// The upper, or maximum, bound has been set but no lower bound has been set.
    MaxLimited(RangeTo<Natural>),
    /// The lower, or minimum, bound **and**, the upper, or maximum, bound has been set.
    MinMaxLimited(Range<Natural>),
    /// The collection must have **exactly** this number of elements.
    Exactly(Natural),
}

///
/// A simple enum used by [`CardinalityConstraintViolation`] to denote the cause of
/// a constraint violation.
///
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, EnumIs)]
pub enum CardinalityBound {
    /// Lower, or Minimum, bound exceeded.
    Min,
    /// Upper, or Maximum, bound exceeded.
    Max,
    /// Exact bound not met.
    Exact,
}

///
/// An error type used to signal a violation of a [`CardinalityConstraint`].
///
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct CardinalityConstraintViolation {
    bound: CardinalityBound,
    expecting: Natural,
    given: UnlimitedNatural,
}

// ------------------------------------------------------------------------------------------------
// Implementations ❯ Iri
// ------------------------------------------------------------------------------------------------

// By hand, these are not local types.
impl DisplayPretty for Iri {
    fn fmt_pretty(
        &self,
        f: &mut Formatter<'_>,
        _: &Indenter,
        prefix_map: &IriPrefixMap,
    ) -> FmtResult {
        write!(
            f,
            "{}",
            if let Some(name) = prefix_map.compress(self) {
                name.to_string()
            } else {
                self.to_string()
            }
        )
    }
}

// By hand, these are not local types.
impl DisplayPretty for IriRef {
    fn fmt_pretty(
        &self,
        f: &mut Formatter<'_>,
        indenter: &Indenter,
        prefix_map: &IriPrefixMap,
    ) -> FmtResult {
        match self {
            IriRef::Iri(iri) => iri.fmt_pretty(f, indenter, prefix_map),
            IriRef::PrefixedName(prefixed_name) => {
                prefixed_name.fmt_pretty(f, indenter, prefix_map)
            }
        }
    }
}

impl_display_pretty!(Name only self);
impl_display_pretty!(Namespace only self);
impl_display_pretty!(LocalName only self);
impl_display_pretty!(PrefixedName only self);

// ------------------------------------------------------------------------------------------------
// Implementations ❯ UnboundedNatural
// ------------------------------------------------------------------------------------------------

const UNLIMITED_STR: &str = "*";
const UNLIMITED_MIN_STR: &str = "*..";
const UNLIMITED_MAX_STR: &str = "..*";
const LIMITED_STR: &str = "..";

impl Display for UnlimitedNatural {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(
            f,
            "{}",
            match self {
                Self::Unlimited => UNLIMITED_STR.to_string(),
                Self::Limited(v) => v.to_string(),
            }
        )
    }
}

impl FromStr for UnlimitedNatural {
    type Err = ApiError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s == UNLIMITED_STR {
            Ok(Self::Unlimited)
        } else {
            Ok(Self::Limited(u128::from_str(s).map_err(|e| {
                ApiError::ValueParser("UnlimitedNatural", e.to_string(), s.to_string())
            })?))
        }
    }
}

impl From<u128> for UnlimitedNatural {
    fn from(value: u128) -> Self {
        Self::Limited(value)
    }
}

impl UnlimitedNatural {
    #[inline(always)]
    pub fn unbounded() -> Self {
        Self::Unlimited
    }

    #[inline(always)]
    pub fn zero() -> Self {
        Self::Limited(0)
    }

    #[inline(always)]
    pub fn is_zero(&self) -> Option<bool> {
        self.try_as_limited().map(|v| v.is_zero())
    }

    #[inline(always)]
    pub fn one() -> Self {
        Self::Limited(1)
    }

    #[inline(always)]
    pub fn is_one(&self) -> Option<bool> {
        self.try_as_limited().map(|v| v.is_one())
    }

    pub fn value(&self) -> Option<u128> {
        match self {
            Self::Unlimited => None,
            Self::Limited(v) => Some(*v),
        }
    }
}

// ------------------------------------------------------------------------------------------------
// Implementations ❯ CardinalityConstraint
// ------------------------------------------------------------------------------------------------

impl Display for CardinalityConstraint {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(
            f,
            "{}",
            match self {
                Self::Unlimited(_) => UNLIMITED_STR.to_string(),
                Self::MinLimited(v) => format!("{}{UNLIMITED_MAX_STR}", v.start),
                Self::MaxLimited(v) => format!("{UNLIMITED_MIN_STR}{}", v.end),
                Self::MinMaxLimited(v) => format!("{}{LIMITED_STR}{}", v.start, v.end),
                Self::Exactly(v) => format!("{v}"),
            }
        )
    }
}

impl FromStr for CardinalityConstraint {
    type Err = ApiError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s == "*" {
            Ok(Self::Unlimited(RangeFull::default()))
        } else if let Some(bound) = s.strip_suffix(UNLIMITED_MAX_STR) {
            Ok(Self::MinLimited(
                u128::from_str(bound).map_err(|e| {
                    ApiError::ValueParser("CardinalityConstraint", e.to_string(), s.to_string())
                })?..,
            ))
        } else if let Some(bound) = s.strip_prefix(UNLIMITED_MIN_STR) {
            Ok(Self::MaxLimited(
                ..u128::from_str(bound).map_err(|e| {
                    ApiError::ValueParser("CardinalityConstraint", e.to_string(), s.to_string())
                })?,
            ))
        } else {
            let parts = s.split(LIMITED_STR).collect::<Vec<_>>();
            if parts.len() == 1 {
                Ok(Self::Exactly(u128::from_str(s).map_err(|e| {
                    ApiError::ValueParser("CardinalityConstraint", e.to_string(), s.to_string())
                })?))
            } else if parts.len() == 2 {
                Ok(Self::MinMaxLimited(
                    u128::from_str(s).map_err(|e| {
                        ApiError::ValueParser("CardinalityConstraint", e.to_string(), s.to_string())
                    })?..u128::from_str(s).map_err(|e| {
                        ApiError::ValueParser("CardinalityConstraint", e.to_string(), s.to_string())
                    })?,
                ))
            } else {
                Err(ApiError::ValueParser(
                    "CardinalityConstraint",
                    "badly formatted min/max constraint".to_string(),
                    s.to_string(),
                ))
            }
        }
    }
}

impl CardinalityConstraint {
    /// Construct a new, unlimited constraint; this allows **any** number of elements.
    #[inline(always)]
    pub fn unlimited() -> Self {
        Self::Unlimited(RangeFull::default())
    }

    /// Construct a new, zero-limited constraint; this allows **zero** elements.
    #[inline(always)]
    pub fn zero() -> Self {
        Self::Exactly(0)
    }

    /// Construct a new, zero-or-one constraint; this allows optional elements.
    #[inline(always)]
    pub fn zero_or_one() -> Self {
        Self::MinMaxLimited(0..1)
    }

    /// Construct a new, zero-or-more constraint; this allows optionally many elements.
    #[inline(always)]
    pub fn zero_or_more() -> Self {
        Self::MinLimited(0..)
    }

    /// Construct a new, one-limited constraint; this requires an element.
    #[inline(always)]
    pub fn one() -> Self {
        Self::Exactly(1)
    }

    /// Construct a new, one-or-more constraint; this requires one or many elements.
    #[inline(always)]
    pub fn one_or_more() -> Self {
        Self::MinLimited(1..)
    }

    /// Construct a new, two-limited constraint; this requires exactly two elements.
    #[inline(always)]
    pub fn two() -> Self {
        Self::Exactly(2)
    }

    /// Construct a new, two-or-more constraint; this requires two or many elements.
    #[inline(always)]
    pub fn two_or_more() -> Self {
        Self::MinLimited(2..)
    }

    pub fn assert_valid_length(&self, length: usize) -> Result<(), ApiError> {
        self.assert_valid(UnlimitedNatural::Limited(length as Natural))
    }

    pub fn assert_valid(&self, value: UnlimitedNatural) -> Result<(), ApiError> {
        match self {
            Self::Unlimited(_) => Ok(()),
            Self::MinLimited(expecting) => {
                if let UnlimitedNatural::Limited(given) = value
                    && expecting.contains(&given)
                {
                    Ok(())
                } else {
                    Err(CardinalityConstraintViolation::min_fail(expecting.start, value).into())
                }
            }
            Self::MaxLimited(expecting) => {
                if let UnlimitedNatural::Limited(given) = value
                    && expecting.contains(&given)
                {
                    Ok(())
                } else {
                    Err(CardinalityConstraintViolation::max_fail(expecting.end, value).into())
                }
            }
            Self::MinMaxLimited(expecting) => match value {
                UnlimitedNatural::Unlimited => todo!(),
                UnlimitedNatural::Limited(given) => {
                    if given < expecting.start {
                        Err(CardinalityConstraintViolation::min_fail(expecting.start, value).into())
                    } else if given > expecting.end {
                        Err(CardinalityConstraintViolation::max_fail(expecting.end, value).into())
                    } else {
                        Ok(())
                    }
                }
            },
            Self::Exactly(expecting) => {
                if let UnlimitedNatural::Limited(given) = value
                    && given == *expecting
                {
                    Ok(())
                } else {
                    Err(CardinalityConstraintViolation::exact_fail(*expecting, value).into())
                }
            }
        }
    }
}

// ------------------------------------------------------------------------------------------------
// Implementations ❯ CardinalityBound
// ------------------------------------------------------------------------------------------------

impl Display for CardinalityBound {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(
            f,
            "{}",
            match self {
                Self::Min => "at least",
                Self::Max => "at most",
                Self::Exact => "exactly",
            }
        )
    }
}

// ------------------------------------------------------------------------------------------------
// Implementations ❯ CardinalityConstraintViolation
// ------------------------------------------------------------------------------------------------

impl Display for CardinalityConstraintViolation {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(
            f,
            "Constrant Violation: cardinality of `{}` does not meet the expected constraint of *`{}`* `{}`.",
            self.given, self.bound, self.expecting
        )
    }
}

impl core::error::Error for CardinalityConstraintViolation {}

impl CardinalityConstraintViolation {
    #[inline(always)]
    pub fn fail(bound: CardinalityBound, expecting: Natural, given: UnlimitedNatural) -> Self {
        Self {
            bound,
            expecting,
            given,
        }
    }

    #[inline(always)]
    pub fn min_fail(expecting: Natural, given: UnlimitedNatural) -> Self {
        Self {
            bound: CardinalityBound::Min,
            expecting,
            given,
        }
    }

    #[inline(always)]
    pub fn max_fail(expecting: Natural, given: UnlimitedNatural) -> Self {
        Self {
            bound: CardinalityBound::Max,
            expecting,
            given,
        }
    }

    #[inline(always)]
    pub fn exact_fail(expecting: Natural, given: UnlimitedNatural) -> Self {
        Self {
            bound: CardinalityBound::Exact,
            expecting,
            given,
        }
    }

    #[inline(always)]
    pub fn min_zero_fail(given: UnlimitedNatural) -> Self {
        Self {
            bound: CardinalityBound::Min,
            expecting: 0,
            given,
        }
    }

    #[inline(always)]
    pub fn max_zero_fail(given: UnlimitedNatural) -> Self {
        Self {
            bound: CardinalityBound::Max,
            expecting: 0,
            given,
        }
    }

    #[inline(always)]
    pub fn exact_zero_fail(given: UnlimitedNatural) -> Self {
        Self {
            bound: CardinalityBound::Exact,
            expecting: 0,
            given,
        }
    }

    #[inline(always)]
    pub fn min_one_fail(given: UnlimitedNatural) -> Self {
        Self {
            bound: CardinalityBound::Min,
            expecting: 1,
            given,
        }
    }

    #[inline(always)]
    pub fn max_one_fail(given: UnlimitedNatural) -> Self {
        Self {
            bound: CardinalityBound::Max,
            expecting: 1,
            given,
        }
    }

    #[inline(always)]
    pub fn exact_one_fail(given: UnlimitedNatural) -> Self {
        Self {
            bound: CardinalityBound::Exact,
            expecting: 1,
            given,
        }
    }
}