From 591511b61dc8901e65afcf6248eb48895d5b5bbf Mon Sep 17 00:00:00 2001 From: francescalb Date: Thu, 19 Jan 2023 15:05:45 +0100 Subject: [PATCH 1/2] Only generate new uuid if not already a valid one --- ontopy/ontology.py | 11 ++++++++--- tests/test_basic.py | 3 +++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/ontopy/ontology.py b/ontopy/ontology.py index 51d09b0c0..199e85190 100644 --- a/ontopy/ontology.py +++ b/ontopy/ontology.py @@ -1167,9 +1167,14 @@ 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. + 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 diff --git a/tests/test_basic.py b/tests/test_basic.py index 74ed7ad71..3b88700e0 100755 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -58,6 +58,9 @@ 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 def test_sync_reasoner(testonto: "Ontology") -> None: From 56b75a727fc19bd9086fd250f9eff6a80a1170d1 Mon Sep 17 00:00:00 2001 From: francescalb Date: Thu, 19 Jan 2023 17:14:47 +0100 Subject: [PATCH 2/2] Added check for prefix in onto.sync_attributes(name_policy=uuid) --- ontopy/ontology.py | 4 ++++ tests/test_basic.py | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/ontopy/ontology.py b/ontopy/ontology.py index 199e85190..dd75384a7 100644 --- a/ontopy/ontology.py +++ b/ontopy/ontology.py @@ -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'``. @@ -1170,6 +1172,8 @@ class prefLabel(owlready2.label): 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( diff --git a/tests/test_basic.py b/tests/test_basic.py index 3b88700e0..81a1f8315 100755 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -62,6 +62,12 @@ class H2O(emmo.Molecule): 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: """Test `ontopy:Ontology.sync_reasoner()`."""