diff --git a/src/ontology/iri_mapped.rs b/src/ontology/iri_mapped.rs index c681cc8..6e5998e 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); @@ -187,7 +213,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,7 +225,7 @@ impl> OntologyIndex for IRIMappedIndex } fn index_take(&mut self, cmp: &AnnotatedComponent) -> Option> { - self.aa_to_iris(cmp).iter().fold(None, |val, iri| { + 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())) @@ -207,7 +233,7 @@ impl> OntologyIndex for IRIMappedIndex } fn index_remove(&mut self, cmp: &AnnotatedComponent) -> bool { - self.aa_to_iris(cmp).iter().fold(false, |val, iri| { + Self::iris_from_component(cmp).iter().fold(false, |val, iri| { self.mut_set_for_iri(iri).remove(cmp) || val }) }