def remove(self, spo, context): """ Remove a triple from the store """ if not self.connection: raise "UpdateEndpoint is not set - call 'open'" (subject, predicate, obj) = spo if not subject: subject = Variable("S") if not predicate: predicate = Variable("P") if not obj: obj = Variable("O") triple = "%s %s %s ." % (subject.n3(), predicate.n3(), obj.n3()) if self.context_aware and context is not None: q = "DELETE { GRAPH %s { %s } } WHERE { GRAPH %s { %s } }" % ( context.identifier.n3(), triple, context.identifier.n3(), triple) else: q = "DELETE { %s } WHERE { %s } " % (triple, triple) r = self._do_update(q) content = r.read() # we expect no content if r.status not in (200, 204): raise Exception("Could not update: %d %s\n%s" % ( r.status, r.reason, content))
def remove(self, spo, context): """ Remove a triple from the store """ if not self.connection: raise Exception("UpdateEndpoint is not set - call 'open'") (subject, predicate, obj) = spo if not subject: subject = Variable("S") if not predicate: predicate = Variable("P") if not obj: obj = Variable("O") triple = "%s %s %s ." % (subject.n3(), predicate.n3(), obj.n3()) if self.context_aware and context is not None: q = "DELETE { GRAPH %s { %s } } WHERE { GRAPH %s { %s } }" % ( context.identifier.n3(), triple, context.identifier.n3(), triple) else: q = "DELETE { %s } WHERE { %s } " % (triple, triple) r = self._do_update(q) content = r.read() # we expect no content if r.status not in (200, 204): raise Exception("Could not update: %d %s\n%s" % ( r.status, r.reason, content))
def fixup(o): if type(o) == BNode: o = Variable(str(o)) elif isinstance(o, Graph): o = URIRef(o.identifier, base=bbbb) if type(o) == Variable: o = URIRef(o.n3(), base=bbbb) return o
def remove(self, spo, context): """ Remove a triple from the store """ if not self.endpoint: raise Exception("UpdateEndpoint is not set - call 'open'") (subject, predicate, obj) = spo if not subject: subject = Variable("S") if not predicate: predicate = Variable("P") if not obj: obj = Variable("O") triple = "%s %s %s ." % (subject.n3(), predicate.n3(), obj.n3()) if self._is_contextual(context): q = "DELETE { GRAPH %s { %s } } WHERE { GRAPH %s { %s } }" % ( context.identifier.n3(), triple, context.identifier.n3(), triple) else: q = "DELETE { %s } WHERE { %s } " % (triple, triple) self._do_update(q)
def triples(self, xxx_todo_changeme2, context=None): """ SELECT ?subj ?pred ?obj WHERE { ?subj ?pred ?obj } """ (s, p, o) = xxx_todo_changeme2 if ( isinstance(s, BNode) or isinstance(p, BNode) or isinstance(o, BNode) ): raise Exception("SPARQLStore does not support Bnodes! See http://www.w3.org/TR/sparql11-query/#BGPsparqlBNodes") vars = [] if not s: s = Variable('s') vars.append(s) if not p: p = Variable('p') vars.append(p) if not o: o = Variable('o') vars.append(o) if vars: v = ' '.join([term.n3() for term in vars]) else: v = '*' if self.context_aware and context is not None: query = "SELECT %s WHERE { GRAPH %s { %s %s %s } }" % \ (v, context.identifier.n3(), s.n3(), p.n3(), o.n3()) else: query = "SELECT %s WHERE { %s %s %s }" % \ (v, s.n3(), p.n3(), o.n3()) self.setQuery(query) doc = ElementTree.parse(SPARQLWrapper.query(self).response) # ElementTree.dump(doc) for rt, vars in TraverseSPARQLResultDOM(doc, asDictionary=True): yield (rt.get(s, s), rt.get(p, p), rt.get(o, o)), None
vars.append(s) if not p: p = Variable('p') vars.append(p) if not o: o = Variable('o') vars.append(o) if vars: v = ' '.join([term.n3() for term in vars]) else: v = '*' query = "SELECT %s WHERE { %s %s %s }" % \ (v, s.n3(), p.n3(), o.n3()) # The ORDER BY is necessary if hasattr(context, LIMIT) or hasattr(context, OFFSET) \ or hasattr(context, ORDERBY): var = None if isinstance(s, Variable): var = s elif isinstance(p, Variable): var = p elif isinstance(o, Variable): var = o elif hasattr(context, ORDERBY) \ and isinstance(getattr(context, ORDERBY), Variable): var = getattr(context, ORDERBY) query = query + ' %s %s' % (ORDERBY, var.n3())
s = Variable("s") vars.append(s) if not p: p = Variable("p") vars.append(p) if not o: o = Variable("o") vars.append(o) if vars: v = " ".join([term.n3() for term in vars]) else: v = "*" query = "SELECT %s WHERE { %s %s %s }" % (v, s.n3(), p.n3(), o.n3()) # The ORDER BY is necessary if hasattr(context, LIMIT) or hasattr(context, OFFSET) or hasattr(context, ORDERBY): var = None if isinstance(s, Variable): var = s elif isinstance(p, Variable): var = p elif isinstance(o, Variable): var = o elif hasattr(context, ORDERBY) and isinstance(getattr(context, ORDERBY), Variable): var = getattr(context, ORDERBY) query = query + " %s %s" % (ORDERBY, var.n3()) try:
def triples(self, xxx_todo_changeme2, context=None): """ - tuple **(s, o, p)** the triple used as filter for the SPARQL select. (None, None, None) means anything. - context **context** the graph effectively calling this method. Returns a tuple of triples executing essentially a SPARQL like SELECT ?subj ?pred ?obj WHERE { ?subj ?pred ?obj } **context** may include three parameter to refine the underlying query: * LIMIT: an integer to limit the number of results * OFFSET: an integer to enable paging of results * ORDERBY: an instance of Variable('s'), Variable('o') or Variable('p') or, by default, the first 'None' from the given triple .. warning:: - Using LIMIT or OFFSET automatically include ORDERBY otherwise this is because the results are retrieved in a not deterministic way (depends on the walking path on the graph) - Using OFFSET without defining LIMIT will discard the first OFFSET - 1 results `` a_graph.LIMIT = limit a_graph.OFFSET = offset triple_generator = a_graph.triples(mytriple): #do something #Removes LIMIT and OFFSET if not required for the next triple() calls del a_graph.LIMIT del a_graph.OFFSET `` """ (s, p, o) = xxx_todo_changeme2 if ( isinstance(s, BNode) or isinstance(p, BNode) or isinstance(o, BNode) ): raise Exception("SPARQLStore does not support Bnodes! " "See http://www.w3.org/TR/sparql11-query/#BGPsparqlBNodes") vars = [] if not s: s = Variable('s') vars.append(s) if not p: p = Variable('p') vars.append(p) if not o: o = Variable('o') vars.append(o) if vars: v = ' '.join([term.n3() for term in vars]) else: v = '*' query = "SELECT %s WHERE { %s %s %s }" % \ (v, s.n3(), p.n3(), o.n3()) # The ORDER BY is necessary if hasattr(context, LIMIT) or hasattr(context, OFFSET) \ or hasattr(context, ORDERBY): var = None if isinstance(s, Variable): var = s elif isinstance(p, Variable): var = p elif isinstance(o, Variable): var = o elif hasattr(context, ORDERBY) \ and isinstance(getattr(context, ORDERBY), Variable): var = getattr(context, ORDERBY) query = query + ' %s %s' % (ORDERBY, var.n3()) try: query = query + ' LIMIT %s' % int(getattr(context, LIMIT)) except (ValueError, TypeError, AttributeError): pass try: query = query + ' OFFSET %s' % int(getattr(context, OFFSET)) except (ValueError, TypeError, AttributeError): pass self.resetQuery() if self._is_contextual(context): self.addDefaultGraph(context.identifier) self.setQuery(query) doc = ElementTree.parse(SPARQLWrapper.query(self).response) # ElementTree.dump(doc) for rt, vars in TraverseSPARQLResultDOM(doc, asDictionary=True): yield (rt.get(s, s), rt.get(p, p), rt.get(o, o)), None
def triples(self, xxx_todo_changeme2, context=None): """ - tuple **(s, o, p)** the triple used as filter for the SPARQL select. (None, None, None) means anything. - context **context** the graph effectively calling this method. Returns a tuple of triples executing essentially a SPARQL like SELECT ?subj ?pred ?obj WHERE { ?subj ?pred ?obj } **context** may include three parameter to refine the underlying query: * LIMIT: an integer to limit the number of results * OFFSET: an integer to enable paging of results * ORDERBY: an instance of Variable('s'), Variable('o') or Variable('p') or, by default, the first 'None' from the given triple .. warning:: - Using LIMIT or OFFSET automatically include ORDERBY otherwise this is because the results are retrieved in a not deterministic way (depends on the walking path on the graph) - Using OFFSET without defining LIMIT will discard the first OFFSET - 1 results `` a_graph.LIMIT = limit a_graph.OFFSET = offset triple_generator = a_graph.triples(mytriple): #do something #Removes LIMIT and OFFSET if not required for the next triple() calls del a_graph.LIMIT del a_graph.OFFSET `` """ (s, p, o) = xxx_todo_changeme2 if (isinstance(s, BNode) or isinstance(p, BNode) or isinstance(o, BNode)): raise Exception( "SPARQLStore does not support Bnodes! " "See http://www.w3.org/TR/sparql11-query/#BGPsparqlBNodes") vars = [] if not s: s = Variable('s') vars.append(s) if not p: p = Variable('p') vars.append(p) if not o: o = Variable('o') vars.append(o) if vars: v = ' '.join([term.n3() for term in vars]) else: v = '*' query = "SELECT %s WHERE { %s %s %s }" % \ (v, s.n3(), p.n3(), o.n3()) # The ORDER BY is necessary if hasattr(context, LIMIT) or hasattr(context, OFFSET) \ or hasattr(context, ORDERBY): var = None if isinstance(s, Variable): var = s elif isinstance(p, Variable): var = p elif isinstance(o, Variable): var = o elif hasattr(context, ORDERBY) \ and isinstance(getattr(context, ORDERBY), Variable): var = getattr(context, ORDERBY) query = query + ' %s %s' % (ORDERBY, var.n3()) try: query = query + ' LIMIT %s' % int(getattr(context, LIMIT)) except (ValueError, TypeError, AttributeError): pass try: query = query + ' OFFSET %s' % int(getattr(context, OFFSET)) except (ValueError, TypeError, AttributeError): pass self.resetQuery() if self._is_contextual(context): self.addDefaultGraph(context.identifier) self.setQuery(query) doc = ElementTree.parse(SPARQLWrapper.query(self).response) # ElementTree.dump(doc) for rt, vars in TraverseSPARQLResultDOM(doc, asDictionary=True): yield (rt.get(s, s), rt.get(p, p), rt.get(o, o)), None