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

Only generate new uuid if not already a valid one #526

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
15 changes: 12 additions & 3 deletions ontopy/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,8 @@ def sync_attributes( # pylint: disable=too-many-branches
should be updated. Valid values are:
None not changed
"uuid" `name_prefix` followed by a global unique id (UUID).
If the name is already valid accoridng to this standard
it will not be regenerated.
"sequential" `name_prefix` followed a sequantial number.
EMMO conventions imply ``name_policy=='uuid'``.

Expand Down Expand Up @@ -1167,9 +1169,16 @@ class prefLabel(owlready2.label):
)
if name_policy == "uuid":
for obj in chain:
obj.name = name_prefix + str(
uuid.uuid5(uuid.NAMESPACE_DNS, obj.name)
)
try:
# Passing the following means that the name is valid
# and need not be regenerated.
if not obj.name.startswith(name_prefix):
raise ValueError
uuid.UUID(obj.name.lstrip(name_prefix), version=5)
except ValueError:
obj.name = name_prefix + str(
uuid.uuid5(uuid.NAMESPACE_DNS, obj.name)
)
elif name_policy == "sequential":
for obj in chain:
counter = 0
Expand Down
9 changes: 9 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ class H2O(emmo.Molecule):
assert water.name.startswith("onto_")
# A UUID is 32 chars long + 4 `-` chars = 36 chars
assert len(water.name) == len(name_prefix) + 36
synced_uuid = water.name
onto.sync_attributes(name_policy="uuid", name_prefix=name_prefix)
assert synced_uuid == water.name

water.name = water.name[1:]
assert water.name.startswith("nto_")
onto.sync_attributes(name_policy="uuid", name_prefix=name_prefix)
assert synced_uuid != water.name
assert water.name.startswith("onto_")


def test_sync_reasoner(testonto: "Ontology") -> None:
Expand Down