Skip to content

Commit

Permalink
Adds partially doc to ontology::iri_mapped plus small refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
filippodebortoli committed Nov 21, 2024
1 parent 71b16a8 commit 0fd9d9a
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/ontology/iri_mapped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<RcStr, RcAnnotatedComponent>::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<A, AA> {
irindex: RefCell<BTreeMap<IRI<A>, BTreeSet<AA>>>,
Expand All @@ -38,7 +63,8 @@ impl<A: ForIRI, AA: ForIndex<A>> IRIMappedIndex<A, AA> {
}
}

fn aa_to_iris(&self, cmp: &AnnotatedComponent<A>) -> HashSet<IRI<A>> {
/// Helper that visits an annotated component and extract all the occurring IRIs.
fn iris_from_component(cmp: &AnnotatedComponent<A>) -> HashSet<IRI<A>> {
let mut w = Walk::new(IRIExtract::default());
w.annotated_component(cmp);

Expand Down Expand Up @@ -187,7 +213,7 @@ impl<'a, A: ForIRI, AA: ForIndex<A>> IntoIterator for &'a IRIMappedIndex<A,AA> {

impl<A: ForIRI, AA: ForIndex<A>> OntologyIndex<A, AA> for IRIMappedIndex<A, AA> {
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());
Expand All @@ -199,15 +225,15 @@ impl<A: ForIRI, AA: ForIndex<A>> OntologyIndex<A, AA> for IRIMappedIndex<A, AA>
}

fn index_take(&mut self, cmp: &AnnotatedComponent<A>) -> Option<AnnotatedComponent<A>> {
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()))
})
}

fn index_remove(&mut self, cmp: &AnnotatedComponent<A>) -> 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
})
}
Expand Down

0 comments on commit 0fd9d9a

Please sign in to comment.