Skip to content

Commit

Permalink
Added a possibilty to copy the ontology
Browse files Browse the repository at this point in the history
This is done by saving it in a temporary file and loading it as new.
  • Loading branch information
francescalb committed Nov 23, 2023
1 parent 12ff93a commit 37a0449
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ontopy/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -1939,6 +1939,18 @@ def new_annotation_property(
"""
return self.new_entity(name, parent, "annotation_property")

def copy(self):
"""Return a copy of the ontology."""
with tempfile.TemporaryDirectory() as handle:
tmpfile = os.path.join(handle, "tmp.owl")

self.save(
tmpfile,
squash=True,
)
ontology = get_ontology(tmpfile).load()
return ontology


class BlankNode:
"""Represents a blank node.
Expand Down
33 changes: 33 additions & 0 deletions tests/ontopy_tests/test_copy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from typing import TYPE_CHECKING
import pytest

if TYPE_CHECKING:
from pathlib import Path


def test_copy(testonto: "Ontology", repo_dir: "Path") -> None:
"""Test adding entities to ontology"""

from ontopy import get_ontology
from ontopy.utils import NoSuchLabelError

# create new reference to ontology
testonto = get_ontology(
repo_dir / "tests" / "testonto" / "testonto.ttl"
).load()
testonto_ref = testonto

# add new entity to ontology
testonto.new_entity("FantasyClass", testonto.TestClass)

assert testonto_ref.FantasyClass == testonto.FantasyClass

# make a copy and check that new entity in original is not in copy
testonto_copy = testonto.copy()

testonto.new_entity("FantasyClass2", testonto_copy.TestClass)

assert testonto.FantasyClass2

with pytest.raises(NoSuchLabelError):
assert testonto_copy.FantasyClass2

0 comments on commit 37a0449

Please sign in to comment.