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

Get SPARQL syntax string from rdflib.path.Path #421

Open
bollwyvl opened this issue Aug 15, 2014 · 3 comments
Open

Get SPARQL syntax string from rdflib.path.Path #421

bollwyvl opened this issue Aug 15, 2014 · 3 comments
Labels
enhancement New feature or request SPARQL

Comments

@bollwyvl
Copy link
Contributor

The SPARQL property paths in rdflib.paths are great for slicing, etc.

However, there is no straightforward way to get back actual, usable SPARQL syntax for more complex query patterns, even though the user would have to understand said syntax to make sense of what comes out of __repr__, or create it with the handy typographic shortcuts!

What I would like is:

from rdflib.namespaces import FOAF, RDFS
path = ~FOAF.knows/RDFS.label
# some magic
"~<http://xmlns.com/foaf/0.1/knows>/<http://www.w3.org/2000/01/rdf-schema#label>"

The __repr__ methods almost do this:

print path
"Path(Path(~http://xmlns.com/foaf/0.1/knows) / http://www.w3.org/2000/01/rdf-schema#label)"

Perhaps implementing __str__, in addition to __repr__, and having each call str and repr repectively, would work:

class InvPath(Path):
    ...

    def __repr__(self):
        return "InvPath(%s)" % (repr(self.arg),)

    def __str__(self):
        return "~%s" % (str(self.arg),)

In the meantime, I am using this: just making this available could be helpful.

from rdflib import paths, term

def path2sparql(path):
    if isinstance(path, paths.InvPath):
        return "~%s" % path2sparql(path.arg)
    elif isinstance(path, paths.SequencePath):
        return "/".join(path2sparql(x) for x in path.args)
    elif isinstance(path, term.URIRef):
        return "<%s>" % path
    elif isinstance(path, paths.AlternativePath):
        return "|".join(path2sparql(x) for x in path.args)
    elif isinstance(path, paths.MulPath):
        return "%s%s" % (path2sparql(path.path), path.mod)
    raise NotImplemented("Unhandled type for property path %s", path)
@gromgull
Copy link
Member

Wondering aloud - all rdflib nodes have a n3 method - the n3 and sparql syntax is (almost always) identiical - clearly path objects have no direct n3 representation, but we could bastardize this method for this purpose.

It's also on a list somewhere to allow reserializing parsed SPARQL query objects as SPARQL strings - this method could be useful there.

@uholzer
Copy link
Contributor

uholzer commented Aug 17, 2014

N3 does have path expressions using the ! and ^ operators. (Although there are no path expressions of arbitrary length.) As N3's and SPARQL's syntax for path expressions differ, you should not use the n3 method to produce SPARQL.

@ghost ghost added the SPARQL label Dec 25, 2021
@mgberg
Copy link
Contributor

mgberg commented Nov 22, 2022

N3 does have path expressions using the ! and ^ operators. (Although there are no path expressions of arbitrary length.) As N3's and SPARQL's syntax for path expressions differ, you should not use the n3 method to produce SPARQL.

#553 Added a n3 method to Paths. Since that method is used by the SPARQLStore by default for generation of queries, is the above comment effectively no longer valid? If so, can this issue be marked as resolved?

@aucampia aucampia added the enhancement New feature or request label Mar 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request SPARQL
Projects
None yet
Development

No branches or pull requests

5 participants