diff --git a/rdflib/plugins/sparql/parser.py b/rdflib/plugins/sparql/parser.py index aeba40b9b..6a9d47fe6 100644 --- a/rdflib/plugins/sparql/parser.py +++ b/rdflib/plugins/sparql/parser.py @@ -479,9 +479,6 @@ def _hexExpand(match): # [84] VerbPath ::= Path VerbPath = Path -# See -VarOrBlankNodeOrIriOrLitOrEmbTP = Var | BlankNode | iri | RDFLiteral | NumericLiteral | BooleanLiteral -EmbTP = Combine(Suppress('<<'), VarOrBlankNodeOrIriOrLitOrEmbTP + Verb + VarOrBlankNodeOrIriOrLitOrEmbTP, Suppress('>>')) VarOrTermOrEmbTP = Var | GraphTerm | EmbTP diff --git a/rdflib/term.py b/rdflib/term.py index aad5f4097..917611f43 100644 --- a/rdflib/term.py +++ b/rdflib/term.py @@ -437,6 +437,53 @@ def skolemize(self, authority=None, basepath=None): skolem = "%s%s" % (basepath, text_type(self)) return URIRef(urljoin(authority, skolem)) +class Triple(Identifier): + """ + Triple: Needed for RDF* + + """ + __slots__ = () + + def __new__(cls, sid=None, + subject=None, predicate=None, object=None): + triple = Identifier.__new__(cls, [subject, predicate, object, sid]) + + + def toPython(self): + return text_type(self) + + def n3(self, namespace_manager=None): + return "_:%s" % self + + def __getnewargs__(self): + return (text_type(self), ) + + if PY2: + def __str__(self): + return self.encode() + + def __repr__(self): + if self.__class__ is BNode: + clsName = "rdflib.term.Triple" + else: + clsName = self.__class__.__name__ + return """%s('%s')""" % (clsName, str(self)) + + 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]] class Literal(Identifier): __doc__ = """ diff --git a/test/test_sparql_star.py b/test/test_sparql_star.py index 5a3ae56df..07eb99787 100644 --- a/test/test_sparql_star.py +++ b/test/test_sparql_star.py @@ -1,9 +1,26 @@ from rdflib import Graph +def test_basic_sparql_star_subject(): + g = Graph() + res = g.query('''SELECT * WHERE { +<< ?s ?p ?o >> ?p2 ?o2 . +}''') -def test_basic_sparql_star(): + +def test_basic_sparql_star_object(): g = Graph() res = g.query('''SELECT * WHERE { -<> ?p2 ?o2 . +?s ?p << ?s2 ?p2 ?o2 >> . }''') + +def test_basic_sparql(): + g = Graph() + res = g.query('''SELECT * WHERE { + ?s ?p ?o . + }''') + + +def test_bind_sparql_star(): + g = Graph() + res = g.query('''SELECT * WHERE { BIND(<< ?s2 ?p2 ?o2 >> AS ?b) }''') \ No newline at end of file