Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
# Conflicts:
#	Cargo.toml
  • Loading branch information
b-gehrke committed Sep 30, 2024
2 parents 2172c8a + 7c85ad6 commit 2688885
Show file tree
Hide file tree
Showing 21 changed files with 366 additions and 21 deletions.
16 changes: 14 additions & 2 deletions gen_pyi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import glob
import inspect
import os
import re
import typing
Expand All @@ -24,7 +25,8 @@
f.write(", ".join([f'"{n}"' for n in pyhornedowl_members]))
f.write("]\n")

implemented_magic = [f"__{x}__" for x in ["invert", "and", "or", "invert"]]
implemented_magic = [f"__{x}__" for x in
["invert", "and", "or", "invert", "iter", "getitem", "setitem", "contains", "len", "delitem"]]

with open("pyhornedowl/__init__.pyi", "w") as f:
f.write("import typing\n")
Expand Down Expand Up @@ -67,7 +69,10 @@
for line in doc.splitlines():
f.write(f" {line}\n")
f.write(" \"\"\"\n")
elif hasattr(member, "__doc__"):

continue

if hasattr(member, "__doc__"):
doc = member.__doc__
if doc is not None:
lines = doc.splitlines()
Expand All @@ -87,6 +92,13 @@
f.write(f" {sign}\n")
doc = "\n".join([f" {l}" for l in lines[annotations_end+2:]])
f.write(f' """\n{doc}\n """\n\n')

continue

if callable(member):
f.write(f" def {member_name}{inspect.signature(member)}:\n ...\n\n")

continue


f.write("\n")
Expand Down
21 changes: 20 additions & 1 deletion pyhornedowl/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ from typing_extensions import deprecated

import model


class PyIndexedOntology:
"""
Represents a loaded ontology.
Expand Down Expand Up @@ -305,6 +306,24 @@ class IndexCreationStrategy:
"""

class PrefixMapping:
def __iter__(self, /):
...

def __len__(self, /):
...

def __getitem__(self, key, /):
...

def __setitem__(self, key, value, /):
...

def __delitem__(self, key, /):
...

def __contains__(self, key, /):
...

def add_default_prefix_names(self) -> None:
"""
Adds the prefix for rdf, rdfs, xsd, and owl
Expand All @@ -329,7 +348,7 @@ class PrefixMapping:
"""
...

def shring_iri(self, iri: str) -> str:
def shrink_iri(self, iri: str) -> str:
"""
Shrinks an absolute IRI to a CURIE. Throws a ValueError on failure
"""
Expand Down
29 changes: 28 additions & 1 deletion src/prefix_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,35 @@ impl From<PrefixMapping> for curie::PrefixMapping {
}
}

#[pyclass]
struct Iter {
inner: std::vec::IntoIter<(String, String)>
}

#[pymethods]
impl Iter {
fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
slf
}

fn __next__(mut slf: PyRefMut<'_, Self>) -> Option<(String, String)> {
slf.inner.next()
}
}

