Skip to content

Commit

Permalink
feat: Parse annotations on object properties
Browse files Browse the repository at this point in the history
  • Loading branch information
floers committed Feb 7, 2023
1 parent 3a89b67 commit 50bffcb
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/parser/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub(crate) enum CollectedAnnotationKey<'a> {
Iri(Cow<'a, str>),
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct CollectedAnnotation<'a> {
pub(crate) subject: Cow<'a, str>,
pub(crate) predicate: Cow<'a, str>,
Expand All @@ -45,6 +45,7 @@ pub(crate) struct OntologyCollector<'a> {

// annotations on things
annotations: HashMap<CollectedAnnotationKey<'a>, CollectedAnnotation<'a>>,
annotations_rev: HashMap<CollectedAnnotation<'a>, CollectedAnnotationKey<'a>>,
pub blank_nodes: HashMap<RdfBlankNode, CollectedBlankNode<'a>>,

axiom_index: HashMap<(String, String, String), usize>,
Expand Down Expand Up @@ -158,7 +159,8 @@ impl<'a> OntologyCollector<'a> {
key: CollectedAnnotationKey<'a>,
value: CollectedAnnotation<'a>,
) {
self.annotations.insert(key, value);
self.annotations.insert(key.clone(), value.clone());
self.annotations_rev.insert(value, key);
}

pub(crate) fn annotation(
Expand All @@ -168,6 +170,13 @@ impl<'a> OntologyCollector<'a> {
self.annotations.get(&ann)
}

pub(crate) fn annotation_on_triple(
&self,
ann: &CollectedAnnotation<'a>,
) -> Option<&CollectedAnnotationKey<'a>> {
self.annotations_rev.get(ann)
}

pub(crate) fn ontology(self) -> Ontology {
let mut o = Ontology::new(self.iri.unwrap());

Expand Down
55 changes: 55 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,61 @@ mod tests {
);
}

#[test]
fn annotations_on_object_property_assertions() {
env_logger::try_init().ok();
let turtle = r##"
<http://field33.com/query_result/00000000-0000-0000-0000-000000000000> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Ontology> .
<http://field33.com/query_result/2060abc6-f459-47ed-9248-0b7fe12c971c> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Ontology> .
<http://www.w3.org/2000/01/rdf-schema#label> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#AnnotationProperty> .
<http://field33.com/ontologies/@fld33/relations/Has> <http://www.w3.org/2000/01/rdf-schema#label> "Has"@en .
<http://field33.com/ontologies/@fld33/relations/Has> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://field33.com/org/org_evlGiemVNyAUTJ7D/node/f63c8031-a7d9-40db-ae87-be04c99537c7> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Axiom> .
<http://field33.com/org/org_evlGiemVNyAUTJ7D/node/f63c8031-a7d9-40db-ae87-be04c99537c7> <http://www.w3.org/2002/07/owl#annotatedTarget> <http://field33.com/org/org_evlGiemVNyAUTJ7D/node/fe6fdda1-fc21-4b99-9269-8c19fc6359b8> .
<http://field33.com/org/org_evlGiemVNyAUTJ7D/node/f63c8031-a7d9-40db-ae87-be04c99537c7> <http://www.w3.org/2002/07/owl#annotatedProperty> <http://field33.com/ontologies/@fld33/relations/Has> .
<http://field33.com/org/org_evlGiemVNyAUTJ7D/node/f63c8031-a7d9-40db-ae87-be04c99537c7> <http://www.w3.org/2002/07/owl#annotatedSource> <http://field33.com/org/org_evlGiemVNyAUTJ7D/node/1afda1af-bbde-48de-a5d7-5f43d389b2a6> .
<http://field33.com/org/org_evlGiemVNyAUTJ7D/node/1afda1af-bbde-48de-a5d7-5f43d389b2a6> <http://field33.com/ontologies/@fld33/relations/Has> <http://field33.com/org/org_evlGiemVNyAUTJ7D/node/fe6fdda1-fc21-4b99-9269-8c19fc6359b8> .
"##;

harriet::TurtleDocument::parse_full(turtle).unwrap();
let o = Ontology::parse(
turtle,
ParserOptionsBuilder::default()
.known(Declaration::AnnotationProperty {
iri: IRI::new("http://query-server.field33.com/ontology/query-field")
.unwrap()
.into(),
annotations: vec![],
})
.build(),
)
.unwrap();
println!("{:#?}", o);
assert_eq!(o.declarations().len(), 2);
assert_eq!(o.axioms().len(), 2);
assert_eq!(
o.axioms()[1],
Axiom::ObjectPropertyAssertion(ObjectPropertyAssertion::new(
IRI::new("http://field33.com/ontologies/@fld33/relations/Has")
.unwrap()
.into(),
IRI::new("http://field33.com/org/org_evlGiemVNyAUTJ7D/node/1afda1af-bbde-48de-a5d7-5f43d389b2a6")
.unwrap()
.into(),
IRI::new("http://field33.com/org/org_evlGiemVNyAUTJ7D/node/fe6fdda1-fc21-4b99-9269-8c19fc6359b8")
.unwrap()
.into(),
vec![Annotation::new(
well_known::owl_annotatedSource().into(),
IRI::new("http://field33.com/org/org_evlGiemVNyAUTJ7D/node/f63c8031-a7d9-40db-ae87-be04c99537c7").unwrap().into(),
vec![]
)]
))
);
}

#[test]
fn class_assertion() {
env_logger::try_init().ok();
Expand Down
23 changes: 22 additions & 1 deletion src/parser/object_property_assertions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use super::collector::CollectedAnnotation;
use super::collector::CollectedAnnotationKey;
use super::collector::MatcherHandler;
use crate::error::Error;
use crate::get_vars;
use crate::owl::well_known;
use crate::owl::Annotation;
use crate::owl::ObjectPropertyAssertion;
use crate::owl::IRI;
use crate::parser::matcher::RdfMatcher;
Expand Down Expand Up @@ -33,12 +37,29 @@ pub(crate) fn push(
if o.object_property_declaration(&predicate).is_some()
|| options.is_object_prop(&predicate)
{
let mut annotations = Vec::new();
if let Some(CollectedAnnotationKey::Iri(iri)) = o
.annotation_on_triple(&CollectedAnnotation {
subject: subject.as_str().into(),
predicate: predicate.as_str().into(),
object: object.as_str().into(),
})
{
if let Ok(iri) = IRI::new(iri) {
annotations.push(Annotation {
annotations: vec![],
iri: well_known::owl_annotatedSource().into(),
value: iri.into(),
})
}
}

o.push_axiom(
ObjectPropertyAssertion::new(
predicate.into(),
subject.into(),
object.into(),
vec![],
annotations,
)
.into(),
)
Expand Down

0 comments on commit 50bffcb

Please sign in to comment.