Skip to content

Error when using a NegatedPropertySet with reverse paths in SPARQL #3140

Open
@william-vw

Description

@william-vw

When running the following using rdflib v.7.1.4:

import rdflib
g = rdflib.Graph()
g.parse(data="""
<http://example.org/gmark/C> <http://example.org/gmark/p2> <http://example.org/gmark/D> .
<http://example.org/gmark/B> <http://example.org/gmark/p1> <http://example.org/gmark/C> .
<http://example.org/gmark/A> <http://example.org/gmark/p0> <http://example.org/gmark/B> .        
""")

query = """
PREFIX : <http://example.org/gmark/> 
SELECT *
WHERE {
    ?x1 !(:p1|^:p2) ?x2
}"""

qres = g.query(query)
for row in qres:
    print(row)

Note the ^ in the NegatedPropertySet; when dropping this the code works. AFAIK it is proper syntax. I also believe it does not raise an error during parsing but rather during execution.

This code gives the following error:

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
Cell In[3], [line 17](vscode-notebook-cell:?execution_count=3&line=17)
      [9](vscode-notebook-cell:?execution_count=3&line=9) query = """
     [10](vscode-notebook-cell:?execution_count=3&line=10) PREFIX : <[http://example.org/gmark/>](http://example.org/gmark/%3E) 
     [11](vscode-notebook-cell:?execution_count=3&line=11) SELECT *
     [12](vscode-notebook-cell:?execution_count=3&line=12) WHERE {
     [13](vscode-notebook-cell:?execution_count=3&line=13)     ?x1 !(:p1|^:p2) ?x2
     [14](vscode-notebook-cell:?execution_count=3&line=14) }"""
     [16](vscode-notebook-cell:?execution_count=3&line=16) qres = g.query(query)
---> [17](vscode-notebook-cell:?execution_count=3&line=17) for row in qres:
     [18](vscode-notebook-cell:?execution_count=3&line=18)     print(row)

File /opt/anaconda3/lib/python3.12/site-packages/rdflib/query.py:372, in Result.__iter__(self)
    [368](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/query.py:368) elif self.type == "SELECT":
    [369](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/query.py:369)     # this iterates over ResultRows of variable bindings
    [371](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/query.py:371)     if self._genbindings:
--> [372](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/query.py:372)         for b in self._genbindings:
    [373](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/query.py:373)             if b:  # don't add a result row in case of empty binding {}
    [374](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/query.py:374)                 self._bindings.append(b)

File /opt/anaconda3/lib/python3.12/site-packages/rdflib/plugins/sparql/evaluate.py:574, in <genexpr>(.0)
    [572](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/plugins/sparql/evaluate.py:572) def evalProject(ctx: QueryContext, project: CompValue):
    [573](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/plugins/sparql/evaluate.py:573)     res = evalPart(ctx, project.p)
--> [574](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/plugins/sparql/evaluate.py:574)     return (row.project(project.PV) for row in res)

File /opt/anaconda3/lib/python3.12/site-packages/rdflib/plugins/sparql/evaluate.py:97, in evalBGP(ctx, bgp)
     [93](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/plugins/sparql/evaluate.py:93) _o = ctx[o]
     [95](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/plugins/sparql/evaluate.py:95) # type error: Item "None" of "Optional[Graph]" has no attribute "triples"
     [96](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/plugins/sparql/evaluate.py:96) # type Argument 1 to "triples" of "Graph" has incompatible type "Tuple[Union[str, Path, None], Union[str, Path, None], Union[str, Path, None]]"; expected "Tuple[Optional[Node], Optional[Node], Optional[Node]]"
---> [97](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/plugins/sparql/evaluate.py:97) for ss, sp, so in ctx.graph.triples((_s, _p, _o)):  # type: ignore[union-attr, arg-type]
     [98](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/plugins/sparql/evaluate.py:98)     if None in (_s, _p, _o):
     [99](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/plugins/sparql/evaluate.py:99)         c = ctx.push()

File /opt/anaconda3/lib/python3.12/site-packages/rdflib/graph.py:635, in Graph.triples(self, triple)
    [633](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/graph.py:633) s, p, o = triple
    [634](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/graph.py:634) if isinstance(p, Path):
--> [635](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/graph.py:635)     for _s, _o in p.eval(self, s, o):
    [636](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/graph.py:636)         yield _s, p, _o
    [637](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/graph.py:637) else:

File /opt/anaconda3/lib/python3.12/site-packages/rdflib/paths.py:507, in NegatedPath.eval(self, graph, subj, obj)
    [505](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/paths.py:505)             break
    [506](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/paths.py:506)     else:
--> [507](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/paths.py:507)         raise Exception("Invalid path in NegatedPath: %s" % a)
    [508](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/paths.py:508) else:
    [509](https://file+.vscode-resource.vscode-cdn.net/opt/anaconda3/lib/python3.12/site-packages/rdflib/paths.py:509)     yield s, o

Exception: Invalid path in NegatedPath: InversePath_InversePath_{}

Any help would be appreciated!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions