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

Added function new_entitiy to ontology #207

Merged
merged 3 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions emmo/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import warnings
import uuid
import tempfile
import types
from collections import defaultdict

import rdflib
Expand Down Expand Up @@ -1014,3 +1015,12 @@ def get_wu_palmer_measure(self, cls1, cls2):
n1 = self.number_of_generations(cls1, cca)
n2 = self.number_of_generations(cls2, cca)
return 2 * ccadepth / (n1 + n2 + 2 * ccadepth)

def new_entity(self, name, parent):
francescalb marked this conversation as resolved.
Show resolved Hide resolved
'''
Makes a new entity in the ontology with given parent.
Return the new entity
'''
with self:
e = types.new_class(name, (parent, ))
return e
15 changes: 7 additions & 8 deletions emmo/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@
onto.imported_ontologies.append(emmo)
onto.base_iri = 'http://emmo.info/examples/test#'

with onto:
# Add entity directly
onto.new_entity('Hydrogen', emmo.Atom)

class Hydrogen(emmo.Atom):
pass
with onto:

# Add entity using python classes
class Oxygen(emmo.Atom):
pass

class H2O(emmo.Molecule):
"""Water molecule."""
emmo.hasSpatialDirectPart.exactly(2, Hydrogen)
emmo.hasSpatialDirectPart.exactly(2, onto.Hydrogen)
emmo.hasSpatialDirectPart.exactly(1, Oxygen)

# Create some
H1 = Hydrogen()
H2 = Hydrogen()
H1 = onto.Hydrogen()
H2 = onto.Hydrogen()
O = Oxygen() # noqa: E741
w = H2O()
w.hasSpatialDirectPart = [H1, H2, O]


onto.sync_attributes(name_policy='sequential', name_prefix='myonto_')
assert onto.base_iri + 'myonto_0' in onto
assert onto.base_iri + 'myonto_6' in onto
Expand All @@ -49,7 +49,6 @@ class H2O(emmo.Molecule):
assert w.name.startswith('onto_')
assert len(w.name) == 5 + 36


# Remove all traces of onto such that they do not mess up other tests
# when running pytest
for e in itertools.chain(onto.classes(), onto.individuals()):
Expand Down