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

defined_namespace_creator: concatenate several rdfs:comments #2254

Merged
merged 5 commits into from
Mar 13, 2023
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
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"))