vemonet / rdflib-endpoint

💫 Deploy SPARQL endpoints from RDFLib Graphs to serve RDF files, machine learning models, or any other logic implemented in Python
https://pypi.org/project/rdflib-endpoint
MIT License
72 stars 18 forks source link

Should be able to use prefixes bound in graph's NamespaceManager #5

Closed Natureshadow closed 3 months ago

Natureshadow commented 1 year ago

Prefixes can be bound to a graph using the NamespaceManager. This makes them globally available, when loading and serializing as well as when querying.

rdflib-endpoint, however, requires prefixes to be declared in the query input even for those bound to the graph, because prepareQuery is not aware of the graph.

It would be convenient if the SPARQL endpoint could use the globally bound prefixes. My suggestion would be to drop the prepareQuery completely. I actually don't see why it is there in the first place, given that the prepared query is never used later on.

vemonet commented 1 year ago

Hi @Natureshadow , thanks for reporting the issue. Indeed the prepareQuery is only used to extract the SPARQL operation done (SELECT, CONSTRUCT, etc), but the .query() is run directly from the query string, not the prepared query, so the issue with the graph namespaces is probably not related to the prepared query specifically, but to .query() in general

It seems like for some reasons RDFLib does not automatically picks up the graph namespaces (only using the query namespaces)

I made some changes to extract the graph namespaces and add them to the .query() with initNs, so normally now rdflib-endpoint will make use of the namespaces defined in the graph (in rdflib-endpoint >=0.2.7)

Natureshadow commented 1 year ago

It seems like for some reasons RDFLib does not automatically picks up the graph namespaces (only using the query namespaces)

Hmm. For all queries I run myself, it picks them up perfectly fine. Also, as said, it works as well in rdflib-endpoint when running .query() (I tested that).

Natureshadow commented 1 year ago

Your change will not solve this, as it does not change the prepareQuery call, which is the only thing that is broken.

vemonet commented 1 year ago

Hi @Natureshadow, I probably did not understand your issue then. Do you have a snippet to reproduce it so I can test it?

If I expose a graph from a turtle file that includes some bindings using rdflib-endpoint, e.g.:

from rdflib import Graph, RDF
from rdflib_endpoint import SparqlEndpoint

g = Graph()
rdf = """@prefix sometest: <https://sometest.org/> .
sometest:Test a <http://test> .
"""
g.parse(data=rdf, format="ttl")

app = SparqlEndpoint(graph=g)

And I run this SPARQL query without defining the sometest prefix:

SELECT * WHERE {
    sometest:Test a ?t .
}

For me it returns the expected results, but from what I understood for you it does not returns anything, because the prefix is not taken into account when executing the query. Is that right?

Note that I made those tests with rdflib 6.2.0

vemonet commented 3 months ago

Closing for inactivity, feel free to re-open it with more details if it is still a problem