Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/rdflib/plugins/sparql/processor.py @ 1:56ad4e20f292 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
| author | guerler |
|---|---|
| date | Fri, 31 Jul 2020 00:32:28 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 0:d30785e31577 | 1:56ad4e20f292 |
|---|---|
| 1 | |
| 2 """ | |
| 3 Code for tying SPARQL Engine into RDFLib | |
| 4 | |
| 5 These should be automatically registered with RDFLib | |
| 6 | |
| 7 """ | |
| 8 | |
| 9 | |
| 10 from rdflib.query import Processor, Result, UpdateProcessor | |
| 11 | |
| 12 from rdflib.plugins.sparql.sparql import Query | |
| 13 | |
| 14 from rdflib.plugins.sparql.parser import parseQuery, parseUpdate | |
| 15 from rdflib.plugins.sparql.algebra import translateQuery, translateUpdate | |
| 16 | |
| 17 from rdflib.plugins.sparql.evaluate import evalQuery | |
| 18 from rdflib.plugins.sparql.update import evalUpdate | |
| 19 | |
| 20 | |
| 21 def prepareQuery(queryString, initNs={}, base=None): | |
| 22 """ | |
| 23 Parse and translate a SPARQL Query | |
| 24 """ | |
| 25 ret = translateQuery(parseQuery(queryString), base, initNs) | |
| 26 ret._original_args = (queryString, initNs, base) | |
| 27 return ret | |
| 28 | |
| 29 | |
| 30 def processUpdate(graph, updateString, initBindings={}, initNs={}, base=None): | |
| 31 """ | |
| 32 Process a SPARQL Update Request | |
| 33 returns Nothing on success or raises Exceptions on error | |
| 34 """ | |
| 35 evalUpdate(graph, translateUpdate( | |
| 36 parseUpdate(updateString), base, initNs), initBindings) | |
| 37 | |
| 38 | |
| 39 class SPARQLResult(Result): | |
| 40 | |
| 41 def __init__(self, res): | |
| 42 Result.__init__(self, res["type_"]) | |
| 43 self.vars = res.get("vars_") | |
| 44 self.bindings = res.get("bindings") | |
| 45 self.askAnswer = res.get("askAnswer") | |
| 46 self.graph = res.get("graph") | |
| 47 | |
| 48 class SPARQLUpdateProcessor(UpdateProcessor): | |
| 49 def __init__(self, graph): | |
| 50 self.graph = graph | |
| 51 | |
| 52 def update(self, strOrQuery, initBindings={}, initNs={}): | |
| 53 if isinstance(strOrQuery, str): | |
| 54 strOrQuery=translateUpdate(parseUpdate(strOrQuery), initNs=initNs) | |
| 55 | |
| 56 return evalUpdate(self.graph, strOrQuery, initBindings) | |
| 57 | |
| 58 | |
| 59 class SPARQLProcessor(Processor): | |
| 60 | |
| 61 def __init__(self, graph): | |
| 62 self.graph = graph | |
| 63 | |
| 64 def query( | |
| 65 self, strOrQuery, initBindings={}, | |
| 66 initNs={}, base=None, DEBUG=False): | |
| 67 """ | |
| 68 Evaluate a query with the given initial bindings, and initial | |
| 69 namespaces. The given base is used to resolve relative URIs in | |
| 70 the query and will be overridden by any BASE given in the query. | |
| 71 """ | |
| 72 | |
| 73 if not isinstance(strOrQuery, Query): | |
| 74 parsetree = parseQuery(strOrQuery) | |
| 75 query = translateQuery(parsetree, base, initNs) | |
| 76 else: | |
| 77 query = strOrQuery | |
| 78 | |
| 79 return evalQuery(self.graph, query, initBindings, base) |
