Skip to content

Commit

Permalink
Update OFN reader to a generic return type
Browse files Browse the repository at this point in the history
  • Loading branch information
phillord committed Jul 2, 2024
1 parent 00c3bdb commit b312472
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 71 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ repos:
pass_filenames: false
stages: [pre-commit, pre-push]
- id: Compile Benches
name: Running Test
name: Compile Benches
language: system
entry: cargo bench --no-run
entry: cargo bench --no-run --quiet
pass_filenames: false
stages: [pre-commit, pre-push]
- repo: https://github.com/pre-commit/pre-commit-hooks
Expand Down
118 changes: 61 additions & 57 deletions src/io/ofn/reader/from_pair.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::BTreeSet;
use std::marker::PhantomData;
use std::str::FromStr;

use curie::Curie;
Expand All @@ -8,7 +9,6 @@ use pest::iterators::Pair;

use crate::error::HornedError;
use crate::model::*;
use crate::ontology::set::SetOntology;
use crate::vocab::{Facet, OWL2Datatype, OWL};

use super::Context;
Expand Down Expand Up @@ -941,63 +941,62 @@ impl<A: ForIRI> FromPair<A> for ObjectPropertyExpression<A> {
}

// ---------------------------------------------------------------------------
pub(crate) struct MutableOntologyWrapper<A: ForIRI, O: MutableOntology<A> + Ontology<A> + Default>(
pub(crate) O,
PhantomData<A>,
);

