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

Fix infinite recursion in directory layout #708

Merged
merged 4 commits into from
Jan 25, 2024
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
8 changes: 4 additions & 4 deletions ontopy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,12 +786,8 @@ def directory_layout(onto):

where `ontoA`, `ontoB` and `ontoC` are imported Ontology objects.
"""
layout = {}

def recur(o):
for imported in o.imported_ontologies:
if imported not in layout:
recur(imported)
baseiri = o.base_iri.rstrip("/#")

# Some heuristics here to reproduce the EMMO layout.
Expand All @@ -809,7 +805,11 @@ def recur(o):
layout[o] = (
baseiri + "/" + os.path.basename(baseiri) if emmolayout else baseiri
)
for imported in o.imported_ontologies:
if imported not in layout:
recur(imported)

layout = {}
recur(onto)

# Strip off initial common prefix from all paths
Expand Down
16 changes: 16 additions & 0 deletions tests/test_utils_directory_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,19 @@ def test_local_directory_layout():

assert layout[omap["http://emmo.info/models#"]] == "models"
assert layout[omap["http://emmo.info/testonto#"]] == "testonto"


def test_local_directory_layout_recursive():
thisdir = Path(__file__).resolve().parent
ontopath = thisdir / "testonto" / "testonto-recursive.ttl"
onto = get_ontology(ontopath).load()
layout = directory_layout(onto)
omap = {o.base_iri: o for o in layout.keys()}

assert (
layout[omap["http://emmo.info/models-recursive#"]] == "models-recursive"
)
assert (
layout[omap["http://emmo.info/testonto-recursive#"]]
== "testonto-recursive"
)
4 changes: 3 additions & 1 deletion tests/testonto/catalog-v001.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<catalog prefer="public" xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
<group id="Folder Repository, directory=, recursive=true, Auto-Update=false, version=2" prefer="public" xml:base="">
<uri name="http://emmo.info/testonto/0.1.0" uri="ontology.ttl"/>
<uri name="http://emmo.info/testonto/0.1.0" uri="testonto.ttl"/>
<uri name="http://emmo.info/testonto/0.1.0/models" uri="models.ttl"/>
<uri name="http://emmo.info/testonto-recursive/0.1.0" uri="testonto-recursive.ttl"/>
<uri name="http://emmo.info/testonto/0.1.0/models-recursive" uri="models-recursive.ttl"/>
</group>
</catalog>
23 changes: 23 additions & 0 deletions tests/testonto/models-recursive.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@prefix : <http://emmo.info/models-recursive#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@base <http://emmo.info/models-recursive> .

<http://emmo.info/models-recursive> rdf:type owl:Ontology ;
owl:imports <http://emmo.info/testonto-recursive/0.1.0> ;
owl:versionIRI <http://emmo.info/testonto/0.1.0/models-recursive> .


# Annotations
skos:prefLabel rdf:type owl:AnnotationProperty .
skos:altLabel rdf:type owl:AnnotationProperty .


:testclass rdf:type owl:Class ;
rdfs:subClassOf owl:Thing ;
skos:prefLabel "TestClass"@en ;
skos:altLabel 25517 . # Test that values given as integers are accepted by ontodoc
33 changes: 33 additions & 0 deletions tests/testonto/testonto-recursive.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@prefix : <http://emmo.info/testonto-recursive#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@base <http://emmo.info/testonto-recursive> .

<http://emmo.info/testonto-recursive> rdf:type owl:Ontology ;
owl:versionIRI <http://emmo.info/testonto-recursive/0.1.0> ;
owl:imports <http://emmo.info/testonto/0.1.0/models-recursive> ;
owl:versionInfo "0.1.0" .


:testclass rdf:type owl:Class ;
rdfs:subClassOf owl:Thing ;
skos:prefLabel "TestClass"@en .

:testobjectproperty rdf:type owl:ObjectProperty ;
rdfs:domain :testclass ;
rdfs:range :testclass ;
skos:prefLabel "hasObjectProperty"@en .

:testannotationproperty rdf:type owl:AnnotationProperty ;
rdfs:domain :testclass ;
rdfs:range rdfs:Literal ;
skos:prefLabel "hasAnnotationProperty"@en .

:testdatatypeproperty rdf:type owl:DatatypeProperty ;
rdfs:domain :testclass ;
rdfs:range xsd:string ;
skos:prefLabel "hasDataProperty"@en .