Skip to content

Commit

Permalink
Make RDFOntology return type generic
Browse files Browse the repository at this point in the history
RDFOntology is now a trait.
The old RDFOntology struct is now called ConcreteRDFOntology.
Quite a few trait implementations, particularly Default have been added.
  • Loading branch information
phillord committed Jul 1, 2024
1 parent 9b33556 commit 5025ae4
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 114 deletions.
4 changes: 2 additions & 2 deletions horned-bin/src/bin/horned_unparsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use clap::ArgMatches;

use horned_bin::config::{parser_app, parser_config};
use horned_owl::error::HornedError;
use horned_owl::io::rdf::reader::RDFOntology;
use horned_owl::io::rdf::reader::ConcreteRDFOntology;
use horned_owl::model::{RcAnnotatedComponent, RcStr};

use std::{fs::File, io::BufReader, path::Path};
Expand Down Expand Up @@ -38,7 +38,7 @@ pub(crate) fn matcher(matches: &ArgMatches) -> Result<(), HornedError> {
HornedError::CommandError("Command requires an INPUT argument".to_string())
})?;

let (_ont, incomplete): (RDFOntology<RcStr, RcAnnotatedComponent>, _) =
let (_ont, incomplete): (ConcreteRDFOntology<RcStr, RcAnnotatedComponent>, _) =
horned_owl::io::rdf::reader::read(
&mut BufReader::new(File::open(Path::new(input))?),
parser_config(matches),
Expand Down
6 changes: 3 additions & 3 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod rdf;

use curie::PrefixMapping;

use self::rdf::reader::{IncompleteParse, RDFOntology};
use self::rdf::reader::{ConcreteRDFOntology, IncompleteParse};
use crate::ontology::indexed::ForIndex;
use crate::{
model::ForIRI,
Expand All @@ -23,7 +23,7 @@ pub enum ResourceType {
pub enum ParserOutput<A: ForIRI, AA: ForIndex<A>> {
OFNParser(SetOntology<A>, PrefixMapping),
OWXParser(SetOntology<A>, PrefixMapping),
RDFParser(RDFOntology<A, AA>, IncompleteParse<A>),
RDFParser(ConcreteRDFOntology<A, AA>, IncompleteParse<A>),
}

impl<A: ForIRI, AA: ForIndex<A>> ParserOutput<A, AA> {
Expand All @@ -35,7 +35,7 @@ impl<A: ForIRI, AA: ForIndex<A>> ParserOutput<A, AA> {
ParserOutput::OWXParser(sop.0, sop.1)
}

pub fn rdf(rop: (RDFOntology<A, AA>, IncompleteParse<A>)) -> ParserOutput<A, AA> {
pub fn rdf(rop: (ConcreteRDFOntology<A, AA>, IncompleteParse<A>)) -> ParserOutput<A, AA> {
ParserOutput::RDFParser(rop.0, rop.1)
}
}
Expand Down
40 changes: 23 additions & 17 deletions src/io/rdf/closure_reader.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
use crate::error::HornedError;
use crate::io::rdf::reader::parser_with_build;
use crate::io::rdf::reader::OntologyParser;
use crate::io::rdf::reader::RDFOntology;
use crate::io::IncompleteParse;
use crate::io::ParserConfiguration;
use crate::io::RDFOntology;
use crate::model::Build;
use crate::model::DocIRI;
use crate::model::ForIRI;
use crate::model::MutableOntology;
use crate::model::IRI;
use crate::ontology::indexed::ForIndex;
use crate::ontology::set::SetIndex;
use crate::resolve::path_to_file_iri;
use crate::resolve::resolve_iri;

use std::collections::HashMap;
use std::marker::PhantomData;
use std::path::PathBuf;

pub struct ClosureOntologyParser<'a, A: ForIRI, AA: ForIndex<A>> {
op: HashMap<IRI<A>, OntologyParser<'a, A, AA>>,
pub struct ClosureOntologyParser<'a, A: ForIRI, AA: ForIndex<A>, O: RDFOntology<A, AA>> {
op: HashMap<IRI<A>, OntologyParser<'a, A, AA, O>>,
import_map: HashMap<IRI<A>, Vec<IRI<A>>>,
b: &'a Build<A>,
config: ParserConfiguration,
p: PhantomData<AA>,
}

impl<'a, A: ForIRI, AA: ForIndex<A>> ClosureOntologyParser<'a, A, AA> {
impl<'a, A: ForIRI, AA: ForIndex<A>, O: RDFOntology<A, AA>> ClosureOntologyParser<'a, A, AA, O> {
pub fn new(b: &'a Build<A>, config: ParserConfiguration) -> Self {
ClosureOntologyParser {
b,
import_map: HashMap::new(),
op: HashMap::new(),
config,
p: Default::default(),
}
}

Expand Down Expand Up @@ -87,17 +90,19 @@ impl<'a, A: ForIRI, AA: ForIndex<A>> ClosureOntologyParser<'a, A, AA> {
let mut p = parser_with_build(&mut s.as_bytes(), self.b, self.config);
let imports = p.parse_imports().unwrap();
p.parse_declarations()?;
let o = p.mut_ontology_ref();
let o: &mut O = p.mut_ontology_ref();

o.insert(DocIRI(new_doc_iri.clone()));

let mut res = if let Some(declared_iri) = o.i().the_ontology_id_or_default().iri {
let si: &SetIndex<A, AA> = o.as_ref();

let mut res = if let Some(declared_iri) = si.the_ontology_id_or_default().iri {
vec![declared_iri]
} else {
vec![]
};

if let Some(declared_iri) = o.i().the_ontology_id_or_default().iri {
if let Some(declared_iri) = si.the_ontology_id_or_default().iri {
self.import_map
.insert(declared_iri.clone(), imports.clone());
self.op.insert(declared_iri, p);
Expand Down Expand Up @@ -136,12 +141,12 @@ impl<'a, A: ForIRI, AA: ForIndex<A>> ClosureOntologyParser<'a, A, AA> {
}

// Return ontology in potentially incompletely parsed state
pub fn as_ontology_vec(self) -> Vec<RDFOntology<A, AA>> {
pub fn as_ontology_vec(self) -> Vec<O> {
todo!()
}

// Return ontology in potentially incompletely parsed state
pub fn as_ontology_vec_and_incomplete(self) -> Vec<(RDFOntology<A, AA>, IncompleteParse<A>)> {
pub fn as_ontology_vec_and_incomplete(self) -> Vec<(O, IncompleteParse<A>)> {
self.op
.into_values()
.map(|op| op.as_ontology_and_incomplete().unwrap())
Expand All @@ -151,10 +156,10 @@ impl<'a, A: ForIRI, AA: ForIndex<A>> ClosureOntologyParser<'a, A, AA> {

// Parse the ontology at IRI, resolving any knowledge from imports necessary
#[allow(clippy::type_complexity)]
pub fn read<A: ForIRI, AA: ForIndex<A>>(
pub fn read<A: ForIRI, AA: ForIndex<A>, O: RDFOntology<A, AA>>(
iri: &IRI<A>,
config: ParserConfiguration,
) -> Result<(RDFOntology<A, AA>, IncompleteParse<A>), HornedError> {
) -> Result<(O, IncompleteParse<A>), HornedError> {
// Do parse, then full parse of first, drop the rest
let b = Build::new();
let mut c = ClosureOntologyParser::new(&b, config);
Expand All @@ -170,11 +175,11 @@ pub fn read<A: ForIRI, AA: ForIndex<A>>(
}

#[allow(clippy::type_complexity)]
pub fn read_closure<A: ForIRI, AA: ForIndex<A>>(
pub fn read_closure<A: ForIRI, AA: ForIndex<A>, O: RDFOntology<A, AA>>(
b: &Build<A>,
iri: &IRI<A>,
config: ParserConfiguration,
) -> Result<Vec<(RDFOntology<A, AA>, IncompleteParse<A>)>, HornedError> {
) -> Result<Vec<(O, IncompleteParse<A>)>, HornedError> {
// Do parse, then full parse, then result the results
let mut c = ClosureOntologyParser::new(b, config);
c.parse_iri(iri, None)?;
Expand All @@ -189,7 +194,7 @@ pub fn read_closure<A: ForIRI, AA: ForIndex<A>>(
#[cfg(test)]
mod test {
use crate::io::rdf::closure_reader::*;
use crate::io::rdf::reader::RcRDFOntology;
use crate::io::rdf::reader::ConcreteRcRDFOntology;
use crate::ontology::set::SetOntology;
use std::path::Path;

Expand All @@ -199,7 +204,7 @@ mod test {
let b = Build::new_rc();
let iri = path_to_file_iri(&b, path);

let (_, ic): (RcRDFOntology, _) = read(&iri, Default::default()).unwrap();
let (_, ic): (ConcreteRcRDFOntology, _) = read(&iri, Default::default()).unwrap();
assert!(ic.is_complete());
}

Expand All @@ -211,7 +216,8 @@ mod test {
let b = Build::new_rc();
let iri = path_to_file_iri(&b, path);

let v: Vec<(RcRDFOntology, _)> = read_closure(&b, &iri, Default::default()).unwrap();
let v: Vec<(ConcreteRcRDFOntology, _)> =
read_closure(&b, &iri, Default::default()).unwrap();
let v: Vec<SetOntology<_>> = v
.into_iter()
.map(|(rdfo, ic)| {
Expand Down
Loading

0 comments on commit 5025ae4

Please sign in to comment.