Skip to content

Commit

Permalink
Merge pull request #526 from emmo-repo/525-sync_attributes-according-…
Browse files Browse the repository at this point in the history
…to-emmo-convention-regenerates-a-new-iri-even-if-it-already-has-a-valid-one

Only generate new uuid if not already a valid one
Also,  the name_prefix  is required to be correct.
  • Loading branch information
francescalb authored Jan 19, 2023
2 parents dd7ea07 + 56b75a7 commit c3f5403
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
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

0 comments on commit c3f5403

Please sign in to comment.