Skip to content

Commit

Permalink
fix: correctly handle resources with multiple comments in defined_nam…
Browse files Browse the repository at this point in the history
…espace_creator (#2254)

FIxed a bug in defined_namespace_creator so that when a resource in an ontology is commented on multiple times (with e.g. rdfs:comment), it creates only a single class variable in the resulting DefinedNamespace where the Python comment is the concatenation of the rdfs:comments.
  • Loading branch information
HanKruiger committed Mar 13, 2023
1 parent f17f975 commit 60d98db
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
3 changes: 2 additions & 1 deletion rdflib/tools/defined_namespace_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def get_target_namespace_elements(
) -> Tuple[List[Tuple[str, str]], List[str]]:
namespaces = {"dcterms": DCTERMS, "owl": OWL, "rdfs": RDFS, "skos": SKOS}
q = """
SELECT DISTINCT ?s ?def
SELECT ?s (GROUP_CONCAT(DISTINCT STR(?def)) AS ?defs)
WHERE {
# all things in the RDF data (anything RDF.type...)
?s a ?o .
Expand All @@ -90,6 +90,7 @@ def get_target_namespace_elements(
# only get results for the target namespace (supplied by user)
FILTER STRSTARTS(STR(?s), "xxx")
}
GROUP BY ?s
""".replace(
"xxx", target_namespace
)
Expand Down
5 changes: 5 additions & 0 deletions test/data/contrived/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Contrived Test Data

This directory contains test data contrived for specific purposes. Files in this
directory should clearly indicate their purpose with a comment.

17 changes: 17 additions & 0 deletions test/data/contrived/multiple-comments.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix ex: <http://example.org/multiline-string-example#> .

# This file contains a RDF class with multiple rdfs:comment properties and is
# used to verify the RDFLib defined namespace creator. It is used in
# </test/test_namespace/test_definednamespace_creator.py>.

<http://example.org/multiline-string-example>
a owl:Ontology .

ex:SomeClass a rdfs:Class, owl:Class;
rdfs:label "Some class"@en;
rdfs:comment "If one uses multiple comment properties, "@en;
rdfs:comment "then it should still only create a single class variable."@en;
rdfs:isDefinedBy <http://example.org/multiline-string-example>;
.
49 changes: 49 additions & 0 deletions test/test_namespace/test_definednamespace_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,52 @@ def test_definednamespace_creator_bad_ns():
universal_newlines=True,
)
assert completed.returncode == 1, "subprocess exited incorrectly (failure expected)"


def test_definednamespace_creator_multiple_comments():
"""
Tests that only a single URIRef is declared, even when multiple
rdfs:comments are linked to the resource.
"""

definednamespace_script = (
Path(__file__).parent.parent.parent
/ "rdflib"
/ "tools"
/ "defined_namespace_creator.py"
)
multiple_comments_data_file = (
Path(__file__).parent.parent / "data" / "contrived" / "multiple-comments.ttl"
)
print("\n")
print(f"Using {definednamespace_script}...")
print(f"Testing {multiple_comments_data_file}...")
completed = subprocess.run(
[
sys.executable,
str(definednamespace_script),
str(multiple_comments_data_file),
"http://example.org/multiline-string-example#",
"MULTILINESTRINGEXAMPLE",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
)
assert completed.returncode == 0, "subprocess exited incorrectly"
assert Path.is_file(
Path("_MULTILINESTRINGEXAMPLE.py")
), "_MULTILINESTRINGEXAMPLE.py file not created"

some_class_count = 0
with open(Path("_MULTILINESTRINGEXAMPLE.py")) as f:
for line in f.readlines():
if "SomeClass: URIRef" in line:
some_class_count += 1

assert (
some_class_count == 1
), f"found {some_class_count} SomeClass definitions instead of 1."

# cleanup
Path.unlink(Path("_MULTILINESTRINGEXAMPLE.py"))

0 comments on commit 60d98db

Please sign in to comment.