def get_collection(self, c_id=None, filter=[]): # todo: ASK and check if collection exists if c_id is not None: result = self.sparql.select(self.marmotta.ldp(encoder.encode(c_id))) graph = result.toDataset().graph(self.marmotta.ldp(encoder.encode(c_id))) contents = self.RDA.graph_to_object(graph) if len(contents) is 0: raise NotFoundError() else: binds = [Bind(Variable('s'), self.marmotta.ldp()), Bind(Variable('p'), LDP.ns.contains)] filters = [ Filter(s=Variable('o'),p=URIRef(v.get('label')) if k.type is RDA.Collection else LDP.contains/LDP.contains/URIRef(v.get('label')), o=v.get('rdf', Literal)(k.value)) for v,k in [ (access(self.RDA.dictionary.get(f.type).inverted, f.path), f) for f in filter ] ] result = self.sparql.list(binds, filters) collections = [dct[Variable('o')] for dct in result.bindings] contents = [] if len(collections): result = self.sparql.select(collections) ds = result.toDataset() graphs = [ds.graph(collection) for collection in collections] for graph in graphs: contents += self.RDA.graph_to_object(graph) return contents
def _enrich(g, database, schema, graph): ns_tbox = _generate_tbox_namespace(graph) # update namespaces ns_abox = default_namespace_of(graph)[0] _update_namespaces(g.namespace_manager,\ ('rws.' + database.lower(), ns_abox), ('geo', Namespace("http://www.opengis.net/ont/geosparql#"))) for entry in schema.from_database(database): source_def = entry['source'] target_def = entry['target'] source_class = _classname_from_def(database, source_def[0]) attributes = [definition['property'] for definition in source_def] properties = [ property_from_mapping(ns_tbox, source_class, attr)\ for attr in attributes ] # select target references q = _generate_query(URIRef(ns_tbox + source_class), attributes, properties) for binding in graph.query(q).bindings: source_uri = binding[Variable('source_id')] if source_uri is None or source_uri == "": continue values = { '['+attr+']': binding[Variable(attr)].toPython() for attr in attributes\ if Variable(attr) in binding.keys() } g += _generate_branch(schema, ns_abox, target_def, source_uri, values)
def rdf_cksum(self, gr): ''' Generate a checksum for a graph. What this method does is ordering the graph by subject, predicate, object, then creating a pickle string and a checksum of it. N.B. The context of the triples is ignored, so isomorphic graphs would have the same checksum regardless of the context(s) they are found in. @TODO This can be later reworked to use a custom hashing algorithm. :param rdflib.Graph: gr The graph to be hashed. :rtype: str :return: SHA1 checksum. ''' # Remove the messageDigest property, which very likely reflects the # previous state of the resource. gr.remove((Variable('s'), nsc['premis'].messageDigest, Variable('o'))) ord_gr = sorted(list(gr), key=lambda x: (x[0], x[1], x[2])) hash = sha1(pickle.dumps(ord_gr)).hexdigest() return hash
def test_24_result_bindings(self): b = list(self.graph.query('SELECT (1 as ?x) {}')) assert b[0][0] == Literal(1) assert b[0]['x'] == Literal(1) assert b[0][Variable('x')] == Literal(1) b = self.graph.query('SELECT (1 as ?x) {}').bindings assert b[0][Variable('x')] == Literal(1) assert b[0]['x'] == Literal(1)
def _construct(self, sparql: str, **kwargs) -> Graph: result = self._select(sparql, **kwargs) S = Variable("s") P = Variable("p") O = Variable("o") neo = Graph() for fact in result.bindings: neo.add((fact[S], fact[P], fact[O])) return neo
def set_member(self, c_id, m_obj): if isinstance(m_obj, Model): m_obj = [m_obj] elif not isinstance(m_obj, list): raise ParseError() c_ldp_id = self.marmotta.ldp(encoder.encode(c_id)) collection = self.get_collection(c_id).pop() # 404 if collection not found if len(set([m.id for m in m_obj])) is not len(m_obj): raise ForbiddenError() if not collection.capabilities.membershipIsMutable: raise ForbiddenError() if collection.capabilities.restrictedToType: for m in m_obj: if not(hasattr(m,"datatype") and m.datatype in collection.capabilities.restrictedToType): raise ForbiddenError() if collection.capabilities.maxLength >= 0: size = self.sparql.size(c_ldp_id).bindings.pop().get(Variable('size')) if int(size) > collection.capabilities.maxLength-len(m_obj): raise ForbiddenError()#"Operation forbidden. Collection of maximum size {} is full.".format(collection.capabilities.maxLength)) ds = Dataset() ldp = ds.graph(identifier=LDP.ns) for m in m_obj: m_id = self.marmotta.ldp(encoder.encode(c_id)+"/member/"+encoder.encode(m.id)) member = ds.graph(identifier=m_id) member += self.RDA.object_to_graph(member.identifier,m) ldp += LDP.add_contains(c_ldp_id+"/member",m_id,False) res = self.sparql.insert(ds) if res.status_code is not 200: raise DBError() return m_obj
def __init__(self,graphNodeList=None): self.propVals = [] if graphNodeList: self._list = graphNodeList self.identifier = Variable(BNode()) else: self._list = graphNodeList and graphNodeList or [] self.identifier = URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#nil')
def _query_bindings(triple, g=None, to_n3=True): (s, p, o) = triple if isinstance(g, Graph): g = g.identifier if s is None: s = Variable("S") if p is None: p = Variable("P") if o is None: o = Variable("O") if g is None: g = Variable("G") if isinstance(s, BNode): s = _bnode_to_nodeid(s) if isinstance(p, BNode): p = _bnode_to_nodeid(p) if isinstance(o, BNode): o = _bnode_to_nodeid(o) if isinstance(g, BNode): g = _bnode_to_nodeid(g) if to_n3: return dict(zip("SPOG", [x.n3() for x in (s, p, o, g)])) else: return dict(zip("SPOG", [x for x in (s, p, o, g)]))
def frame2triple(self, root: dict, parent: Variable, triples: list, extra: list, pref=None) -> None: """ convert a json frame to s-p-o triples recursively :param root: json frame :param parent: subject for now :param triples: a list to store the results :param extra: indeces of extra triples """ for k, v in root.items(): if k == '@context': continue p = self.to_node(k) if isinstance(v, str): o = self.to_node(v) else: if (parent.toPython(), k) in self.exist_triples: o_str = self.exist_triples[(parent.n3(), k)] if o_str[0] in ['$', '?']: o = Variable(o_str) elif o_str[0] in ['"', "'"]: o = Literal(o_str.strip('"').strip("'")) elif o_str[0] in ['<']: o = URIRef(o_str) else: o = Literal(o_str.strip('"').strip("'")) else: o = Variable('var%d' % self.current_naming) self.current_naming += 1 extra.append(len(triples)) if not isinstance(p, Literal): triples.append(pref + [[parent, p, o]]) else: print('failed to find context of %s.' % p) if isinstance(v, dict) and len(v): self.frame2triple(v, o, triples, extra, pref + [[parent, p, o]])
def test_14_initBindings(self): TST = Namespace('http://example.com/ns/') self.graph.add((TST.a, TST.b, TST.c)) self.graph.add((TST.d, TST.e, TST.f)) result = self.graph.query( "SELECT * { ?s ?p ?o }", initBindings={ "p": TST.b, Variable("o"): TST.c, }, ) assert result.type == "SELECT", result.type assert len(result) == 1
def f(bindings): if unBound: # @@note, param must be reassigned to avoid tricky issues of scope # see: http://docs.python.org/ref/naming.html _param = isinstance(param, Variable) and param or Variable( param[1:]) val = bindings[_param] if isinstance(val, Literal): return getLiteralValue(val) else: return val else: return value
def _sparql_select(self, q, cursor): log.debug("_sparql_select") results = cursor.execute(q.encode("utf-8")) vars = [Variable(col[0]) for col in results.description] var_dict = VirtuosoResultRow.prepare_var_dict(vars) def f(): for r in results: try: yield VirtuosoResultRow([resolve(cursor, x) for x in r], var_dict) except Exception, e: log.debug("skip row, because of %s", e) pass
def get_member(self, c_id, m_id=None, filter=[]): # todo: ASK and check if member exists if m_id is not None: if not isinstance(m_id, list): m_id = [m_id] members = [self.marmotta.ldp(encoder.encode(c_id)+"/member/"+encoder.encode(id)) for id in m_id] # ds = self.sparql.select(ids).toDataset() # contents = [self.RDA.graph_to_member(ds.graph(id)) for id in ids] else: id = self.marmotta.ldp(encoder.encode(c_id)) if not self.sparql.ask(id, self.RDA.ns.Collection).askAnswer: raise NotFoundError lst = self.sparql.list([Bind(Variable('s'), self.marmotta.ldp(encoder.encode(c_id)+"/member")), Bind(Variable('p'), LDP.ns.contains)]) members = [dct[Variable('o')] for dct in lst.bindings] contents=[] if len(members): dataset = self.sparql.select(members).toDataset() graphs = [dataset.graph(member) for member in members] for graph in graphs: contents += self.RDA.graph_to_object(graph) if m_id is not None and len(contents) is 0: raise NotFoundError() return contents
def testToldBNode(self): for s, p, o in self.graph.triples((None, RDF.type, None)): pass query = """SELECT ?obj WHERE { %s ?prop ?obj }""" % s.n3() print query rt = self.graph.query(query, DEBUG=debug) self.failUnless( len(rt) == 1, "BGP should only match the 'told' BNode by name (result set size: %s)" % len(rt)) bindings = {Variable('subj'): s} query = """SELECT ?obj WHERE { ?subj ?prop ?obj }""" print query rt = self.graph.query(query, initBindings=bindings, DEBUG=debug) self.failUnless( len(rt) == 1, "BGP should only match the 'told' BNode by name (result set size: %s, BNode: %s)" % (len(rt), s.n3()))
def _select(self, sparql: str, **kwargs) -> dict: that = self._wrapper() that.setQuery(sparql) that.setReturnFormat(JSON) json_result = that.queryAndConvert() res = {} res["type_"] = "SELECT" res["vars_"] = [Variable(v) for v in json_result["head"]["vars"]] column = OrderedDict() bindings = [] for json_row in json_result["results"]["bindings"]: rdf_row = {} for variable in res["vars_"]: if str(variable) in json_row: rdf_row[variable] = self.jsonToNode( json_row[str(variable)]) else: rdf_row[variable] = None bindings.append(rdf_row) res["bindings"] = bindings return SPARQLResult(res)
def _sparql_select(self, q, cursor, must_close): log.debug("_sparql_select") results = cursor.execute(q) vars = [Variable(col[0]) for col in results.description] var_dict = VirtuosoResultRow.prepare_var_dict(vars) def f(): try: for r in results: try: yield VirtuosoResultRow( [resolve(cursor, x) for x in r], var_dict) except Exception as e: log.debug("skip row, because of %s", e) pass finally: if must_close: cursor.close() e = EagerIterator(f()) e.vars = vars e.selectionF = e.vars return e
def test_rdflib_mysql_test(self): """ test taken from rdflib/test/test_mysql.py """ implies = URIRef("http://www.w3.org/2000/10/swap/log#implies") testN3=""" @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix : <http://test/> . {:a :b :c;a :foo} => {:a :d :c,?y}. _:foo a rdfs:Class. :a :d :c.""" #Thorough test suite for formula-aware store g = self.rdflib_graph g.parse(data=testN3, format="n3") #print g.store for s,p,o in g.triples((None,implies,None)): formulaA = s formulaB = o self.assertTrue(type(formulaA)==QuotedGraph and type(formulaB)==QuotedGraph) a = URIRef('http://test/a') b = URIRef('http://test/b') c = URIRef('http://test/c') d = URIRef('http://test/d') v = Variable('y') universe = ConjunctiveGraph(g.store) #test formula as terms self.assertTrue(len(list(universe.triples((formulaA,implies,formulaB))))==1) #test variable as term and variable roundtrip self.assertTrue(len(list(formulaB.triples((None,None,v))))==1) for s,p,o in formulaB.triples((None,d,None)): if o != c: self.assertTrue(isinstance(o,Variable)) self.assertTrue(o == v) s = list(universe.subjects(RDF.type, RDFS.Class))[0] self.assertTrue(isinstance(s,BNode)) self.assertTrue( len(list(universe.triples((None,implies,None)))) == 1) self.assertTrue( len(list(universe.triples((None,RDF.type,None)))) ==1) self.assertTrue( len(list(formulaA.triples((None,RDF.type,None))))==1) self.assertTrue( len(list(formulaA.triples((None,None,None))))==2) self.assertTrue( len(list(formulaB.triples((None,None,None))))==2) self.assertTrue( len(list(universe.triples((None,None,None))))==3) self.assertTrue( len(list(formulaB.triples((None,URIRef('http://test/d'),None))))==2) self.assertTrue( len(list(universe.triples((None,URIRef('http://test/d'),None))))==1) #context tests #test contexts with triple argument self.assertTrue( len(list(universe.contexts((a,d,c))))==1) #Remove test cases universe.remove((None,implies,None)) self.assertTrue( len(list(universe.triples((None,implies,None))))==0) self.assertTrue( len(list(formulaA.triples((None,None,None))))==2) self.assertTrue( len(list(formulaB.triples((None,None,None))))==2) formulaA.remove((None,b,None)) self.assertTrue( len(list(formulaA.triples((None,None,None))))==1) formulaA.remove((None,RDF.type,None)) self.assertTrue( len(list(formulaA.triples((None,None,None))))==0) universe.remove((None,RDF.type,RDFS.Class)) #remove_context tests universe.remove_context(formulaB) self.assertTrue( len(list(universe.triples((None,RDF.type,None))))==0) self.assertTrue( len(universe)==1) self.assertTrue( len(list(formulaB.triples((None,None,None))))==0) universe.remove((None,None,None)) self.assertTrue( len(universe)==0)
"illegal argument, pattern must be a tuple of 3 or 4 element, got %s" % len(tupl)) if len(tupl) == 3: (s, p, o) = tupl f = None else: (s, p, o, f) = tupl final = [] for c in (s, p, o): if isinstance(c, Variable): if not c in self.unbounds: self.unbounds.append(c) final.append(c) elif isinstance(c, BNode): #Do nothing - BNode name management is handled by SPARQL parser final.append(c) else: final.append(_createResource(c)) final.append(f) return tuple(final) def fetchTerminalExpression(self): yield self if __name__ == '__main__': v1 = Variable("a") g = BasicGraphPattern([("a", "?b", 24), ("?r", "?c", 12345), (v1, "?c", 3333)]) print g
def ask_member(self, c_id, m_id): if not isinstance(m_id, list): m_id = [m_id] ids = [self.marmotta.ldp(encoder.encode(c_id)+"/member/"+encoder.encode(m)) for m in m_id] result = self.sparql.find(ids,self.RDA.ns.Member) return float(result.bindings.pop().get(Variable('size')))/len(m_id)
import pyparsing match_definitions = [ (parser.BaseDecl, [ ('BASE <s:ex>', URIRef('s:ex')), ('BASE <>', URIRef('')), ]), (parser.PrefixDecl, [ ('PREFIX s: <s:ex>', components.PrefixDeclaration( 's:', 's:ex')), ]), (parser.Var, [ ('?foo', Variable('foo')), ('$bar', Variable('bar')), ]), (parser.IRIref, [ ('pre:local', components.QName('pre:local')), ('<s:ex>', URIRef('s:ex')), ]), (parser.RDFLiteral, [ ('"foo"', Literal("foo")), ('"foo"@en-US', Literal("foo", lang='en-US')), ("'bar'^^pre:type", components.ParsedDatatypedLiteral( "bar", components.QName('pre:type'))), ("'bar'^^<s:type>", components.ParsedDatatypedLiteral( "bar", URIRef('s:type'))),
def testN3Store(store="default", configString=None): g = ConjunctiveGraph(store=store) if configString: g.destroy(configString) g.open(configString) g.parse(data=testN3, format="n3") print g.store try: for s, p, o in g.triples((None, implies, None)): formulaA = s formulaB = o assert type(formulaA) == QuotedGraph and type(formulaB) == QuotedGraph a = URIRef('http://test/a') b = URIRef('http://test/b') c = URIRef('http://test/c') d = URIRef('http://test/d') v = Variable('y') universe = ConjunctiveGraph(g.store) #test formula as terms assert len(list(universe.triples((formulaA, implies, formulaB)))) == 1 #test variable as term and variable roundtrip assert len(list(formulaB.triples((None, None, v)))) == 1 for s, p, o in formulaB.triples((None, d, None)): if o != c: assert isinstance(o, Variable) assert o == v s = list(universe.subjects(RDF.type, RDFS.Class))[0] assert isinstance(s, BNode) assert len(list(universe.triples((None, implies, None)))) == 1 assert len(list(universe.triples((None, RDF.type, None)))) == 1 assert len(list(formulaA.triples((None, RDF.type, None)))) == 1 assert len(list(formulaA.triples((None, None, None)))) == 2 assert len(list(formulaB.triples((None, None, None)))) == 2 assert len(list(universe.triples((None, None, None)))) == 3 assert len( list(formulaB.triples((None, URIRef('http://test/d'), None)))) == 2 assert len( list(universe.triples((None, URIRef('http://test/d'), None)))) == 1 #context tests #test contexts with triple argument assert len(list(universe.contexts((a, d, c)))) == 1 #Remove test cases universe.remove((None, implies, None)) assert len(list(universe.triples((None, implies, None)))) == 0 assert len(list(formulaA.triples((None, None, None)))) == 2 assert len(list(formulaB.triples((None, None, None)))) == 2 formulaA.remove((None, b, None)) assert len(list(formulaA.triples((None, None, None)))) == 1 formulaA.remove((None, RDF.type, None)) assert len(list(formulaA.triples((None, None, None)))) == 0 universe.remove((None, RDF.type, RDFS.Class)) #remove_context tests universe.remove_context(formulaB) assert len(list(universe.triples((None, RDF.type, None)))) == 0 assert len(universe) == 1 assert len(formulaB) == 0 universe.remove((None, None, None)) assert len(universe) == 0 g.store.destroy(configString) except: g.store.destroy(configString) raise
def ask_collection(self, c_id): if not isinstance(c_id, list): c_id = [c_id] ids = [self.marmotta.ldp(encoder.encode(id)) for id in c_id] result = self.sparql.find(ids,self.RDA.ns.Collection) return float(result.bindings.pop().get(Variable('size')))/len(c_id)
def univar(self, label, sic=False): if not sic: self.counter += 1 label += str(self.counter) return Variable(label)