use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use crate::owl::{AnnotationAssertion, Axiom, Declaration, IRIBuilder, ResourceId, IRI};
#[cfg(feature = "wasm")]
#[wasm_bindgen::prelude::wasm_bindgen]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Ontology {
pub(crate) iri: IRI,
pub(crate) imports: HashMap<String, IRI>,
pub(crate) owl: crate::owl::Ontology,
}
#[cfg(not(feature = "wasm"))]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Ontology {
pub(crate) iri: IRI,
pub(crate) imports: HashMap<String, IRI>,
pub(crate) owl: crate::owl::Ontology,
}
impl Ontology {
pub fn new(iri: IRI) -> Self {
Self {
iri,
imports: Default::default(),
owl: crate::owl::Ontology::new(vec![], vec![]),
}
}
pub fn iri(&self) -> &IRI {
&self.iri
}
pub fn imports(&self) -> &HashMap<String, IRI> {
&self.imports
}
pub fn push_import(&mut self, name: &str, iri: IRI) -> Option<IRI> {
self.imports.insert(name.into(), iri)
}
pub fn iri_builder(&self) -> IRIBuilder {
IRIBuilder::construct(self.iri.clone(), &self.imports)
}
pub fn declarations(&self) -> &Vec<Declaration> {
&self.owl.declarations
}
pub fn axioms(&self) -> &Vec<Axiom> {
&self.owl.axioms
}
pub fn annotation_assertions_for_resource_id(
&self,
resource_id: &ResourceId,
) -> Vec<AnnotationAssertion> {
let mut annotations = vec![];
for axiom in self.axioms() {
if let Some(axiom_annotations) = match axiom {
Axiom::AnnotationAssertion(apa) => {
if apa.resource_ids.contains(resource_id) {
Some(apa.annotations.clone())
} else {
None
}
}
Axiom::DataPropertyAssertion(dpa) => {
if dpa.resource_ids.contains(resource_id) {
Some(dpa.annotations.clone())
} else {
None
}
}
Axiom::ObjectPropertyAssertion(opa) => {
if opa.resource_ids.contains(resource_id) {
Some(opa.annotations.clone())
} else {
None
}
}
_ => {
None
}
} {
for annotation in &axiom_annotations {
annotations.push(annotation.clone().to_assertion(resource_id.to_owned()));
}
}
}
for axiom in self.axioms() {
if let Axiom::AnnotationAssertion(annotation_assertion) = axiom {
if &annotation_assertion.subject == resource_id {
annotations.push(annotation_assertion.clone());
}
}
}
annotations.dedup();
annotations
}
}
impl Ontology {
pub fn set_owl(&mut self, owl: crate::owl::Ontology) {
self.owl = owl
}
pub fn push_axiom(&mut self, axiom: Axiom) {
self.owl.axioms.push(axiom)
}
pub fn push_declaration(&mut self, declaration: Declaration) {
self.owl.declarations.push(declaration)
}
}
impl From<(IRI, crate::owl::Ontology)> for Ontology {
fn from((iri, owl): (IRI, crate::owl::Ontology)) -> Self {
Self {
iri,
imports: Default::default(),
owl,
}
}
}