1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
'''
Created on Feb 7, 2012
@author: cgueret
'''
# http://www.w3.org/TR/sparql11-http-rdf-update/
# http://sparql-wrapper.sourceforge.net/resources/doc/
import logging
from rdflib import ConjunctiveGraph, RDF, Literal
from rdflib.term import URIRef
from rdflib.namespace import XSD
from SPARQLWrapper import SPARQLWrapper, Wrapper
import httplib
import uuid
import time
import traceback
import dbus
import binascii
from semanticxo import util
from semanticxo.namespace import OLPC_RESOURCE, OLPC_TERMS, DC_TERMS, \
OLPC_GRAPHS
class GSResource(object):
'''
A Semantic DataStore entry is the description of a single resource
'''
def __init__(self, graph, resource_uri):
'''
Constructor
'''
logging.debug("[GraphStore] Wrap resource %s in graph %s", resource_uri, graph)
self._resource_uri = resource_uri
self._graph = graph
def get_resource_uri(self):
'''
Return the URI for the resource
'''
return self._resource_uri
def add_link_to(self, link_name, target):
'''
Create a triple that points to the GSResource target
'''
target_uri = target.get_resource_uri()
self.add(OLPC_TERMS[link_name], target_uri)
def clean(self):
'''
Erase all the triples associated to this resource
'''
# Preserve the type
category = self.get_type()
uid = self.get_uid()
# remove all
self._graph.remove((self._resource_uri, None, None))
# add the type and the uid again
self.add(RDF.type, category)
self.add(OLPC_TERMS['uid'], Literal(uid))
def get_type(self):
'''
Get the type of the resource
'''
for (_,_,o) in self._graph.triples((self._resource_uri, RDF.type, None)):
return o
def get_uid(self):
'''
Return the UID
'''
for (_,_,o) in self._graph.triples((self._resource_uri, OLPC_TERMS['uid'], None)):
return o
def get(self, prop):
'''
Return the different values associated to a particular property
'''
results = []
p = None
if type(prop) != URIRef:
p = OLPC_TERMS[prop]
else:
p = prop
for (_,_,o) in self._graph.triples((self._resource_uri, p, None)):
results.append(o)
return results
def get_properties(self):
'''
Return the different values associated to a particular property
'''
results = []
for (_,p,_) in self._graph.triples((self._resource_uri, None, None)):
results.append(p)
return results
def add(self, p, o):
'''
Add a triple to the graph
'''
try:
key = p
if type(key) != URIRef:
key = OLPC_TERMS[key]
value = o
if type(value) != URIRef and type(value) != Literal:
if type(value) == dbus.ByteArray:
hexvalue = binascii.hexlify(value)
value = Literal(hexvalue, datatype=XSD.hexBinary)
else:
value = Literal(value)
self._graph.add((self._resource_uri, key, value))
except:
logging.debug('[SDS] Can not store value of type %s for %s', type(value), p)
traceback.print_exc()
class GSGraph(object):
'''
A Semantic DataStore object is a named graph with an assigned UUID.
It is a wrapper around a set of triples that contains, at least a triple
indicating that the named graph is a Semantic DataStore object
'''
def __init__(self, graph_uri, payload=None):
'''
Constructor
'''
# The URI of that graph
self._graph_uri = graph_uri
if payload == None:
# No RDF has been given, initialize the graph with default values
self._graph = ConjunctiveGraph()
self._graph.add((self._graph_uri, RDF.type, OLPC_TERMS['DataGraph']))
self._graph.add((self._graph_uri, DC_TERMS['author'], util.device_uri()))
else:
# Load the given RDF
self._graph = payload
def get_graph_uri(self):
return self._graph_uri
def as_ntriples(self):
'''
Return the content of the graph as NTriples
'''
# Update last modification time
self._graph.remove((self._graph_uri, DC_TERMS['modified'], None))
self._graph.add((self._graph_uri, DC_TERMS['modified'], Literal(int(time.time()))))
return self._graph.serialize(format='nt')
def as_rdfxml(self):
'''
Return the content of the graph as RDF/XML
'''
# Update last modification time
self._graph.remove((self._graph_uri, DC_TERMS['modified'], None))
self._graph.add((self._graph_uri, DC_TERMS['modified'], Literal(int(time.time()))))
return self._graph.serialize()
def create_resource(self, uid=None, category=None):
'''
Return a new resource entry to be added to an instance of GSGraph
'''
if category == None:
category = 'GenericResource'
if uid == None:
uid = str(uuid.uuid4())
resource = GSResource(graph=self._graph, resource_uri=OLPC_RESOURCE[uid])
resource.add(RDF.type, OLPC_TERMS[category])
resource.add(OLPC_TERMS['uid'], Literal(uid))
return resource
def get_resource(self, uri):
'''
Return a wrapper object for a resource in the graph
'''
resource_uri = uri
if type(resource_uri) != URIRef:
resource_uri = URIRef(uri)
return GSResource(graph=self._graph, resource_uri=uri)
def get_resources_list(self, restrict=None):
results = []
restrict_uri = None
if restrict != None:
restrict_uri = OLPC_TERMS[restrict]
for t in self._graph.triples((None, RDF.type, restrict_uri)):
(s,_,o) = t
if o != OLPC_TERMS['DataGraph']:
results.append(s)
return results
def add_share(self, contact):
'''
Add the unique identifier (IRI) of someone else to share the graph with
'''
self._graph.add((self._graph_uri, OLPC_TERMS['shared_with'], contact))
def remove_share(self, contact):
'''
Remove the sharing autorisation with the unique identifier (IRI)
'''
self._graph.remove((self._graph_uri, OLPC_TERMS['shared_with'], contact))
class GraphStore(object):
'''
The Semantic DataStore is an interface to the triple store.
'''
def __init__(self, hostname=None):
'''
Constructor
@param store_address the address of the triple store
'''
if hostname == None:
hostname = 'localhost:8080'
self.store_address = hostname
def get_graph(self, object_iri):
'''
Return an instance of a DataStore object
'''
""
query_string = "CONSTRUCT {?s ?p ?o} WHERE { GRAPH <%s> {?s ?p ?o} }" % object_iri
sparql = SPARQLWrapper("http://%s/sparql" % self.store_address)
sparql.setReturnFormat(Wrapper.RDF)
sparql.setQuery(query_string)
return GSGraph(object_iri, sparql.query().convert())
def get_graphs_list(self, restrict=None):
'''
Returns a list of the objects stored in this store
'''
# ask for all instances of data graphs
query_string = "SELECT DISTINCT ?graph WHERE { ?graph a <%s>. ?graph <http://purl.org/dc/terms/modified> ?mdate.} ORDER BY ?mdate" % OLPC_TERMS['DataGraph']
if restrict != None:
query_string = "SELECT DISTINCT ?graph WHERE { GRAPH ?graph {?e a <%s>. } ?graph <http://purl.org/dc/terms/modified> ?mdate. } ORDER BY ?mdate " % OLPC_TERMS[restrict]
sparql = SPARQLWrapper("http://%s/sparql" % self.store_address)
sparql.setReturnFormat(Wrapper.JSON)
sparql.setQuery(query_string)
results = sparql.query().convert()
liste = [result["graph"]["value"] for result in results["results"]["bindings"]]
return liste
def get_graphs_modification_date(self):
'''
Return a map associating to each public graph its modification date
'''
graphs = {}
query_string = ""
query_string +="SELECT DISTINCT ?graph ?date WHERE {"
query_string +="?graph a <%s> ." % OLPC_TERMS['DataGraph']
query_string +="?graph <%s> ?date ." % DC_TERMS['modified']
query_string +="?graph <%s> ?share." % OLPC_TERMS['shared_with']
query_string +="FILTER ("
query_string +="?share = <%s>" % util.public_uri()
query_string +="||"
query_string +="?share = <%s>" % util.device_uri()
query_string +=")"
query_string +="}"
sparql = SPARQLWrapper("http://%s/sparql" % self.store_address)
sparql.setReturnFormat(Wrapper.JSON)
sparql.setQuery(query_string)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
graphs[result['graph']['value']] = result['date']['value']
return graphs
def persist_graph(self, gs_graph):
'''
Save an object into the store
'''
iri = gs_graph.get_graph_uri()
payload = gs_graph.as_rdfxml()
headers = { 'Accept' : '*/*', 'Content-Type': 'application/rdf+xml' }
conn = httplib.HTTPConnection(self.store_address)
conn.request("PUT", "/data/?graph=%s" % iri, body=payload, headers=headers)
conn.getresponse()
conn.close()
def delete_graph(self, ds_object):
'''
Delete an object from the store
'''
conn = httplib.HTTPConnection(self.store_address)
conn.request("DELETE", "/data/?graph=%s" % ds_object.get_graph_uri())
conn.close()
def create_graph(self):
'''
Return a new object for the store
'''
uid = str(uuid.uuid4())
graph_iri = OLPC_GRAPHS[uid]
return GSGraph(graph_iri)
def get_graph_by_content(self, uid):
'''
Return an object which contains a description about UID
'''
# First, try to find a graph
query_string = """
SELECT DISTINCT ?graph WHERE {
GRAPH ?graph {?s <%s> '%s'}.
?graph a <%s>.
}
""" % (OLPC_TERMS['uid'], uid, OLPC_TERMS['DataGraph'])
sparql = SPARQLWrapper("http://%s/sparql" % self.store_address)
sparql.setReturnFormat(Wrapper.JSON)
sparql.setQuery(query_string)
results = sparql.query().convert()
graphs = [result["graph"]["value"] for result in results["results"]["bindings"]]
if len(graphs) == 0:
return None
# Load the object
query_string = "CONSTRUCT {?s ?p ?o} WHERE { GRAPH <%s> {?s ?p ?o} }" % graphs[0]
sparql = SPARQLWrapper("http://%s/sparql" % self.store_address)
sparql.setReturnFormat(Wrapper.RDF)
sparql.setQuery(query_string)
return GSGraph(graphs[0], sparql.query().convert())
def get_metadata(self, uid, properties=None):
resource_iri = OLPC_RESOURCE[uid]
metadata = {}
query = 'SELECT DISTINCT ?p ?o WHERE { <%s> ?p ?o. }' % resource_iri
sparql = SPARQLWrapper("http://%s/sparql" % self.store_address)
sparql.setReturnFormat(Wrapper.JSON)
sparql.setQuery(query)
res = sparql.query().convert()
for result in res['results']['bindings']:
if result['p']['value'].startswith(OLPC_TERMS):
key = result['p']['value'].split(OLPC_TERMS)[1]
if properties == None or key in properties:
r = None
if result['o']['type'] == 'literal':
if 'datatype' in result['o']:
if result['o']['datatype'] == str(XSD.integer):
r = int(result['o']['value'])
elif result['o']['datatype'] == str(XSD.long):
r = long(result['o']['value'])
elif result['o']['datatype'] == str(XSD.hexBinary):
r = dbus.ByteArray(binascii.unhexlify(result['o']['value']))
else:
logging.debug('[SDS] Unknown data type %s', result['o']['datatype'])
traceback.print_exc()
else:
r = str(result['o']['value'])
elif result['o']['type'] == 'uri':
r = str(result['o']['value'])
metadata[key] = r
# HACK: This is expected to be always present
if 'creation_time' not in metadata:
metadata['creation_time'] = int(time.time())
return metadata
def execute_select(self, query):
sparql = SPARQLWrapper("http://%s/sparql" % self.store_address)
sparql.setReturnFormat(Wrapper.JSON)
sparql.setQuery(query)
res = sparql.query().convert()
return res['results']['bindings']
_instance = None
def get_instance():
global _instance
if _instance is None:
_instance = GraphStore()
return _instance
if __name__ == '__main__':
semantic_ds = GraphStore('localhost')
#ds_object = semantic_ds.create_object()
#semantic_ds.persist_object(ds_object)
#objects = semantic_ds.get_objects_list()
#for o in objects:
# print semantic_ds.get_object(o).as_ntriples()
print semantic_ds.get_metadata('5e6a2172-9e23-4732-a0b8-5766026dce7b')
|