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

Path query chaining issue #715

Closed
bgyori opened this issue Feb 13, 2017 · 4 comments
Closed

Path query chaining issue #715

bgyori opened this issue Feb 13, 2017 · 4 comments
Labels
bug Something isn't working in-resolution
Milestone

Comments

@bgyori
Copy link

bgyori commented Feb 13, 2017

Consider the following RDF file example.rdf

<http://example.org/A> <http://example.org/relations/isa> <http://example.org/X> .
<http://example.org/A> <http://example.org/relations/isa> <http://example.org/Y> .
<http://example.org/B> <http://example.org/relations/isa> <http://example.org/X> .

With rdflib 4.2.2 I do

import rdflib
g = rdflib.Graph()
g.parse('example.rdf', format='nt')
g.query('SELECT ?child ?parent WHERE {?child <http://example.org/relations/isa> ?parent .}')

and print the results, and I get the correct relationships:

http://example.org/A http://example.org/Y
http://example.org/B http://example.org/X
http://example.org/A http://example.org/X

However, when changing the query to

g.query('SELECT ?child ?parent WHERE {?child <http://example.org/relations/isa>+ ?parent .}')

I get

http://example.org/A http://example.org/Y
http://example.org/A http://example.org/X
http://example.org/B http://example.org/X
http://example.org/B http://example.org/Y

where the relationship B isa+ Y was incorrectly inferred. Is this a bug or am I misunderstanding the meaning of + in path chaining?

@joernhees
Copy link
Member

reproducible in 5.0.0-dev:

In [1]: from rdflib import URIRef, Graph
INFO:rdflib:RDFLib Version: 5.0.0-dev

In [2]: g = Graph()

In [3]: a, b, x, y = [URIRef(s) for s in "abxy"]

In [4]: isa = URIRef('isa')

In [5]: g.add((a, isa, x))

In [6]: g.add((a, isa, y))

In [7]: g.add((b, isa, x))

In [8]: list(g)
Out[8]:
[(rdflib.term.URIRef(u'a'),
  rdflib.term.URIRef(u'isa'),
  rdflib.term.URIRef(u'y')),
 (rdflib.term.URIRef(u'b'),
  rdflib.term.URIRef(u'isa'),
  rdflib.term.URIRef(u'x')),
 (rdflib.term.URIRef(u'a'),
  rdflib.term.URIRef(u'isa'),
  rdflib.term.URIRef(u'x'))]

In [9]: list(g.query('SELECT ?child ?parent WHERE {?child <isa> ?parent .}'))
Out[9]:
[(rdflib.term.URIRef(u'a'), rdflib.term.URIRef(u'y')),
 (rdflib.term.URIRef(u'b'), rdflib.term.URIRef(u'x')),
 (rdflib.term.URIRef(u'a'), rdflib.term.URIRef(u'x'))]

In [10]: list(g.query('SELECT ?child ?parent WHERE {?child <isa>+ ?parent .}'))
Out[10]:
[(rdflib.term.URIRef(u'a'), rdflib.term.URIRef(u'y')),
 (rdflib.term.URIRef(u'a'), rdflib.term.URIRef(u'x')),
 (rdflib.term.URIRef(u'b'), rdflib.term.URIRef(u'x')),
 (rdflib.term.URIRef(u'b'), rdflib.term.URIRef(u'y'))]

In [11]: list(g.query('SELECT ?child ?parent WHERE {?child <isa>* ?parent .}'))
Out[11]:
[(rdflib.term.URIRef(u'a'), rdflib.term.URIRef(u'a')),
 (rdflib.term.URIRef(u'y'), rdflib.term.URIRef(u'y')),
 (rdflib.term.URIRef(u'b'), rdflib.term.URIRef(u'b')),
 (rdflib.term.URIRef(u'x'), rdflib.term.URIRef(u'x')),
 (rdflib.term.URIRef(u'a'), rdflib.term.URIRef(u'y')),
 (rdflib.term.URIRef(u'a'), rdflib.term.URIRef(u'x')),
 (rdflib.term.URIRef(u'b'), rdflib.term.URIRef(u'x')),
 (rdflib.term.URIRef(u'b'), rdflib.term.URIRef(u'y'))]

@gromgull
Copy link
Member

This is unrelated to SPARQL, if I follow on from @joernhees's ipython sesssion:

In [67]: list(g.triples( ( None, isa*'+', None ) ) )
Out[67]:
[(rdflib.term.URIRef(u'a'), Path(isa+), rdflib.term.URIRef(u'x')),
 (rdflib.term.URIRef(u'a'), Path(isa+), rdflib.term.URIRef(u'y')),
 (rdflib.term.URIRef(u'b'), Path(isa+), rdflib.term.URIRef(u'x')),
 (rdflib.term.URIRef(u'b'), Path(isa+), rdflib.term.URIRef(u'y'))]

@gromgull
Copy link
Member

gromgull commented Feb 14, 2017

This means the bug is in here somewhere: https://github.com/RDFLib/rdflib/blob/master/rdflib/paths.py#L380-L388

@gromgull gromgull removed the SPARQL label Feb 14, 2017
jeromedockes added a commit to jeromedockes/rdflib that referenced this issue Apr 12, 2018
gromgull added a commit that referenced this issue May 14, 2018
@joernhees
Copy link
Member

fixed by #822

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working in-resolution
Projects
None yet
Development

No branches or pull requests

3 participants