Skip to content

Commit

Permalink
Working on RDFLib#955 embedded triples in values clasuses need to be …
Browse files Browse the repository at this point in the history
…well formed.
  • Loading branch information
JervenBolleman committed Mar 14, 2020
1 parent 6049464 commit c3b9d36
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
14 changes: 11 additions & 3 deletions rdflib/plugins/sparql/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,16 @@ def _hexExpand(match):
GraphOrDefault = ParamList('graph', Keyword(
'DEFAULT')) | Optional(Keyword('GRAPH')) + ParamList('graph', iri)

# Inside a values clause the EmbeddedTriple must be fully resolvable.
KnownEmbTP = Suppress('<<') + iri + (iri | A) + (iri | RDFLiteral | NumericLiteral | BooleanLiteral) + Suppress('>>')
KnownEmbTP.setParseAction(lambda x:
rdflib.EmbeddedTriple(subject=x[0],
predicate=x[1],
object=x[2]))


# [65] DataBlockValue ::= iri | RDFLiteral | NumericLiteral | BooleanLiteral | 'UNDEF'
DataBlockValue = iri | RDFLiteral | NumericLiteral | BooleanLiteral | Keyword(
'UNDEF')
DataBlockValue = iri | RDFLiteral | NumericLiteral | BooleanLiteral | Keyword('UNDEF') | KnownEmbTP

# [78] Verb ::= VarOrIri | A
Verb = VarOrIri | A
Expand All @@ -435,7 +442,8 @@ def _hexExpand(match):
#VarOrBlankNodeOrIriOrLitOrEmbTP = Forward()
#VarOrBlankNodeOrIriOrLitOrEmbTP <<= Var | BlankNode | iri | RDFLiteral | NumericLiteral | BooleanLiteral | VarOrBlankNodeOrIriOrLitOrEmbTP
VarOrBlankNodeOrIriOrLitOrEmbTP = Var | BlankNode | iri | RDFLiteral | NumericLiteral | BooleanLiteral
EmbTP = Suppress('<<') + VarOrBlankNodeOrIriOrLitOrEmbTP + Verb+ VarOrBlankNodeOrIriOrLitOrEmbTP + Suppress('>>')

EmbTP = Suppress('<<') + VarOrBlankNodeOrIriOrLitOrEmbTP + Verb + VarOrBlankNodeOrIriOrLitOrEmbTP + Suppress('>>')
EmbTP.setParseAction(lambda x:
rdflib.EmbeddedTriple(subject=x[0],
predicate=x[1],
Expand Down
22 changes: 22 additions & 0 deletions test/test_sparql_star.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from rdflib import Graph
from rdflib.namespace import RDF, Namespace
from pyparsing import ParseException
import unittest


Expand Down Expand Up @@ -93,5 +94,26 @@ def test_constant_sparql_object(self):
rl = list(res)
self.assertEqual(1 , len(rl))

def test_constant_sparql_star_values(self):
g = Graph()
g.parse(data=self.reif_as_subject, format="ttl")
res = g.query('''PREFIX ex:<http://example.org/> SELECT * WHERE {
VALUES(?reif) {(<< ex:subject ex:predicate ex:object>>)}
?reif a ex:about .
}''')
rl = list(res)
self.assertEqual(1 , len(rl))

#This should fail as the embedded triple is not well formed
def test_non_constant_sparql_star_values(self):
g = Graph()
g.parse(data=self.reif_as_subject, format="ttl")
with self.assertRaises(ParseException):
g.query('''PREFIX ex:<http://example.org/> SELECT * WHERE {
VALUES(?reif) {(<< ex:subject ex:predicate ?o>>)}
?reif a ex:about .
}''')


if __name__ == '__main__':
unittest.main()

0 comments on commit c3b9d36

Please sign in to comment.