Skip to main content

owl_structural_model/
annotation.rs

1//! OWL annotation values.
2
3use iri_rfc3987::Iri;
4
5use crate::{BlankNodeId, OwlLiteral};
6
7/// An OWL annotation.
8#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
9pub struct Annotation<L = OwlLiteral, B = BlankNodeId> {
10    property: Iri<String>,
11    value: AnnotationValue<L, B>,
12}
13
14impl<L, B> Annotation<L, B> {
15    /// Create an annotation from an annotation property IRI and value.
16    #[must_use]
17    pub fn new(property: Iri<String>, value: AnnotationValue<L, B>) -> Self {
18        Self { property, value }
19    }
20
21    /// Return the annotation property IRI.
22    #[must_use]
23    pub fn property(&self) -> &Iri<String> {
24        &self.property
25    }
26
27    /// Return the annotation value.
28    #[must_use]
29    pub fn value(&self) -> &AnnotationValue<L, B> {
30        &self.value
31    }
32}
33
34/// A value that can appear in an OWL annotation.
35#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
36pub enum AnnotationValue<L = OwlLiteral, B = BlankNodeId> {
37    /// An IRI-valued annotation.
38    Iri(Iri<String>),
39    /// An anonymous-individual-valued annotation.
40    AnonymousIndividual(B),
41    /// A literal-valued annotation.
42    Literal(L),
43}
44
45impl<L, B> AnnotationValue<L, B> {
46    /// Construct an annotation value from a caller-owned anonymous identity.
47    #[must_use]
48    pub fn anonymous_individual(identity: B) -> Self {
49        Self::AnonymousIndividual(identity)
50    }
51}
52
53/// Subject of an OWL annotation assertion.
54#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
55pub enum AnnotationSubject<B = BlankNodeId> {
56    /// An IRI subject.
57    Iri(Iri<String>),
58    /// An anonymous individual subject.
59    AnonymousIndividual(B),
60}
61
62/// Axiom annotations attached to one structural axiom.
63#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
64pub struct AxiomAnnotations<L = OwlLiteral, B = BlankNodeId> {
65    // bounded: annotations belong to one structural axiom.
66    annotations: Vec<Annotation<L, B>>,
67}
68
69impl<L, B> AxiomAnnotations<L, B> {
70    /// Create an empty annotation set.
71    #[must_use]
72    pub fn empty() -> Self {
73        Self::default()
74    }
75
76    /// Create annotations from a bounded axiom-local collection.
77    #[must_use]
78    pub fn new(annotations: Vec<Annotation<L, B>>) -> Self {
79        Self { annotations }
80    }
81
82    /// Return the annotations.
83    #[must_use]
84    pub fn as_slice(&self) -> &[Annotation<L, B>] {
85        &self.annotations
86    }
87
88    /// Return the annotation count.
89    #[must_use]
90    pub fn len(&self) -> usize {
91        self.annotations.len()
92    }
93
94    /// Return whether no annotations are present.
95    #[must_use]
96    pub fn is_empty(&self) -> bool {
97        self.annotations.is_empty()
98    }
99}
100
101impl<L, B> Default for AxiomAnnotations<L, B> {
102    fn default() -> Self {
103        Self {
104            annotations: Vec::new(),
105        }
106    }
107}