Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ttl standard for emmo #204

Merged
merged 4 commits into from
Aug 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 36 additions & 17 deletions emmo/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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.
Expand All @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions emmo/tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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')
Expand Down
7 changes: 6 additions & 1 deletion emmo/tests/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also switch to ttl here? Both owl and ttl is available.

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()
Expand Down