diff --git a/emmo/ontology.py b/emmo/ontology.py index 2b4b46dc4..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] @@ -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): 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') 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()