1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#![doc = include_str!("../README.md")]
#[macro_use]
extern crate thiserror;
#[macro_use]
extern crate pest_derive;
extern crate curie;
extern crate horned_owl;
extern crate pest;
mod as_ofn;
mod error;
mod from_ofn;
mod from_pair;
mod parser;
use std::collections::HashMap;
use std::fmt::Debug;
use std::fmt::Formatter;
use std::fmt::Write;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use curie::PrefixMapping;
use horned_owl::model::AnnotatedAxiom;
use horned_owl::model::Build;
use horned_owl::ontology::set::SetOntology;
pub use self::as_ofn::AsFunctional;
pub use self::as_ofn::Functional;
pub use self::error::Error;
pub use self::error::Result;
pub use self::from_ofn::FromFunctional;
#[derive(Default)]
pub struct Context<'a> {
build: Option<&'a Build>,
prefixes: Option<&'a PrefixMapping>,
}
impl<'a> Debug for Context<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
f.debug_struct("Context")
.field("build", &self.build)
.field(
"prefixes",
&match &self.prefixes {
None => format!("{}", "None"),
Some(p) => {
format!("{:?}", p.mappings().collect::<HashMap<_, _>>())
}
},
)
.finish()
}
}
impl<'a> Context<'a> {
pub fn new<B, P>(build: B, prefixes: P) -> Self
where
B: Into<Option<&'a Build>>,
P: Into<Option<&'a PrefixMapping>>,
{
Self {
build: build.into(),
prefixes: prefixes.into(),
}
}
pub fn iri<S: Into<String>>(&self, s: S) -> horned_owl::model::IRI
where
S: Into<String>,
{
match self.build {
Some(b) => b.iri(s),
None => Build::default().iri(s),
}
}
}
impl<'a> From<&'a Build> for Context<'a> {
fn from(build: &'a Build) -> Context<'a> {
Self {
build: Some(build),
prefixes: None,
}
}
}
impl<'a> From<&'a PrefixMapping> for Context<'a> {
fn from(prefixes: &'a PrefixMapping) -> Context<'a> {
Self {
build: None,
prefixes: Some(prefixes),
}
}
}
#[inline]
pub fn from_str<S: AsRef<str>>(src: S) -> Result<(SetOntology, PrefixMapping)> {
FromFunctional::from_ofn(src.as_ref())
}
#[inline]
pub fn from_reader<R: Read>(mut r: R) -> Result<(SetOntology, PrefixMapping)> {
let mut s = String::new();
r.read_to_string(&mut s)?;
from_str(s)
}
#[inline]
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<(SetOntology, PrefixMapping)> {
File::open(path).map_err(Error::from).and_then(from_reader)
}
#[inline]
pub fn to_string<'a, O, P>(ontology: O, prefixes: P) -> String
where
O: Iterator<Item = &'a AnnotatedAxiom>,
P: Into<Option<&'a PrefixMapping>>,
{
let p = prefixes.into();
let mut dest = String::new();
if let Some(pm) = p {
write!(dest, "{}", pm.as_ofn()).expect("cannot fail to write to String");
}
let ctx = Context::new(None, p);
for axiom in ontology {
writeln!(dest, "{}", axiom.as_ofn_ctx(&ctx)).expect("cannot fail to write to String");
}
dest
}