#[pymethods]
impl PrefixMapping {

/// __iter__(self) -> typing.Iterable[typing.Tuple[str, str]]
///
/// Get an iterator over all prefixes
fn __iter__(slf: PyRef<'_, Self>) -> PyResult<Py<Iter>> {
let iter = Iter {
inner: slf.0.mappings().map(|(x,y)| ((x.clone(), y.clone()))).collect::<Vec<(String, String)>>().into_iter(),
};
Py::new(slf.py(), iter)
}

pub fn __getitem__(&self, key: &str) -> PyResult<String> {
self.0
.expand_curie(&curie::Curie::new(Some(key), ""))
Expand Down Expand Up @@ -102,7 +129,7 @@ impl PrefixMapping {
.map_err(to_py_err!("Invalid or unknown prefix"))
}

/// shring_iri(self, iri: str) -> str
/// shrink_iri(self, iri: str) -> str
///
/// Shrinks an absolute IRI to a CURIE. Throws a ValueError on failure
pub fn shrink_iri(&self, iri: &str) -> PyResult<String> {
Expand Down
28 changes: 21 additions & 7 deletions test/resources/gen.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

FILES=("simple" "prefix")

EMPTY_PREFIX="https://example.com/"

for f in "${FILES[@]}"; do
robot convert -i "$SCRIPT_DIR/$f.omn" -o "$SCRIPT_DIR/$f.ofn"
sed -i "1iPrefix(:=<$EMPTY_PREFIX>)" "$SCRIPT_DIR/$f.ofn"

robot convert -i "$SCRIPT_DIR/$f.omn" -o "$SCRIPT_DIR/$f.owx"
sed -i "10i <Prefix name=\"\" IRI=\"$EMPTY_PREFIX\"/>" "$SCRIPT_DIR/$f.owx"

robot convert -i "$SCRIPT_DIR/$f.omn" -o "$SCRIPT_DIR/$f.owl"
# no empty prefix in rdf/xml?

ln -f "$SCRIPT_DIR/$f.omn" "$SCRIPT_DIR/$f.omn.raw"
ln -f "$SCRIPT_DIR/$f.ofn" "$SCRIPT_DIR/$f.ofn.raw"
ln -f "$SCRIPT_DIR/$f.owx" "$SCRIPT_DIR/$f.owx.raw"
ln -f "$SCRIPT_DIR/$f.owl" "$SCRIPT_DIR/$f.owl.raw"
done



robot convert -i "$SCRIPT_DIR/simple.omn" -o "$SCRIPT_DIR/simple.ofn"
robot convert -i "$SCRIPT_DIR/simple.omn" -o "$SCRIPT_DIR/simple.owx"
robot convert -i "$SCRIPT_DIR/simple.omn" -o "$SCRIPT_DIR/simple.owl"
ln -f "$SCRIPT_DIR/simple.omn" "$SCRIPT_DIR/simple.omn.raw"
ln -f "$SCRIPT_DIR/simple.ofn" "$SCRIPT_DIR/simple.ofn.raw"
ln -f "$SCRIPT_DIR/simple.owx" "$SCRIPT_DIR/simple.owx.raw"
ln -f "$SCRIPT_DIR/simple.owl" "$SCRIPT_DIR/simple.owl.raw"
18 changes: 18 additions & 0 deletions test/resources/prefix.ofn
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Prefix(:=<https://example.com/>)
Prefix(ex1:=<https://example.com/1>)
Prefix(ex2:=<https://example.com/2>)
Prefix(ex3:=<https://example.com/3>)
Prefix(owl:=<http://www.w3.org/2002/07/owl#>)
Prefix(rdf:=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>)
Prefix(xml:=<http://www.w3.org/XML/1998/namespace>)
Prefix(xsd:=<http://www.w3.org/2001/XMLSchema#>)
Prefix(rdfs:=<http://www.w3.org/2000/01/rdf-schema#>)


Ontology(
Declaration(Class(<https://example.com/A>))
Declaration(Class(ex1:A))
Declaration(Class(ex2:A))
Declaration(Class(ex3:A))

)
18 changes: 18 additions & 0 deletions test/resources/prefix.ofn.raw
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Prefix(:=<https://example.com/>)
Prefix(ex1:=<https://example.com/1>)
Prefix(ex2:=<https://example.com/2>)
Prefix(ex3:=<https://example.com/3>)
Prefix(owl:=<http://www.w3.org/2002/07/owl#>)
Prefix(rdf:=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>)
Prefix(xml:=<http://www.w3.org/XML/1998/namespace>)
Prefix(xsd:=<http://www.w3.org/2001/XMLSchema#>)
Prefix(rdfs:=<http://www.w3.org/2000/01/rdf-schema#>)


Ontology(
Declaration(Class(<https://example.com/A>))
Declaration(Class(ex1:A))
Declaration(Class(ex2:A))
Declaration(Class(ex3:A))

)
10 changes: 10 additions & 0 deletions test/resources/prefix.omn
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Prefix: : <https://example.com/>
Prefix: ex1: <https://example.com/1>
Prefix: ex2: <https://example.com/2>
Prefix: ex3: <https://example.com/3>
Ontology:
Class: ex1:A
Class: ex2:A
Class: ex3:A
Class: A

10 changes: 10 additions & 0 deletions test/resources/prefix.omn.raw
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Prefix: : <https://example.com/>
Prefix: ex1: <https://example.com/1>
Prefix: ex2: <https://example.com/2>
Prefix: ex3: <https://example.com/3>
Ontology:
Class: ex1:A
Class: ex2:A
Class: ex3:A
Class: A

53 changes: 53 additions & 0 deletions test/resources/prefix.owl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0"?>
<rdf:RDF xmlns="http://www.w3.org/2002/07/owl#"
xml:base="http://www.w3.org/2002/07/owl"
xmlns:ex1="https://example.com/1"
xmlns:ex2="https://example.com/2"
xmlns:ex3="https://example.com/3"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<Ontology/>



<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Classes
//
///////////////////////////////////////////////////////////////////////////////////////
-->




<!-- https://example.com/A -->

<Class rdf:about="https://example.com/A"/>



<!-- https://example.com/1A -->

<Class rdf:about="https://example.com/1A"/>



<!-- https://example.com/2A -->

<Class rdf:about="https://example.com/2A"/>



<!-- https://example.com/3A -->

<Class rdf:about="https://example.com/3A"/>
</rdf:RDF>



<!-- Generated by the OWL API (version 4.5.29) https://github.com/owlcs/owlapi -->

53 changes: 53 additions & 0 deletions test/resources/prefix.owl.raw
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0"?>
<rdf:RDF xmlns="http://www.w3.org/2002/07/owl#"
xml:base="http://www.w3.org/2002/07/owl"
xmlns:ex1="https://example.com/1"
xmlns:ex2="https://example.com/2"
xmlns:ex3="https://example.com/3"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<Ontology/>



<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Classes
//
///////////////////////////////////////////////////////////////////////////////////////
-->




<!-- https://example.com/A -->

<Class rdf:about="https://example.com/A"/>



<!-- https://example.com/1A -->

<Class rdf:about="https://example.com/1A"/>



<!-- https://example.com/2A -->

<Class rdf:about="https://example.com/2A"/>



<!-- https://example.com/3A -->

<Class rdf:about="https://example.com/3A"/>
</rdf:RDF>



<!-- Generated by the OWL API (version 4.5.29) https://github.com/owlcs/owlapi -->

34 changes: 34 additions & 0 deletions test/resources/prefix.owx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0"?>
<Ontology xmlns="http://www.w3.org/2002/07/owl#"
xml:base="http://www.w3.org/2002/07/owl#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<Prefix name="ex1" IRI="https://example.com/1"/>
<Prefix name="ex2" IRI="https://example.com/2"/>
<Prefix name="" IRI="https://example.com/"/>
<Prefix name="ex3" IRI="https://example.com/3"/>
<Prefix name="owl" IRI="http://www.w3.org/2002/07/owl#"/>
<Prefix name="rdf" IRI="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>
<Prefix name="xml" IRI="http://www.w3.org/XML/1998/namespace"/>
<Prefix name="xsd" IRI="http://www.w3.org/2001/XMLSchema#"/>
<Prefix name="rdfs" IRI="http://www.w3.org/2000/01/rdf-schema#"/>
<Declaration>
<Class IRI="https://example.com/A"/>
</Declaration>
<Declaration>
<Class abbreviatedIRI="ex1:A"/>
</Declaration>
<Declaration>
<Class abbreviatedIRI="ex2:A"/>
</Declaration>
<Declaration>
<Class abbreviatedIRI="ex3:A"/>
</Declaration>
</Ontology>



<!-- Generated by the OWL API (version 4.5.29) https://github.com/owlcs/owlapi -->

Loading

0 comments on commit 2688885

Please sign in to comment.