From d7c7acdbcf75e3e50998b1ef57785a1bea6da486 Mon Sep 17 00:00:00 2001 From: Filippo De Bortoli Date: Thu, 21 Nov 2024 10:15:28 +0100 Subject: [PATCH 1/3] Adds partially doc to ontology::iri_mapped plus small refactoring. --- src/ontology/iri_mapped.rs | 68 ++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/src/ontology/iri_mapped.rs b/src/ontology/iri_mapped.rs index c681cc8..134ffd1 100644 --- a/src/ontology/iri_mapped.rs +++ b/src/ontology/iri_mapped.rs @@ -25,6 +25,31 @@ use super::set::SetIndex; use std::collections::HashSet; +/// Implements an ontology index that stores for each IRI a set of annotated components in which this IRI occurs. +/// +/// # Examples +/// +/// The following snippet shows the effect of adding an axiom involving two IRIs to a new index. +/// ``` +/// # use horned_owl::ontology::iri_mapped::IRIMappedIndex; +/// # use horned_owl::{model::*, ontology::indexed::OntologyIndex}; +/// # fn test_index_example() { +/// let build = Build::new(); +/// let mut idx = IRIMappedIndex::::new(); +/// let cmp = AnnotatedComponent { +/// component: Component::DisjointClasses(DisjointClasses(vec![ +/// ClassExpression::Class(build.class("http://www.example.com/#A")), +/// ClassExpression::Class(build.class("http://www.example.com/#B")), +/// ])), +/// ann: Default::default(), +/// }; +/// +/// idx.index_insert(RcAnnotatedComponent::new(cmp)); +/// +/// assert!(idx.component_for_iri(&build.iri("http://www.example.com/#A")).next().is_some()); +/// assert!(idx.component_for_iri(&build.iri("http://www.example.com/#B")).next().is_some()); +/// # } +/// ``` #[derive(Debug, Eq, PartialEq)] pub struct IRIMappedIndex { irindex: RefCell, BTreeSet>>, @@ -38,7 +63,8 @@ impl> IRIMappedIndex { } } - fn aa_to_iris(&self, cmp: &AnnotatedComponent) -> HashSet> { + /// Helper that visits an annotated component and extract all the occurring IRIs. + fn iris_from_component(cmp: &AnnotatedComponent) -> HashSet> { let mut w = Walk::new(IRIExtract::default()); w.annotated_component(cmp); @@ -66,20 +92,9 @@ impl> IRIMappedIndex { unsafe { (*self.components_as_ptr(iri)).get_mut(iri).unwrap() } } - /* - /// Gets an iterator that visits the annotated components of the ontology. - pub fn iter(&self) -> IRIMappedIter { - IRIMappedIter { - ont: self, - inner: None, - iris: unsafe { (*self.irindex.as_ptr()).keys().collect() }, - } - } - */ - - /// Fetch the AnnotatedComponent for a given IRI + /// Iterates over the annotated components associated to `iri` by the index. /// - /// See also `component` for access to the `Component` without annotations. + /// Use [`component()`](Self::component) to iterate over components without annotations. pub fn component_for_iri(&self, iri: &IRI) -> impl Iterator> { self.set_for_iri(iri) // Iterate over option @@ -89,8 +104,9 @@ impl> IRIMappedIndex { .map(|rc| rc.borrow()) } - /// Fetch the Component set iterator for a given iri + /// Iterates over the components (without annotations) associated to `iri` by the index. /// + /// Use [`component_for_iri()`](Self::component_for_iri) to iterate over annotated components. pub fn component(&self, iri: &IRI) -> impl Iterator> { self.component_for_iri(iri).map(|ann| &ann.component) } @@ -187,7 +203,7 @@ impl<'a, A: ForIRI, AA: ForIndex> IntoIterator for &'a IRIMappedIndex { impl> OntologyIndex for IRIMappedIndex { fn index_insert(&mut self, cmp: AA) -> bool { - let iris = self.aa_to_iris(cmp.borrow()); + let iris = Self::iris_from_component(cmp.borrow()); if !iris.is_empty() { for iri in iris.iter() { self.mut_set_for_iri(iri).insert(cmp.clone()); @@ -199,17 +215,21 @@ impl> OntologyIndex for IRIMappedIndex } fn index_take(&mut self, cmp: &AnnotatedComponent) -> Option> { - self.aa_to_iris(cmp).iter().fold(None, |val, iri| { - self.mut_set_for_iri(iri) - .take(cmp) - .map_or(val, |c| Some(c.unwrap())) - }) + Self::iris_from_component(cmp) + .iter() + .fold(None, |val, iri| { + self.mut_set_for_iri(iri) + .take(cmp) + .map_or(val, |c| Some(c.unwrap())) + }) } fn index_remove(&mut self, cmp: &AnnotatedComponent) -> bool { - self.aa_to_iris(cmp).iter().fold(false, |val, iri| { - self.mut_set_for_iri(iri).remove(cmp) || val - }) + Self::iris_from_component(cmp) + .iter() + .fold(false, |val, iri| { + self.mut_set_for_iri(iri).remove(cmp) || val + }) } } From 665a362c87849f551bdab153a1146d0dd8c021b3 Mon Sep 17 00:00:00 2001 From: Filippo De Bortoli Date: Thu, 21 Nov 2024 13:39:42 +0100 Subject: [PATCH 2/3] Linting ontology::iri_mapped. --- src/ontology/iri_mapped.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ontology/iri_mapped.rs b/src/ontology/iri_mapped.rs index 134ffd1..106bff4 100644 --- a/src/ontology/iri_mapped.rs +++ b/src/ontology/iri_mapped.rs @@ -350,7 +350,6 @@ mod test { #[test] fn test_ontology_cons() { let _ = IRIMappedOntology::new_arc(); - assert!(true); } #[test] @@ -465,7 +464,7 @@ mod test { let mut v: Vec<_> = irindex .iter() - .flat_map(|(i, s)| s.into_iter().map(move |c| (i.clone(), c.clone()))) + .flat_map(|(i, s)| s.iter().map(move |c| (i.clone(), c.clone()))) .collect(); v.sort(); From bb57b70f266e9c9c47f9ae6834da968545a36080 Mon Sep 17 00:00:00 2001 From: Filippo De Bortoli Date: Thu, 21 Nov 2024 15:32:17 +0100 Subject: [PATCH 3/3] Refining the doctest on ontology::iri_mapped. --- src/ontology/iri_mapped.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/ontology/iri_mapped.rs b/src/ontology/iri_mapped.rs index 106bff4..560487c 100644 --- a/src/ontology/iri_mapped.rs +++ b/src/ontology/iri_mapped.rs @@ -1,9 +1,9 @@ -//! Access `AnnotatedComponent` by iri. +//! An index to access all the [annotated components](crate::model::AnnotatedComponent) referencing a given [IRI](crate::model::IRI). //! # Overview //! -//! This module provides an `IRIMappedIndex` which provides rapid -//! access to all components associated with a given IRI. +//! This module provides [IRIMappedIndex], which provides rapid +//! access to all components associated with a given IRI, and the concrete ontology implementation [IRIMappedOntology] using this index. //! use super::indexed::ForIndex; use super::set::SetOntology; @@ -33,9 +33,12 @@ use std::collections::HashSet; /// ``` /// # use horned_owl::ontology::iri_mapped::IRIMappedIndex; /// # use horned_owl::{model::*, ontology::indexed::OntologyIndex}; +/// # use std::rc::Rc; /// # fn test_index_example() { -/// let build = Build::new(); -/// let mut idx = IRIMappedIndex::::new(); +/// let build: Build> = Build::new(); +/// let mut idx = IRIMappedIndex::new(); +/// +/// // We create a component to add to the index. /// let cmp = AnnotatedComponent { /// component: Component::DisjointClasses(DisjointClasses(vec![ /// ClassExpression::Class(build.class("http://www.example.com/#A")), @@ -44,8 +47,10 @@ use std::collections::HashSet; /// ann: Default::default(), /// }; /// -/// idx.index_insert(RcAnnotatedComponent::new(cmp)); +/// // We insert the annotated component that we just created in the index. +/// idx.index_insert(cmp); /// +/// // The index now lists the annotated component for both entities occurring in it. /// assert!(idx.component_for_iri(&build.iri("http://www.example.com/#A")).next().is_some()); /// assert!(idx.component_for_iri(&build.iri("http://www.example.com/#B")).next().is_some()); /// # }