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

Pull request for issue 2957 - subselect fails under optional #3077

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
97 changes: 97 additions & 0 deletions test/test_sparql/test_subselectOptional.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 6 18:21:28 2024

See issue https://github.com/RDFLib/rdflib/issues/2957.

@author: Administrator
"""
from rdflib import Graph, Literal, URIRef, Variable
import os

current_dir = os.getcwd()

GraphString = '''

prefix ex: <https://www.example.org/>

ex:document ex:subject "Nice cars" .

ex:document ex:theme "Car" .

'''

someGraph = Graph()

someGraph.parse(data=GraphString , format="turtle")

# This query states first a normal triple pattern and then an optional clause with a subselect query.
Query1 = someGraph.query('''

prefix ex: <https://www.example.org/>

select ?subject ?theme

where {

?doc1 ex:subject ?subject.

OPTIONAL
{
select ?theme
where {
?doc1 ex:theme ?theme.
}
}
}
''')

filepath = current_dir+"/output1.txt"

# Print the results with variable names and their bindings
print ('\nResults for Query1: \n')
for row in Query1:
print ("Result 'subject: "+ str(row.subject) + "'\nResult should show 'subject: Nice cars' \n")
print ("Result 'theme: " + str(row.theme) + "'\nResult should show 'theme: Car'")

with open(filepath, 'w', encoding='utf-8') as file:
file.write(str(row))
# Result should yield two rows:
# subject: Nice cars
# theme: Car


# This query states first an optional clause with a subselect query and then a normal triple pattern.
Query2 = someGraph.query('''

prefix ex: <https://www.example.org/>

select ?subject ?theme

where {

OPTIONAL
{
select ?theme
where {
?doc1 ex:theme ?theme.
}
}
?doc1 ex:subject ?subject.
}
''')

filepath = current_dir+"/output2.txt"

# Print the results with variable names and their bindings
print ('\nResults for Query2: \n')
for row in Query2:
print ("Result 'subject: "+ str(row.subject) + "'\nResult should show 'subject: Nice cars' \n")
print ("Result 'theme: " + str(row.theme) + "'\nResult should show 'theme: Car'")

with open(filepath, 'w', encoding='utf-8') as file:
file.write(str(row))

# Result should yield two rows:
# subject: Nice cars
# theme: Car