Skip to content

Commit

Permalink
Support functional syntax in horned-bin
Browse files Browse the repository at this point in the history
  • Loading branch information
phillord committed May 14, 2024
1 parent 602333f commit cfdd169
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 20 deletions.
5 changes: 5 additions & 0 deletions horned-bin/src/bin/horned_dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ pub(crate) fn matcher(matches: &ArgMatches) -> Result<(), HornedError> {
)?;

match r {
horned_owl::io::ParserOutput::OFNParser(ont, map) => {
let hash_map: HashMap<&String, &String> = map.mappings().collect();
println!("Ontology:\n{:#?}\n\nMapping:\n{:#?}", ont, hash_map);
Ok(())
}
horned_owl::io::ParserOutput::OWXParser(ont, map) => {
let hash_map: HashMap<&String, &String> = map.mappings().collect();
println!("Ontology:\n{:#?}\n\nMapping:\n{:#?}", ont, hash_map);
Expand Down
4 changes: 4 additions & 0 deletions horned-bin/src/bin/horned_round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ pub(crate) fn matcher(matches: &ArgMatches) -> Result<(), HornedError> {
)?;

let rtn = match res {
horned_owl::io::ParserOutput::OFNParser(so, pm) => {
let amo: RcComponentMappedOntology = so.into();
horned_owl::io::owx::writer::write(&mut stdout(), &amo, Some(&pm))
}
horned_owl::io::ParserOutput::OWXParser(so, pm) => {
let amo: RcComponentMappedOntology = so.into();
horned_owl::io::owx::writer::write(&mut stdout(), &amo, Some(&pm))
Expand Down
17 changes: 13 additions & 4 deletions horned-bin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::{

pub fn path_type(path: &Path) -> Option<ResourceType> {
match path.extension().and_then(|s| s.to_str()) {
Some("ofn") => Some(ResourceType::OFN),
Some("owx") => Some(ResourceType::OWX),
Some("owl") => Some(ResourceType::RDF),
_ => None,
Expand All @@ -27,15 +28,20 @@ pub fn parse_path(
config: ParserConfiguration
) -> Result<ParserOutput<RcStr, RcAnnotatedComponent>, HornedError> {
Ok(match path_type(path) {
Some(ResourceType::OFN) => {
let file = File::open(&path)?;
let mut bufreader = BufReader::new(file);
ParserOutput::ofn(horned_owl::io::ofn::reader::read(&mut bufreader, config)?)
}
Some(ResourceType::OWX) => {
let file = File::open(&path)?;
let mut bufreader = BufReader::new(file);
horned_owl::io::owx::reader::read(&mut bufreader, config)?.into()
ParserOutput::owx(horned_owl::io::owx::reader::read(&mut bufreader, config)?)
}
Some(ResourceType::RDF) => {
let b = Build::new();
let iri = horned_owl::resolve::path_to_file_iri(&b, path);
horned_owl::io::rdf::closure_reader::read(&iri, config)?.into()
ParserOutput::rdf(horned_owl::io::rdf::closure_reader::read(&iri, config)?)
}
None => {
return Err(HornedError::CommandError(format!(
Expand All @@ -54,12 +60,15 @@ pub fn parse_imports(
let file = File::open(&path)?;
let mut bufreader = BufReader::new(file);
Ok(match path_type(path) {
Some(ResourceType::OWX) => horned_owl::io::owx::reader::read(&mut bufreader, config)?.into(),
Some(ResourceType::OFN) => ParserOutput::ofn(horned_owl::io::owx::reader::read(&mut bufreader, config)?),
Some(ResourceType::OWX) => ParserOutput::owx(horned_owl::io::owx::reader::read(&mut bufreader, config)?),
Some(ResourceType::RDF) => {
let b = Build::new();
let mut p = horned_owl::io::rdf::reader::parser_with_build(&mut bufreader, &b, config);
p.parse_imports()?;
p.as_ontology_and_incomplete()?.into()
ParserOutput::rdf(
p.as_ontology_and_incomplete()?
)
}
None => {
return Err(HornedError::CommandError(format!(
Expand Down
38 changes: 22 additions & 16 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,31 @@ use crate::{
};

pub enum ResourceType {
OFN,
OWX,
RDF,
}

pub enum ParserOutput<A: ForIRI, AA: ForIndex<A>> {
OFNParser(SetOntology<A>, PrefixMapping),
OWXParser(SetOntology<A>, PrefixMapping),
RDFParser(RDFOntology<A, AA>, IncompleteParse<A>),
}

impl<A:ForIRI, AA: ForIndex<A>> ParserOutput<A, AA> {
pub fn ofn(sop: (SetOntology<A>, PrefixMapping)) -> ParserOutput<A, AA> {
ParserOutput::OFNParser(sop.0, sop.1)
}

pub fn owx(sop: (SetOntology<A>, PrefixMapping)) -> ParserOutput<A, AA> {
ParserOutput::OWXParser(sop.0, sop.1)
}

pub fn rdf(rop: (RDFOntology<A, AA>, IncompleteParse<A>)) -> ParserOutput<A, AA> {
ParserOutput::RDFParser(rop.0, rop.1)
}
}

#[derive(Clone, Copy, Debug, Default)]
pub struct ParserConfiguration {
// Shared Config will go here
Expand All @@ -32,12 +48,12 @@ pub struct ParserConfiguration {
}

#[derive(Clone, Copy, Debug, Default)]
pub struct RDFParserConfiguration {
pub lax: bool
pub struct OWXParserConfiguration {
}

#[derive(Clone, Copy, Debug, Default)]
pub struct OWXParserConfiguration {
pub struct RDFParserConfiguration {
pub lax: bool
}

impl<A: ForIRI, AA: ForIndex<A>> ParserOutput<A, AA> {
Expand All @@ -49,29 +65,18 @@ impl<A: ForIRI, AA: ForIndex<A>> ParserOutput<A, AA> {
Option<IncompleteParse<A>>,
) {
match self {
ParserOutput::OFNParser(o, m) => (o, Some(m), None),
ParserOutput::OWXParser(o, m) => (o, Some(m), None),
ParserOutput::RDFParser(o, i) => (o.into(), None, Some(i)),
}
}
}

impl<A: ForIRI, AA: ForIndex<A>> From<(SetOntology<A>, PrefixMapping)> for ParserOutput<A, AA> {
fn from(sop: (SetOntology<A>, PrefixMapping)) -> ParserOutput<A, AA> {
ParserOutput::OWXParser(sop.0, sop.1)
}
}

impl<A: ForIRI, AA: ForIndex<A>> From<(RDFOntology<A, AA>, IncompleteParse<A>)>
for ParserOutput<A, AA>
{
fn from(rop: (RDFOntology<A, AA>, IncompleteParse<A>)) -> ParserOutput<A, AA> {
ParserOutput::RDFParser(rop.0, rop.1)
}
}

impl<A: ForIRI, AA: ForIndex<A>> From<ParserOutput<A, AA>> for SetOntology<A> {
fn from(p: ParserOutput<A, AA>) -> SetOntology<A> {
match p {
ParserOutput::OFNParser(so, _) => so,
ParserOutput::OWXParser(so, _) => so,
ParserOutput::RDFParser(rdfo, _) => rdfo.into(),
}
Expand All @@ -81,6 +86,7 @@ impl<A: ForIRI, AA: ForIndex<A>> From<ParserOutput<A, AA>> for SetOntology<A> {
impl<A: ForIRI, AA: ForIndex<A>> From<ParserOutput<A, AA>> for ComponentMappedOntology<A, AA> {
fn from(p: ParserOutput<A, AA>) -> ComponentMappedOntology<A, AA> {
match p {
ParserOutput::OFNParser(so, _) => so.into(),
ParserOutput::OWXParser(so, _) => so.into(),
ParserOutput::RDFParser(rdfo, _) => rdfo.into(),
}
Expand Down

0 comments on commit cfdd169

Please sign in to comment.