def get_fragment_triples(fid): if not r.sismember('fragments', fid): raise NotFound('The fragment {} does not exist'.format(fid)) if r.get('fragments:{}:pulling'.format(fid)) is not None: triples = [u'{} {} {} .'.format(s.n3(), p.n3(), o.n3()) for (c, s, p, o) in load_stream_triples(fid, '+inf')] response = make_response('\n'.join(triples)) response.headers['content-type'] = 'text/n3' return response return 'hello!'
def __replace_fragment(fid): """ Recreate fragment <fid> cached data and all its data-contexts from the corresponding stream (Redis) :param fid: :return: """ tps = cache.get_context(fid).subjects(RDF.type, AGORA.TriplePattern) cache.remove_context(cache.get_context("/" + fid)) for tp in tps: cache.remove_context(cache.get_context(str((fid, __triple_pattern(cache, tp))))) fragment_triples = load_stream_triples(fid, calendar.timegm(dt.now().timetuple())) for c, s, p, o in fragment_triples: cache.get_context(str((fid, c))).add((s, p, o)) cache.get_context("/" + fid).add((s, p, o)) with r.pipeline() as pipe: pipe.delete("fragments:{}:stream".format(fid)) pipe.execute()
def fragment(self, stream=False, timestamp=None, result_set=False): def __transform(x): if x.startswith('"'): return Literal(x.replace('"', ''), datatype=XSD.string).n3(self.graph(stream).namespace_manager) return x def __build_query_pattern(x): if '"' in x: return '{ %s }' % x return 'OPTIONAL { %s }' % x def __read_contexts(): contexts = self.sink.fragment_contexts triple_patterns = {context: eval(context)[1] for context in contexts} for context in self.sink.fragment_contexts: for (s, p, o) in self.graph().get_context(context): yield triple_patterns[context], s, p, o if timestamp is None: timestamp = calendar.timegm(dt.now().timetuple()) from_streaming = stream and not self.sink.backed if from_streaming: triples = load_stream_triples(self.sink.fragment_id, timestamp) return triples, stream elif stream: return __read_contexts(), from_streaming gp = [' '.join([__transform(self.sink.map(part)) for part in tp.split(' ')]) for tp in self.sink.gp] if result_set: # where_gp = ' '.join(map(__build_query_pattern, gp)) where_gp = ' . '.join(gp) # TODO: Consider using selective OPTIONAL clauses query = """SELECT %s WHERE { %s }""" % (' '.join(self.sink.preferred_labels), where_gp) else: where_gp = ' . '.join(gp) query = """CONSTRUCT WHERE { %s }""" % where_gp result = [] try: result = self.graph(data=True).query(query) except Exception, e: # ParseException from query log.warning(e.message)