From b4f92eceabee57b45a8818e54d0acd4f8c7d6325 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn?= <5106696+b-gehrke@users.noreply.github.com> Date: Mon, 11 Nov 2024 15:00:26 +0100 Subject: [PATCH] Fixed removal of empty prefix --- src/ontology.rs | 3 ++- src/prefix_mapping.rs | 9 +++++++++ test/test_id.py | 21 +++++++++++---------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/ontology.rs b/src/ontology.rs index cc51b23..8b93b40 100644 --- a/src/ontology.rs +++ b/src/ontology.rs @@ -153,6 +153,7 @@ impl PyIndexedOntology { ) -> PyResult> { let iri: String = self.iri(py, iri, iri_is_absolute)?.into(); let mapping = self.mapping.borrow_mut(py); + let res = mapping.0.shrink_iri(iri.as_str()); if let Ok(curie) = res { @@ -1072,7 +1073,7 @@ impl PyIndexedOntology { amo.insert(component.clone()); } - let mapping = self.mapping.borrow_mut(py); + let mapping = self.mapping.borrow(py); let result = match serialization { ResourceType::OFN => { diff --git a/src/prefix_mapping.rs b/src/prefix_mapping.rs index 7d05661..6eb6960 100644 --- a/src/prefix_mapping.rs +++ b/src/prefix_mapping.rs @@ -118,6 +118,15 @@ impl PrefixMapping { /// Remove a prefix from the mapping. pub fn remove_prefix(&mut self, prefix: &str) { self.0.remove_prefix(prefix); + + if prefix == "" { + let mut new_mapping = curie::PrefixMapping::default(); + for (p, v) in self.0.mappings() { + new_mapping.add_prefix(p, v).expect("Cannot happen since self.0 contains only valid prefix mappings"); + } + + self.0 = new_mapping; + } } /// expand_curie(self, curie: str) -> str diff --git a/test/test_id.py b/test/test_id.py index b23af4d..b3d5937 100644 --- a/test/test_id.py +++ b/test/test_id.py @@ -1,5 +1,6 @@ import unittest +import pyhornedowl from test_base import simple_ontology @@ -15,29 +16,29 @@ def test_id_from_iri_empty(self): self.assertNotEqual(expected, actual) def test_id_from_absolute(self): - o = simple_ontology() - o.prefix_mapping.add_prefix("ex", "https://example.com/") + o = pyhornedowl.PyIndexedOntology() + o.prefix_mapping.add_prefix("EX", "https://example.com/") - expected = ":A" + expected = "EX:A" actual = o.get_id_for_iri("https://example.com/A") self.assertEqual(expected, actual) def test_id_from_curie_empty_prefix(self): - o = simple_ontology() - o.prefix_mapping.add_prefix("ex", "https://example.com/") + o = pyhornedowl.PyIndexedOntology() + o.prefix_mapping.add_prefix("", "https://example.com/") - expected = ":A" + expected = "A" actual = o.get_id_for_iri(":A") self.assertEqual(expected, actual) def test_id_from_curie_defined_prefix(self): - o = simple_ontology() - o.prefix_mapping.add_prefix("ex", "https://example.com/") + o = pyhornedowl.PyIndexedOntology() + o.prefix_mapping.add_prefix("EX", "https://example.com/") - expected = ":A" - actual = o.get_id_for_iri("ex:A") + expected = "EX:A" + actual = o.get_id_for_iri("EX:A") self.assertEqual(expected, actual)