Skip to content

Commit

Permalink
Working on RDFLib#955 Parsing code brings the execution to the right …
Browse files Browse the repository at this point in the history
…spot in the evaluate stack. However, functionality wise it is a step back. Commiting and pushing this to share the work so others can have a look.
  • Loading branch information
JervenBolleman committed May 10, 2020
1 parent f1e07ec commit c714879
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 83 deletions.
3 changes: 2 additions & 1 deletion rdflib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"BNode",
"Literal",
"Variable",
"EmbeddedTriple",
"Namespace",
"Dataset",
"Graph",
Expand Down Expand Up @@ -177,7 +178,7 @@
Literal work, eq, __neq__, __lt__, etc.
"""

from rdflib.term import URIRef, BNode, Literal, Variable
from rdflib.term import URIRef, BNode, Literal, Variable, EmbeddedTriple

from rdflib.namespace import Namespace

Expand Down
50 changes: 1 addition & 49 deletions rdflib/plugins/sparql/algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from functools import reduce

from rdflib import Literal, Variable, URIRef, BNode
from rdflib import Literal, Variable, URIRef, BNode, EmbeddedTriple

from rdflib.namespace import RDF

Expand Down Expand Up @@ -245,47 +245,6 @@ def collectAndRemoveFilters(parts):

return None

def collectExpandEmpTP(parts):
"""
FILTER expressions apply to the whole group graph pattern in which
they appear.
http://www.w3.org/TR/sparql11-query/#sparqlCollectFilters
"""



i = 0
while i < len(parts):
p = parts[i]
if p.name == 'TriplesBlock':
newTriples = []
for t in p.triples:
if isinstance(t[0], CompValue) and t[0].name == 'EmpTP':
r = Variable('__EMP_s_'+ str(i))
e = t[0]
t[0] = r
newTriples.append([e.s[0], e.p[0], e.o[0]])
t.extend([r, RDF.type, RDF.Statement,
r, RDF.subject, e.s[0],
r, RDF.predicate, e.p[0],
r, RDF.object, e.o[0]])
if isinstance(t[2], CompValue) and t[2].name == 'EmpTP':
r = Variable('__EMP_o_'+str(i))
e = t[2]
t[2] = r
newTriples.append([e.s[0], e.p[0], e.o[0]])
newTriples.append([r, RDF.type, RDF.Statement,
r, RDF.subject, e.s[0],
r, RDF.predicate, e.p[0],
r, RDF.object, e.o[0]])
for t in newTriples:
p.triples.append(t)
i += 1

return parts

def translateGroupOrUnionGraphPattern(graphPattern):
A = None

Expand Down Expand Up @@ -320,7 +279,6 @@ def translateGroupGraphPattern(graphPattern):

filters = collectAndRemoveFilters(graphPattern.part)

graphPattern.part = collectExpandEmpTP(graphPattern.part)
g = []
for p in graphPattern.part:
if p.name == 'TriplesBlock':
Expand Down Expand Up @@ -353,8 +311,6 @@ def translateGroupGraphPattern(graphPattern):
G = Join(p1=G, p2=p)
elif p.name == 'Bind':
G = Extend(G, p.expr, p.var)
elif p.name == "EmpTP":
G = Project(p, p.o)
else:
raise Exception('Unknown part in GroupGraphPattern: %s - %s' %
(type(p), p.name))
Expand Down Expand Up @@ -728,10 +684,6 @@ def translatePrologue(p, base, initNs=None, prologue=None):

return prologue


def translateEmbTP(u):
return u

def translateQuads(quads):
if quads.triples:
alltriples = triples(quads.triples)
Expand Down
18 changes: 8 additions & 10 deletions rdflib/plugins/sparql/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import requests
from pyparsing import ParseException

from rdflib import Variable, Graph, BNode, URIRef, Literal
from rdflib import Variable, Graph, BNode, URIRef, Literal, EmbeddedTriple
from six import iteritems, itervalues

from rdflib.plugins.sparql import CUSTOM_EVALS
Expand Down Expand Up @@ -50,6 +50,7 @@ def evalBGP(ctx, bgp):
_p = ctx[p]
_o = ctx[o]


for ss, sp, so in ctx.graph.triples((_s, _p, _o)):
if None in (_s, _p, _o):
c = ctx.push()
Expand Down Expand Up @@ -127,14 +128,10 @@ def evalEmbTP(ctx, reif):
from rdflib import RDF
v = Variable('reif')

p = CompValue()
p.name = 'Project'
p.p = CompValue()
p.p.name = 'BGP'
p.p.triples = [[v, RDF.subject, reif[0]],
[v,RDF.predicate, reif[1]],
[v,RDF.object, reif[2]]
]
p = CompValue('Project')
p.p = CompValue('BGP')

p.p.triples = [v, RDF.subject,reif.subject(), v,RDF.predicate, reif.predicate(), v,RDF.object, reif.object()]
p.PV = [v]
return evalProject(ctx, p)

Expand Down Expand Up @@ -255,7 +252,8 @@ def evalPart(ctx, part):
return evalExtend(ctx, part)
elif part.name == 'Minus':
return evalMinus(ctx, part)

elif part.name == 'EmbTP':
return evalEmbTP(ctx, part)
elif part.name == 'Project':
return evalProject(ctx, part)
elif part.name == 'Slice':
Expand Down
9 changes: 8 additions & 1 deletion rdflib/plugins/sparql/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,15 @@ def _hexExpand(match):
GraphNode = VarOrTerm | TriplesNode

#Should be recursive but it is not yet so
#VarOrBlankNodeOrIriOrLitOrEmbTP = Forward()
#VarOrBlankNodeOrIriOrLitOrEmbTP <<= Var | BlankNode | iri | RDFLiteral | NumericLiteral | BooleanLiteral | VarOrBlankNodeOrIriOrLitOrEmbTP
VarOrBlankNodeOrIriOrLitOrEmbTP = Var | BlankNode | iri | RDFLiteral | NumericLiteral | BooleanLiteral
EmbTP = Suppress('<<') + Comp('EmpTP',ParamList('s', VarOrBlankNodeOrIriOrLitOrEmbTP) + ParamList('p', Verb) + ParamList('o', VarOrBlankNodeOrIriOrLitOrEmbTP)) + Suppress('>>')
EmbTP = Suppress('<<') + VarOrBlankNodeOrIriOrLitOrEmbTP + Verb+ VarOrBlankNodeOrIriOrLitOrEmbTP + Suppress('>>')
EmbTP.setParseAction(lambda x:
rdflib.EmbeddedTriple(subject=x[0],
predicate=x[1],
object=x[2])
)

VarOrTermOrEmbTP = Var | GraphTerm | EmbTP

Expand Down
56 changes: 34 additions & 22 deletions rdflib/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
'URIRef',
'BNode',
'Literal',
'EmbeddedTriple',

'Variable',
'Statement',
Expand Down Expand Up @@ -436,53 +437,64 @@ def skolemize(self, authority=None, basepath=None):
skolem = "%s%s" % (basepath, text_type(self))
return URIRef(urljoin(authority, skolem))

class Triple(Identifier):
class EmbeddedTriple:
"""
Triple: Needed for RDF*
"""
__slots__ = ()
_subject = None
_predicate = None
_object = None
_sid = None

def __new__(cls, sid=None,
subject=None, predicate=None, object=None):
triple = Identifier.__new__(cls, [subject, predicate, object, sid])
def __init__(self, sid=None,
subject=None, predicate=None, object=None):
self._subject = subject
self._predicate = predicate
self._object = object
self._sid = sid


def toPython(self):
return text_type(self)
return text_type(self._subject) + text_type(self._predicate) + text_type(self._object)

def n3(self, namespace_manager=None):
return "_:%s" % self

def __getnewargs__(self):
return (text_type(self), )
return "triple:%s" % self

if PY2:
def __str__(self):
return self.encode()
return self._subject.encode() + self._predicate.encode() + self._object.encode()

def __repr__(self):
if self.__class__ is BNode:
clsName = "rdflib.term.Triple"
if self.__class__ is EmbeddedTriple:
clsName = "rdflib.term.EmbeddedTriple"
else:
clsName = self.__class__.__name__
return """%s('%s')""" % (clsName, str(self))
return """%s('%s','%s','%s')""" % (clsName, str(self._subject),str(self._predicate),str(self._object))

def asQuad(self):
_subject = self.value[0]
_predicate = self.value[1]
_object = self.value[2]
_sid = self.value[3]
_rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
_type = URIRef('type',base=_rdf)
_rdf_object = URIRef('object', base=_rdf)
_rdf_predicate = URIRef('predicate', base=_rdf)
_rdf_subject = URIRef('subject', base=_rdf)
_rdf_statement = URIRef('Statement', base=_rdf)
return [[_sid, _type , _rdf_statement],
[_sid , _rdf_object, _object],
[_sid, _rdf_predicate, _predicate],
[_sid, _rdf_subject, _subject]]
return [[self._sid, _type , _rdf_statement],
[self._sid , _rdf_object, self. object],
[self._sid, _rdf_predicate, self._predicate],
[self._sid, _rdf_subject, self._subject]]

def subject(self):
return self._subject

def predicate(self):
return self._predicate

def object(self):
return self._object

def sid(self):
return self._sid

class Literal(Identifier):
__doc__ = """
Expand Down

0 comments on commit c714879

Please sign in to comment.