macro_rules! impl_ontology {
($ty:ident) => {
impl<A: ForIRI> FromPair<A> for $ty<A> {
const RULE: Rule = Rule::Ontology;
fn from_pair_unchecked(pair: Pair<Rule>, ctx: &Context<'_, A>) -> Result<Self> {
debug_assert!(pair.as_rule() == Rule::Ontology);
let mut pairs = pair.into_inner();
let mut pair = pairs.next().unwrap();

let mut ontology = $ty::default();
let mut ontology_id = OntologyID::default();

// Parse ontology IRI and Version IRI if any
if pair.as_rule() == Rule::OntologyIRI {
let inner = pair.into_inner().next().unwrap();
ontology_id.iri = Some(IRI::from_pair(inner, ctx)?);
pair = pairs.next().unwrap();
if pair.as_rule() == Rule::VersionIRI {
let inner = pair.into_inner().next().unwrap();
ontology_id.viri = Some(IRI::from_pair(inner, ctx)?);
pair = pairs.next().unwrap();
}
}
ontology.insert(ontology_id);
impl<A: ForIRI, O: MutableOntology<A> + Ontology<A> + Default> FromPair<A>
for MutableOntologyWrapper<A, O>
{
const RULE: Rule = Rule::Ontology;
fn from_pair_unchecked(pair: Pair<Rule>, ctx: &Context<'_, A>) -> Result<Self> {
debug_assert!(pair.as_rule() == Rule::Ontology);
let mut pairs = pair.into_inner();
let mut pair = pairs.next().unwrap();

// Process imports
for p in pair.into_inner() {
ontology.insert(Import::from_pair(p, ctx)?);
}
let mut ontology: O = Default::default();
let mut ontology_id = OntologyID::default();

// Process ontology annotations
for pair in pairs.next().unwrap().into_inner() {
ontology.insert(OntologyAnnotation::from_pair(pair, ctx)?);
}
// Parse ontology IRI and Version IRI if any
if pair.as_rule() == Rule::OntologyIRI {
let inner = pair.into_inner().next().unwrap();
ontology_id.iri = Some(IRI::from_pair(inner, ctx)?);
pair = pairs.next().unwrap();
if pair.as_rule() == Rule::VersionIRI {
let inner = pair.into_inner().next().unwrap();
ontology_id.viri = Some(IRI::from_pair(inner, ctx)?);
pair = pairs.next().unwrap();
}
}
ontology.insert(ontology_id);

// Process axioms, ignore SWRL rules
for pair in pairs.next().unwrap().into_inner() {
let inner = pair.into_inner().next().unwrap();
match inner.as_rule() {
Rule::Axiom => {
ontology.insert(AnnotatedComponent::from_pair(inner, ctx)?);
}
rule => {
unreachable!("unexpected rule in Ontology::from_pair: {:?}", rule);
}
}
}
// Process imports
for p in pair.into_inner() {
ontology.insert(Import::from_pair(p, ctx)?);
}

// Process ontology annotations
for pair in pairs.next().unwrap().into_inner() {
ontology.insert(OntologyAnnotation::from_pair(pair, ctx)?);
}

Ok(ontology)
// Process axioms, ignore SWRL rules
for pair in pairs.next().unwrap().into_inner() {
let inner = pair.into_inner().next().unwrap();
match inner.as_rule() {
Rule::Axiom => {
ontology.insert(AnnotatedComponent::from_pair(inner, ctx)?);
}
rule => {
unreachable!("unexpected rule in Ontology::from_pair: {:?}", rule);
}
}
}
};
}

impl_ontology!(SetOntology);
// impl_ontology!(AxiomMappedOntology);
Ok(MutableOntologyWrapper(ontology, Default::default()))
}
}

// ---------------------------------------------------------------------------

Expand Down Expand Up @@ -1035,17 +1034,18 @@ impl<A: ForIRI> FromPair<A> for PrefixMapping {

// ---------------------------------------------------------------------------

impl<A, O> FromPair<A> for (O, PrefixMapping)
impl<A, O> FromPair<A> for (MutableOntologyWrapper<A, O>, PrefixMapping)
where
A: ForIRI,
O: Ontology<A> + FromPair<A>,
O: Default + MutableOntology<A> + Ontology<A>,
{
const RULE: Rule = Rule::OntologyDocument;
fn from_pair_unchecked(pair: Pair<Rule>, ctx: &Context<'_, A>) -> Result<Self> {
let mut pairs = pair.into_inner();
let prefixes = PrefixMapping::from_pair(pairs.next().unwrap(), ctx)?;
let context = Context::new(ctx.build, &prefixes);
O::from_pair(pairs.next().unwrap(), &context).map(|ont| (ont, prefixes))
MutableOntologyWrapper::from_pair(pairs.next().unwrap(), &context)
.map(|ont| (ont, prefixes))
}
}

Expand Down Expand Up @@ -1117,6 +1117,7 @@ mod tests {

use super::*;
use crate::io::ofn::reader::lexer::OwlFunctionalLexer;
use crate::ontology::set::SetOntology;

use test_generator::test_resources;

Expand Down Expand Up @@ -1244,8 +1245,10 @@ mod tests {
.next()
.unwrap();

let doc: (SetOntology<String>, PrefixMapping) =
FromPair::from_pair(pair, &Context::new(&build, &prefixes)).unwrap();
let doc: (
MutableOntologyWrapper<_, SetOntology<String>>,
PrefixMapping,
) = FromPair::from_pair(pair, &Context::new(&build, &prefixes)).unwrap();
assert_eq!(
doc.1.mappings().collect::<HashSet<_>>(),
expected.mappings().collect::<HashSet<_>>()
Expand Down Expand Up @@ -1291,7 +1294,8 @@ mod tests {
let build = Build::new();
let prefixes = PrefixMapping::default();
let ctx = Context::new(&build, &prefixes);
let item: (SetOntology<Rc<str>>, _) = FromPair::from_pair(pair, &ctx).unwrap();
let item: (MutableOntologyWrapper<_, SetOntology<Rc<str>>>, _) =
FromPair::from_pair(pair, &ctx).unwrap();

let path = resource
.replace("owl-functional", "owl-xml")
Expand All @@ -1301,6 +1305,6 @@ mod tests {
crate::io::owx::reader::read(&mut Cursor::new(&owx), Default::default()).unwrap();

// pretty_assertions::assert_eq!(item.1, expected.1);
pretty_assertions::assert_eq!(item.0, expected.0);
pretty_assertions::assert_eq!(item.0 .0, expected.0);
}
}
17 changes: 10 additions & 7 deletions src/io/ofn/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ use crate::error::HornedError;
use crate::io::ParserConfiguration;
use crate::model::Build;
use crate::model::ForIRI;
use crate::model::RcStr;
use crate::ontology::set::SetOntology;
use crate::model::MutableOntology;
use crate::model::Ontology;

mod from_pair;
mod lexer;

use self::from_pair::FromPair;
use self::from_pair::MutableOntologyWrapper;
use self::lexer::OwlFunctionalLexer;
use self::lexer::Rule;

Expand All @@ -27,18 +28,18 @@ impl<'a, A: ForIRI> Context<'a, A> {
}
}

pub fn read<R: BufRead>(
pub fn read<A: ForIRI, O: MutableOntology<A> + Ontology<A> + Default, R: BufRead>(
bufread: R,
_config: ParserConfiguration,
) -> Result<(SetOntology<RcStr>, PrefixMapping), HornedError> {
) -> Result<(O, PrefixMapping), HornedError> {
let b = Build::new();
read_with_build(bufread, &b)
}

pub fn read_with_build<A: ForIRI, R: BufRead>(
pub fn read_with_build<A: ForIRI, O: MutableOntology<A> + Ontology<A> + Default, R: BufRead>(
mut bufread: R,
build: &Build<A>,
) -> Result<(SetOntology<A>, PrefixMapping), HornedError> {
) -> Result<(O, PrefixMapping), HornedError> {
let prefixes = PrefixMapping::default();
let ctx = Context::new(build, &prefixes);

Expand All @@ -49,5 +50,7 @@ pub fn read_with_build<A: ForIRI, R: BufRead>(
.next()
.unwrap();

FromPair::from_pair(pair, &ctx)
let wrapper: Result<(MutableOntologyWrapper<A, O>, PrefixMapping), HornedError> =
FromPair::from_pair(pair, &ctx);
wrapper.map(|r| (r.0 .0, r.1))
}
7 changes: 3 additions & 4 deletions src/io/ofn/writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,11 @@ mod test {
let reader = std::fs::File::open(&resource)
.map(std::io::BufReader::new)
.unwrap();
let (ont, prefixes) = crate::io::ofn::reader::read(reader, Default::default()).unwrap();
let (ont, prefixes): (ComponentMappedOntology<RcStr, AnnotatedComponent<RcStr>>, _) =
crate::io::ofn::reader::read(reader, Default::default()).unwrap();

let component_mapped: ComponentMappedOntology<RcStr, AnnotatedComponent<RcStr>> =
ont.clone().into();
let mut writer = Vec::new();
crate::io::ofn::writer::write(&mut writer, &component_mapped, Some(&prefixes)).unwrap();
crate::io::ofn::writer::write(&mut writer, &ont, Some(&prefixes)).unwrap();

let (ont2, prefixes2) =
crate::io::ofn::reader::read(std::io::Cursor::new(&writer), Default::default())
Expand Down
2 changes: 1 addition & 1 deletion src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2044,7 +2044,7 @@ pub enum DArgument<A> {
pub trait Ontology<A> {}

/// Add or remove axioms to an `MutableOntology`
pub trait MutableOntology<A> {
pub trait MutableOntology<A>: Ontology<A> {
/// Insert an axiom into the ontology.
///
/// # Examples
Expand Down
2 changes: 2 additions & 0 deletions src/ontology/component_mapped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ impl<A: ForIRI, AA: ForIndex<A>> Default for ComponentMappedOntology<A, AA> {
}
}

impl<A: ForIRI, AA: ForIndex<A>> Ontology<A> for ComponentMappedOntology<A, AA> {}

impl<A: ForIRI, AA: ForIndex<A>> MutableOntology<A> for ComponentMappedOntology<A, AA> {
fn insert<IAA>(&mut self, cmp: IAA) -> bool
where
Expand Down

0 comments on commit b312472

Please sign in to comment.