From 0d71cd5157965c2b130edfb69318e82f9757bccb Mon Sep 17 00:00:00 2001 From: francescalb Date: Sat, 21 Aug 2021 10:40:57 +0200 Subject: [PATCH 1/4] Added choice for specifying namespace in get_by_label and get_by_label_all --- emmo/ontology.py | 47 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/emmo/ontology.py b/emmo/ontology.py index 2b4b46dc4..e28afc32c 100644 --- a/emmo/ontology.py +++ b/emmo/ontology.py @@ -179,42 +179,59 @@ def __objclass__(self): # Play nice with inspect... pass - def get_by_label(self, value, label_annotations=None): - """Returns entity by label annotation value `value`. + def get_by_label(self, label, label_annotations=None, namespace=None): + """Returns entity with label annotation `label`. `label_annotations` is a sequence of label annotation names to look up. Defaults to the `label_annotations` property. + If `namespace` is provided, it should be the last component of + the base iri of an ontology (with trailing slash (/) or hash + (#) stripped off). The search for a matching label will be + limited to this namespace. + If several entities have the same label, only the one which is - found first is returned. + found first is returned.Use get_by_label_all() to get all matches. A NoSuchLabelError is raised if `label` cannot be found. Note ---- The current implementation also supports "*" as a wildcard - matching any number of characters. + matching any number of characters. This may change in the future. """ + if 'namespaces' in self.__dict__: + if namespace: + if namespace in self.namespaces: + for e in self.get_by_label_all( + label, label_annotations=label_annotations): + if e.namespace == self.namespaces[namespace]: + return e + raise NoSuchLabelError('No label annotations matches "%s" in ' + 'namespace "%s"' % (label, namespace)) + elif label in self.namespaces: + return self.namespaces[label] + if label_annotations is None: annotations = (la.name for la in self.label_annotations) else: annotations = (s.name if hasattr(s, 'storid') else s for s in label_annotations) for key in annotations: - e = self.search_one(**{key: value}) + e = self.search_one(**{key: label}) if e: return e - if self._special_labels and value in self._special_labels: - return self._special_labels[value] + if self._special_labels and label in self._special_labels: + return self._special_labels[label] - e = self.world[self.base_iri + value] + e = self.world[self.base_iri + label] if e: return e - raise NoSuchLabelError('No label annotations matches %s' % value) + raise NoSuchLabelError('No label annotations matches %s' % label) - def get_by_label_all(self, value, label_annotations=None): + def get_by_label_all(self, label, label_annotations=None, namespace=None): """Like get_by_label(), but returns a list with all matching labels. Returns an empty list if no matches could be found. @@ -224,11 +241,13 @@ def get_by_label_all(self, value, label_annotations=None): else: annotations = (s.name if hasattr(s, 'storid') else s for s in label_annotations) - e = self.world.search(**{annotations.__next__(): value}) + e = self.world.search(**{annotations.__next__(): label}) for key in annotations: - e.extend(self.world.search(**{key: value})) - if self._special_labels and value in self._special_labels: - e.append(self._special_labels[value]) + e.extend(self.world.search(**{key: label})) + if self._special_labels and label in self._special_labels: + e.append(self._special_labels[label]) + if namespace: + return [ns for ns in e if ns.namespace.name == namespace] return e def add_label_annotation(self, iri): From 0bdf3b36f70a01a2dc544f02b49359f96e12d70d Mon Sep 17 00:00:00 2001 From: francescalb Date: Sun, 22 Aug 2021 11:45:10 +0200 Subject: [PATCH 2/4] Changed default loading of emmo, emmo-inferred and development to .ttl --- emmo/ontology.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/emmo/ontology.py b/emmo/ontology.py index e28afc32c..35e3494ab 100644 --- a/emmo/ontology.py +++ b/emmo/ontology.py @@ -71,13 +71,13 @@ def get_ontology(self, base_iri='emmo-inferred'): if base_iri == 'emmo': base_iri = ( 'https://raw.githubusercontent.com/emmo-repo/' - 'EMMO/master/emmo.owl') + 'EMMO/master/emmo.ttl') elif base_iri == 'emmo-inferred': base_iri = ( - 'https://emmo-repo.github.io/latest-stable/emmo-inferred.owl') + 'https://emmo-repo.github.io/latest-stable/emmo-inferred.ttl') elif base_iri == 'emmo-development': base_iri = ( - 'https://emmo-repo.github.io/development/emmo-inferred.owl') + 'https://emmo-repo.github.io/development/emmo-inferred.ttl') if base_iri in self.ontologies: onto = self.ontologies[base_iri] From 8ef14d5d70fe6d6bf09d62f17f030704f9a3873b Mon Sep 17 00:00:00 2001 From: francescalb Date: Sun, 22 Aug 2021 11:47:21 +0200 Subject: [PATCH 3/4] changed to search for .ttl from .owl in test_catalog --- emmo/tests/test_catalog.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/emmo/tests/test_catalog.py b/emmo/tests/test_catalog.py index 2a8e221cc..0b2de5786 100644 --- a/emmo/tests/test_catalog.py +++ b/emmo/tests/test_catalog.py @@ -32,10 +32,10 @@ d = read_catalog('https://raw.githubusercontent.com/emmo-repo/EMMO/master/' 'catalog-v001.xml') -assert any(v.endswith('/emmo.owl') for v in d.values()) +assert any(v.endswith('/emmo.ttl') for v in d.values()) d = read_catalog('https://raw.githubusercontent.com/emmo-repo/EMMO/master') -assert any(v.endswith('/emmo.owl') for v in d.values()) +assert any(v.endswith('/emmo.ttl') for v in d.values()) try: read_catalog( @@ -54,7 +54,7 @@ d = read_catalog('https://raw.githubusercontent.com/emmo-repo/EMMO/master/' 'catalog-v001.xml', baseuri='/abc') -assert '/abc/emmo.owl' in d.values() +assert '/abc/emmo.ttl' in d.values() write_catalog(d, 'tmp-catalog.xml') From 44a5abf78ac5a83b92005d64aa29320d29469b99 Mon Sep 17 00:00:00 2001 From: francescalb Date: Sun, 22 Aug 2021 11:51:36 +0200 Subject: [PATCH 4/4] Added loading of owl file in test_load as .ttl isn ow default for emmo --- emmo/tests/test_load.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/emmo/tests/test_load.py b/emmo/tests/test_load.py index 3470c3b6a..8571a0d9b 100755 --- a/emmo/tests/test_load.py +++ b/emmo/tests/test_load.py @@ -9,7 +9,7 @@ # Check that the defaults works -emmo = get_ontology('emmo').load() # owl format +emmo = get_ontology('emmo').load() # ttl format assert emmo.Atom.prefLabel.first() == 'Atom' emmo = get_ontology('emmo-inferred').load() @@ -18,6 +18,11 @@ emmo = get_ontology('emmo-development').load() # ttl format assert emmo.Atom.prefLabel.first() == 'Atom' +emmo = get_ontology('https://emmo-repo.github.io/latest-stable/' + 'emmo-inferred.owl').load() # owl format +assert emmo.Atom.prefLabel.first() == 'Atom' + + # Load a local ontology with catalog testonto = os.path.join(os.path.dirname(__file__), 'testonto', 'testonto.ttl') o = get_ontology(testonto).load()