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 functionality for more than one parent in new_entity #295

Merged
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
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