From 1dc74822b1d2d94424ccdf52d037d1523c469824 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Sat, 27 Aug 2022 13:37:31 +0200 Subject: [PATCH 1/6] Added spo arguments to get_unabbreviated_triples() --- ontopy/ontology.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ontopy/ontology.py b/ontopy/ontology.py index ee0887896..cd391d501 100644 --- a/ontopy/ontology.py +++ b/ontopy/ontology.py @@ -252,12 +252,16 @@ def __eq__(self, other): other.get_unabbreviated_triples(blank="_:b") ) - def get_unabbreviated_triples(self, blank=None): + def get_unabbreviated_triples( + self, subject=None, predicate=None, obj=None, blank=None + ): """Returns all triples unabbreviated. If `label` is given, it will be used to represent blank nodes. """ - return World.get_unabbreviated_triples(self, blank=blank) + return World.get_unabbreviated_triples( + self, subject=subject, predicate=predicate, obj=obj, blank=blank + ) def get_by_label( self, label: str, label_annotations: str = None, prefix: str = None From 932c2f39a96df81ddde3c5b6b1404b553f4cbe12 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Sat, 27 Aug 2022 14:00:28 +0200 Subject: [PATCH 2/6] Made get_unabbreviated_triples() on an ontology, only return the triples in the given ontology. --- ontopy/ontology.py | 49 +++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/ontopy/ontology.py b/ontopy/ontology.py index cd391d501..e7ac87bf3 100644 --- a/ontopy/ontology.py +++ b/ontopy/ontology.py @@ -122,6 +122,23 @@ def get_ontology(self, base_iri="emmo-inferred"): return onto + def _unabbreviate(self, abb, blank=None): + """Return unabbreviation of `abb`. + + The `abb` argument is normally be an integer corresponding to + a store id. If it is not an integer, it is assumed to already + be unabbreviated and returned as is. + + If `blank` is given, it will be used to represent blank nodes + (corresponding to a negative store id). + """ + if isinstance(abb, int): + # negative storid corresponds to blank nodes + if abb >= 0: + return super()._unabbreviate(abb) + return BlankNode(self, abb) if blank is None else blank + return abb + def get_unabbreviated_triples( self, subject=None, predicate=None, obj=None, blank=None ): @@ -133,17 +150,12 @@ def get_unabbreviated_triples( If `blank` is given, it will be used to represent blank nodes. """ - - def _unabbreviate(i): - if isinstance(i, int): - # negative storid corresponds to blank nodes - if i >= 0: - return self._unabbreviate(i) - return BlankNode(self, i) if blank is None else blank - return i - for s, p, o in self.get_triples(subject, predicate, obj): - yield (_unabbreviate(s), _unabbreviate(p), _unabbreviate(o)) + yield ( + self._unabbreviate(s, blank=blank), + self._unabbreviate(p, blank=blank), + self._unabbreviate(o, blank=blank), + ) class Ontology(owlready2.Ontology): # pylint: disable=too-many-public-methods @@ -255,17 +267,21 @@ def __eq__(self, other): def get_unabbreviated_triples( self, subject=None, predicate=None, obj=None, blank=None ): - """Returns all triples unabbreviated. + """Returns all matching triples unabbreviated. - If `label` is given, it will be used to represent blank nodes. + If `blank` is given, it will be used to represent blank nodes. """ - return World.get_unabbreviated_triples( - self, subject=subject, predicate=predicate, obj=obj, blank=blank - ) + # pylint: disable=invalid-name + for s, p, o in self.get_triples(subject, predicate, obj): + yield ( + self.world._unabbreviate(s, blank=blank), + self.world._unabbreviate(p, blank=blank), + self.world._unabbreviate(o, blank=blank), + ) def get_by_label( self, label: str, label_annotations: str = None, prefix: str = None - ): # pylint: disable=too-many-arguments,too-many-branches + ): """Returns entity with label annotation `label`. Args: @@ -290,6 +306,7 @@ def get_by_label( The current implementation also supports "*" as a wildcard matching any number of characters. This may change in the future. """ + # pylint: disable=too-many-arguments,too-many-branches if not isinstance(label, str): raise TypeError( f"Invalid label definition, must be a string: {label!r}" From d815d4f14ba45a974ed897920db4bd43d60462ae Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Sat, 27 Aug 2022 14:19:16 +0200 Subject: [PATCH 3/6] Made _unabbrivate() a help function --- ontopy/ontology.py | 53 +++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/ontopy/ontology.py b/ontopy/ontology.py index e7ac87bf3..8e920a97f 100644 --- a/ontopy/ontology.py +++ b/ontopy/ontology.py @@ -4,7 +4,7 @@ If desirable some of these additions may be moved back into owlready2. """ # pylint: disable=too-many-lines,fixme,arguments-differ,protected-access -from typing import TYPE_CHECKING, Union, Sequence +from typing import TYPE_CHECKING, Optional, Union, Sequence import os import itertools import inspect @@ -122,23 +122,6 @@ def get_ontology(self, base_iri="emmo-inferred"): return onto - def _unabbreviate(self, abb, blank=None): - """Return unabbreviation of `abb`. - - The `abb` argument is normally be an integer corresponding to - a store id. If it is not an integer, it is assumed to already - be unabbreviated and returned as is. - - If `blank` is given, it will be used to represent blank nodes - (corresponding to a negative store id). - """ - if isinstance(abb, int): - # negative storid corresponds to blank nodes - if abb >= 0: - return super()._unabbreviate(abb) - return BlankNode(self, abb) if blank is None else blank - return abb - def get_unabbreviated_triples( self, subject=None, predicate=None, obj=None, blank=None ): @@ -152,9 +135,9 @@ def get_unabbreviated_triples( """ for s, p, o in self.get_triples(subject, predicate, obj): yield ( - self._unabbreviate(s, blank=blank), - self._unabbreviate(p, blank=blank), - self._unabbreviate(o, blank=blank), + _unabbreviate(self, s, blank=blank), + _unabbreviate(self, p, blank=blank), + _unabbreviate(self, o, blank=blank), ) @@ -274,9 +257,9 @@ def get_unabbreviated_triples( # pylint: disable=invalid-name for s, p, o in self.get_triples(subject, predicate, obj): yield ( - self.world._unabbreviate(s, blank=blank), - self.world._unabbreviate(p, blank=blank), - self.world._unabbreviate(o, blank=blank), + _unabbreviate(self, s, blank=blank), + _unabbreviate(self, p, blank=blank), + _unabbreviate(self, o, blank=blank), ) def get_by_label( @@ -1637,3 +1620,25 @@ def flatten(items): yield sub_item else: yield item + + +def _unabbreviate( + onto: Union[World, Ontology], + storid: Union[int, str], + blank: Optional[str] = None, +): + """Return unabbreviation of `storid`. + + The `storid` argument is normally be an integer corresponding to + a store id. If it is not an integer, it is assumed to already + be unabbreviated and returned as is. + + If `blank` is given, it will be used to represent blank nodes + (corresponding to a negative store id). + """ + if isinstance(storid, int): + # negative storid corresponds to blank nodes + if storid >= 0: + return onto._unabbreviate(storid) + return BlankNode(onto, storid) if blank is None else blank + return storid From bab1e81b514aff2bb0f9d54fc8421ea644c8e3e5 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Sat, 27 Aug 2022 19:44:26 +0200 Subject: [PATCH 4/6] Made a separate help function out of get_unabbreviated_triples() --- ontopy/ontology.py | 62 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 16 deletions(-) diff --git a/ontopy/ontology.py b/ontopy/ontology.py index 8e920a97f..4fd1fe76b 100644 --- a/ontopy/ontology.py +++ b/ontopy/ontology.py @@ -133,12 +133,9 @@ def get_unabbreviated_triples( If `blank` is given, it will be used to represent blank nodes. """ - for s, p, o in self.get_triples(subject, predicate, obj): - yield ( - _unabbreviate(self, s, blank=blank), - _unabbreviate(self, p, blank=blank), - _unabbreviate(self, o, blank=blank), - ) + return _get_unabbreviated_triples( + self, subject=subject, predicate=predicate, obj=obj, blank=blank + ) class Ontology(owlready2.Ontology): # pylint: disable=too-many-public-methods @@ -255,12 +252,9 @@ def get_unabbreviated_triples( If `blank` is given, it will be used to represent blank nodes. """ # pylint: disable=invalid-name - for s, p, o in self.get_triples(subject, predicate, obj): - yield ( - _unabbreviate(self, s, blank=blank), - _unabbreviate(self, p, blank=blank), - _unabbreviate(self, o, blank=blank), - ) + return _get_unabbreviated_triples( + self, subject=subject, predicate=predicate, obj=obj, blank=blank + ) def get_by_label( self, label: str, label_annotations: str = None, prefix: str = None @@ -396,8 +390,13 @@ def add_label_annotation(self, iri): if self._label_annotations is None: self._label_annotations = [] label_annotation = iri if hasattr(iri, "storid") else self.world[iri] - if not label_annotation: - raise ValueError(f"IRI not in ontology: {iri}") + if label_annotation is None: + warnings.warn(f"adding new IRI to ontology: {iri}") + name = iri.rsplit("/")[-1].rsplit("#")[-1] + with self: + label_annotation = types.new_class( + name, (owlready2.AnnotationProperty,) + ) if label_annotation not in self._label_annotations: self._label_annotations.append(label_annotation) @@ -536,7 +535,7 @@ def _load( # pylint: disable=too-many-arguments,too-many-locals,too-many-branch """Help function for load().""" web_protocol = "http://", "https://", "ftp://" - url = filename if filename else self.base_iri.rstrip("/#") + url = str(filename) if filename else self.base_iri.rstrip("/#") if url.startswith(web_protocol): baseurl = os.path.dirname(url) catalogurl = baseurl + "/" + catalog_file @@ -1161,7 +1160,7 @@ class prefLabel(owlready2.label): counter = 0 while f"{self.base_iri}{name_prefix}{counter}" in self: counter += 1 - obj.name = name_prefix + str(counter) + obj.name = f"{name_prefix}{counter}" elif name_policy is not None: raise TypeError(f"invalid name_policy: {name_policy!r}") @@ -1642,3 +1641,34 @@ def _unabbreviate( return onto._unabbreviate(storid) return BlankNode(onto, storid) if blank is None else blank return storid + + +def _get_unabbreviated_triples( + self, subject=None, predicate=None, obj=None, blank=None +): + """Returns all matching triples unabbreviated. + + If `blank` is given, it will be used to represent blank nodes. + """ + # pylint: disable=invalid-name + abb = ( + None if subject is None else self._abbreviate(subject), + None if predicate is None else self._abbreviate(predicate), + None if obj is None else self._abbreviate(obj), + ) + for s, p, o in self._get_obj_triples_spo_spo(*abb): + yield ( + _unabbreviate(self, s, blank=blank), + _unabbreviate(self, p, blank=blank), + _unabbreviate(self, o, blank=blank), + ) + for s, p, o, d in self._get_data_triples_spod_spod(*abb, d=""): + yield ( + _unabbreviate(self, s, blank=blank), + _unabbreviate(self, p, blank=blank), + f'"{o}"{d}' + if isinstance(d, str) + else f'"{o}"^^{_unabbreviate(self, d)}' + if d + else o, + ) From 5736966eab09dfd2c26cf3b9e801c412da8e4833 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Sat, 27 Aug 2022 19:52:39 +0200 Subject: [PATCH 5/6] Fixed docstrings --- ontopy/ontology.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ontopy/ontology.py b/ontopy/ontology.py index 4fd1fe76b..a16fa56db 100644 --- a/ontopy/ontology.py +++ b/ontopy/ontology.py @@ -1626,7 +1626,7 @@ def _unabbreviate( storid: Union[int, str], blank: Optional[str] = None, ): - """Return unabbreviation of `storid`. + """Help function returning unabbreviation of `storid`. The `storid` argument is normally be an integer corresponding to a store id. If it is not an integer, it is assumed to already @@ -1646,7 +1646,7 @@ def _unabbreviate( def _get_unabbreviated_triples( self, subject=None, predicate=None, obj=None, blank=None ): - """Returns all matching triples unabbreviated. + """Help function returning all matching triples unabbreviated. If `blank` is given, it will be used to represent blank nodes. """ From e42bbb877d50f7d22225583b5281b0f67e0f8303 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Sat, 27 Aug 2022 20:38:47 +0200 Subject: [PATCH 6/6] cleanup --- ontopy/ontology.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ontopy/ontology.py b/ontopy/ontology.py index a16fa56db..5e11d1d48 100644 --- a/ontopy/ontology.py +++ b/ontopy/ontology.py @@ -393,10 +393,9 @@ def add_label_annotation(self, iri): if label_annotation is None: warnings.warn(f"adding new IRI to ontology: {iri}") name = iri.rsplit("/")[-1].rsplit("#")[-1] + bases = (owlready2.AnnotationProperty,) with self: - label_annotation = types.new_class( - name, (owlready2.AnnotationProperty,) - ) + label_annotation = types.new_class(name, bases) if label_annotation not in self._label_annotations: self._label_annotations.append(label_annotation)