From 56f1317d424e37d88f3091f74a71a88709cbf4a1 Mon Sep 17 00:00:00 2001 From: Christophe Gueret Date: Thu, 08 Nov 2012 22:49:11 +0000 Subject: Updated patch_my_xo with latest versions of everything --- diff --git a/activity_review/src/dist/Review-1.xo b/activity_review/src/dist/Review-1.xo new file mode 100644 index 0000000..ef7c651 --- /dev/null +++ b/activity_review/src/dist/Review-1.xo Binary files differ diff --git a/patch_my_xo/semanticxo.tar.gz b/patch_my_xo/semanticxo.tar.gz index 0ca84cb..4535567 100644 --- a/patch_my_xo/semanticxo.tar.gz +++ b/patch_my_xo/semanticxo.tar.gz Binary files differ diff --git a/patch_my_xo/semanticxo/etc/cron.d/replicate-graphs b/patch_my_xo/semanticxo/etc/cron.d/replicate-graphs index 9eabbd6..345feae 100644 --- a/patch_my_xo/semanticxo/etc/cron.d/replicate-graphs +++ b/patch_my_xo/semanticxo/etc/cron.d/replicate-graphs @@ -1,6 +1,6 @@ -# Check if there are new graphs to replicate every 5 minutes +# Check if there are new graphs to replicate every 2 minutes MAILTO="" PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin SHELL=/bin/bash -*/5 * * * * root /usr/sbin/replicate-graphs 1>/dev/null 2>/dev/null +*/2 * * * * root /usr/sbin/replicate-graphs 1>/dev/null 2>/dev/null diff --git a/patch_my_xo/semanticxo/usr/lib/python2.7/site-packages/semanticxo/graphstore.py b/patch_my_xo/semanticxo/usr/lib/python2.7/site-packages/semanticxo/graphstore.py index 844169d..547ac64 100644 --- a/patch_my_xo/semanticxo/usr/lib/python2.7/site-packages/semanticxo/graphstore.py +++ b/patch_my_xo/semanticxo/usr/lib/python2.7/site-packages/semanticxo/graphstore.py @@ -6,7 +6,7 @@ Created on Feb 7, 2012 # 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 import ConjunctiveGraph, RDF, RDFS, Literal from rdflib.term import URIRef from rdflib.namespace import XSD from SPARQLWrapper import SPARQLWrapper, Wrapper @@ -60,18 +60,19 @@ class GSResource(object): 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)): + 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)): + for (_, _, o) in self._graph.triples((self._resource_uri, OLPC_TERMS['uid'], None)): return o def get(self, prop): @@ -84,7 +85,7 @@ class GSResource(object): p = OLPC_TERMS[prop] else: p = prop - for (_,_,o) in self._graph.triples((self._resource_uri, p, None)): + for (_, _, o) in self._graph.triples((self._resource_uri, p, None)): results.append(o) return results @@ -93,7 +94,7 @@ class GSResource(object): Return the different values associated to a particular property ''' results = [] - for (_,p,_) in self._graph.triples((self._resource_uri, None, None)): + for (_, p, _) in self._graph.triples((self._resource_uri, None, None)): results.append(p) return results @@ -118,6 +119,15 @@ class GSResource(object): 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. @@ -170,9 +180,14 @@ class GSGraph(object): 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): @@ -190,10 +205,19 @@ class GSGraph(object): if restrict != None: restrict_uri = OLPC_TERMS[restrict] for t in self._graph.triples((None, RDF.type, restrict_uri)): - (s,_,o) = t + (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): ''' @@ -207,6 +231,13 @@ class GSGraph(object): ''' 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. @@ -221,17 +252,6 @@ class GraphStore(object): 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 @@ -246,23 +266,62 @@ class GraphStore(object): 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 +="}" + 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) @@ -291,14 +350,50 @@ class GraphStore(object): conn.request("DELETE", "/data/?graph=%s" % ds_object.get_graph_uri()) conn.close() - def create_graph(self): + def create_graph(self, name=None): ''' Return a new object for the store ''' uid = str(uuid.uuid4()) graph_iri = OLPC_GRAPHS[uid] - return GSGraph(graph_iri) + 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 @@ -318,12 +413,8 @@ class GraphStore(object): 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()) + # Return the graph + return self.get_graph(graphs[0]) def get_metadata(self, uid, properties=None): resource_iri = OLPC_RESOURCE[uid] @@ -378,9 +469,9 @@ def get_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() + # 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') diff --git a/patch_my_xo/semanticxo/usr/lib/python2.7/site-packages/semanticxo/replicationservice.py b/patch_my_xo/semanticxo/usr/lib/python2.7/site-packages/semanticxo/replicationservice.py index dbf5a61..d3e8fce 100644 --- a/patch_my_xo/semanticxo/usr/lib/python2.7/site-packages/semanticxo/replicationservice.py +++ b/patch_my_xo/semanticxo/usr/lib/python2.7/site-packages/semanticxo/replicationservice.py @@ -30,7 +30,6 @@ class GraphReplicator(object): # Get the list of contacts contacts = addressbook.get_neighbours() - print contacts # Iterate through all of them for host in contacts.itervalues(): diff --git a/patch_my_xo/setup.sh b/patch_my_xo/setup.sh index 1bd4df1..3c43228 100644 --- a/patch_my_xo/setup.sh +++ b/patch_my_xo/setup.sh @@ -37,6 +37,7 @@ setup() echo -n $"Install demo activities" sudo -u olpc sugar-install-bundle /tmp/Mail-1.xo sudo -u olpc sugar-install-bundle /tmp/JournalBrowser-1.xo + sudo -u olpc sugar-install-bundle /tmp/Review-1.xo success echo } -- cgit v0.9.1