Skip to content

Commit

Permalink
Added functionality for more than one parent in new_entity (#295)
Browse files Browse the repository at this point in the history
* Added functionality for more than one parent in new_entity

* Added that parents in new_enetity can be any iterable

* Included checking that the parent actually is an owlready2.ThingClass

* Fixed so that generator not 'used up' if parents given as generator
  • Loading branch information
francescalb authored Nov 17, 2021
1 parent 2b005fe commit 85f6d18
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
23 changes: 18 additions & 5 deletions ontopy/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import uuid
import tempfile
import types
from typing import Union
from collections import defaultdict
from collections.abc import Iterable

import rdflib
from rdflib.util import guess_format
Expand All @@ -37,6 +39,7 @@
ReadCatalogError,
_validate_installed_version,
LabelDefinitionError,
ThingClassDefinitionError,
)
from ontopy.ontograph import OntoGraph # FIXME: deprecate...

Expand Down Expand Up @@ -1210,19 +1213,29 @@ def get_wu_palmer_measure(self, cls1, cls2):
generations2 = self.number_of_generations(cls2, cca)
return 2 * ccadepth / (generations1 + generations2 + 2 * ccadepth)

def new_entity(self, name: str, parent: ThingClass) -> ThingClass:
def new_entity(
self, name: str, parent: Union[ThingClass, Iterable]
) -> ThingClass:
"""Create and return new entity
Makes a new entity in the ontology with given parent.
Makes a new entity in the ontology with given parent(s).
Return the new entity.
Throws exception if name consists of more than one word.
"""
if len(name.split(" ")) > 1:
raise LabelDefinitionError(
f"Error in label name definition {name}: "
"Label consists of more than one word."
f"Error in label name definition '{name}': "
f"Label consists of more than one word."
)
parents = tuple(parent) if isinstance(parent, Iterable) else (parent,)
for thing in parents:
if not isinstance(thing, owlready2.ThingClass):
raise ThingClassDefinitionError(
f"Error in parent definition: "
f"'{thing}' is not an owlready2.ThingClass."
)

with self:
entity = types.new_class(name, (parent,))
entity = types.new_class(name, parents)
return entity
4 changes: 4 additions & 0 deletions ontopy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class LabelDefinitionError(Exception):
"""Error in label definition."""


class ThingClassDefinitionError(Exception):
"""Error in ThingClass definition."""


def isinteractive():
"""Returns true if we are running from an interactive interpreater,
false otherwise."""
Expand Down

0 comments on commit 85f6d18

Please sign in to comment.