diff options
author | cgueret <christophe.gueret@gmail.com> | 2012-02-29 07:00:20 (GMT) |
---|---|---|
committer | cgueret <christophe.gueret@gmail.com> | 2012-02-29 07:00:20 (GMT) |
commit | 18ed6e69d1ae28d34c75c4525d6aa0532fe82a6e (patch) | |
tree | e831c3596e85a15e8d50c3a90c355ea1f9f4724b /common | |
parent | 856f2e63d880bd3924d71d9eb51d0ad4cb68a5b2 (diff) |
Fixed update of DSObjects
Diffstat (limited to 'common')
-rw-r--r-- | common/src/semanticxo/semanticdatastore.py | 51 |
1 files changed, 48 insertions, 3 deletions
diff --git a/common/src/semanticxo/semanticdatastore.py b/common/src/semanticxo/semanticdatastore.py index 287e977..6ebff24 100644 --- a/common/src/semanticxo/semanticdatastore.py +++ b/common/src/semanticxo/semanticdatastore.py @@ -29,7 +29,8 @@ class SemanticDSEntry(object): logging.debug("[SDS] Create entry with parent %s and uid %s", parent, uid) self._resource_uri = OLPC_RESOURCE[uid] self._parent = parent - self._parent.add(self._resource_uri, RDF.type, OLPC_TERMS[category]) + self._type = OLPC_TERMS[category] + self._parent.add(self._resource_uri, RDF.type, self._type) self._parent.add(self._resource_uri, OLPC_TERMS['uid'], Literal(uid)) def get_resource_uri(self): @@ -45,6 +46,15 @@ class SemanticDSEntry(object): target_uri = target.get_resource_uri() self._parent.add(self._resource_uri, OLPC_TERMS[link_name], target_uri) + def clean(self): + ''' + Erase all the triples associated to this resource + ''' + # remove all + self._parent.remove(self._resource_uri, None, None) + # add the type again + self._parent.add(self._resource_uri, RDF.type, self._type) + def parse_metadata(self, metadata): ''' Turn metadata about a resource into triples @@ -108,6 +118,12 @@ class SemanticDSObject(object): ''' self._graph.add((s, p, o)) + def remove(self, s, p, o): + ''' + Remove a raw triple + ''' + self._graph.remove((s, p, o)) + def create_entry(self, uid=None, category=None): ''' Return a new object entry to be added to an instance of SemanticDSObject @@ -117,7 +133,11 @@ class SemanticDSObject(object): if uid == None: uid = str(uuid.uuid4()) return SemanticDSEntry(parent=self, uid=uid, category=category) - + + def get_entry(self, uid, category): + return SemanticDSEntry(parent=self, uid=uid, category=category) + + class SemanticDS(object): ''' The Semantic DataStore is an interface to the triple store. @@ -184,7 +204,32 @@ class SemanticDS(object): graph_iri = OLPC_GRAPHS[uid] return SemanticDSObject(graph_iri) - + def get_object_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.XML) + sparql.setQuery(query_string) + return SemanticDSObject(graphs[0], sparql.query().convert()) + def get_metadata(self, uid, properties=None): resource_iri = OLPC_RESOURCE[uid] metadata = {} |