comparison planemo/lib/python3.7/site-packages/rdflib/plugins/sparql/results/rdfresults.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 from rdflib import Graph, Namespace, RDF, Variable
2
3 from rdflib.query import Result, ResultParser
4
5 RS = Namespace('http://www.w3.org/2001/sw/DataAccess/tests/result-set#')
6
7
8 class RDFResultParser(ResultParser):
9 def parse(self, source, **kwargs):
10 return RDFResult(source, **kwargs)
11
12
13 class RDFResult(Result):
14
15 def __init__(self, source, **kwargs):
16
17 if not isinstance(source, Graph):
18 graph = Graph()
19 graph.load(source, **kwargs)
20 else:
21 graph = source
22
23 rs = graph.value(predicate=RDF.type, object=RS.ResultSet)
24 # there better be only one :)
25
26 if rs is None:
27 type_ = 'CONSTRUCT'
28
29 # use a new graph
30 g = Graph()
31 g += graph
32
33 else:
34
35 askAnswer = graph.value(rs, RS.boolean)
36
37 if askAnswer is not None:
38 type_ = 'ASK'
39 else:
40 type_ = 'SELECT'
41
42 Result.__init__(self, type_)
43
44 if type_ == 'SELECT':
45 self.vars = [Variable(v) for v in graph.objects(rs,
46 RS.resultVariable)]
47
48 self.bindings = []
49
50 for s in graph.objects(rs, RS.solution):
51 sol = {}
52 for b in graph.objects(s, RS.binding):
53 sol[Variable(graph.value(
54 b, RS.variable))] = graph.value(b, RS.value)
55 self.bindings.append(sol)
56 elif type_ == 'ASK':
57 self.askAnswer = askAnswer.value
58 if askAnswer.value == None:
59 raise Exception('Malformed boolean in ask answer!')
60 elif type_ == 'CONSTRUCT':
61 self.graph = g