def test5(): g = j.JenaGraph() g.read("./data/d-nops.ttl") triple_masks = [(None, None, None), (None, j.U("rdf:type"), None), (j.U("e:nop1"), None, None), (None, None, j.L("End")), (None, None, j.U("rdf:yes")), (j.U("e:nop1-start"), j.U("alg:next"), None)] c = 0 for t in triple_masks: print c, g.triples(*t) c += 1
def test7(): g = j.JenaGraph() g.read("./data/d-nops.ttl") df = g.select("select * { ?s ?p ?o }", initial_binding={'s': j.U("e:nop1-start")}) print df print g.select("select * { ?s ?p ?o }", initial_binding={'s': df.loc[0, 'o'] }) # blank node in binding works print "====" print g.select("select * { ?s ?p ?o }", initial_binding={'p': j.U("rdf:type")}) print g.select("select * { ?s ?p ?o }", initial_binding={'o': j.U("alg:AlgorithmEnd")}) print g.select("select * { ?s ?p ?o }", initial_binding={'o': j.L("Start")})
def load(self, fuseki_con): rq = "select ?class_uri from ?shacl_g { ?class_uri rdf:type rdfs:Class }" df = fuseki_con.select(rq, {'shacl_g': j.U('testdb:shacl-defs')}) for t in df.itertuples(): #print t.class_uri shacl_class_def = SHACLClass(t.class_uri) shacl_class_def.load_details(fuseki_con) self.classes[t.class_uri] = shacl_class_def
def select(self, rq, initial_bindings_): print "Fuseki::select:", rq, initial_bindings_ initial_bindings = {k: j.U(v) for k, v in initial_bindings_.items()} rq_res = self.fuseki_conn.select(rq, initial_bindings) print "Fuseki::select res:" ret = {} for col in rq_res.columns: ret[col] = to_json_UBL(rq_res.loc[:, col]) return ret
def process_insert(self, r, fuseki_con): if not 'class' in r: raise Exception('class must be specfied') class_uri = ttl_uri_to_U(r['class']) del r['class'] if not class_uri in self.shacl_defs.get_classes(): raise Exception('unknown class %s' % class_uri) class_members = self.shacl_defs.get_class_members(class_uri) subj = j.U("testdb:" + uuid.uuid4().hex) triples = [] triples.append((subj, j.U("rdf:type"), class_uri)) for member in filter(lambda x: is_ttl_uri(x), r.keys()): member_uri = ttl_uri_to_U(member) if not member_uri in class_members.members.keys(): raise Exception("no such member: %s" % member) triples.append((subj, member_uri, j.L(r[member]))) g = j.JenaGraph() ipdb.set_trace() g.add_triples(triples) fuseki_con.write_model(g)
def test6(): g = j.JenaGraph() g.read("./data/d-nops.ttl") ts = g.triples(j.U("e:nop1-start"), j.U("alg:next"), None) print ts[0][2] res = g.triples(ts[0][2], None, None) print res print len(res) new_triples = [(ts[0][2], j.U("rdfs:label"), j.L("Chudo"))] g.add_triples(new_triples) res = g.triples(ts[0][2], None, None) print res print len(res) g.remove_triples(new_triples) res = g.triples(ts[0][2], None, None) print res print len(res)
def to_UBL_initial_bindins(initialBindings): initial_bindings = {} for k, v in initialBindings.items(): if v.ublType == FusekiTest.EnumUBLType.U: vv = j.U(v.resource) elif v.ublType == FusekiTest.EnumUBLType.B: vv = j.B(v.resource) elif v.ublType == FusekiTest.EnumUBLType.L: vv = j.L(v.resource) else: raise Exception("unknown ubltype") initial_bindings[k] = vv return initial_bindings
def test8(): g = j.JenaGraph() g.read("./data/d-nops.ttl") nodes = g.select("select distinct ?s { ?s ?p ?o }") nodes_d = {} print nodes for node in nodes.itertuples(): nodes_d[node.s] = True print nodes_d keys = sorted(nodes_d.keys()) last_key = j.U(keys[-1].jena_resource.getURI()) #last_key = keys[-1] for key in keys: print nodes_d[key], key print "LAST KEY:", last_key, last_key in nodes_d, nodes_d[ last_key] if last_key in nodes_d else None print[isinstance(x.get(), ji.RDFNode) for x in nodes_d.keys()]
def load_details(self, fuseki_con): rq = """select ?class_uri ?mpath ?mdt ?mclass from ?shacl_g { { ?this_class_uri rdfs:subClassOf+ ?class_uri } union { bind(?this_class_uri as ?class_uri) } ?shape rdf:type sh:NodeShape; sh:targetClass ?class_uri. ?shape sh:property ?shape_prop. ?shape_prop sh:path ?mpath. { ?shape_prop sh:datatype ?mdt } union { ?shape_prop sh:class ?mclass } } """ df = fuseki_con.select(rq, { 'shacl_g': j.U('testdb:shacl-defs'), 'this_class_uri': self.class_uri }) for r in df.itertuples(): self.members[r.mpath] = (r.class_uri, r.mpath, r.mdt, r.mclass)
def process_insertupdate(self, r, fuseki_con): #ipdb.set_trace() if not 'class' in r: raise Exception('class must be specfied') class_uri = ttl_uri_to_U(r['class']) del r['class'] if not class_uri in self.shacl_defs.get_classes(): raise Exception('unknown class %s' % class_uri) maybe_subj = j.U("testdb:" + uuid.uuid4().hex) values = [] for col in filter(lambda x: is_ttl_uri(x), r.keys()): values.append((col, r[col])) #ipdb.set_trace() values_s = "(" + ")(".join([v[0] + ' "' + v[1] + '"' for v in values]) + ")" key = ttl_uri_to_U(r['key']) key_value = j.L(r[r['key']]) rq = """ delete { ?s ?pred ?old_v } insert { ?s ?pred ?v } where { optional {?sx ?key ?key_val} bind(if(bound(?sx), ?sx, ?maybe_s) as ?s) values (?pred ?v) { %s } optional {?s ?pred ?old_v} } """ % values_s bindings = {"maybe_s": maybe_subj, "key": key, "key_val": key_value} #print rq #print bindings #ipdb.set_trace() fuseki_con.update(rq, bindings)
def ttl_uri_to_U(s): if not is_ttl_uri(s): raise Exception("string is not turtle uri") return j.U(s.replace("<", "").replace(">", ""))
import sys sys.path.append("/home/asmirnov/pyjenautils") from pyjenautils import fuseki, prefix, jenagraph as j if __name__ == "__main__": #prefix.add_prefix("sh", "http://www.w3.org/ns/shacl#") fuseki_conn = fuseki.FusekiConnection("http://localhost:3030/testdb") if 1: print fuseki_conn.select("select * { bind(?_p as ?p) ?s ?p ?o }", initial_binding={'_p': j.U("rdf:type")}) print fuseki_conn.select("select * { ?s ?p ?o }", initial_binding={'p': j.U("rdf:type")}) print fuseki_conn.select("select * { ?s ?p ?v }", initial_binding={'v': j.L(12)}) print fuseki_conn.select("select ?s ?p ?o { ?s ?p ?o }", initial_binding={}) if 0: # test of blak node binding print fuseki_conn.select( "select * from <testdb:shacl-defs> { ?s ?p ?o }") print "-------------------" df = fuseki_conn.select("select * from ?g { ?s ?p ?o }", initial_binding={ '?g': j.U('testdb:shacl-defs'), '?o': j.U('testdb:member33') }) print df
def construct(self, rq, initial_bindings_): print "Fuseki::construct:", rq, initial_bindings_ initial_bindings = {k: j.U(v) for k, v in initial_bindings_.items()} rq_res = self.fuseki_conn.construct(rq, initial_bindings) print "Fuseki::construct res:" return map(lambda row: map(to_UBL, row), rq_res)
def update(self, rq, initial_bindings_): print "Fuseki::update:", rq, initial_bindings_ initial_bindings = {k: j.U(v) for k, v in initial_bindings_.items()} self.fuseki_conn.update(rq, initial_bindings) print "DONE Fuseki::update:"