Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/patch_my_xo/semanticxo/usr/lib/python2.7/site-packages/semanticxo/graphstore.py
blob: 547ac6491fccd0cafa87cebb3a7059f5dae13dc2 (plain)
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
'''
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, RDFS, 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()
			
	def set(self, key, o):
		'''
		Set a triple, erasing all others using the same predicate
		'''
		if type(key) != URIRef:
			key = OLPC_TERMS[key]
		self._graph.remove((self._resource_uri, key, None))
		self.add(key, o)
		
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())
		# Create the resource
		resource = GSResource(graph=self._graph, resource_uri=OLPC_RESOURCE[uid])
		resource.add(RDF.type, OLPC_TERMS[category])
		resource.add(OLPC_TERMS['uid'], Literal(uid))
		# Declare it to the graph object
		self._graph.add((self._graph_uri, RDFS.member, resource.get_resource_uri()))
		print "Connect "
		print (self._graph_uri, RDFS.member, resource.get_resource_uri())
		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 find_resources(self, key, value):
		results = []
		if type(key) != URIRef:
			key = OLPC_TERMS[key]
		for t in self._graph.triples((None, key, value)):
			(s, _, o) = t
			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))

	def set(self, key, value):
		'''
		Assign a specific key/value pair to the graph
		'''
		self._graph.remove((self._graph_uri, key, None))
		self._graph.add((self._graph_uri, key, Literal(value)))
		
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_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_resources_list(self, restrict=None):
		'''
		Return a list of URIs for instances of a particular class
		'''
		results = []
		graph_ids = self.get_graphs_list(restrict=restrict)
		for graph_id in graph_ids[::-1]:
			graph = self.get_graph(graph_id)
			resource_ids = graph.get_resources_list(restrict=restrict)
			for resource_id in resource_ids:
				results.append(resource_id)
		return results
	
	def get_resource(self, identifier):
		'''
		Return the indicated resource
		'''
		# First find the graph that contains that resource and for which we are the author
		graphs = []
		query_string = ""
		query_string += "SELECT DISTINCT ?graph WHERE {"
		query_string += "?graph a <%s>." % OLPC_TERMS['DataGraph']
		query_string += "?graph <%s> ?mdate." % DC_TERMS['modified']
		query_string += "?graph <%s> <%s>." % (RDFS.member, identifier)
		# query_string += "?graph <%s> <%s>." % (DC_TERMS['author'], util.device_uri())
		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.append(result['graph']['value'])
		if len(graphs) == 0:
			return None
		
		# Load the graph and return the requested resource
		graph = self.get_graph(graphs[0])
		return graph.get_resource(identifier)
		
	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, name=None):
		'''
		Return a new object for the store
		'''
		uid = str(uuid.uuid4())
		graph_iri = OLPC_GRAPHS[uid]
		graph = GSGraph(graph_iri)
		if name != None:
			graph.set(OLPC_TERMS["name"], name)
		return graph

	def get_graph(self, object_iri):
		'''
		Return an instance of a GSGraph
		'''
		if type(object_iri) != URIRef:
			object_iri = URIRef(object_iri)
		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_graph_by_name(self, name):
		'''
		Return an instance of a GSGraph that has the indicated name and has been authored by this device
		'''
		# Look for a matching graph
		query_string = "SELECT DISTINCT ?graph WHERE {"
		query_string += "?graph a <%s>." % OLPC_TERMS['DataGraph']
		query_string += "?graph <%s> <%s>." % (DC_TERMS['author'], util.device_uri())
		query_string += "?graph <%s> \"%s\"." % (OLPC_TERMS['name'], Literal(name))
		query_string += "}"
		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
		
		# Return the graph
		return self.get_graph(graphs[0])
	
	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
		
		# Return the graph
		return self.get_graph(graphs[0])
		
	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')