def calculate_edges((offset_limit, config)): g = ConjunctiveGraph(config['graph_store'], config['graph_identifier']) g.open(config['db_configstring'], create=False) engine = create_engine(config['alchemy_configstring']) Session = sessionmaker(bind=engine) session = Session() results = session.query(Node).filter_by(node_type="subject").order_by( Node.node_id.desc()).offset(offset_limit[0]).limit( offset_limit[1]).all() for result in results: query = "SELECT ?p ?o WHERE { <%s> ?p ?o }" % result.node_uri items = g.query(query) for item in items: if str(item[0] ) != "http://www.w3.org/1999/02/22-rdf-syntax-ns#type": neighbors = session.query(Node).filter_by( node_uri=item[1]).all() if len(neighbors) > 0: for neighbor in neighbors: result.add_neighbors(str(item[0]), neighbor) session.commit() g.close() session.close()
def query_handler(source_id, query, context): # Attempt to cnonicalize this object by round-tripping decoding and re-encoding query = json.loads(json.dumps(query, sort_keys=True)) rdf_store = context["rdf_store"] graph = ConjunctiveGraph(rdf_store) graph.open(DB_URI) results = graph.query( "select ?subject ?predicate ?object where {?subject ?predicate ?object}" ) # Inelegant, but works: serialize to JSON string, then re-parse statements = json.loads(results.serialize(format="json")) graph.close() context = { "@context": { "@vocab": "http://facta.kendra.io/vocab#", "kv": "http://facta.kendra.io/vocab#", "kendra": "http://kendra.io/types#", "kuid": "http://kendra.io/uuid#", "schema": "http://schema.org/", "xsd": "http://www.w3.org/2001/XMLSchema#" } } compacted_graph, contained = s2j.result_data_to_jsonld( statements, context) result = s2j.extract_salient_results( compacted_graph, contained, ["kendra:InclusionRelationship", "kendra:TextSelection"]) result = s2j.quicksearch(result, query) return {"result": result}
def do(action): store = plugin.get("SQLAlchemy", Store)(identifier=identifier, configuration=db_uri) graph = ConjunctiveGraph(store) action(graph, db_uri) try: graph.close() except: pass
def example_1(): """Creates a ConjunctiveGraph and performs some BerkeleyDB tasks with it """ path = mktemp() # Declare we are using a BerkeleyDB Store graph = ConjunctiveGraph("BerkeleyDB") # Open previously created store, or create it if it doesn't exist yet # (always doesn't exist in this example as using temp file location) rt = graph.open(path, create=False) if rt == NO_STORE: # There is no underlying BerkeleyDB infrastructure, so create it print("Creating new DB") graph.open(path, create=True) else: print("Using existing DB") assert rt == VALID_STORE, "The underlying store is corrupt" print("Triples in graph before add:", len(graph)) print("(will always be 0 when using temp file for DB)") # Now we'll add some triples to the graph & commit the changes EG = Namespace("http://example.net/test/") graph.bind("eg", EG) graph.add((EG["pic:1"], EG.name, Literal("Jane & Bob"))) graph.add((EG["pic:2"], EG.name, Literal("Squirrel in Tree"))) graph.commit() print("Triples in graph after add:", len(graph)) print("(should be 2)") # display the graph in Turtle print(graph.serialize()) # close when done, otherwise BerkeleyDB will leak lock entries. graph.close() graph = None # reopen the graph graph = ConjunctiveGraph("BerkeleyDB") graph.open(path, create=False) print("Triples still in graph:", len(graph)) print("(should still be 2)") graph.close() # Clean up the temp folder to remove the BerkeleyDB database files... for f in os.listdir(path): os.unlink(path + "/" + f) os.rmdir(path)
def rdf_data(rdfobject, f): input = ConjunctiveGraph() input.open("store2", create=True) input.parse(data=rdfobject, format=f) #print(input.serialize(format='json-ld', auto_compact=True, indent=4)) for s, p, o in input: g.add((s, p, o)) input.close()
class ConfigTest(unittest.TestCase): def setUp(self): self.store = plugin.get("Elasticsearch", Store)() self.graph = ConjunctiveGraph(self.store) def tearDown(self): self.graph.close() def test_success(self): with patch.object(requests.Session, 'get') as p: self.graph.open('http://localhost:9200/collection', create=True) p.assert_called_with('http://localhost:9200/collection')
def ConvertToSQLLITE (filename,destinationFileName): _graph = ConjunctiveGraph() _graph.parse(filename, format="nt") sql = ConjunctiveGraph('SQLite') sql.open(destinationFileName, create=True) for t in _graph.triples((None,None,None)): sql.add(t) sql.commit() sql.close()
def blogs(): g = ConjunctiveGraph("Sleepycat") g.open("store") for person, blog in g.subject_objects(predicate=w.Blog): name = g.value(subject=person, predicate=w.Name) for title, feed_url in discover_feeds(blog): if title: title = "%s (%s)" % (name, title) else: title = name logging.info("found %s <%s>" % (title, feed_url)) yield title, feed_url g.close()
class SQLATestCase(unittest.TestCase): identifier = URIRef("rdflib_test") dburi = Literal("sqlite://") def setUp(self): self.store = plugin.get( "SQLAlchemy", Store)(identifier=self.identifier) self.graph = ConjunctiveGraph(self.store, identifier=self.identifier) self.graph.open(self.dburi, create=True) def tearDown(self): self.graph.destroy(self.dburi) try: self.graph.close() except: pass def test_registerplugins(self): # I doubt this is quite right for a fresh pip installation, # this test is mainly here to fill a coverage gap. registerplugins() self.assert_(plugin.get("SQLAlchemy", Store) is not None) p = plugin._plugins self.assert_(("SQLAlchemy", Store) in p, p) del p[("SQLAlchemy", Store)] plugin._plugins = p registerplugins() self.assert_(("SQLAlchemy", Store) in p, p) def test_namespaces(self): self.assert_(list(self.graph.namespaces()) != []) def test_contexts_without_triple(self): self.assert_(list(self.graph.contexts()) == []) def test_contexts_with_triple(self): statemnt = (michel, likes, pizza) self.assert_(self.graph.contexts(triple=statemnt) != []) def test__len(self): self.assert_(self.store.__len__() == 0) def test__remove_context(self): self.store._remove_context(self.identifier)
class DeepGraphStore(): store_name = 'SQLite' def __init__(self, create=False, parse=None): self.parse = parse self.create = create self.graph = None def setUp(self): self.path = "" + random_file_generating() self.graph = Graph(store=self.store_name) self.graph.open(self.path, create=self.create) if self.create: if not self.parse: self.graph.parse("http://njh.me/foaf.rdf", format='xml') else: self.graph.parse(self.parse) self.graph.commit() def open(self, path): self.graph = ConjunctiveGraph(self.store_name) self.path = path self.graph.open(self.path, create=False) def query(self, sparql_query): return self.graph.query(sparql_query) def parse(self, path_to_file_): self.graph.parse(path_to_file_) def load(self, triples): self.graph.load(triples) def close(self): self.graph.close() def size(self): size = self.graph.__len__() size = len(self.graph) # self.close() return size
def __init__(self, location, repository, inmemory=False): super(RDFLibStore, self).__init__(location, repository) self.inmemory = inmemory self.closed = False graphid = URIRef("file://" + self.repository) g = ConjunctiveGraph(store=self._storeid(), identifier=graphid) if os.path.exists(self.location): g.open(self.location, create=False) else: g.open(self.location, create=True) l = logging.getLogger(__name__) if inmemory: l.debug("Loading store into memory") ig = ConjunctiveGraph(identifier=graphid) ig.addN(g.quads()) g.close() self.graph = ig else: l.debug("Using on-disk store") self.graph = g
def calculate_edges((offset_limit, config)): g = ConjunctiveGraph(config['graph_store'], config['graph_identifier']) g.open(config['db_configstring'], create=False) engine = create_engine(config['alchemy_configstring']) Session = sessionmaker(bind=engine) session = Session() results = session.query(Node).filter_by(node_type="subject").order_by(Node.node_id.desc()).offset(offset_limit[0]).limit(offset_limit[1]).all() for result in results: query = "SELECT ?p ?o WHERE { <%s> ?p ?o }" % result.node_uri items = g.query(query) for item in items: if str(item[0]) != "http://www.w3.org/1999/02/22-rdf-syntax-ns#type": neighbors = session.query(Node).filter_by(node_uri=item[1]).all() if len(neighbors) > 0: for neighbor in neighbors: result.add_neighbors(str(item[0]), neighbor) session.commit() g.close() session.close()
def build_item_data_graph(item_type, item_properties, item_data_graph_path, endpoint_url, max_num_objects, create=True): item_data_graph = ConjunctiveGraph("Sleepycat") item_data_graph.open(item_data_graph_path, create=create) for item_property in item_properties: item_data_query = RETRIEVE_ITEM_PROPERTIES_QUERY % ( item_type, item_type, item_type, item_type, item_type, item_property) sparql_client = SPARQLWrapper(endpoint_url, returnFormat=JSON) sparql_client.setTimeout(604800) sparql_client.setQuery(item_data_query) results = sparql_client.queryAndConvert() num_bindings = len(results["results"]["bindings"]) added_triples = defaultdict(lambda: defaultdict(lambda: 0)) for i, binding in enumerate(results["results"]["bindings"]): print("[{}/{}]".format(i + 1, num_bindings)) subject = URIRef(binding["s"]["value"]) predicate = URIRef(binding["p"]["value"]) if binding["o"]["type"] == "literal": object = Literal(binding["o"]["value"], datatype=binding["o"]["datatype"]) else: object = URIRef(binding["o"]["value"]) if max_num_objects is not None: if added_triples[subject][predicate] < max_num_objects: triple = (subject, predicate, object) added_triples[subject][predicate] += 1 item_data_graph.add(triple) else: triple = (subject, predicate, object) item_data_graph.add(triple) item_data_graph.close()
class Graph(Store): name = 'graph' def __init__(self, logger=logging.getLogger(__name__), *args, **kwargs): super(Plugin, self).__init__(*args, **kwargs) self.logger = logger self._open_store() self.store_id = Literal(str(uuid.uuid4())) def _open_store(self, store='IOMemory'): self.logger.debug("opening store...") self.handle = ConjunctiveGraph(store, identifier="permanent") def _close_store(self): self.logger.debug("closing store") self.handle.close(commit_pending_transaction=True) def search(self, data): return [data] # http://en.wikipedia.org/wiki/Resource_Description_Framework def submit(self, data): subject = Literal(data["indicator"]) for k in data: if k == "indicator": self.handle.add((subject, RDF.type, Literal(data["itype"]), self.store_id)) else: subject = Literal(data["indicator"]) self.handle.add((subject, Literal(k), Literal(data[k]), self.store_id)) self.logger.debug(self.handle.serialize(format="trig")) return [data]
class ElasticTestCase(unittest.TestCase): identifier = URIRef("rdflib_test") dburi = Literal("http://localhost:9200/collection") def setUp(self): self.store = plugin.get("Elasticsearch", Store)(identifier=self.identifier, configuration=self.dburi) self.graph = ConjunctiveGraph(self.store, identifier=self.identifier) self.graph.open(self.dburi, create=True) def tearDown(self): self.graph.destroy(self.dburi) self.graph.close() def test_registerplugins(self): # I doubt this is quite right for a fresh pip installation, # this test is mainly here to fill a coverage gap. registerplugins() self.assertIsNotNone(plugin.get("Elasticsearch", Store)) p = plugin._plugins self.assertIn(("Elasticsearch", Store), p) del p[("Elasticsearch", Store)] plugin._plugins = p registerplugins() self.assertIn(("Elasticsearch", Store), p) def test_namespaces(self): self.assertNotEqual(list(self.graph.namespaces()), []) def test_contexts_without_triple(self): self.assertEqual(list(self.graph.contexts()), []) def test_contexts_result(self): g = self.graph.get_context(ctx_id) g.add((michel, likes, pizza)) actual = list(self.store.contexts()) self.assertEqual(actual[0], ctx_id) def test_contexts_with_triple(self): statemnt = (michel, likes, pizza) self.assertEqual(list(self.graph.contexts(triple=statemnt)), []) def test__len(self): self.assertEqual(self.store.__len__(), 0) def test_triples_choices(self): # Set this so we're not including selects for both asserted and literal tables for # a choice self.store.STRONGLY_TYPED_TERMS = True # Set the grouping of terms self.store.max_terms_per_where = 2 results = [((michel, likes, pizza), ctx_id)] # force execution of the generator for x in self.store.triples_choices( (None, likes, [michel, pizza, likes])): print("x=" + str(x)) print("results=" + str(results)) assert x in results
class ContextTestCase(unittest.TestCase): storetest = True store_name = "default" create = True michel = URIRef(u"michel") tarek = URIRef(u"tarek") bob = URIRef(u"bob") likes = URIRef(u"likes") hates = URIRef(u"hates") pizza = URIRef(u"pizza") cheese = URIRef(u"cheese") c1 = URIRef(u"context-1") c2 = URIRef(u"context-2") def setUp(self): self.graph = ConjunctiveGraph(self.store_name, self.identifier) self.graph.open(self.tmppath, create=self.create) # self.store = plugin.get(self.store_name, store.Store)( # configuration=self.tmppath, identifier=self.identifier) # self.graph = ConjunctiveGraph(self.store_name, identifier=self.identifier) # self.graph.destroy(self.tmppath) # self.graph.open(self.tmppath) def tearDown(self): # self.graph.destroy(self.tmppath) try: self.graph.close() except: pass if os.path.exists(self.tmppath): os.unlink(self.tmppath) def get_context(self, identifier): assert isinstance(identifier, URIRef) or isinstance(identifier, BNode), type(identifier) return Graph(store=self.graph.store, identifier=identifier, namespace_manager=self) def addStuff(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 graph = Graph(self.graph.store, c1) graph.add((tarek, likes, pizza)) graph.add((tarek, likes, cheese)) graph.add((michel, likes, pizza)) graph.add((michel, likes, cheese)) graph.add((bob, likes, cheese)) graph.add((bob, hates, pizza)) graph.add((bob, hates, michel)) # gasp! def removeStuff(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 graph = Graph(self.graph.store, c1) graph.remove((tarek, likes, pizza)) graph.remove((tarek, likes, cheese)) graph.remove((michel, likes, pizza)) graph.remove((michel, likes, cheese)) graph.remove((bob, likes, cheese)) graph.remove((bob, hates, pizza)) graph.remove((bob, hates, michel)) # gasp! def addStuffInMultipleContexts(self): c1 = self.c1 c2 = self.c2 triple = (self.pizza, self.hates, self.tarek) # revenge! # add to default context self.graph.add(triple) # add to context 1 graph = Graph(self.graph.store, c1) graph.add(triple) # add to context 2 graph = Graph(self.graph.store, c2) graph.add(triple) def testConjunction(self): self.addStuffInMultipleContexts() triple = (self.pizza, self.likes, self.pizza) # add to context 1 graph = Graph(self.graph.store, self.c1) graph.add(triple) self.assertEquals(len(self.graph), len(graph)) def testAdd(self): self.addStuff() def testRemove(self): self.addStuff() self.removeStuff() def testLenInOneContext(self): c1 = self.c1 # make sure context is empty self.graph.remove_context(self.get_context(c1)) graph = Graph(self.graph.store, c1) oldLen = len(self.graph) for i in range(0, 10): graph.add((BNode(), self.hates, self.hates)) self.assertEquals(len(graph), oldLen + 10) self.assertEquals(len(self.get_context(c1)), oldLen + 10) self.graph.remove_context(self.get_context(c1)) self.assertEquals(len(self.graph), oldLen) self.assertEquals(len(graph), 0) def testLenInMultipleContexts(self): oldLen = len(self.graph) self.addStuffInMultipleContexts() # addStuffInMultipleContexts is adding the same triple to # three different contexts. So it's only + 1 self.assertEquals(len(self.graph), oldLen + 1) graph = Graph(self.graph.store, self.c1) self.assertEquals(len(graph), oldLen + 1) def testRemoveInMultipleContexts(self): c1 = self.c1 c2 = self.c2 triple = (self.pizza, self.hates, self.tarek) # revenge! self.addStuffInMultipleContexts() # triple should be still in store after removing it from c1 + c2 self.assert_(triple in self.graph) graph = Graph(self.graph.store, c1) graph.remove(triple) self.assert_(triple in self.graph) graph = Graph(self.graph.store, c2) graph.remove(triple) self.assert_(triple in self.graph) self.graph.remove(triple) # now gone! self.assert_(triple not in self.graph) # add again and see if remove without context removes all triples! self.addStuffInMultipleContexts() self.graph.remove(triple) self.assert_(triple not in self.graph) def testContexts(self): triple = (self.pizza, self.hates, self.tarek) # revenge! self.addStuffInMultipleContexts() def cid(c): if not isinstance(c, basestring): return c.identifier return c self.assert_(self.c1 in map(cid, self.graph.contexts())) self.assert_(self.c2 in map(cid, self.graph.contexts())) contextList = map(cid, list(self.graph.contexts(triple))) self.assert_(self.c1 in contextList) self.assert_(self.c2 in contextList) def testRemoveContext(self): c1 = self.c1 self.addStuffInMultipleContexts() self.assertEquals(len(Graph(self.graph.store, c1)), 1) self.assertEquals(len(self.get_context(c1)), 1) self.graph.remove_context(self.get_context(c1)) self.assert_(self.c1 not in self.graph.contexts()) def testRemoveAny(self): Any = None self.addStuffInMultipleContexts() self.graph.remove((Any, Any, Any)) self.assertEquals(len(self.graph), 0) def testTriples(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 asserte = self.assertEquals triples = self.graph.triples graph = self.graph c1graph = Graph(self.graph.store, c1) c1triples = c1graph.triples Any = None self.addStuff() # unbound subjects with context asserte(len(list(c1triples((Any, likes, pizza)))), 2) asserte(len(list(c1triples((Any, hates, pizza)))), 1) asserte(len(list(c1triples((Any, likes, cheese)))), 3) asserte(len(list(c1triples((Any, hates, cheese)))), 0) # unbound subjects without context, same results! asserte(len(list(triples((Any, likes, pizza)))), 2) asserte(len(list(triples((Any, hates, pizza)))), 1) asserte(len(list(triples((Any, likes, cheese)))), 3) asserte(len(list(triples((Any, hates, cheese)))), 0) # unbound objects with context asserte(len(list(c1triples((michel, likes, Any)))), 2) asserte(len(list(c1triples((tarek, likes, Any)))), 2) asserte(len(list(c1triples((bob, hates, Any)))), 2) asserte(len(list(c1triples((bob, likes, Any)))), 1) # unbound objects without context, same results! asserte(len(list(triples((michel, likes, Any)))), 2) asserte(len(list(triples((tarek, likes, Any)))), 2) asserte(len(list(triples((bob, hates, Any)))), 2) asserte(len(list(triples((bob, likes, Any)))), 1) # unbound predicates with context asserte(len(list(c1triples((michel, Any, cheese)))), 1) asserte(len(list(c1triples((tarek, Any, cheese)))), 1) asserte(len(list(c1triples((bob, Any, pizza)))), 1) asserte(len(list(c1triples((bob, Any, michel)))), 1) # unbound predicates without context, same results! asserte(len(list(triples((michel, Any, cheese)))), 1) asserte(len(list(triples((tarek, Any, cheese)))), 1) asserte(len(list(triples((bob, Any, pizza)))), 1) asserte(len(list(triples((bob, Any, michel)))), 1) # unbound subject, objects with context asserte(len(list(c1triples((Any, hates, Any)))), 2) asserte(len(list(c1triples((Any, likes, Any)))), 5) # unbound subject, objects without context, same results! asserte(len(list(triples((Any, hates, Any)))), 2) asserte(len(list(triples((Any, likes, Any)))), 5) # unbound predicates, objects with context asserte(len(list(c1triples((michel, Any, Any)))), 2) asserte(len(list(c1triples((bob, Any, Any)))), 3) asserte(len(list(c1triples((tarek, Any, Any)))), 2) # unbound predicates, objects without context, same results! asserte(len(list(triples((michel, Any, Any)))), 2) asserte(len(list(triples((bob, Any, Any)))), 3) asserte(len(list(triples((tarek, Any, Any)))), 2) # unbound subjects, predicates with context asserte(len(list(c1triples((Any, Any, pizza)))), 3) asserte(len(list(c1triples((Any, Any, cheese)))), 3) asserte(len(list(c1triples((Any, Any, michel)))), 1) # unbound subjects, predicates without context, same results! asserte(len(list(triples((Any, Any, pizza)))), 3) asserte(len(list(triples((Any, Any, cheese)))), 3) asserte(len(list(triples((Any, Any, michel)))), 1) # all unbound with context asserte(len(list(c1triples((Any, Any, Any)))), 7) # all unbound without context, same result! asserte(len(list(triples((Any, Any, Any)))), 7) for c in [graph, self.get_context(c1)]: # unbound subjects asserte(set(c.subjects(likes, pizza)), set((michel, tarek))) asserte(set(c.subjects(hates, pizza)), set((bob,))) asserte(set(c.subjects(likes, cheese)), set([tarek, bob, michel])) asserte(set(c.subjects(hates, cheese)), set()) # unbound objects asserte(set(c.objects(michel, likes)), set([cheese, pizza])) asserte(set(c.objects(tarek, likes)), set([cheese, pizza])) asserte(set(c.objects(bob, hates)), set([michel, pizza])) asserte(set(c.objects(bob, likes)), set([cheese])) # unbound predicates asserte(set(c.predicates(michel, cheese)), set([likes])) asserte(set(c.predicates(tarek, cheese)), set([likes])) asserte(set(c.predicates(bob, pizza)), set([hates])) asserte(set(c.predicates(bob, michel)), set([hates])) asserte(set(c.subject_objects(hates)), set([(bob, pizza), (bob, michel)])) asserte( set(c.subject_objects(likes)), set([(tarek, cheese), (michel, cheese), (michel, pizza), (bob, cheese), (tarek, pizza)]), ) asserte(set(c.predicate_objects(michel)), set([(likes, cheese), (likes, pizza)])) asserte(set(c.predicate_objects(bob)), set([(likes, cheese), (hates, pizza), (hates, michel)])) asserte(set(c.predicate_objects(tarek)), set([(likes, cheese), (likes, pizza)])) asserte(set(c.subject_predicates(pizza)), set([(bob, hates), (tarek, likes), (michel, likes)])) asserte(set(c.subject_predicates(cheese)), set([(bob, likes), (tarek, likes), (michel, likes)])) asserte(set(c.subject_predicates(michel)), set([(bob, hates)])) asserte( set(c), set( [ (bob, hates, michel), (bob, likes, cheese), (tarek, likes, pizza), (michel, likes, pizza), (michel, likes, cheese), (bob, hates, pizza), (tarek, likes, cheese), ] ), ) # remove stuff and make sure the graph is empty again self.removeStuff() asserte(len(list(c1triples((Any, Any, Any)))), 0) asserte(len(list(triples((Any, Any, Any)))), 0)
class TestSparql11(unittest.TestCase): def setUp(self): self.longMessage = True self.graph = ConjunctiveGraph('SPARQLUpdateStore') root = "http://localhost:3030/ukpp/" self.graph.open((root + "sparql", root + "update")) # clean out the store for c in self.graph.contexts(): c.remove((None, None, None)) def tearDown(self): self.graph.close() def testSimpleGraph(self): g = self.graph.get_context(graphuri) g.add((tarek, likes, pizza)) g.add((bob, likes, pizza)) g.add((bob, likes, cheese)) g2 = self.graph.get_context(othergraphuri) g2.add((michel, likes, pizza)) self.assertEquals(3, len(g), 'graph contains 3 triples') self.assertEquals(1, len(g2), 'other graph contains 1 triple') r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(2, len(list(r)), "two people like pizza") r = g.triples((None, likes, pizza)) self.assertEquals(2, len(list(r)), "two people like pizza") # Test initBindings r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }", initBindings={'s': tarek}) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = g.triples((tarek, likes, pizza)) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = g.triples((tarek, likes, cheese)) self.assertEquals(0, len(list(r)), "tarek doesn't like cheese") g2.add((tarek, likes, pizza)) g.remove((tarek, likes, pizza)) r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(1, len(list(r)), "only bob likes pizza") def testConjunctiveDefault(self): g = self.graph.get_context(graphuri) g.add((tarek, likes, pizza)) g2 = self.graph.get_context(othergraphuri) g2.add((bob, likes, pizza)) g.add((tarek, hates, cheese)) self.assertEquals(2, len(g), 'graph contains 2 triples') self.assertEquals(3, len(self.graph), 'default union graph contains three triples') r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(2, len(list(r)), "two people like pizza") r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }", initBindings={'s': tarek}) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = self.graph.triples((tarek, likes, pizza)) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = self.graph.triples((tarek, likes, cheese)) self.assertEquals(0, len(list(r)), "tarek doesn't like cheese") g2.remove((bob, likes, pizza)) r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(1, len(list(r)), "only tarek likes pizza")
class TestSparql11(unittest.TestCase): def setUp(self): self.longMessage = True self.graph = ConjunctiveGraph('SPARQLUpdateStore') root = "http://localhost:3030/ukpp/" self.graph.open((root + "sparql", root + "update")) # clean out the store for c in self.graph.contexts(): c.remove((None, None, None)) assert len(c) == 0 def tearDown(self): self.graph.close() def testSimpleGraph(self): g = self.graph.get_context(graphuri) g.add((tarek, likes, pizza)) g.add((bob, likes, pizza)) g.add((bob, likes, cheese)) g2 = self.graph.get_context(othergraphuri) g2.add((michel, likes, pizza)) self.assertEquals(3, len(g), 'graph contains 3 triples') self.assertEquals(1, len(g2), 'other graph contains 1 triple') r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(2, len(list(r)), "two people like pizza") r = g.triples((None, likes, pizza)) self.assertEquals(2, len(list(r)), "two people like pizza") # Test initBindings r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }", initBindings={'s': tarek}) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = g.triples((tarek, likes, pizza)) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = g.triples((tarek, likes, cheese)) self.assertEquals(0, len(list(r)), "tarek doesn't like cheese") g2.add((tarek, likes, pizza)) g.remove((tarek, likes, pizza)) r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(1, len(list(r)), "only bob likes pizza") def testConjunctiveDefault(self): g = self.graph.get_context(graphuri) g.add((tarek, likes, pizza)) g2 = self.graph.get_context(othergraphuri) g2.add((bob, likes, pizza)) g.add((tarek, hates, cheese)) self.assertEquals(2, len(g), 'graph contains 2 triples') self.assertEquals(3, len(self.graph), 'default union graph contains three triples') r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(2, len(list(r)), "two people like pizza") r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }", initBindings={'s': tarek}) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = self.graph.triples((tarek, likes, pizza)) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = self.graph.triples((tarek, likes, cheese)) self.assertEquals(0, len(list(r)), "tarek doesn't like cheese") g2.remove((bob, likes, pizza)) r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(1, len(list(r)), "only tarek likes pizza") def testUpdate(self): self.graph.update("INSERT DATA { GRAPH <urn:graph> { <urn:michel> <urn:likes> <urn:pizza> . } }") g = self.graph.get_context(graphuri) self.assertEquals(1, len(g), 'graph contains 1 triples') def testUpdateWithInitNs(self): self.graph.update( "INSERT DATA { GRAPH ns:graph { ns:michel ns:likes ns:pizza . } }", initNs={'ns': URIRef('urn:')} ) g = self.graph.get_context(graphuri) self.assertEquals( set(g.triples((None,None,None))), set([(michel,likes,pizza)]), 'only michel likes pizza' ) def testUpdateWithInitBindings(self): self.graph.update( "INSERT { GRAPH <urn:graph> { ?a ?b ?c . } } WherE { }", initBindings={ 'a': URIRef('urn:michel'), 'b': URIRef('urn:likes'), 'c': URIRef('urn:pizza'), } ) g = self.graph.get_context(graphuri) self.assertEquals( set(g.triples((None,None,None))), set([(michel,likes,pizza)]), 'only michel likes pizza' ) def testMultipleUpdateWithInitBindings(self): self.graph.update( "INSERT { GRAPH <urn:graph> { ?a ?b ?c . } } WHERE { };" "INSERT { GRAPH <urn:graph> { ?d ?b ?c . } } WHERE { }", initBindings={ 'a': URIRef('urn:michel'), 'b': URIRef('urn:likes'), 'c': URIRef('urn:pizza'), 'd': URIRef('urn:bob'), } ) g = self.graph.get_context(graphuri) self.assertEquals( set(g.triples((None,None,None))), set([(michel,likes,pizza), (bob,likes,pizza)]), 'michel and bob like pizza' )
class Rdflib(Store): name = 'rdflib' def __init__(self, logger=logging.getLogger(__name__), *args, **kwargs): self.logger = logger self._open_store() self.store_id = Literal(str(uuid.uuid4())) def _open_store(self, store='IOMemory'): self.logger.debug("opening store...") self.handle = ConjunctiveGraph(store, identifier="permanent") def _close_store(self): self.logger.debug("closing store") self.handle.close(commit_pending_transaction=True) def indicators_search(self, token, data): rv = [] for s, p, o in self.handle.triples((Literal(data['indicator']), None, None)): rv.append((s, p, o)) return rv # http://en.wikipedia.org/wiki/Resource_Description_Framework def indicators_create(self, token, data): if type(data) is not list: data = [data] for d in data: subject = Literal(d["indicator"]) for k in d: if k == "indicator": self.handle.add((subject, RDF.type, Literal(d["itype"]), self.store_id)) else: subject = Literal(d["indicator"]) self.handle.add((subject, Literal(k), Literal(d[k]), self.store_id)) self.logger.debug(self.handle.serialize(format="trig")) return len(data) def ping(self, token): return True def _token_generate(self): return binascii.b2a_hex(os.urandom(TOKEN_LENGTH)) def tokens_admin_exists(self): return True def tokens_create(self, data): return True def tokens_delete(self, data): return True def tokens_search(self, data): return True def token_admin(self, token): return True def token_read(self, token): return True def token_write(self, token): return True def token_edit(self, data): return True def token_last_activity_at(self, token, timestamp=None): return True
class ContextTestCase(unittest.TestCase): store_name = 'default' path = None storetest = True create = True michel = URIRef(u'michel') tarek = URIRef(u'tarek') bob = URIRef(u'bob') likes = URIRef(u'likes') hates = URIRef(u'hates') pizza = URIRef(u'pizza') cheese = URIRef(u'cheese') c1 = URIRef(u'context-1') c2 = URIRef(u'context-2') def setUp(self): self.graph = ConjunctiveGraph(store=self.store_name) self.graph.destroy(self.path) self.graph.open(self.path, create=self.create) def tearDown(self): self.graph.destroy(self.path) try: self.graph.close() except: pass def get_context(self, identifier): assert isinstance(identifier, URIRef) or \ isinstance(identifier, BNode), type(identifier) return Graph(store=self.graph.store, identifier=identifier, namespace_manager=self) def addStuff(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 graph = Graph(self.graph.store, c1) graph.add((tarek, likes, pizza)) graph.add((tarek, likes, cheese)) graph.add((michel, likes, pizza)) graph.add((michel, likes, cheese)) graph.add((bob, likes, cheese)) graph.add((bob, hates, pizza)) graph.add((bob, hates, michel)) # gasp! def removeStuff(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 graph = Graph(self.graph.store, c1) graph.remove((tarek, likes, pizza)) graph.remove((tarek, likes, cheese)) graph.remove((michel, likes, pizza)) graph.remove((michel, likes, cheese)) graph.remove((bob, likes, cheese)) graph.remove((bob, hates, pizza)) graph.remove((bob, hates, michel)) # gasp! def addStuffInMultipleContexts(self): c1 = self.c1 c2 = self.c2 triple = (self.pizza, self.hates, self.tarek) # revenge! # add to default context self.graph.add(triple) # add to context 1 graph = Graph(self.graph.store, c1) graph.add(triple) # add to context 2 graph = Graph(self.graph.store, c2) graph.add(triple) def testConjunction(self): self.addStuffInMultipleContexts() triple = (self.pizza, self.likes, self.pizza) # add to context 1 graph = Graph(self.graph.store, self.c1) graph.add(triple) # self.assertEquals(len(self.graph), len(graph)) self.assertEquals( len(list(self.graph.triples((None, None, None)))), len(list(graph.triples((None, None, None))))) def testAdd(self): self.addStuff() def testRemove(self): self.addStuff() self.removeStuff() def testLenInOneContext(self): c1 = self.c1 # make sure context is empty self.graph.remove_context(self.get_context(c1)) graph = Graph(self.graph.store, c1) oldLen = len(self.graph) for i in range(0, 10): graph.add((BNode(), self.hates, self.hates)) self.assertEquals(len(graph), oldLen + 10) self.assertEquals(len(self.get_context(c1)), oldLen + 10) self.graph.remove_context(self.get_context(c1)) self.assertEquals(len(self.graph), oldLen) self.assertEquals(len(graph), 0) def testLenInMultipleContexts(self): oldLen = len(self.graph) self.addStuffInMultipleContexts() # addStuffInMultipleContexts is adding the same triple to # three different contexts. So it's only + 1 self.assertEquals(len(self.graph), oldLen + 1) graph = Graph(self.graph.store, self.c1) self.assertEquals(len(graph), oldLen + 1) def testRemoveInMultipleContexts(self): c1 = self.c1 c2 = self.c2 triple = (self.pizza, self.hates, self.tarek) # revenge! self.addStuffInMultipleContexts() # triple should be still in store after removing it from c1 + c2 self.assert_(triple in self.graph) graph = Graph(self.graph.store, c1) graph.remove(triple) self.assert_(triple in self.graph) graph = Graph(self.graph.store, c2) graph.remove(triple) self.assert_(triple in self.graph) self.graph.remove(triple) # now gone! self.assert_(triple not in self.graph) # add again and see if remove without context removes all triples! self.addStuffInMultipleContexts() self.graph.remove(triple) self.assert_(triple not in self.graph) def testContexts(self): triple = (self.pizza, self.hates, self.tarek) # revenge! self.addStuffInMultipleContexts() def cid(c): if not isinstance(c, basestring): return c.identifier return c self.assert_(self.c1 in map(cid, self.graph.contexts())) self.assert_(self.c2 in map(cid, self.graph.contexts())) contextList = map(cid, list(self.graph.contexts(triple))) self.assert_(self.c1 in contextList) self.assert_(self.c2 in contextList) def testRemoveContext(self): c1 = self.c1 self.addStuffInMultipleContexts() self.assertEquals(len(Graph(self.graph.store, c1)), 1) self.assertEquals(len(self.get_context(c1)), 1) self.graph.remove_context(self.get_context(c1)) self.assert_(self.c1 not in self.graph.contexts()) def testRemoveAny(self): Any = None self.addStuffInMultipleContexts() self.graph.remove((Any, Any, Any)) self.assertEquals(len(self.graph), 0) def testTriples(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 asserte = self.assertEquals triples = self.graph.triples graph = self.graph c1graph = Graph(self.graph.store, c1) c1triples = c1graph.triples Any = None self.addStuff() # unbound subjects with context asserte(len(list(c1triples((Any, likes, pizza)))), 2) asserte(len(list(c1triples((Any, hates, pizza)))), 1) asserte(len(list(c1triples((Any, likes, cheese)))), 3) asserte(len(list(c1triples((Any, hates, cheese)))), 0) # unbound subjects without context, same results! asserte(len(list(triples((Any, likes, pizza)))), 2) asserte(len(list(triples((Any, hates, pizza)))), 1) asserte(len(list(triples((Any, likes, cheese)))), 3) asserte(len(list(triples((Any, hates, cheese)))), 0) # unbound objects with context asserte(len(list(c1triples((michel, likes, Any)))), 2) asserte(len(list(c1triples((tarek, likes, Any)))), 2) asserte(len(list(c1triples((bob, hates, Any)))), 2) asserte(len(list(c1triples((bob, likes, Any)))), 1) # unbound objects without context, same results! asserte(len(list(triples((michel, likes, Any)))), 2) asserte(len(list(triples((tarek, likes, Any)))), 2) asserte(len(list(triples((bob, hates, Any)))), 2) asserte(len(list(triples((bob, likes, Any)))), 1) # unbound predicates with context asserte(len(list(c1triples((michel, Any, cheese)))), 1) asserte(len(list(c1triples((tarek, Any, cheese)))), 1) asserte(len(list(c1triples((bob, Any, pizza)))), 1) asserte(len(list(c1triples((bob, Any, michel)))), 1) # unbound predicates without context, same results! asserte(len(list(triples((michel, Any, cheese)))), 1) asserte(len(list(triples((tarek, Any, cheese)))), 1) asserte(len(list(triples((bob, Any, pizza)))), 1) asserte(len(list(triples((bob, Any, michel)))), 1) # unbound subject, objects with context asserte(len(list(c1triples((Any, hates, Any)))), 2) asserte(len(list(c1triples((Any, likes, Any)))), 5) # unbound subject, objects without context, same results! asserte(len(list(triples((Any, hates, Any)))), 2) asserte(len(list(triples((Any, likes, Any)))), 5) # unbound predicates, objects with context asserte(len(list(c1triples((michel, Any, Any)))), 2) asserte(len(list(c1triples((bob, Any, Any)))), 3) asserte(len(list(c1triples((tarek, Any, Any)))), 2) # unbound predicates, objects without context, same results! asserte(len(list(triples((michel, Any, Any)))), 2) asserte(len(list(triples((bob, Any, Any)))), 3) asserte(len(list(triples((tarek, Any, Any)))), 2) # unbound subjects, predicates with context asserte(len(list(c1triples((Any, Any, pizza)))), 3) asserte(len(list(c1triples((Any, Any, cheese)))), 3) asserte(len(list(c1triples((Any, Any, michel)))), 1) # unbound subjects, predicates without context, same results! asserte(len(list(triples((Any, Any, pizza)))), 3) asserte(len(list(triples((Any, Any, cheese)))), 3) asserte(len(list(triples((Any, Any, michel)))), 1) # all unbound with context asserte(len(list(c1triples((Any, Any, Any)))), 7) # all unbound without context, same result! asserte(len(list(triples((Any, Any, Any)))), 7) for c in [graph, self.get_context(c1)]: # unbound subjects asserte(set(c.subjects(likes, pizza)), set((michel, tarek))) asserte(set(c.subjects(hates, pizza)), set((bob,))) asserte(set(c.subjects(likes, cheese)), set([tarek, bob, michel])) asserte(set(c.subjects(hates, cheese)), set()) # unbound objects asserte(set(c.objects(michel, likes)), set([cheese, pizza])) asserte(set(c.objects(tarek, likes)), set([cheese, pizza])) asserte(set(c.objects(bob, hates)), set([michel, pizza])) asserte(set(c.objects(bob, likes)), set([cheese])) # unbound predicates asserte(set(c.predicates(michel, cheese)), set([likes])) asserte(set(c.predicates(tarek, cheese)), set([likes])) asserte(set(c.predicates(bob, pizza)), set([hates])) asserte(set(c.predicates(bob, michel)), set([hates])) asserte(set(c.subject_objects(hates)), set([(bob, pizza), (bob, michel)])) asserte(set(c.subject_objects(likes)), set([(tarek, cheese), (michel, cheese), (michel, pizza), (bob, cheese), (tarek, pizza)])) asserte(set(c.predicate_objects(michel)), set([(likes, cheese), (likes, pizza)])) asserte(set(c.predicate_objects(bob)), set([(likes, cheese), (hates, pizza), (hates, michel)])) asserte(set(c.predicate_objects(tarek)), set([(likes, cheese), (likes, pizza)])) asserte(set(c.subject_predicates(pizza)), set([(bob, hates), (tarek, likes), (michel, likes)])) asserte(set(c.subject_predicates(cheese)), set([(bob, likes), (tarek, likes), (michel, likes)])) asserte(set(c.subject_predicates(michel)), set([(bob, hates)])) asserte(set(c), set([ (bob, hates, michel), (bob, likes, cheese), (tarek, likes, pizza), (michel, likes, pizza), (michel, likes, cheese), (bob, hates, pizza), (tarek, likes, cheese)])) # remove stuff and make sure the graph is empty again self.removeStuff() asserte(len(list(c1triples((Any, Any, Any)))), 0) asserte(len(list(triples((Any, Any, Any)))), 0)
class TestSparql11(unittest.TestCase): def setUp(self): self.longMessage = True self.graph = ConjunctiveGraph('SPARQLUpdateStore') root = HOST + DB self.graph.open((root + "sparql", root + "update")) # clean out the store for c in self.graph.contexts(): c.remove((None, None, None)) assert len(c) == 0 def tearDown(self): self.graph.close() def testSimpleGraph(self): g = self.graph.get_context(graphuri) g.add((tarek, likes, pizza)) g.add((bob, likes, pizza)) g.add((bob, likes, cheese)) g2 = self.graph.get_context(othergraphuri) g2.add((michel, likes, pizza)) self.assertEqual(3, len(g), 'graph contains 3 triples') self.assertEqual(1, len(g2), 'other graph contains 1 triple') r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEqual(2, len(list(r)), "two people like pizza") r = g.triples((None, likes, pizza)) self.assertEqual(2, len(list(r)), "two people like pizza") # Test initBindings r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }", initBindings={'s': tarek}) self.assertEqual(1, len(list(r)), "i was asking only about tarek") r = g.triples((tarek, likes, pizza)) self.assertEqual(1, len(list(r)), "i was asking only about tarek") r = g.triples((tarek, likes, cheese)) self.assertEqual(0, len(list(r)), "tarek doesn't like cheese") g2.add((tarek, likes, pizza)) g.remove((tarek, likes, pizza)) r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEqual(1, len(list(r)), "only bob likes pizza") def testConjunctiveDefault(self): g = self.graph.get_context(graphuri) g.add((tarek, likes, pizza)) g2 = self.graph.get_context(othergraphuri) g2.add((bob, likes, pizza)) g.add((tarek, hates, cheese)) self.assertEqual(2, len(g), 'graph contains 2 triples') # the following are actually bad tests as they depend on your endpoint, # as pointed out in the sparqlstore.py code: # # For ConjunctiveGraphs, reading is done from the "default graph" Exactly # what this means depends on your endpoint, because SPARQL does not offer a # simple way to query the union of all graphs as it would be expected for a # ConjuntiveGraph. ## # Fuseki/TDB has a flag for specifying that the default graph # is the union of all graphs (tdb:unionDefaultGraph in the Fuseki config). self.assertEqual( 3, len(self.graph), 'default union graph should contain three triples but contains:\n' '%s' % list(self.graph)) r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEqual(2, len(list(r)), "two people like pizza") r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }", initBindings={'s': tarek}) self.assertEqual(1, len(list(r)), "i was asking only about tarek") r = self.graph.triples((tarek, likes, pizza)) self.assertEqual(1, len(list(r)), "i was asking only about tarek") r = self.graph.triples((tarek, likes, cheese)) self.assertEqual(0, len(list(r)), "tarek doesn't like cheese") g2.remove((bob, likes, pizza)) r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEqual(1, len(list(r)), "only tarek likes pizza") def testUpdate(self): self.graph.update( "INSERT DATA { GRAPH <urn:graph> { <urn:michel> <urn:likes> <urn:pizza> . } }" ) g = self.graph.get_context(graphuri) self.assertEqual(1, len(g), 'graph contains 1 triples') def testUpdateWithInitNs(self): self.graph.update( "INSERT DATA { GRAPH ns:graph { ns:michel ns:likes ns:pizza . } }", initNs={'ns': URIRef('urn:')}) g = self.graph.get_context(graphuri) self.assertEqual(set(g.triples((None, None, None))), set([(michel, likes, pizza)]), 'only michel likes pizza') def testUpdateWithInitBindings(self): self.graph.update( "INSERT { GRAPH <urn:graph> { ?a ?b ?c . } } WherE { }", initBindings={ 'a': URIRef('urn:michel'), 'b': URIRef('urn:likes'), 'c': URIRef('urn:pizza'), }) g = self.graph.get_context(graphuri) self.assertEqual(set(g.triples((None, None, None))), set([(michel, likes, pizza)]), 'only michel likes pizza') def testUpdateWithBlankNode(self): self.graph.update( "INSERT DATA { GRAPH <urn:graph> { _:blankA <urn:type> <urn:Blank> } }" ) g = self.graph.get_context(graphuri) for t in g.triples((None, None, None)): self.assertTrue(isinstance(t[0], BNode)) self.assertEqual(t[1].n3(), "<urn:type>") self.assertEqual(t[2].n3(), "<urn:Blank>") def testUpdateWithBlankNodeSerializeAndParse(self): self.graph.update( "INSERT DATA { GRAPH <urn:graph> { _:blankA <urn:type> <urn:Blank> } }" ) g = self.graph.get_context(graphuri) string = g.serialize(format='ntriples').decode('utf-8') raised = False try: Graph().parse(data=string, format="ntriples") except Exception as e: raised = True self.assertFalse(raised, 'Exception raised when parsing: ' + string) def testMultipleUpdateWithInitBindings(self): self.graph.update( "INSERT { GRAPH <urn:graph> { ?a ?b ?c . } } WHERE { };" "INSERT { GRAPH <urn:graph> { ?d ?b ?c . } } WHERE { }", initBindings={ 'a': URIRef('urn:michel'), 'b': URIRef('urn:likes'), 'c': URIRef('urn:pizza'), 'd': URIRef('urn:bob'), }) g = self.graph.get_context(graphuri) self.assertEqual(set(g.triples((None, None, None))), set([(michel, likes, pizza), (bob, likes, pizza)]), 'michel and bob like pizza') def testNamedGraphUpdate(self): g = self.graph.get_context(graphuri) r1 = "INSERT DATA { <urn:michel> <urn:likes> <urn:pizza> }" g.update(r1) self.assertEqual(set(g.triples((None, None, None))), set([(michel, likes, pizza)]), 'only michel likes pizza') r2 = "DELETE { <urn:michel> <urn:likes> <urn:pizza> } " + \ "INSERT { <urn:bob> <urn:likes> <urn:pizza> } WHERE {}" g.update(r2) self.assertEqual(set(g.triples((None, None, None))), set([(bob, likes, pizza)]), 'only bob likes pizza') says = URIRef("urn:says") # Strings with unbalanced curly braces tricky_strs = [ "With an unbalanced curly brace %s " % brace for brace in ["{", "}"] ] for tricky_str in tricky_strs: r3 = """INSERT { ?b <urn:says> "%s" } WHERE { ?b <urn:likes> <urn:pizza>} """ % tricky_str g.update(r3) values = set() for v in g.objects(bob, says): values.add(str(v)) self.assertEqual(values, set(tricky_strs)) # Complicated Strings r4strings = [] r4strings.append(r'''"1: adfk { ' \\\" \" { "''') r4strings.append(r'''"2: adfk } <foo> #éï \\"''') r4strings.append(r"""'3: adfk { " \\\' \' { '""") r4strings.append(r"""'4: adfk } <foo> #éï \\'""") r4strings.append(r'''"""5: adfk { ' \\\" \" { """''') r4strings.append(r'''"""6: adfk } <foo> #éï \\"""''') r4strings.append('"""7: ad adsfj \n { \n sadfj"""') r4strings.append(r"""'''8: adfk { " \\\' \' { '''""") r4strings.append(r"""'''9: adfk } <foo> #éï \\'''""") r4strings.append("'''10: ad adsfj \n { \n sadfj'''") r4 = "\n".join([ u'INSERT DATA { <urn:michel> <urn:says> %s } ;' % s for s in r4strings ]) g.update(r4) values = set() for v in g.objects(michel, says): values.add(text_type(v)) self.assertEqual( values, set([ re.sub( r"\\(.)", r"\1", re.sub(r"^'''|'''$|^'|'$|" + r'^"""|"""$|^"|"$', r"", s)) for s in r4strings ])) # IRI Containing ' or # # The fragment identifier must not be misinterpreted as a comment # (commenting out the end of the block). # The ' must not be interpreted as the start of a string, causing the } # in the literal to be identified as the end of the block. r5 = """INSERT DATA { <urn:michel> <urn:hates> <urn:foo'bar?baz;a=1&b=2#fragment>, "'}" }""" g.update(r5) values = set() for v in g.objects(michel, hates): values.add(text_type(v)) self.assertEqual(values, set([u"urn:foo'bar?baz;a=1&b=2#fragment", u"'}"])) # Comments r6 = u""" INSERT DATA { <urn:bob> <urn:hates> <urn:bob> . # No closing brace: } <urn:bob> <urn:hates> <urn:michel>. } #Final { } comment""" g.update(r6) values = set() for v in g.objects(bob, hates): values.add(v) self.assertEqual(values, set([bob, michel])) def testNamedGraphUpdateWithInitBindings(self): g = self.graph.get_context(graphuri) r = "INSERT { ?a ?b ?c } WHERE {}" g.update(r, initBindings={'a': michel, 'b': likes, 'c': pizza}) self.assertEqual(set(g.triples((None, None, None))), set([(michel, likes, pizza)]), 'only michel likes pizza') def testEmptyNamedGraph(self): empty_graph_iri = "urn:empty-graph-1" self.graph.update("CREATE GRAPH <%s>" % empty_graph_iri) named_graphs = [ text_type(r[0]) for r in self.graph.query("SELECT ?name WHERE { GRAPH ?name {} }") ] # Some SPARQL endpoint backends (like TDB) are not able to find empty named graphs # (at least with this query) if empty_graph_iri in named_graphs: self.assertTrue( empty_graph_iri in [text_type(g.identifier) for g in self.graph.contexts()]) def testEmptyLiteral(self): # test for https://github.com/RDFLib/rdflib/issues/457 # also see test_issue457.py which is sparql store independent! g = self.graph.get_context(graphuri) g.add((URIRef('http://example.com/s'), URIRef('http://example.com/p'), Literal(''))) o = tuple(g)[0][2] self.assertEqual(o, Literal(''), repr(o))
resultcount = 0 # for row in qres: # # print row # resultcount += 1 # # print row['s']['value'] +" - "+ row['p']['value'] +" - "+ row['o']['value'] +" - "+ row['label']['value'] # if (row['s']['value'].startswith('http://')): # s = URIRef(row['s']['value']) # else: # s = Literal(row['s']['value']) # if (row['p']['value'].startswith('http://')): # p = URIRef(row['p']['value']) # else: # p = Literal(row['p']['value']) # if (row['o']['value'].startswith('http://')): # o = URIRef(row['o']['value']) # else: # o = Literal(row['o']['value']) # graph.add((s,p,o)) # graph.add((URIRef(row['s']['value']), URIRef(row['p']['value']), URIRef(row['o']['value']))) print "---" graph.close() # print len(g) # prints 2 # import pprint # for stmt in g: # pprint.pprint(stmt)
print('Triples in graph before add: ', len(graph)) # Now we'll add some triples to the graph & commit the changes rdflib = Namespace('http://rdflib.net/test/') graph.bind('test', 'http://rdflib.net/test/') graph.add((rdflib['pic:1'], rdflib.name, Literal('Jane & Bob'))) graph.add((rdflib['pic:2'], rdflib.name, Literal('Squirrel in Tree'))) print('Triples in graph after add: ', len(graph)) # display the graph in RDF/XML print(graph.serialize(format='n3')) # close when done, otherwise sleepycat will leak lock entries. graph.close() graph = None # reopen the graph graph = ConjunctiveGraph('Sleepycat') graph.open(path, create=False) print('Triples still in graph: ', len(graph)) graph.close() # Clean up the temp folder to remove the Sleepycat database files... import os
class TestSparql11(unittest.TestCase): def setUp(self): self.longMessage = True self.graph = ConjunctiveGraph('SPARQLUpdateStore') root = "http://localhost:3030/ukpp/" self.graph.open((root + "sparql", root + "update")) # clean out the store for c in self.graph.contexts(): c.remove((None, None, None)) assert len(c) == 0 def tearDown(self): self.graph.close() def testSimpleGraph(self): g = self.graph.get_context(graphuri) g.add((tarek, likes, pizza)) g.add((bob, likes, pizza)) g.add((bob, likes, cheese)) g2 = self.graph.get_context(othergraphuri) g2.add((michel, likes, pizza)) self.assertEquals(3, len(g), 'graph contains 3 triples') self.assertEquals(1, len(g2), 'other graph contains 1 triple') r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(2, len(list(r)), "two people like pizza") r = g.triples((None, likes, pizza)) self.assertEquals(2, len(list(r)), "two people like pizza") # Test initBindings r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }", initBindings={'s': tarek}) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = g.triples((tarek, likes, pizza)) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = g.triples((tarek, likes, cheese)) self.assertEquals(0, len(list(r)), "tarek doesn't like cheese") g2.add((tarek, likes, pizza)) g.remove((tarek, likes, pizza)) r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(1, len(list(r)), "only bob likes pizza") def testConjunctiveDefault(self): g = self.graph.get_context(graphuri) g.add((tarek, likes, pizza)) g2 = self.graph.get_context(othergraphuri) g2.add((bob, likes, pizza)) g.add((tarek, hates, cheese)) self.assertEquals(2, len(g), 'graph contains 2 triples') self.assertEquals(3, len(self.graph), 'default union graph contains three triples') r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(2, len(list(r)), "two people like pizza") r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }", initBindings={'s': tarek}) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = self.graph.triples((tarek, likes, pizza)) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = self.graph.triples((tarek, likes, cheese)) self.assertEquals(0, len(list(r)), "tarek doesn't like cheese") g2.remove((bob, likes, pizza)) r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(1, len(list(r)), "only tarek likes pizza") def testUpdate(self): self.graph.update( "INSERT DATA { GRAPH <urn:graph> { <urn:michel> <urn:likes> <urn:pizza> . } }" ) g = self.graph.get_context(graphuri) self.assertEquals(1, len(g), 'graph contains 1 triples') def testUpdateWithInitNs(self): self.graph.update( "INSERT DATA { GRAPH ns:graph { ns:michel ns:likes ns:pizza . } }", initNs={'ns': URIRef('urn:')}) g = self.graph.get_context(graphuri) self.assertEquals(set(g.triples((None, None, None))), set([(michel, likes, pizza)]), 'only michel likes pizza') def testUpdateWithInitBindings(self): self.graph.update( "INSERT { GRAPH <urn:graph> { ?a ?b ?c . } } WherE { }", initBindings={ 'a': URIRef('urn:michel'), 'b': URIRef('urn:likes'), 'c': URIRef('urn:pizza'), }) g = self.graph.get_context(graphuri) self.assertEquals(set(g.triples((None, None, None))), set([(michel, likes, pizza)]), 'only michel likes pizza') def testMultipleUpdateWithInitBindings(self): self.graph.update( "INSERT { GRAPH <urn:graph> { ?a ?b ?c . } } WHERE { };" "INSERT { GRAPH <urn:graph> { ?d ?b ?c . } } WHERE { }", initBindings={ 'a': URIRef('urn:michel'), 'b': URIRef('urn:likes'), 'c': URIRef('urn:pizza'), 'd': URIRef('urn:bob'), }) g = self.graph.get_context(graphuri) self.assertEquals(set(g.triples((None, None, None))), set([(michel, likes, pizza), (bob, likes, pizza)]), 'michel and bob like pizza') def testNamedGraphUpdate(self): g = self.graph.get_context(graphuri) r1 = "INSERT DATA { <urn:michel> <urn:likes> <urn:pizza> }" g.update(r1) self.assertEquals(set(g.triples((None, None, None))), set([(michel, likes, pizza)]), 'only michel likes pizza') r2 = "DELETE { <urn:michel> <urn:likes> <urn:pizza> } " + \ "INSERT { <urn:bob> <urn:likes> <urn:pizza> } WHERE {}" g.update(r2) self.assertEquals(set(g.triples((None, None, None))), set([(bob, likes, pizza)]), 'only bob likes pizza') says = URIRef("urn:says") # Strings with unbalanced curly braces tricky_strs = [ "With an unbalanced curly brace %s " % brace for brace in ["{", "}"] ] for tricky_str in tricky_strs: r3 = """INSERT { ?b <urn:says> "%s" } WHERE { ?b <urn:likes> <urn:pizza>} """ % tricky_str g.update(r3) values = set() for v in g.objects(bob, says): values.add(str(v)) self.assertEquals(values, set(tricky_strs)) # Complicated Strings r4strings = [] r4strings.append(ur'''"1: adfk { ' \\\" \" { "''') r4strings.append(ur'''"2: adfk } <foo> #éï \\"''') r4strings.append(ur"""'3: adfk { " \\\' \' { '""") r4strings.append(ur"""'4: adfk } <foo> #éï \\'""") r4strings.append(ur'''"""5: adfk { ' \\\" \" { """''') r4strings.append(ur'''"""6: adfk } <foo> #éï \\"""''') r4strings.append(u'"""7: ad adsfj \n { \n sadfj"""') r4strings.append(ur"""'''8: adfk { " \\\' \' { '''""") r4strings.append(ur"""'''9: adfk } <foo> #éï \\'''""") r4strings.append(u"'''10: ad adsfj \n { \n sadfj'''") r4 = "\n".join([ u'INSERT DATA { <urn:michel> <urn:says> %s } ;' % s for s in r4strings ]) g.update(r4) values = set() for v in g.objects(michel, says): values.add(unicode(v)) self.assertEquals( values, set([ re.sub( ur"\\(.)", ur"\1", re.sub(ur"^'''|'''$|^'|'$|" + ur'^"""|"""$|^"|"$', ur"", s)) for s in r4strings ]))
class ContextTestCase(unittest.TestCase): store = 'default' slow = True tmppath = None def setUp(self): print self.store self.graph = ConjunctiveGraph(store=self.store) if self.store == "MySQL": from mysql import configString from rdflib.store.MySQL import MySQL path=configString MySQL().destroy(path) else: self.tmppath = mkdtemp() self.graph.open(self.tmppath, create=True) self.michel = URIRef(u'michel') self.tarek = URIRef(u'tarek') self.bob = URIRef(u'bob') self.likes = URIRef(u'likes') self.hates = URIRef(u'hates') self.pizza = URIRef(u'pizza') self.cheese = URIRef(u'cheese') self.c1 = URIRef(u'context-1') self.c2 = URIRef(u'context-2') # delete the graph for each test! self.graph.remove((None, None, None)) def tearDown(self): self.graph.close() shutil.rmtree(self.tmppath) def get_context(self, identifier): assert isinstance(identifier, URIRef) or \ isinstance(identifier, BNode), type(identifier) return Graph(store=self.graph.store, identifier=identifier, namespace_manager=self) def addStuff(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 graph = Graph(self.graph.store, c1) graph.add((tarek, likes, pizza)) graph.add((tarek, likes, cheese)) graph.add((michel, likes, pizza)) graph.add((michel, likes, cheese)) graph.add((bob, likes, cheese)) graph.add((bob, hates, pizza)) graph.add((bob, hates, michel)) # gasp! def removeStuff(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 graph = Graph(self.graph.store, c1) graph.remove((tarek, likes, pizza)) graph.remove((tarek, likes, cheese)) graph.remove((michel, likes, pizza)) graph.remove((michel, likes, cheese)) graph.remove((bob, likes, cheese)) graph.remove((bob, hates, pizza)) graph.remove((bob, hates, michel)) # gasp! def addStuffInMultipleContexts(self): c1 = self.c1 c2 = self.c2 triple = (self.pizza, self.hates, self.tarek) # revenge! # add to default context self.graph.add(triple) # add to context 1 graph = Graph(self.graph.store, c1) graph.add(triple) # add to context 2 graph = Graph(self.graph.store, c2) graph.add(triple) def testConjunction(self): self.addStuffInMultipleContexts() triple = (self.pizza, self.likes, self.pizza) # add to context 1 graph = Graph(self.graph.store, self.c1) graph.add(triple) self.assertEquals(len(self.graph), len(graph)) def testAdd(self): self.addStuff() def testRemove(self): self.addStuff() self.removeStuff() def testLenInOneContext(self): c1 = self.c1 # make sure context is empty self.graph.remove_context(self.get_context(c1)) graph = Graph(self.graph.store, c1) oldLen = len(self.graph) for i in range(0, 10): graph.add((BNode(), self.hates, self.hates)) self.assertEquals(len(graph), oldLen + 10) self.assertEquals(len(self.get_context(c1)), oldLen + 10) self.graph.remove_context(self.get_context(c1)) self.assertEquals(len(self.graph), oldLen) self.assertEquals(len(graph), 0) def testLenInMultipleContexts(self): oldLen = len(self.graph) self.addStuffInMultipleContexts() # addStuffInMultipleContexts is adding the same triple to # three different contexts. So it's only + 1 self.assertEquals(len(self.graph), oldLen + 1) graph = Graph(self.graph.store, self.c1) self.assertEquals(len(graph), oldLen + 1) def testRemoveInMultipleContexts(self): c1 = self.c1 c2 = self.c2 triple = (self.pizza, self.hates, self.tarek) # revenge! self.addStuffInMultipleContexts() # triple should be still in store after removing it from c1 + c2 self.assert_(triple in self.graph) graph = Graph(self.graph.store, c1) graph.remove(triple) self.assert_(triple in self.graph) graph = Graph(self.graph.store, c2) graph.remove(triple) self.assert_(triple in self.graph) self.graph.remove(triple) # now gone! self.assert_(triple not in self.graph) # add again and see if remove without context removes all triples! self.addStuffInMultipleContexts() self.graph.remove(triple) self.assert_(triple not in self.graph) def testContexts(self): triple = (self.pizza, self.hates, self.tarek) # revenge! self.addStuffInMultipleContexts() def cid(c): return c.identifier self.assert_(self.c1 in map(cid, self.graph.contexts())) self.assert_(self.c2 in map(cid, self.graph.contexts())) contextList = map(cid, list(self.graph.contexts(triple))) self.assert_(self.c1 in contextList) self.assert_(self.c2 in contextList) def testRemoveContext(self): c1 = self.c1 self.addStuffInMultipleContexts() self.assertEquals(len(Graph(self.graph.store, c1)), 1) self.assertEquals(len(self.get_context(c1)), 1) self.graph.remove_context(self.get_context(c1)) self.assert_(self.c1 not in self.graph.contexts()) def testRemoveAny(self): Any = None self.addStuffInMultipleContexts() self.graph.remove((Any, Any, Any)) self.assertEquals(len(self.graph), 0) def testTriples(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 asserte = self.assertEquals triples = self.graph.triples graph = self.graph c1graph = Graph(self.graph.store, c1) c1triples = c1graph.triples Any = None self.addStuff() # unbound subjects with context asserte(len(list(c1triples((Any, likes, pizza)))), 2) asserte(len(list(c1triples((Any, hates, pizza)))), 1) asserte(len(list(c1triples((Any, likes, cheese)))), 3) asserte(len(list(c1triples((Any, hates, cheese)))), 0) # unbound subjects without context, same results! asserte(len(list(triples((Any, likes, pizza)))), 2) asserte(len(list(triples((Any, hates, pizza)))), 1) asserte(len(list(triples((Any, likes, cheese)))), 3) asserte(len(list(triples((Any, hates, cheese)))), 0) # unbound objects with context asserte(len(list(c1triples((michel, likes, Any)))), 2) asserte(len(list(c1triples((tarek, likes, Any)))), 2) asserte(len(list(c1triples((bob, hates, Any)))), 2) asserte(len(list(c1triples((bob, likes, Any)))), 1) # unbound objects without context, same results! asserte(len(list(triples((michel, likes, Any)))), 2) asserte(len(list(triples((tarek, likes, Any)))), 2) asserte(len(list(triples((bob, hates, Any)))), 2) asserte(len(list(triples((bob, likes, Any)))), 1) # unbound predicates with context asserte(len(list(c1triples((michel, Any, cheese)))), 1) asserte(len(list(c1triples((tarek, Any, cheese)))), 1) asserte(len(list(c1triples((bob, Any, pizza)))), 1) asserte(len(list(c1triples((bob, Any, michel)))), 1) # unbound predicates without context, same results! asserte(len(list(triples((michel, Any, cheese)))), 1) asserte(len(list(triples((tarek, Any, cheese)))), 1) asserte(len(list(triples((bob, Any, pizza)))), 1) asserte(len(list(triples((bob, Any, michel)))), 1) # unbound subject, objects with context asserte(len(list(c1triples((Any, hates, Any)))), 2) asserte(len(list(c1triples((Any, likes, Any)))), 5) # unbound subject, objects without context, same results! asserte(len(list(triples((Any, hates, Any)))), 2) asserte(len(list(triples((Any, likes, Any)))), 5) # unbound predicates, objects with context asserte(len(list(c1triples((michel, Any, Any)))), 2) asserte(len(list(c1triples((bob, Any, Any)))), 3) asserte(len(list(c1triples((tarek, Any, Any)))), 2) # unbound predicates, objects without context, same results! asserte(len(list(triples((michel, Any, Any)))), 2) asserte(len(list(triples((bob, Any, Any)))), 3) asserte(len(list(triples((tarek, Any, Any)))), 2) # unbound subjects, predicates with context asserte(len(list(c1triples((Any, Any, pizza)))), 3) asserte(len(list(c1triples((Any, Any, cheese)))), 3) asserte(len(list(c1triples((Any, Any, michel)))), 1) # unbound subjects, predicates without context, same results! asserte(len(list(triples((Any, Any, pizza)))), 3) asserte(len(list(triples((Any, Any, cheese)))), 3) asserte(len(list(triples((Any, Any, michel)))), 1) # all unbound with context asserte(len(list(c1triples((Any, Any, Any)))), 7) # all unbound without context, same result! asserte(len(list(triples((Any, Any, Any)))), 7) for c in [graph, self.get_context(c1)]: # unbound subjects asserte(set(c.subjects(likes, pizza)), set((michel, tarek))) asserte(set(c.subjects(hates, pizza)), set((bob,))) asserte(set(c.subjects(likes, cheese)), set([tarek, bob, michel])) asserte(set(c.subjects(hates, cheese)), set()) # unbound objects asserte(set(c.objects(michel, likes)), set([cheese, pizza])) asserte(set(c.objects(tarek, likes)), set([cheese, pizza])) asserte(set(c.objects(bob, hates)), set([michel, pizza])) asserte(set(c.objects(bob, likes)), set([cheese])) # unbound predicates asserte(set(c.predicates(michel, cheese)), set([likes])) asserte(set(c.predicates(tarek, cheese)), set([likes])) asserte(set(c.predicates(bob, pizza)), set([hates])) asserte(set(c.predicates(bob, michel)), set([hates])) asserte(set(c.subject_objects(hates)), set([(bob, pizza), (bob, michel)])) asserte(set(c.subject_objects(likes)), set([(tarek, cheese), (michel, cheese), (michel, pizza), (bob, cheese), (tarek, pizza)])) asserte(set(c.predicate_objects(michel)), set([(likes, cheese), (likes, pizza)])) asserte(set(c.predicate_objects(bob)), set([(likes, cheese), (hates, pizza), (hates, michel)])) asserte(set(c.predicate_objects(tarek)), set([(likes, cheese), (likes, pizza)])) asserte(set(c.subject_predicates(pizza)), set([(bob, hates), (tarek, likes), (michel, likes)])) asserte(set(c.subject_predicates(cheese)), set([(bob, likes), (tarek, likes), (michel, likes)])) asserte(set(c.subject_predicates(michel)), set([(bob, hates)])) asserte(set(c), set([(bob, hates, michel), (bob, likes, cheese), (tarek, likes, pizza), (michel, likes, pizza), (michel, likes, cheese), (bob, hates, pizza), (tarek, likes, cheese)])) # remove stuff and make sure the graph is empty again self.removeStuff() asserte(len(list(c1triples((Any, Any, Any)))), 0) asserte(len(list(triples((Any, Any, Any)))), 0)
class TestSparql11(unittest.TestCase): def setUp(self): self.longMessage = True self.graph = ConjunctiveGraph('SPARQLUpdateStore') root = HOST + DB self.graph.open((root + "sparql", root + "update")) # clean out the store for c in self.graph.contexts(): c.remove((None, None, None)) assert len(c) == 0 def tearDown(self): self.graph.close() def testSimpleGraph(self): g = self.graph.get_context(graphuri) g.add((tarek, likes, pizza)) g.add((bob, likes, pizza)) g.add((bob, likes, cheese)) g2 = self.graph.get_context(othergraphuri) g2.add((michel, likes, pizza)) self.assertEquals(3, len(g), 'graph contains 3 triples') self.assertEquals(1, len(g2), 'other graph contains 1 triple') r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(2, len(list(r)), "two people like pizza") r = g.triples((None, likes, pizza)) self.assertEquals(2, len(list(r)), "two people like pizza") # Test initBindings r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }", initBindings={'s': tarek}) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = g.triples((tarek, likes, pizza)) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = g.triples((tarek, likes, cheese)) self.assertEquals(0, len(list(r)), "tarek doesn't like cheese") g2.add((tarek, likes, pizza)) g.remove((tarek, likes, pizza)) r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(1, len(list(r)), "only bob likes pizza") def testConjunctiveDefault(self): g = self.graph.get_context(graphuri) g.add((tarek, likes, pizza)) g2 = self.graph.get_context(othergraphuri) g2.add((bob, likes, pizza)) g.add((tarek, hates, cheese)) self.assertEquals(2, len(g), 'graph contains 2 triples') # the following are actually bad tests as they depend on your endpoint, # as pointed out in the sparqlstore.py code: # ## For ConjunctiveGraphs, reading is done from the "default graph" Exactly ## what this means depends on your endpoint, because SPARQL does not offer a ## simple way to query the union of all graphs as it would be expected for a ## ConjuntiveGraph. ## ## Fuseki/TDB has a flag for specifying that the default graph ## is the union of all graphs (tdb:unionDefaultGraph in the Fuseki config). self.assertEquals( 3, len(self.graph), 'default union graph should contain three triples but contains:\n' '%s' % list(self.graph)) r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(2, len(list(r)), "two people like pizza") r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }", initBindings={'s': tarek}) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = self.graph.triples((tarek, likes, pizza)) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = self.graph.triples((tarek, likes, cheese)) self.assertEquals(0, len(list(r)), "tarek doesn't like cheese") g2.remove((bob, likes, pizza)) r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(1, len(list(r)), "only tarek likes pizza") def testUpdate(self): self.graph.update( "INSERT DATA { GRAPH <urn:graph> { <urn:michel> <urn:likes> <urn:pizza> . } }" ) g = self.graph.get_context(graphuri) self.assertEquals(1, len(g), 'graph contains 1 triples') def testUpdateWithInitNs(self): self.graph.update( "INSERT DATA { GRAPH ns:graph { ns:michel ns:likes ns:pizza . } }", initNs={'ns': URIRef('urn:')}) g = self.graph.get_context(graphuri) self.assertEquals(set(g.triples((None, None, None))), set([(michel, likes, pizza)]), 'only michel likes pizza') def testUpdateWithInitBindings(self): self.graph.update( "INSERT { GRAPH <urn:graph> { ?a ?b ?c . } } WherE { }", initBindings={ 'a': URIRef('urn:michel'), 'b': URIRef('urn:likes'), 'c': URIRef('urn:pizza'), }) g = self.graph.get_context(graphuri) self.assertEquals(set(g.triples((None, None, None))), set([(michel, likes, pizza)]), 'only michel likes pizza') def testMultipleUpdateWithInitBindings(self): self.graph.update( "INSERT { GRAPH <urn:graph> { ?a ?b ?c . } } WHERE { };" "INSERT { GRAPH <urn:graph> { ?d ?b ?c . } } WHERE { }", initBindings={ 'a': URIRef('urn:michel'), 'b': URIRef('urn:likes'), 'c': URIRef('urn:pizza'), 'd': URIRef('urn:bob'), }) g = self.graph.get_context(graphuri) self.assertEquals(set(g.triples((None, None, None))), set([(michel, likes, pizza), (bob, likes, pizza)]), 'michel and bob like pizza') def testNamedGraphUpdate(self): g = self.graph.get_context(graphuri) r1 = "INSERT DATA { <urn:michel> <urn:likes> <urn:pizza> }" g.update(r1) self.assertEquals(set(g.triples((None, None, None))), set([(michel, likes, pizza)]), 'only michel likes pizza') r2 = "DELETE { <urn:michel> <urn:likes> <urn:pizza> } " + \ "INSERT { <urn:bob> <urn:likes> <urn:pizza> } WHERE {}" g.update(r2) self.assertEquals(set(g.triples((None, None, None))), set([(bob, likes, pizza)]), 'only bob likes pizza') says = URIRef("urn:says") # Strings with unbalanced curly braces tricky_strs = [ "With an unbalanced curly brace %s " % brace for brace in ["{", "}"] ] for tricky_str in tricky_strs: r3 = """INSERT { ?b <urn:says> "%s" } WHERE { ?b <urn:likes> <urn:pizza>} """ % tricky_str g.update(r3) values = set() for v in g.objects(bob, says): values.add(str(v)) self.assertEquals(values, set(tricky_strs)) # Complicated Strings r4strings = [] r4strings.append(ur'''"1: adfk { ' \\\" \" { "''') r4strings.append(ur'''"2: adfk } <foo> #éï \\"''') r4strings.append(ur"""'3: adfk { " \\\' \' { '""") r4strings.append(ur"""'4: adfk } <foo> #éï \\'""") r4strings.append(ur'''"""5: adfk { ' \\\" \" { """''') r4strings.append(ur'''"""6: adfk } <foo> #éï \\"""''') r4strings.append(u'"""7: ad adsfj \n { \n sadfj"""') r4strings.append(ur"""'''8: adfk { " \\\' \' { '''""") r4strings.append(ur"""'''9: adfk } <foo> #éï \\'''""") r4strings.append(u"'''10: ad adsfj \n { \n sadfj'''") r4 = "\n".join([ u'INSERT DATA { <urn:michel> <urn:says> %s } ;' % s for s in r4strings ]) g.update(r4) values = set() for v in g.objects(michel, says): values.add(unicode(v)) self.assertEquals( values, set([ re.sub( ur"\\(.)", ur"\1", re.sub(ur"^'''|'''$|^'|'$|" + ur'^"""|"""$|^"|"$', ur"", s)) for s in r4strings ]))
class MirbaseDB(object): def __init__(self, db_path): self.g = ConjunctiveGraph() self.path = db_path self.choices = set() self.labels = {} def create_graph(self): self.g.open(self.path + mirbasegraph_name, create=True) data = self.parse_mirbase(self.path) #g = ConjunctiveGraph(store="SPARQLUpdateStore") # g.bind() mirna_class = URIRef("http://purl.obolibrary.org/obo/SO_0000276") for mid in data: mirna_instance = URIRef(MIRBASE + data[mid]["acc"]) self.g.add((mirna_instance, RDF.type, mirna_class)) label = Literal(data[mid]["name"]) self.g.add((mirna_instance, RDFS.label, label)) description = Literal(data[mid]["description"]) self.g.add((mirna_instance, RDFS.comment, description)) for p in data[mid]["previous_names"]: if p.strip(): previous_name = Literal(p) self.g.add((mirna_instance, MIRBASE["previous_acc"], previous_name)) for mature in data[mid]["mature"]: mature_instance = URIRef(MIRBASE + data[mid]["mature"][mature]["acc"]) self.g.add((mature_instance, RDF.type, mirna_class)) mature_label = Literal(data[mid]["mature"][mature]["name"]) self.g.add((mature_instance, RDFS.label, mature_label)) for mature_p in data[mid]["mature"][mature]["previous_names"]: if mature_p.strip(): mature_previous_name = Literal(mature_p) self.g.add((mature_instance, MIRBASE["previous_acc"], mature_previous_name)) self.g.add( (mirna_instance, MIRBASE["stemloopOf"], mature_instance)) self.get_label_to_acc() self.choices = self.labels.keys() goa_data = self.parse_goa_gaf("data/goa_human_rna.gaf") for label in self.labels: if label in goa_data: for go_id in goa_data[label]: mirna_instance = self.labels[label] self.g.add( (mirna_instance, MIRBASE["goa"], Literal(go_id))) def parse_goa_gaf(self, gaf_file): goa_dic = {} with open(gaf_file) as gaf: for l in gaf: if l[0] != "!": values = l.split("\t") db_id = values[1] go_id = values[4] mirna_id = values[9].replace(".", "") mirna_id = mirna_id.replace("H**o sapiens ", "") mirna_id = mirna_id.replace("(human) ", "") mirna_id = mirna_id.replace("microRNA miR", "miR") if not mirna_id.startswith("hsa-"): mirna_id = "hsa-" + mirna_id mirna = self.map_label(mirna_id) if mirna[1] != 100: print mirna_id, mirna if mirna[0] not in goa_dic: goa_dic[mirna[0]] = [] goa_dic[mirna[0]].append(go_id) print "# of mirnas with gos: {}".format(len(goa_dic)) return goa_dic def parse_mirbase(self, mirbase_root): mirna_dic = {} with open(mirbase_root + "mirna.txt") as mirnas: for m in mirnas: props = m.strip().split("\t") mname = props[2] mid = props[0] macc = props[1] mdesc = props[4] mprev = props[3].split(";") if int(props[-1]) != 22: # not h**o sapiens continue mirna_dic[mid] = {} mirna_dic[mid]["name"] = mname mirna_dic[mid]["acc"] = macc mirna_dic[mid]["previous_names"] = mprev mirna_dic[mid]["description"] = mdesc mature_dic = {} with open(mirbase_root + "mirna_mature.txt") as mirnas: for m in mirnas: props = m.strip().split("\t") mname = props[1] mid = props[0] macc = props[3] # mdesc = props[4] mprev = props[2].split(";") if not mname.startswith("hsa-"): # not h**o sapiens continue mature_dic[mid] = {} mature_dic[mid]["name"] = mname mature_dic[mid]["previous_names"] = mprev mature_dic[mid]["acc"] = macc with open(mirbase_root + "mirna_pre_mature.txt") as mirnas: for m in mirnas: props = m.strip().split("\t") mid, matureid = props[:2] if mid in mirna_dic: if "mature" not in mirna_dic[mid]: mirna_dic[mid]["mature"] = {} mirna_dic[mid]["mature"][matureid] = mature_dic[matureid] # pp.pprint(mirna_dic) return mirna_dic def map_label(self, label): global mirbasedic result = ("", 0) # first check if a chebi mappings dictionary is loaded in memory if label in mirbasedic: result = mirbasedic[label] else: new_label = label.lower() if new_label.startswith("h-"): new_label = new_label[2:] elif new_label.startswith("pre-"): new_label = new_label[4:] new_label = new_label.replace("microrna", "mir") new_label = new_label.replace("mirna", "mir") if "-" not in new_label: #fix cases like miR200c if " " in new_label: new_label = new_label.replace(" ", "-") else: x = re.split('(\d+)', new_label) new_label = x[0] + "-" + "".join(x[1:]) else: mirna = new_label.split("-") if len(mirna[1]) > 2: # print "mapping", mirna[1] if mirna[1][-3].isdigit() and mirna[1][-2].isalpha( ) and mirna[1][-1].isdigit(): # print "match!", new_label = mirna[0] + "-" + mirna[ 1][:-1] + "-" + mirna[1][-1] # print new_label if not new_label.startswith("hsa-"): new_label = "hsa-" + new_label if label[0].isdigit(): new_label = "hsa-mir-" + label elif label[0] == "-" and label[1:].isdigit(): new_label = "hsa-mir" + label result = process.extractOne(new_label, self.choices) # print result # result = process.extract(label, choices, limit=3) print result if result[1] != 100: #print # print "original:", new_label.encode("utf-8"), result # if label[-1].isdigit(): # label += "a" # else: new_new_label = new_label + "-1" revised_result = process.extractOne(new_new_label, self.choices) # logging.info(str(revised_result)) if revised_result[1] != 100: new_new_label = new_label + "a" revised_result = process.extractOne( new_new_label, self.choices) # logging.info(str(revised_result)) if revised_result[1] != 100: new_new_label += "-1" revised_result2 = process.extractOne( new_new_label, self.choices) # logging.info(str(revised_result2)) if revised_result2[1] > revised_result[1]: revised_result = revised_result2 if revised_result[1] > result[1]: result = revised_result new_label = new_new_label #print "revised:", new_label.encode("utf-8"), result # print "mapped {} to {} to {}".format(label, new_label, result) if result[1] == 100: mirbasedic[label] = result return result def load_graph(self): if os.path.isfile(self.path + mirbasegraph_name): logging.info("Loading miRbase...") self.g.load(self.path + mirbasegraph_name) # print "Opened graph with {} triples".format(len(self.g)) self.get_label_to_acc() self.choices = self.labels.keys() logging.info("done.") else: logging.info("miRBase graph not found") def get_label_to_acc(self): for subj, pred, obj in self.g.triples((None, RDFS.label, None)): self.labels[str(obj)] = subj for subj, pred, obj in self.g.triples((None, RDFS.label, None)): self.labels[str(obj)] = subj def save_graph(self): self.g.serialize(self.path + mirbasegraph_name, format='pretty-xml') print 'Triples in graph after add: ', len(self.g) self.g.close()
class TestSparql11(unittest.TestCase): def setUp(self): self.longMessage = True self.graph = ConjunctiveGraph('SPARQLUpdateStore') root = HOST + DB self.graph.open((root + "sparql", root + "update")) # clean out the store for c in self.graph.contexts(): c.remove((None, None, None)) assert len(c) == 0 def tearDown(self): self.graph.close() def testSimpleGraph(self): g = self.graph.get_context(graphuri) g.add((tarek, likes, pizza)) g.add((bob, likes, pizza)) g.add((bob, likes, cheese)) g2 = self.graph.get_context(othergraphuri) g2.add((michel, likes, pizza)) self.assertEquals(3, len(g), 'graph contains 3 triples') self.assertEquals(1, len(g2), 'other graph contains 1 triple') r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(2, len(list(r)), "two people like pizza") r = g.triples((None, likes, pizza)) self.assertEquals(2, len(list(r)), "two people like pizza") # Test initBindings r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }", initBindings={'s': tarek}) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = g.triples((tarek, likes, pizza)) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = g.triples((tarek, likes, cheese)) self.assertEquals(0, len(list(r)), "tarek doesn't like cheese") g2.add((tarek, likes, pizza)) g.remove((tarek, likes, pizza)) r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(1, len(list(r)), "only bob likes pizza") def testConjunctiveDefault(self): g = self.graph.get_context(graphuri) g.add((tarek, likes, pizza)) g2 = self.graph.get_context(othergraphuri) g2.add((bob, likes, pizza)) g.add((tarek, hates, cheese)) self.assertEquals(2, len(g), 'graph contains 2 triples') # the following are actually bad tests as they depend on your endpoint, # as pointed out in the sparqlstore.py code: # ## For ConjunctiveGraphs, reading is done from the "default graph" Exactly ## what this means depends on your endpoint, because SPARQL does not offer a ## simple way to query the union of all graphs as it would be expected for a ## ConjuntiveGraph. ## ## Fuseki/TDB has a flag for specifying that the default graph ## is the union of all graphs (tdb:unionDefaultGraph in the Fuseki config). self.assertEquals(3, len(self.graph), 'default union graph should contain three triples but contains:\n' '%s' % list(self.graph)) r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(2, len(list(r)), "two people like pizza") r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }", initBindings={'s': tarek}) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = self.graph.triples((tarek, likes, pizza)) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = self.graph.triples((tarek, likes, cheese)) self.assertEquals(0, len(list(r)), "tarek doesn't like cheese") g2.remove((bob, likes, pizza)) r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(1, len(list(r)), "only tarek likes pizza") def testUpdate(self): self.graph.update("INSERT DATA { GRAPH <urn:graph> { <urn:michel> <urn:likes> <urn:pizza> . } }") g = self.graph.get_context(graphuri) self.assertEquals(1, len(g), 'graph contains 1 triples') def testUpdateWithInitNs(self): self.graph.update( "INSERT DATA { GRAPH ns:graph { ns:michel ns:likes ns:pizza . } }", initNs={'ns': URIRef('urn:')} ) g = self.graph.get_context(graphuri) self.assertEquals( set(g.triples((None,None,None))), set([(michel,likes,pizza)]), 'only michel likes pizza' ) def testUpdateWithInitBindings(self): self.graph.update( "INSERT { GRAPH <urn:graph> { ?a ?b ?c . } } WherE { }", initBindings={ 'a': URIRef('urn:michel'), 'b': URIRef('urn:likes'), 'c': URIRef('urn:pizza'), } ) g = self.graph.get_context(graphuri) self.assertEquals( set(g.triples((None,None,None))), set([(michel,likes,pizza)]), 'only michel likes pizza' ) def testMultipleUpdateWithInitBindings(self): self.graph.update( "INSERT { GRAPH <urn:graph> { ?a ?b ?c . } } WHERE { };" "INSERT { GRAPH <urn:graph> { ?d ?b ?c . } } WHERE { }", initBindings={ 'a': URIRef('urn:michel'), 'b': URIRef('urn:likes'), 'c': URIRef('urn:pizza'), 'd': URIRef('urn:bob'), } ) g = self.graph.get_context(graphuri) self.assertEquals( set(g.triples((None,None,None))), set([(michel,likes,pizza), (bob,likes,pizza)]), 'michel and bob like pizza' ) def testNamedGraphUpdate(self): g = self.graph.get_context(graphuri) r1 = "INSERT DATA { <urn:michel> <urn:likes> <urn:pizza> }" g.update(r1) self.assertEquals( set(g.triples((None,None,None))), set([(michel,likes,pizza)]), 'only michel likes pizza' ) r2 = "DELETE { <urn:michel> <urn:likes> <urn:pizza> } " + \ "INSERT { <urn:bob> <urn:likes> <urn:pizza> } WHERE {}" g.update(r2) self.assertEquals( set(g.triples((None, None, None))), set([(bob, likes, pizza)]), 'only bob likes pizza' ) says = URIRef("urn:says") # Strings with unbalanced curly braces tricky_strs = ["With an unbalanced curly brace %s " % brace for brace in ["{", "}"]] for tricky_str in tricky_strs: r3 = """INSERT { ?b <urn:says> "%s" } WHERE { ?b <urn:likes> <urn:pizza>} """ % tricky_str g.update(r3) values = set() for v in g.objects(bob, says): values.add(str(v)) self.assertEquals(values, set(tricky_strs)) # Complicated Strings r4strings = [] r4strings.append(ur'''"1: adfk { ' \\\" \" { "''') r4strings.append(ur'''"2: adfk } <foo> #éï \\"''') r4strings.append(ur"""'3: adfk { " \\\' \' { '""") r4strings.append(ur"""'4: adfk } <foo> #éï \\'""") r4strings.append(ur'''"""5: adfk { ' \\\" \" { """''') r4strings.append(ur'''"""6: adfk } <foo> #éï \\"""''') r4strings.append(u'"""7: ad adsfj \n { \n sadfj"""') r4strings.append(ur"""'''8: adfk { " \\\' \' { '''""") r4strings.append(ur"""'''9: adfk } <foo> #éï \\'''""") r4strings.append(u"'''10: ad adsfj \n { \n sadfj'''") r4 = "\n".join([ u'INSERT DATA { <urn:michel> <urn:says> %s } ;' % s for s in r4strings ]) g.update(r4) values = set() for v in g.objects(michel, says): values.add(unicode(v)) self.assertEquals(values, set([re.sub(ur"\\(.)", ur"\1", re.sub(ur"^'''|'''$|^'|'$|" + ur'^"""|"""$|^"|"$', ur"", s)) for s in r4strings]))
class BerkeleyDBTestCase(unittest.TestCase): def setUp(self): if not has_bsddb: self.skipTest("skipping as berkleydb is missing") self.store_name = "BerkeleyDB" self.path = mktemp() self.g = ConjunctiveGraph(store=self.store_name) self.rt = self.g.open(self.path, create=True) assert self.rt == VALID_STORE, "The underlying store is corrupt" assert ( len(self.g) == 0 ), "There must be zero triples in the graph just after store (file) creation" data = """ PREFIX : <https://example.org/> :a :b :c . :d :e :f . :d :g :h . """ self.g.parse(data=data, format="ttl") def tearDown(self): self.g.close() def test_write(self): assert ( len(self.g) == 3 ), "There must be three triples in the graph after the first data chunk parse" data2 = """ PREFIX : <https://example.org/> :d :i :j . """ self.g.parse(data=data2, format="ttl") assert ( len(self.g) == 4 ), "There must be four triples in the graph after the second data chunk parse" data3 = """ PREFIX : <https://example.org/> :d :i :j . """ self.g.parse(data=data3, format="ttl") assert ( len(self.g) == 4 ), "There must still be four triples in the graph after the thrd data chunk parse" def test_read(self): sx = None for s in self.g.subjects( predicate=URIRef("https://example.org/e"), object=URIRef("https://example.org/f"), ): sx = s assert sx == URIRef("https://example.org/d") def test_sparql_query(self): q = """ PREFIX : <https://example.org/> SELECT (COUNT(*) AS ?c) WHERE { :d ?p ?o . }""" c = 0 for row in self.g.query(q): c = int(row.c) assert c == 2, "SPARQL COUNT must return 2" def test_sparql_insert(self): q = """ PREFIX : <https://example.org/> INSERT DATA { :x :y :z . }""" self.g.update(q) assert len(self.g) == 4, "After extra triple insert, length must be 4" def test_multigraph(self): q = """ PREFIX : <https://example.org/> INSERT DATA { GRAPH :m { :x :y :z . } GRAPH :n { :x :y :z . } }""" self.g.update(q) q = """ SELECT (COUNT(?g) AS ?c) WHERE { SELECT DISTINCT ?g WHERE { GRAPH ?g { ?s ?p ?o } } } """ c = 0 for row in self.g.query(q): c = int(row.c) assert c == 3, "SPARQL COUNT must return 3 (default, :m & :n)" def test_open_shut(self): assert len(self.g) == 3, "Initially we must have 3 triples from setUp" self.g.close() self.g = None # reopen the graph self.g = ConjunctiveGraph("BerkeleyDB") self.g.open(self.path, create=False) assert ( len(self.g) == 3 ), "After close and reopen, we should still have the 3 originally added triples"
txy_list.append((t, x, y)) accident_url_list.append(ident) yield from accident_coverage_triples(txy_list, accident_url_list) parser = ArgumentParser() parser.add_argument( '--tweets', type=FileType('r'), default='data/tweets.json' ) parser.add_argument( '--streets', type=FileType('r'), default='data/streets.json' ) parser.add_argument( '--out', type=FileType('wb'), default='data/accidents.ttl' ) if __name__ == '__main__': args = parser.parse_args() streets = load(args.streets) tweets = load(args.tweets) print('Loaded {} tweets and {} streets'.format(len(tweets), len(streets))) fuseki = ConjunctiveGraph(store='SPARQLUpdateStore') fuseki.open(('http://localhost:3030/accidents/query', 'http://localhost:3030/accidents/update')) default = 'urn:x-arq:DefaultGraph' add_namespaces(fuseki) fuseki.addN((s,p,o,default) for s,p,o in build_graph(tweets)) fuseki.close()
class SQLATestCase(unittest.TestCase): identifier = URIRef("rdflib_test") dburi = Literal('sqlite://') def setUp(self): self.store = plugin.get( "SQLAlchemy", Store)(identifier=self.identifier) self.graph = ConjunctiveGraph(self.store, identifier=self.identifier) self.graph.open(self.dburi, create=True) def tearDown(self): self.graph.destroy(self.dburi) try: self.graph.close() except: pass def test_registerplugins(self): # I doubt this is quite right for a fresh pip installation, # this test is mainly here to fill a coverage gap. from rdflib_sqlalchemy import registerplugins from rdflib import plugin from rdflib.store import Store registerplugins() self.assert_(plugin.get('SQLAlchemy', Store) is not None) p = plugin._plugins self.assert_(('SQLAlchemy', Store) in p, p) del p[('SQLAlchemy', Store)] plugin._plugins = p registerplugins() self.assert_(('SQLAlchemy', Store) in p, p) def test_skolemisation(self): from rdflib_sqlalchemy.SQLAlchemy import skolemise testbnode = BNode() statemnt = (michel, likes, testbnode) res = skolemise(statemnt) self.assert_('bnode:N' in str(res[2]), res) def test_deskolemisation(self): from rdflib_sqlalchemy.SQLAlchemy import deskolemise testbnode = BNode() statemnt = (michel, likes, testbnode) res = deskolemise(statemnt) self.assert_(str(res[2]).startswith('N'), res) def test_redeskolemisation(self): from rdflib_sqlalchemy.SQLAlchemy import skolemise, deskolemise testbnode = BNode() statemnt = skolemise((michel, likes, testbnode)) res = deskolemise(statemnt) self.assert_(str(res[2]).startswith('N'), res) def test__parse_rfc1738_args(self): from rdflib_sqlalchemy.SQLAlchemy import _parse_rfc1738_args self.assertRaises(ValueError, _parse_rfc1738_args, 'Not parseable') def test_namespaces(self): self.assert_(list(self.graph.namespaces()) != []) def test_contexts_without_triple(self): self.assert_(list(self.graph.contexts()) == []) def test_contexts_with_triple(self): statemnt = (michel, likes, pizza) self.assert_(self.graph.contexts(triple=statemnt) != []) def test__len(self): self.assert_(self.store.__len__() == 0) def test__remove_context(self): self.store._remove_context(self.identifier)
class ContextTestCase(unittest.TestCase): storetest = True identifier = URIRef("rdflib_test") michel = URIRef(u'michel') tarek = URIRef(u'tarek') bob = URIRef(u'bob') likes = URIRef(u'likes') hates = URIRef(u'hates') pizza = URIRef(u'pizza') cheese = URIRef(u'cheese') c1 = URIRef(u'context-1') c2 = URIRef(u'context-2') def setUp(self, uri='sqlite://', storename=None): store = plugin.get(storename, Store)(identifier=self.identifier) self.graph = ConjunctiveGraph(store, identifier=self.identifier) self.graph.open(uri, create=True) def tearDown(self, uri='sqlite://'): self.graph.destroy(uri) try: self.graph.close() except: pass def get_context(self, identifier): assert isinstance(identifier, URIRef) or \ isinstance(identifier, BNode), type(identifier) return Graph(store=self.graph.store, identifier=identifier, namespace_manager=self) def addStuff(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 graph = Graph(self.graph.store, c1) graph.add((tarek, likes, pizza)) graph.add((tarek, likes, cheese)) graph.add((michel, likes, pizza)) graph.add((michel, likes, cheese)) graph.add((bob, likes, cheese)) graph.add((bob, hates, pizza)) graph.add((bob, hates, michel)) # gasp! def removeStuff(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 graph = Graph(self.graph.store, c1) graph.remove((tarek, likes, pizza)) graph.remove((tarek, likes, cheese)) graph.remove((michel, likes, pizza)) graph.remove((michel, likes, cheese)) graph.remove((bob, likes, cheese)) graph.remove((bob, hates, pizza)) graph.remove((bob, hates, michel)) # gasp! def addStuffInMultipleContexts(self): c1 = self.c1 c2 = self.c2 triple = (self.pizza, self.hates, self.tarek) # revenge! # add to default context self.graph.add(triple) # add to context 1 graph = Graph(self.graph.store, c1) graph.add(triple) # add to context 2 graph = Graph(self.graph.store, c2) graph.add(triple) def testConjunction(self): self.addStuffInMultipleContexts() triple = (self.pizza, self.likes, self.pizza) # add to context 1 graph = Graph(self.graph.store, self.c1) graph.add(triple) # print("Graph", graph.identifier, graph.serialize(format="nt")) # print("Selfgraph", self.graph.identifier, # self.graph.serialize(format="nt")) self.assertEquals(len(self.graph.store), len(graph.store)) def testAdd(self): self.addStuff() def testRemove(self): self.addStuff() self.removeStuff() def testLenInOneContext(self): c1 = self.c1 # make sure context is empty self.graph.remove_context(self.get_context(c1)) graph = Graph(self.graph.store, c1) oldLen = len(self.graph) for i in range(0, 10): graph.add((BNode(), self.hates, self.hates)) self.assertEquals(len(graph), oldLen + 10) self.assertEquals(len(self.get_context(c1)), oldLen + 10) self.graph.remove_context(self.get_context(c1)) self.assertEquals(len(self.graph), oldLen) self.assertEquals(len(graph), 0) def testLenInMultipleContexts(self): oldLen = len(self.graph.store) print("Original", oldLen, self.graph.store) self.addStuffInMultipleContexts() newLen = len(self.graph.store) print("MultipleContexts", newLen, self.graph.store) # addStuffInMultipleContexts is adding the same triple to # three different contexts. So it's only + 1 print("No context", len(list(self.graph.triples((None, None, None))))) print("Context context-1", len( list(self.graph.triples((None, None, None), context=self.c1)))) print("Context context-2", len( list(self.graph.triples((None, None, None), context=self.c2)))) self.assertEquals(len(self.graph.store), oldLen + 1, [self.graph.store, oldLen + 1]) graph = Graph(self.graph.store, self.c1) self.assertEquals(len(graph.store), oldLen + 1, [graph.store, oldLen + 1]) def testRemoveInMultipleContexts(self): c1 = self.c1 c2 = self.c2 triple = (self.pizza, self.hates, self.tarek) # revenge! self.addStuffInMultipleContexts() # triple should be still in store after removing it from c1 + c2 self.assert_(triple in self.graph) graph = Graph(self.graph.store, c1) graph.remove(triple) self.assert_(triple in self.graph) graph = Graph(self.graph.store, c2) graph.remove(triple) self.assert_(triple in self.graph) self.graph.remove(triple) # now gone! self.assert_(triple not in self.graph) # add again and see if remove without context removes all triples! self.addStuffInMultipleContexts() self.graph.remove(triple) self.assert_(triple not in self.graph) def testContexts(self): triple = (self.pizza, self.hates, self.tarek) # revenge! self.addStuffInMultipleContexts() def cid(c): if (PY3 and not isinstance(c,(str, bytes))) or not isinstance(c, basestring): return c.identifier return c self.assert_(self.c1 in list(map(cid, self.graph.contexts()))) self.assert_(self.c2 in list(map(cid, self.graph.contexts()))) contextList = list(map(cid, list(self.graph.contexts(triple)))) self.assert_(self.c1 in contextList) self.assert_(self.c2 in contextList) def testRemoveContext(self): c1 = self.c1 self.addStuffInMultipleContexts() self.assertEquals(len(Graph(self.graph.store, c1)), 1) self.assertEquals(len(self.get_context(c1)), 1) self.graph.remove_context(self.get_context(c1)) self.assert_(self.c1 not in self.graph.contexts()) def testRemoveAny(self): Any = None self.addStuffInMultipleContexts() self.graph.remove((Any, Any, Any)) self.assertEquals(len(self.graph), 0) def testTriples(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 asserte = self.assertEquals triples = self.graph.triples graph = self.graph c1graph = Graph(self.graph.store, c1) c1triples = c1graph.triples Any = None self.addStuff() # unbound subjects with context asserte(len(list(c1triples((Any, likes, pizza)))), 2) asserte(len(list(c1triples((Any, hates, pizza)))), 1) asserte(len(list(c1triples((Any, likes, cheese)))), 3) asserte(len(list(c1triples((Any, hates, cheese)))), 0) # unbound subjects without context, same results! asserte(len(list(triples((Any, likes, pizza)))), 2) asserte(len(list(triples((Any, hates, pizza)))), 1) asserte(len(list(triples((Any, likes, cheese)))), 3) asserte(len(list(triples((Any, hates, cheese)))), 0) # unbound objects with context asserte(len(list(c1triples((michel, likes, Any)))), 2) asserte(len(list(c1triples((tarek, likes, Any)))), 2) asserte(len(list(c1triples((bob, hates, Any)))), 2) asserte(len(list(c1triples((bob, likes, Any)))), 1) # unbound objects without context, same results! asserte(len(list(triples((michel, likes, Any)))), 2) asserte(len(list(triples((tarek, likes, Any)))), 2) asserte(len(list(triples((bob, hates, Any)))), 2) asserte(len(list(triples((bob, likes, Any)))), 1) # unbound predicates with context asserte(len(list(c1triples((michel, Any, cheese)))), 1) asserte(len(list(c1triples((tarek, Any, cheese)))), 1) asserte(len(list(c1triples((bob, Any, pizza)))), 1) asserte(len(list(c1triples((bob, Any, michel)))), 1) # unbound predicates without context, same results! asserte(len(list(triples((michel, Any, cheese)))), 1) asserte(len(list(triples((tarek, Any, cheese)))), 1) asserte(len(list(triples((bob, Any, pizza)))), 1) asserte(len(list(triples((bob, Any, michel)))), 1) # unbound subject, objects with context asserte(len(list(c1triples((Any, hates, Any)))), 2) asserte(len(list(c1triples((Any, likes, Any)))), 5) # unbound subject, objects without context, same results! asserte(len(list(triples((Any, hates, Any)))), 2) asserte(len(list(triples((Any, likes, Any)))), 5) # unbound predicates, objects with context asserte(len(list(c1triples((michel, Any, Any)))), 2) asserte(len(list(c1triples((bob, Any, Any)))), 3) asserte(len(list(c1triples((tarek, Any, Any)))), 2) # unbound predicates, objects without context, same results! asserte(len(list(triples((michel, Any, Any)))), 2) asserte(len(list(triples((bob, Any, Any)))), 3) asserte(len(list(triples((tarek, Any, Any)))), 2) # unbound subjects, predicates with context asserte(len(list(c1triples((Any, Any, pizza)))), 3) asserte(len(list(c1triples((Any, Any, cheese)))), 3) asserte(len(list(c1triples((Any, Any, michel)))), 1) # unbound subjects, predicates without context, same results! asserte(len(list(triples((Any, Any, pizza)))), 3) asserte(len(list(triples((Any, Any, cheese)))), 3) asserte(len(list(triples((Any, Any, michel)))), 1) # all unbound with context asserte(len(list(c1triples((Any, Any, Any)))), 7) # all unbound without context, same result! asserte(len(list(triples((Any, Any, Any)))), 7) for c in [graph, self.get_context(c1)]: # unbound subjects asserte(set(c.subjects(likes, pizza)), set((michel, tarek))) asserte(set(c.subjects(hates, pizza)), set((bob,))) asserte(set(c.subjects(likes, cheese)), set([tarek, bob, michel])) asserte(set(c.subjects(hates, cheese)), set()) # unbound objects asserte(set(c.objects(michel, likes)), set([cheese, pizza])) asserte(set(c.objects(tarek, likes)), set([cheese, pizza])) asserte(set(c.objects(bob, hates)), set([michel, pizza])) asserte(set(c.objects(bob, likes)), set([cheese])) # unbound predicates asserte(set(c.predicates(michel, cheese)), set([likes])) asserte(set(c.predicates(tarek, cheese)), set([likes])) asserte(set(c.predicates(bob, pizza)), set([hates])) asserte(set(c.predicates(bob, michel)), set([hates])) asserte(set( c.subject_objects(hates)), set([(bob, pizza), (bob, michel)])) asserte(set(c.subject_objects(likes)), set([(tarek, cheese), (michel, cheese), (michel, pizza), (bob, cheese), (tarek, pizza)])) asserte(set(c.predicate_objects( michel)), set([(likes, cheese), (likes, pizza)])) asserte(set(c.predicate_objects(bob)), set([(likes, cheese), (hates, pizza), (hates, michel)])) asserte(set(c.predicate_objects( tarek)), set([(likes, cheese), (likes, pizza)])) asserte(set(c.subject_predicates( pizza)), set([(bob, hates), (tarek, likes), (michel, likes)])) asserte(set(c.subject_predicates(cheese)), set([( bob, likes), (tarek, likes), (michel, likes)])) asserte(set(c.subject_predicates(michel)), set([(bob, hates)])) asserte(set(c), set([ (bob, hates, michel), (bob, likes, cheese), (tarek, likes, pizza), (michel, likes, pizza), (michel, likes, cheese), (bob, hates, pizza), (tarek, likes, cheese)])) # remove stuff and make sure the graph is empty again self.removeStuff() asserte(len(list(c1triples((Any, Any, Any)))), 0) asserte(len(list(triples((Any, Any, Any)))), 0)
class TestSparql11(unittest.TestCase): def setUp(self): self.longMessage = True self.graph = ConjunctiveGraph('SPARQLUpdateStore') root = "http://localhost:3030/ukpp/" self.graph.open((root + "sparql", root + "update")) # clean out the store for c in self.graph.contexts(): c.remove((None, None, None)) assert len(c) == 0 def tearDown(self): self.graph.close() def testSimpleGraph(self): g = self.graph.get_context(graphuri) g.add((tarek, likes, pizza)) g.add((bob, likes, pizza)) g.add((bob, likes, cheese)) g2 = self.graph.get_context(othergraphuri) g2.add((michel, likes, pizza)) self.assertEquals(3, len(g), 'graph contains 3 triples') self.assertEquals(1, len(g2), 'other graph contains 1 triple') r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(2, len(list(r)), "two people like pizza") r = g.triples((None, likes, pizza)) self.assertEquals(2, len(list(r)), "two people like pizza") # Test initBindings r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }", initBindings={'s': tarek}) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = g.triples((tarek, likes, pizza)) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = g.triples((tarek, likes, cheese)) self.assertEquals(0, len(list(r)), "tarek doesn't like cheese") g2.add((tarek, likes, pizza)) g.remove((tarek, likes, pizza)) r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(1, len(list(r)), "only bob likes pizza") def testConjunctiveDefault(self): g = self.graph.get_context(graphuri) g.add((tarek, likes, pizza)) g2 = self.graph.get_context(othergraphuri) g2.add((bob, likes, pizza)) g.add((tarek, hates, cheese)) self.assertEquals(2, len(g), 'graph contains 2 triples') self.assertEquals(3, len(self.graph), 'default union graph contains three triples') r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(2, len(list(r)), "two people like pizza") r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }", initBindings={'s': tarek}) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = self.graph.triples((tarek, likes, pizza)) self.assertEquals(1, len(list(r)), "i was asking only about tarek") r = self.graph.triples((tarek, likes, cheese)) self.assertEquals(0, len(list(r)), "tarek doesn't like cheese") g2.remove((bob, likes, pizza)) r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEquals(1, len(list(r)), "only tarek likes pizza") def testUpdate(self): self.graph.update("INSERT DATA { GRAPH <urn:graph> { <urn:michel> <urn:likes> <urn:pizza> . } }") g = self.graph.get_context(graphuri) self.assertEquals(1, len(g), 'graph contains 1 triples') def testUpdateWithInitNs(self): self.graph.update( "INSERT DATA { GRAPH ns:graph { ns:michel ns:likes ns:pizza . } }", initNs={'ns': URIRef('urn:')} ) g = self.graph.get_context(graphuri) self.assertEquals( set(g.triples((None,None,None))), set([(michel,likes,pizza)]), 'only michel likes pizza' ) def testUpdateWithInitBindings(self): self.graph.update( "INSERT { GRAPH <urn:graph> { ?a ?b ?c . } } WherE { }", initBindings={ 'a': URIRef('urn:michel'), 'b': URIRef('urn:likes'), 'c': URIRef('urn:pizza'), } ) g = self.graph.get_context(graphuri) self.assertEquals( set(g.triples((None,None,None))), set([(michel,likes,pizza)]), 'only michel likes pizza' ) def testMultipleUpdateWithInitBindings(self): self.graph.update( "INSERT { GRAPH <urn:graph> { ?a ?b ?c . } } WHERE { };" "INSERT { GRAPH <urn:graph> { ?d ?b ?c . } } WHERE { }", initBindings={ 'a': URIRef('urn:michel'), 'b': URIRef('urn:likes'), 'c': URIRef('urn:pizza'), 'd': URIRef('urn:bob'), } ) g = self.graph.get_context(graphuri) self.assertEquals( set(g.triples((None,None,None))), set([(michel,likes,pizza), (bob,likes,pizza)]), 'michel and bob like pizza' ) def testNamedGraphUpdate(self): g = self.graph.get_context(graphuri) r1 = "INSERT DATA { <urn:michel> <urn:likes> <urn:pizza> }" g.update(r1) self.assertEquals( set(g.triples((None,None,None))), set([(michel,likes,pizza)]), 'only michel likes pizza' ) r2 = "DELETE { <urn:michel> <urn:likes> <urn:pizza> } " + \ "INSERT { <urn:bob> <urn:likes> <urn:pizza> } WHERE {}" g.update(r2) self.assertEquals( set(g.triples((None, None, None))), set([(bob, likes, pizza)]), 'only bob likes pizza' ) says = URIRef("urn:says") # Strings with unbalanced curly braces tricky_strs = ["With an unbalanced curly brace %s " % brace for brace in ["{", "}"]] for tricky_str in tricky_strs: r3 = """INSERT { ?b <urn:says> "%s" } WHERE { ?b <urn:likes> <urn:pizza>} """ % tricky_str g.update(r3) values = set() for v in g.objects(bob, says): values.add(str(v)) self.assertEquals(values, set(tricky_strs)) # Complicated Strings r4strings = [] r4strings.append(ur'''"1: adfk { ' \\\" \" { "''') r4strings.append(ur'''"2: adfk } <foo> #éï \\"''') r4strings.append(ur"""'3: adfk { " \\\' \' { '""") r4strings.append(ur"""'4: adfk } <foo> #éï \\'""") r4strings.append(ur'''"""5: adfk { ' \\\" \" { """''') r4strings.append(ur'''"""6: adfk } <foo> #éï \\"""''') r4strings.append(u'"""7: ad adsfj \n { \n sadfj"""') r4strings.append(ur"""'''8: adfk { " \\\' \' { '''""") r4strings.append(ur"""'''9: adfk } <foo> #éï \\'''""") r4strings.append(u"'''10: ad adsfj \n { \n sadfj'''") r4 = "\n".join([ u'INSERT DATA { <urn:michel> <urn:says> %s } ;' % s for s in r4strings ]) g.update(r4) values = set() for v in g.objects(michel, says): values.add(unicode(v)) self.assertEquals(values, set([re.sub(ur"\\(.)", ur"\1", re.sub(ur"^'''|'''$|^'|'$|" + ur'^"""|"""$|^"|"$', ur"", s)) for s in r4strings]))
class Rdflib(Store): name = 'rdflib' def __init__(self, logger=logging.getLogger(__name__), *args, **kwargs): self.logger = logger self._open_store() self.store_id = Literal(str(uuid.uuid4())) def _open_store(self, store='IOMemory'): self.logger.debug("opening store...") self.handle = ConjunctiveGraph(store, identifier="permanent") def _close_store(self): self.logger.debug("closing store") self.handle.close(commit_pending_transaction=True) def indicators_search(self, data): rv = [] for s, p, o in self.handle.triples((Literal(data['indicator']), None, None)): rv.append((s, p, o)) return rv # http://en.wikipedia.org/wiki/Resource_Description_Framework def indicators_create(self, data): if not isinstance(data, list): data = [data] for d in data: subject = Literal(d["indicator"]) for k in d: if k == "indicator": self.handle.add((subject, RDF.type, Literal(d["itype"]), self.store_id)) else: subject = Literal(d["indicator"]) self.handle.add((subject, Literal(k), Literal(d[k]), self.store_id)) self.logger.debug(self.handle.serialize(format="trig")) return len(data) def indicators_upsert(self, data): return self.indicators_create(data) def ping(self, token): return True def _token_generate(self): return binascii.b2a_hex(os.urandom(TOKEN_LENGTH)) def tokens_admin_exists(self): return True def tokens_create(self, data): return True def tokens_delete(self, data): return True def tokens_search(self, data): return True def token_admin(self, token): return True def token_read(self, token): return True def token_write(self, token): return True def token_edit(self, data): return True def token_last_activity_at(self, token, timestamp=None): return True
class TestSparql11(unittest.TestCase): def setUp(self): self.longMessage = True self.graph = ConjunctiveGraph('SPARQLUpdateStore') root = HOST + DB self.graph.open((root + "sparql", root + "update")) # clean out the store for c in self.graph.contexts(): c.remove((None, None, None)) assert len(c) == 0 def tearDown(self): self.graph.close() def testSimpleGraph(self): g = self.graph.get_context(graphuri) g.add((tarek, likes, pizza)) g.add((bob, likes, pizza)) g.add((bob, likes, cheese)) g2 = self.graph.get_context(othergraphuri) g2.add((michel, likes, pizza)) self.assertEqual(3, len(g), 'graph contains 3 triples') self.assertEqual(1, len(g2), 'other graph contains 1 triple') r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEqual(2, len(list(r)), "two people like pizza") r = g.triples((None, likes, pizza)) self.assertEqual(2, len(list(r)), "two people like pizza") # Test initBindings r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }", initBindings={'s': tarek}) self.assertEqual(1, len(list(r)), "i was asking only about tarek") r = g.triples((tarek, likes, pizza)) self.assertEqual(1, len(list(r)), "i was asking only about tarek") r = g.triples((tarek, likes, cheese)) self.assertEqual(0, len(list(r)), "tarek doesn't like cheese") g2.add((tarek, likes, pizza)) g.remove((tarek, likes, pizza)) r = g.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEqual(1, len(list(r)), "only bob likes pizza") def testConjunctiveDefault(self): g = self.graph.get_context(graphuri) g.add((tarek, likes, pizza)) g2 = self.graph.get_context(othergraphuri) g2.add((bob, likes, pizza)) g.add((tarek, hates, cheese)) self.assertEqual(2, len(g), 'graph contains 2 triples') # the following are actually bad tests as they depend on your endpoint, # as pointed out in the sparqlstore.py code: # ## For ConjunctiveGraphs, reading is done from the "default graph" Exactly ## what this means depends on your endpoint, because SPARQL does not offer a ## simple way to query the union of all graphs as it would be expected for a ## ConjuntiveGraph. ## ## Fuseki/TDB has a flag for specifying that the default graph ## is the union of all graphs (tdb:unionDefaultGraph in the Fuseki config). self.assertEqual(3, len(self.graph), 'default union graph should contain three triples but contains:\n' '%s' % list(self.graph)) r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEqual(2, len(list(r)), "two people like pizza") r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }", initBindings={'s': tarek}) self.assertEqual(1, len(list(r)), "i was asking only about tarek") r = self.graph.triples((tarek, likes, pizza)) self.assertEqual(1, len(list(r)), "i was asking only about tarek") r = self.graph.triples((tarek, likes, cheese)) self.assertEqual(0, len(list(r)), "tarek doesn't like cheese") g2.remove((bob, likes, pizza)) r = self.graph.query("SELECT * WHERE { ?s <urn:likes> <urn:pizza> . }") self.assertEqual(1, len(list(r)), "only tarek likes pizza") def testUpdate(self): self.graph.update("INSERT DATA { GRAPH <urn:graph> { <urn:michel> <urn:likes> <urn:pizza> . } }") g = self.graph.get_context(graphuri) self.assertEqual(1, len(g), 'graph contains 1 triples') def testUpdateWithInitNs(self): self.graph.update( "INSERT DATA { GRAPH ns:graph { ns:michel ns:likes ns:pizza . } }", initNs={'ns': URIRef('urn:')} ) g = self.graph.get_context(graphuri) self.assertEqual( set(g.triples((None,None,None))), set([(michel,likes,pizza)]), 'only michel likes pizza' ) def testUpdateWithInitBindings(self): self.graph.update( "INSERT { GRAPH <urn:graph> { ?a ?b ?c . } } WherE { }", initBindings={ 'a': URIRef('urn:michel'), 'b': URIRef('urn:likes'), 'c': URIRef('urn:pizza'), } ) g = self.graph.get_context(graphuri) self.assertEqual( set(g.triples((None,None,None))), set([(michel,likes,pizza)]), 'only michel likes pizza' ) def testMultipleUpdateWithInitBindings(self): self.graph.update( "INSERT { GRAPH <urn:graph> { ?a ?b ?c . } } WHERE { };" "INSERT { GRAPH <urn:graph> { ?d ?b ?c . } } WHERE { }", initBindings={ 'a': URIRef('urn:michel'), 'b': URIRef('urn:likes'), 'c': URIRef('urn:pizza'), 'd': URIRef('urn:bob'), } ) g = self.graph.get_context(graphuri) self.assertEqual( set(g.triples((None,None,None))), set([(michel,likes,pizza), (bob,likes,pizza)]), 'michel and bob like pizza' ) def testNamedGraphUpdate(self): g = self.graph.get_context(graphuri) r1 = "INSERT DATA { <urn:michel> <urn:likes> <urn:pizza> }" g.update(r1) self.assertEqual( set(g.triples((None,None,None))), set([(michel,likes,pizza)]), 'only michel likes pizza' ) r2 = "DELETE { <urn:michel> <urn:likes> <urn:pizza> } " + \ "INSERT { <urn:bob> <urn:likes> <urn:pizza> } WHERE {}" g.update(r2) self.assertEqual( set(g.triples((None, None, None))), set([(bob, likes, pizza)]), 'only bob likes pizza' ) says = URIRef("urn:says") # Strings with unbalanced curly braces tricky_strs = ["With an unbalanced curly brace %s " % brace for brace in ["{", "}"]] for tricky_str in tricky_strs: r3 = """INSERT { ?b <urn:says> "%s" } WHERE { ?b <urn:likes> <urn:pizza>} """ % tricky_str g.update(r3) values = set() for v in g.objects(bob, says): values.add(str(v)) self.assertEqual(values, set(tricky_strs)) # Complicated Strings r4strings = [] r4strings.append(r'''"1: adfk { ' \\\" \" { "''') r4strings.append(r'''"2: adfk } <foo> #éï \\"''') r4strings.append(r"""'3: adfk { " \\\' \' { '""") r4strings.append(r"""'4: adfk } <foo> #éï \\'""") r4strings.append(r'''"""5: adfk { ' \\\" \" { """''') r4strings.append(r'''"""6: adfk } <foo> #éï \\"""''') r4strings.append('"""7: ad adsfj \n { \n sadfj"""') r4strings.append(r"""'''8: adfk { " \\\' \' { '''""") r4strings.append(r"""'''9: adfk } <foo> #éï \\'''""") r4strings.append("'''10: ad adsfj \n { \n sadfj'''") r4 = "\n".join([ u'INSERT DATA { <urn:michel> <urn:says> %s } ;' % s for s in r4strings ]) g.update(r4) values = set() for v in g.objects(michel, says): values.add(text_type(v)) self.assertEqual(values, set([re.sub(r"\\(.)", r"\1", re.sub(r"^'''|'''$|^'|'$|" + r'^"""|"""$|^"|"$', r"", s)) for s in r4strings])) # IRI Containing ' or # # The fragment identifier must not be misinterpreted as a comment # (commenting out the end of the block). # The ' must not be interpreted as the start of a string, causing the } # in the literal to be identified as the end of the block. r5 = """INSERT DATA { <urn:michel> <urn:hates> <urn:foo'bar?baz;a=1&b=2#fragment>, "'}" }""" g.update(r5) values = set() for v in g.objects(michel, hates): values.add(text_type(v)) self.assertEqual(values, set([u"urn:foo'bar?baz;a=1&b=2#fragment", u"'}"])) # Comments r6 = u""" INSERT DATA { <urn:bob> <urn:hates> <urn:bob> . # No closing brace: } <urn:bob> <urn:hates> <urn:michel>. } #Final { } comment""" g.update(r6) values = set() for v in g.objects(bob, hates): values.add(v) self.assertEqual(values, set([bob, michel])) def testNamedGraphUpdateWithInitBindings(self): g = self.graph.get_context(graphuri) r = "INSERT { ?a ?b ?c } WHERE {}" g.update(r, initBindings={ 'a': michel, 'b': likes, 'c': pizza }) self.assertEqual( set(g.triples((None, None, None))), set([(michel, likes, pizza)]), 'only michel likes pizza' ) def testEmptyNamedGraph(self): empty_graph_iri = "urn:empty-graph-1" self.graph.update("CREATE GRAPH <%s>" % empty_graph_iri) named_graphs = [text_type(r[0]) for r in self.graph.query( "SELECT ?name WHERE { GRAPH ?name {} }")] # Some SPARQL endpoint backends (like TDB) are not able to find empty named graphs # (at least with this query) if empty_graph_iri in named_graphs: self.assertTrue(empty_graph_iri in [text_type(g.identifier) for g in self.graph.contexts()]) def testEmptyLiteral(self): # test for https://github.com/RDFLib/rdflib/issues/457 # also see test_issue457.py which is sparql store independent! g = self.graph.get_context(graphuri) g.add(( URIRef('http://example.com/s'), URIRef('http://example.com/p'), Literal(''))) o = tuple(g)[0][2] self.assertEqual(o, Literal(''), repr(o))
''' from rdflib import ConjunctiveGraph, plugin from rdflib.store import Store, VALID_STORE from rdflib import URIRef from api import BASE_GRAPH_URI, _get_mysql_config_string store = plugin.get('MySQL', Store)(identifier='rdfstore') rt = store.open(_get_mysql_config_string(), create=True) assert rt == VALID_STORE,"The underlying store is corrupted" citg = ConjunctiveGraph(store, identifier=URIRef(BASE_GRAPH_URI)) ''' citg = ConjunctiveGraph('MySQL', identifier=URIRef(BASE_GRAPH_URI)) rt = citg.open(_get_mysql_config_string(), create=True) assert rt == VALID_STORE,"The underlying store is corrupted" ''' citg.commit() citg.close() store.close() print "Successfully initialized database"
class ContextTestCase(unittest.TestCase): store_name = 'default' path = None storetest = True create = True michel = URIRef(u'michel') tarek = URIRef(u'tarek') bob = URIRef(u'bob') likes = URIRef(u'likes') hates = URIRef(u'hates') pizza = URIRef(u'pizza') cheese = URIRef(u'cheese') c1 = URIRef(u'context-1') c2 = URIRef(u'context-2') def setUp(self): self.graph = ConjunctiveGraph(store=self.store_name) self.graph.destroy(self.path) if isinstance(self.path, type(None)): if self.store_name == "SQLite": self.path = mkstemp(prefix='test',dir='/tmp') else: self.path = mkdtemp(prefix='test',dir='/tmp') self.graph.open(self.path, create=self.create) def tearDown(self): self.graph.destroy(self.path) try: self.graph.close() except: pass import os if hasattr(self,'path') and self.path is not None: if os.path.exists(self.path): if os.path.isdir(self.path): for f in os.listdir(self.path): os.unlink(self.path+'/'+f) os.rmdir(self.path) elif len(self.path.split(':')) == 1: os.unlink(self.path) else: os.remove(self.path) def get_context(self, identifier): assert isinstance(identifier, URIRef) or \ isinstance(identifier, BNode), type(identifier) return Graph(store=self.graph.store, identifier=identifier, namespace_manager=self) def addStuff(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 graph = Graph(self.graph.store, c1) graph.add((tarek, likes, pizza)) graph.add((tarek, likes, cheese)) graph.add((michel, likes, pizza)) graph.add((michel, likes, cheese)) graph.add((bob, likes, cheese)) graph.add((bob, hates, pizza)) graph.add((bob, hates, michel)) # gasp! def removeStuff(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 graph = Graph(self.graph.store, c1) graph.remove((tarek, likes, pizza)) graph.remove((tarek, likes, cheese)) graph.remove((michel, likes, pizza)) graph.remove((michel, likes, cheese)) graph.remove((bob, likes, cheese)) graph.remove((bob, hates, pizza)) graph.remove((bob, hates, michel)) # gasp! def addStuffInMultipleContexts(self): c1 = self.c1 c2 = self.c2 triple = (self.pizza, self.hates, self.tarek) # revenge! # add to default context self.graph.add(triple) # add to context 1 graph = Graph(self.graph.store, c1) graph.add(triple) # add to context 2 graph = Graph(self.graph.store, c2) graph.add(triple) def testConjunction(self): self.addStuffInMultipleContexts() triple = (self.pizza, self.likes, self.pizza) # add to context 1 graph = Graph(self.graph.store, self.c1) graph.add(triple) self.assertEquals(len(self.graph), len(graph)) def testAdd(self): self.addStuff() def testRemove(self): self.addStuff() self.removeStuff() def testLenInOneContext(self): c1 = self.c1 # make sure context is empty self.graph.remove_context(self.get_context(c1)) graph = Graph(self.graph.store, c1) oldLen = len(self.graph) for i in range(0, 10): graph.add((BNode(), self.hates, self.hates)) self.assertEquals(len(graph), oldLen + 10) self.assertEquals(len(self.get_context(c1)), oldLen + 10) self.graph.remove_context(self.get_context(c1)) self.assertEquals(len(self.graph), oldLen) self.assertEquals(len(graph), 0) def testLenInMultipleContexts(self): oldLen = len(self.graph) self.addStuffInMultipleContexts() # addStuffInMultipleContexts is adding the same triple to # three different contexts. So it's only + 1 self.assertEquals(len(self.graph), oldLen + 1) graph = Graph(self.graph.store, self.c1) self.assertEquals(len(graph), oldLen + 1) def testRemoveInMultipleContexts(self): c1 = self.c1 c2 = self.c2 triple = (self.pizza, self.hates, self.tarek) # revenge! self.addStuffInMultipleContexts() # triple should be still in store after removing it from c1 + c2 self.assert_(triple in self.graph) graph = Graph(self.graph.store, c1) graph.remove(triple) self.assert_(triple in self.graph) graph = Graph(self.graph.store, c2) graph.remove(triple) self.assert_(triple in self.graph) self.graph.remove(triple) # now gone! self.assert_(triple not in self.graph) # add again and see if remove without context removes all triples! self.addStuffInMultipleContexts() self.graph.remove(triple) self.assert_(triple not in self.graph) def testContexts(self): triple = (self.pizza, self.hates, self.tarek) # revenge! self.addStuffInMultipleContexts() def cid(c): if not isinstance(c, basestring): return c.identifier return c self.assert_(self.c1 in map(cid, self.graph.contexts())) self.assert_(self.c2 in map(cid, self.graph.contexts())) contextList = map(cid, list(self.graph.contexts(triple))) self.assert_(self.c1 in contextList) self.assert_(self.c2 in contextList) def testRemoveContext(self): c1 = self.c1 self.addStuffInMultipleContexts() self.assertEquals(len(Graph(self.graph.store, c1)), 1) self.assertEquals(len(self.get_context(c1)), 1) self.graph.remove_context(self.get_context(c1)) self.assert_(self.c1 not in self.graph.contexts()) def testRemoveAny(self): Any = None self.addStuffInMultipleContexts() self.graph.remove((Any, Any, Any)) self.assertEquals(len(self.graph), 0) def testTriples(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 asserte = self.assertEquals triples = self.graph.triples graph = self.graph c1graph = Graph(self.graph.store, c1) c1triples = c1graph.triples Any = None self.addStuff() # unbound subjects with context asserte(len(list(c1triples((Any, likes, pizza)))), 2) asserte(len(list(c1triples((Any, hates, pizza)))), 1) asserte(len(list(c1triples((Any, likes, cheese)))), 3) asserte(len(list(c1triples((Any, hates, cheese)))), 0) # unbound subjects without context, same results! asserte(len(list(triples((Any, likes, pizza)))), 2) asserte(len(list(triples((Any, hates, pizza)))), 1) asserte(len(list(triples((Any, likes, cheese)))), 3) asserte(len(list(triples((Any, hates, cheese)))), 0) # unbound objects with context asserte(len(list(c1triples((michel, likes, Any)))), 2) asserte(len(list(c1triples((tarek, likes, Any)))), 2) asserte(len(list(c1triples((bob, hates, Any)))), 2) asserte(len(list(c1triples((bob, likes, Any)))), 1) # unbound objects without context, same results! asserte(len(list(triples((michel, likes, Any)))), 2) asserte(len(list(triples((tarek, likes, Any)))), 2) asserte(len(list(triples((bob, hates, Any)))), 2) asserte(len(list(triples((bob, likes, Any)))), 1) # unbound predicates with context asserte(len(list(c1triples((michel, Any, cheese)))), 1) asserte(len(list(c1triples((tarek, Any, cheese)))), 1) asserte(len(list(c1triples((bob, Any, pizza)))), 1) asserte(len(list(c1triples((bob, Any, michel)))), 1) # unbound predicates without context, same results! asserte(len(list(triples((michel, Any, cheese)))), 1) asserte(len(list(triples((tarek, Any, cheese)))), 1) asserte(len(list(triples((bob, Any, pizza)))), 1) asserte(len(list(triples((bob, Any, michel)))), 1) # unbound subject, objects with context asserte(len(list(c1triples((Any, hates, Any)))), 2) asserte(len(list(c1triples((Any, likes, Any)))), 5) # unbound subject, objects without context, same results! asserte(len(list(triples((Any, hates, Any)))), 2) asserte(len(list(triples((Any, likes, Any)))), 5) # unbound predicates, objects with context asserte(len(list(c1triples((michel, Any, Any)))), 2) asserte(len(list(c1triples((bob, Any, Any)))), 3) asserte(len(list(c1triples((tarek, Any, Any)))), 2) # unbound predicates, objects without context, same results! asserte(len(list(triples((michel, Any, Any)))), 2) asserte(len(list(triples((bob, Any, Any)))), 3) asserte(len(list(triples((tarek, Any, Any)))), 2) # unbound subjects, predicates with context asserte(len(list(c1triples((Any, Any, pizza)))), 3) asserte(len(list(c1triples((Any, Any, cheese)))), 3) asserte(len(list(c1triples((Any, Any, michel)))), 1) # unbound subjects, predicates without context, same results! asserte(len(list(triples((Any, Any, pizza)))), 3) asserte(len(list(triples((Any, Any, cheese)))), 3) asserte(len(list(triples((Any, Any, michel)))), 1) # all unbound with context asserte(len(list(c1triples((Any, Any, Any)))), 7) # all unbound without context, same result! asserte(len(list(triples((Any, Any, Any)))), 7) for c in [graph, self.get_context(c1)]: # unbound subjects asserte(set(c.subjects(likes, pizza)), set((michel, tarek))) asserte(set(c.subjects(hates, pizza)), set((bob,))) asserte(set(c.subjects(likes, cheese)), set([tarek, bob, michel])) asserte(set(c.subjects(hates, cheese)), set()) # unbound objects asserte(set(c.objects(michel, likes)), set([cheese, pizza])) asserte(set(c.objects(tarek, likes)), set([cheese, pizza])) asserte(set(c.objects(bob, hates)), set([michel, pizza])) asserte(set(c.objects(bob, likes)), set([cheese])) # unbound predicates asserte(set(c.predicates(michel, cheese)), set([likes])) asserte(set(c.predicates(tarek, cheese)), set([likes])) asserte(set(c.predicates(bob, pizza)), set([hates])) asserte(set(c.predicates(bob, michel)), set([hates])) asserte(set(c.subject_objects(hates)), set([(bob, pizza), (bob, michel)])) asserte(set(c.subject_objects(likes)), set([(tarek, cheese), (michel, cheese), (michel, pizza), (bob, cheese), (tarek, pizza)])) asserte(set(c.predicate_objects(michel)), set([(likes, cheese), (likes, pizza)])) asserte(set(c.predicate_objects(bob)), set([(likes, cheese), (hates, pizza), (hates, michel)])) asserte(set(c.predicate_objects(tarek)), set([(likes, cheese), (likes, pizza)])) asserte(set(c.subject_predicates(pizza)), set([(bob, hates), (tarek, likes), (michel, likes)])) asserte(set(c.subject_predicates(cheese)), set([(bob, likes), (tarek, likes), (michel, likes)])) asserte(set(c.subject_predicates(michel)), set([(bob, hates)])) asserte(set(c), set([(bob, hates, michel), (bob, likes, cheese), (tarek, likes, pizza), (michel, likes, pizza), (michel, likes, cheese), (bob, hates, pizza), (tarek, likes, cheese)])) # remove stuff and make sure the graph is empty again self.removeStuff() asserte(len(list(c1triples((Any, Any, Any)))), 0) asserte(len(list(triples((Any, Any, Any)))), 0)
class ContextTestCase(unittest.TestCase): storetest = True identifier = URIRef("rdflib_test") michel = URIRef(u"michel") tarek = URIRef(u"tarek") bob = URIRef(u"bob") likes = URIRef(u"likes") hates = URIRef(u"hates") pizza = URIRef(u"pizza") cheese = URIRef(u"cheese") c1 = URIRef(u"context-1") c2 = URIRef(u"context-2") def setUp(self, uri="sqlite://", storename=None): store = plugin.get(storename, Store)(identifier=self.identifier) self.graph = ConjunctiveGraph(store, identifier=self.identifier) self.graph.open(uri, create=True) def tearDown(self, uri="sqlite://"): self.graph.destroy(uri) self.graph.close() def get_context(self, identifier): assert isinstance(identifier, URIRef) or \ isinstance(identifier, BNode), type(identifier) return Graph(store=self.graph.store, identifier=identifier, namespace_manager=self) def addStuff(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 graph = Graph(self.graph.store, c1) graph.add((tarek, likes, pizza)) graph.add((tarek, likes, cheese)) graph.add((michel, likes, pizza)) graph.add((michel, likes, cheese)) graph.add((bob, likes, cheese)) graph.add((bob, hates, pizza)) graph.add((bob, hates, michel)) # gasp! def removeStuff(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 graph = Graph(self.graph.store, c1) graph.remove((tarek, likes, pizza)) graph.remove((tarek, likes, cheese)) graph.remove((michel, likes, pizza)) graph.remove((michel, likes, cheese)) graph.remove((bob, likes, cheese)) graph.remove((bob, hates, pizza)) graph.remove((bob, hates, michel)) # gasp! def addStuffInMultipleContexts(self): c1 = self.c1 c2 = self.c2 triple = (self.pizza, self.hates, self.tarek) # revenge! # add to default context self.graph.add(triple) # add to context 1 graph = Graph(self.graph.store, c1) graph.add(triple) # add to context 2 graph = Graph(self.graph.store, c2) graph.add(triple) def testConjunction(self): self.addStuffInMultipleContexts() triple = (self.pizza, self.likes, self.pizza) # add to context 1 graph = Graph(self.graph.store, self.c1) graph.add(triple) self.assertEqual(len(self.graph.store), len(graph.store)) def testAdd(self): self.addStuff() def testRemove(self): self.addStuff() self.removeStuff() def testLenInOneContext(self): c1 = self.c1 # make sure context is empty self.graph.remove_context(self.get_context(c1)) graph = Graph(self.graph.store, c1) oldLen = len(self.graph) for i in range(0, 10): graph.add((BNode(), self.hates, self.hates)) self.assertEqual(len(graph), oldLen + 10) self.assertEqual(len(self.get_context(c1)), oldLen + 10) self.graph.remove_context(self.get_context(c1)) self.assertEqual(len(self.graph), oldLen) self.assertEqual(len(graph), 0) def testLenInMultipleContexts(self): oldLen = len(self.graph.store) self.addStuffInMultipleContexts() # addStuffInMultipleContexts is adding the same triple to # three different contexts. So it's only + 1 self.assertEqual(len(self.graph.store), oldLen + 1, [self.graph.store, oldLen + 1]) graph = Graph(self.graph.store, self.c1) self.assertEqual(len(graph.store), oldLen + 1, [graph.store, oldLen + 1]) def testRemoveInMultipleContexts(self): c1 = self.c1 c2 = self.c2 triple = (self.pizza, self.hates, self.tarek) # revenge! self.addStuffInMultipleContexts() # triple should be still in store after removing it from c1 + c2 self.assertIn(triple, self.graph) graph = Graph(self.graph.store, c1) graph.remove(triple) self.assertIn(triple, self.graph) graph = Graph(self.graph.store, c2) graph.remove(triple) self.assertIn(triple, self.graph) self.graph.remove(triple) # now gone! self.assertNotIn(triple, self.graph) # add again and see if remove without context removes all triples! self.addStuffInMultipleContexts() self.graph.remove(triple) self.assertNotIn(triple, self.graph) def testContexts(self): triple = (self.pizza, self.hates, self.tarek) # revenge! self.addStuffInMultipleContexts() def cid(c): if not isinstance(c, string_types): return c.identifier return c self.assertIn(self.c1, list(map(cid, self.graph.contexts()))) self.assertIn(self.c2, list(map(cid, self.graph.contexts()))) contextList = list(map(cid, list(self.graph.contexts(triple)))) self.assertIn(self.c1, contextList) self.assertIn(self.c2, contextList) def testRemoveContext(self): c1 = self.c1 self.addStuffInMultipleContexts() self.assertEqual(len(Graph(self.graph.store, c1)), 1) self.assertEqual(len(self.get_context(c1)), 1) self.graph.remove_context(self.get_context(c1)) self.assertNotIn(self.c1, self.graph.contexts()) def testRemoveAny(self): Any = None self.addStuffInMultipleContexts() self.graph.remove((Any, Any, Any)) self.assertEqual(len(self.graph), 0) def testTriples(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 asserte = self.assertEqual triples = self.graph.triples graph = self.graph c1graph = Graph(self.graph.store, c1) c1triples = c1graph.triples Any = None self.addStuff() # unbound subjects with context asserte(len(list(c1triples((Any, likes, pizza)))), 2) asserte(len(list(c1triples((Any, hates, pizza)))), 1) asserte(len(list(c1triples((Any, likes, cheese)))), 3) asserte(len(list(c1triples((Any, hates, cheese)))), 0) # unbound subjects without context, same results! asserte(len(list(triples((Any, likes, pizza)))), 2) asserte(len(list(triples((Any, hates, pizza)))), 1) asserte(len(list(triples((Any, likes, cheese)))), 3) asserte(len(list(triples((Any, hates, cheese)))), 0) # unbound objects with context asserte(len(list(c1triples((michel, likes, Any)))), 2) asserte(len(list(c1triples((tarek, likes, Any)))), 2) asserte(len(list(c1triples((bob, hates, Any)))), 2) asserte(len(list(c1triples((bob, likes, Any)))), 1) # unbound objects without context, same results! asserte(len(list(triples((michel, likes, Any)))), 2) asserte(len(list(triples((tarek, likes, Any)))), 2) asserte(len(list(triples((bob, hates, Any)))), 2) asserte(len(list(triples((bob, likes, Any)))), 1) # unbound predicates with context asserte(len(list(c1triples((michel, Any, cheese)))), 1) asserte(len(list(c1triples((tarek, Any, cheese)))), 1) asserte(len(list(c1triples((bob, Any, pizza)))), 1) asserte(len(list(c1triples((bob, Any, michel)))), 1) # unbound predicates without context, same results! asserte(len(list(triples((michel, Any, cheese)))), 1) asserte(len(list(triples((tarek, Any, cheese)))), 1) asserte(len(list(triples((bob, Any, pizza)))), 1) asserte(len(list(triples((bob, Any, michel)))), 1) # unbound subject, objects with context asserte(len(list(c1triples((Any, hates, Any)))), 2) asserte(len(list(c1triples((Any, likes, Any)))), 5) # unbound subject, objects without context, same results! asserte(len(list(triples((Any, hates, Any)))), 2) asserte(len(list(triples((Any, likes, Any)))), 5) # unbound predicates, objects with context asserte(len(list(c1triples((michel, Any, Any)))), 2) asserte(len(list(c1triples((bob, Any, Any)))), 3) asserte(len(list(c1triples((tarek, Any, Any)))), 2) # unbound predicates, objects without context, same results! asserte(len(list(triples((michel, Any, Any)))), 2) asserte(len(list(triples((bob, Any, Any)))), 3) asserte(len(list(triples((tarek, Any, Any)))), 2) # unbound subjects, predicates with context asserte(len(list(c1triples((Any, Any, pizza)))), 3) asserte(len(list(c1triples((Any, Any, cheese)))), 3) asserte(len(list(c1triples((Any, Any, michel)))), 1) # unbound subjects, predicates without context, same results! asserte(len(list(triples((Any, Any, pizza)))), 3) asserte(len(list(triples((Any, Any, cheese)))), 3) asserte(len(list(triples((Any, Any, michel)))), 1) # all unbound with context asserte(len(list(c1triples((Any, Any, Any)))), 7) # all unbound without context, same result! asserte(len(list(triples((Any, Any, Any)))), 7) for c in [graph, self.get_context(c1)]: # unbound subjects asserte(set(c.subjects(likes, pizza)), set((michel, tarek))) asserte(set(c.subjects(hates, pizza)), set((bob,))) asserte(set(c.subjects(likes, cheese)), set([tarek, bob, michel])) asserte(set(c.subjects(hates, cheese)), set()) # unbound objects asserte(set(c.objects(michel, likes)), set([cheese, pizza])) asserte(set(c.objects(tarek, likes)), set([cheese, pizza])) asserte(set(c.objects(bob, hates)), set([michel, pizza])) asserte(set(c.objects(bob, likes)), set([cheese])) # unbound predicates asserte(set(c.predicates(michel, cheese)), set([likes])) asserte(set(c.predicates(tarek, cheese)), set([likes])) asserte(set(c.predicates(bob, pizza)), set([hates])) asserte(set(c.predicates(bob, michel)), set([hates])) asserte(set( c.subject_objects(hates)), set([(bob, pizza), (bob, michel)])) asserte(set(c.subject_objects(likes)), set([(tarek, cheese), (michel, cheese), (michel, pizza), (bob, cheese), (tarek, pizza)])) asserte(set(c.predicate_objects( michel)), set([(likes, cheese), (likes, pizza)])) asserte(set(c.predicate_objects(bob)), set([(likes, cheese), (hates, pizza), (hates, michel)])) asserte(set(c.predicate_objects( tarek)), set([(likes, cheese), (likes, pizza)])) asserte(set(c.subject_predicates( pizza)), set([(bob, hates), (tarek, likes), (michel, likes)])) asserte(set(c.subject_predicates(cheese)), set([( bob, likes), (tarek, likes), (michel, likes)])) asserte(set(c.subject_predicates(michel)), set([(bob, hates)])) asserte(set(c), set([ (bob, hates, michel), (bob, likes, cheese), (tarek, likes, pizza), (michel, likes, pizza), (michel, likes, cheese), (bob, hates, pizza), (tarek, likes, cheese)])) # remove stuff and make sure the graph is empty again self.removeStuff() asserte(len(list(c1triples((Any, Any, Any)))), 0) asserte(len(list(triples((Any, Any, Any)))), 0)
class ContextTestCase(unittest.TestCase): store = 'default' slow = True tmppath = None def setUp(self): try: self.graph = ConjunctiveGraph(store=self.store) except ImportError: raise SkipTest( "Dependencies for store '%s' not available!" % self.store) if self.store == "SQLite": _, self.tmppath = mkstemp( prefix='test', dir='/tmp', suffix='.sqlite') else: self.tmppath = mkdtemp() self.graph.open(self.tmppath, create=True) self.michel = URIRef(u'michel') self.tarek = URIRef(u'tarek') self.bob = URIRef(u'bob') self.likes = URIRef(u'likes') self.hates = URIRef(u'hates') self.pizza = URIRef(u'pizza') self.cheese = URIRef(u'cheese') self.c1 = URIRef(u'context-1') self.c2 = URIRef(u'context-2') # delete the graph for each test! self.graph.remove((None, None, None)) def tearDown(self): self.graph.close() if os.path.isdir(self.tmppath): shutil.rmtree(self.tmppath) else: os.remove(self.tmppath) def addStuff(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 graph = Graph(self.graph.store, c1) graph.add((tarek, likes, pizza)) graph.add((tarek, likes, cheese)) graph.add((michel, likes, pizza)) graph.add((michel, likes, cheese)) graph.add((bob, likes, cheese)) graph.add((bob, hates, pizza)) graph.add((bob, hates, michel)) # gasp! def removeStuff(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 graph = Graph(self.graph.store, c1) graph.remove((tarek, likes, pizza)) graph.remove((tarek, likes, cheese)) graph.remove((michel, likes, pizza)) graph.remove((michel, likes, cheese)) graph.remove((bob, likes, cheese)) graph.remove((bob, hates, pizza)) graph.remove((bob, hates, michel)) # gasp! def addStuffInMultipleContexts(self): c1 = self.c1 c2 = self.c2 triple = (self.pizza, self.hates, self.tarek) # revenge! # add to default context self.graph.add(triple) # add to context 1 graph = Graph(self.graph.store, c1) graph.add(triple) # add to context 2 graph = Graph(self.graph.store, c2) graph.add(triple) def testConjunction(self): if self.store == "SQLite": raise SkipTest("Skipping known issue with __len__") self.addStuffInMultipleContexts() triple = (self.pizza, self.likes, self.pizza) # add to context 1 graph = Graph(self.graph.store, self.c1) graph.add(triple) self.assertEqual(len(self.graph), len(graph)) def testAdd(self): self.addStuff() def testRemove(self): self.addStuff() self.removeStuff() def testLenInOneContext(self): c1 = self.c1 # make sure context is empty self.graph.remove_context(self.graph.get_context(c1)) graph = Graph(self.graph.store, c1) oldLen = len(self.graph) for i in range(0, 10): graph.add((BNode(), self.hates, self.hates)) self.assertEqual(len(graph), oldLen + 10) self.assertEqual(len(self.graph.get_context(c1)), oldLen + 10) self.graph.remove_context(self.graph.get_context(c1)) self.assertEqual(len(self.graph), oldLen) self.assertEqual(len(graph), 0) def testLenInMultipleContexts(self): if self.store == "SQLite": raise SkipTest("Skipping known issue with __len__") oldLen = len(self.graph) self.addStuffInMultipleContexts() # addStuffInMultipleContexts is adding the same triple to # three different contexts. So it's only + 1 self.assertEqual(len(self.graph), oldLen + 1) graph = Graph(self.graph.store, self.c1) self.assertEqual(len(graph), oldLen + 1) def testRemoveInMultipleContexts(self): c1 = self.c1 c2 = self.c2 triple = (self.pizza, self.hates, self.tarek) # revenge! self.addStuffInMultipleContexts() # triple should be still in store after removing it from c1 + c2 self.assertTrue(triple in self.graph) graph = Graph(self.graph.store, c1) graph.remove(triple) self.assertTrue(triple in self.graph) graph = Graph(self.graph.store, c2) graph.remove(triple) self.assertTrue(triple in self.graph) self.graph.remove(triple) # now gone! self.assertTrue(triple not in self.graph) # add again and see if remove without context removes all triples! self.addStuffInMultipleContexts() self.graph.remove(triple) self.assertTrue(triple not in self.graph) def testContexts(self): triple = (self.pizza, self.hates, self.tarek) # revenge! self.addStuffInMultipleContexts() def cid(c): return c.identifier self.assertTrue(self.c1 in map(cid, self.graph.contexts())) self.assertTrue(self.c2 in map(cid, self.graph.contexts())) contextList = list(map(cid, list(self.graph.contexts(triple)))) self.assertTrue(self.c1 in contextList, (self.c1, contextList)) self.assertTrue(self.c2 in contextList, (self.c2, contextList)) def testRemoveContext(self): c1 = self.c1 self.addStuffInMultipleContexts() self.assertEqual(len(Graph(self.graph.store, c1)), 1) self.assertEqual(len(self.graph.get_context(c1)), 1) self.graph.remove_context(self.graph.get_context(c1)) self.assertTrue(self.c1 not in self.graph.contexts()) def testRemoveAny(self): Any = None self.addStuffInMultipleContexts() self.graph.remove((Any, Any, Any)) self.assertEqual(len(self.graph), 0) def testTriples(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 asserte = self.assertEqual triples = self.graph.triples graph = self.graph c1graph = Graph(self.graph.store, c1) c1triples = c1graph.triples Any = None self.addStuff() # unbound subjects with context asserte(len(list(c1triples((Any, likes, pizza)))), 2) asserte(len(list(c1triples((Any, hates, pizza)))), 1) asserte(len(list(c1triples((Any, likes, cheese)))), 3) asserte(len(list(c1triples((Any, hates, cheese)))), 0) # unbound subjects without context, same results! asserte(len(list(triples((Any, likes, pizza)))), 2) asserte(len(list(triples((Any, hates, pizza)))), 1) asserte(len(list(triples((Any, likes, cheese)))), 3) asserte(len(list(triples((Any, hates, cheese)))), 0) # unbound objects with context asserte(len(list(c1triples((michel, likes, Any)))), 2) asserte(len(list(c1triples((tarek, likes, Any)))), 2) asserte(len(list(c1triples((bob, hates, Any)))), 2) asserte(len(list(c1triples((bob, likes, Any)))), 1) # unbound objects without context, same results! asserte(len(list(triples((michel, likes, Any)))), 2) asserte(len(list(triples((tarek, likes, Any)))), 2) asserte(len(list(triples((bob, hates, Any)))), 2) asserte(len(list(triples((bob, likes, Any)))), 1) # unbound predicates with context asserte(len(list(c1triples((michel, Any, cheese)))), 1) asserte(len(list(c1triples((tarek, Any, cheese)))), 1) asserte(len(list(c1triples((bob, Any, pizza)))), 1) asserte(len(list(c1triples((bob, Any, michel)))), 1) # unbound predicates without context, same results! asserte(len(list(triples((michel, Any, cheese)))), 1) asserte(len(list(triples((tarek, Any, cheese)))), 1) asserte(len(list(triples((bob, Any, pizza)))), 1) asserte(len(list(triples((bob, Any, michel)))), 1) # unbound subject, objects with context asserte(len(list(c1triples((Any, hates, Any)))), 2) asserte(len(list(c1triples((Any, likes, Any)))), 5) # unbound subject, objects without context, same results! asserte(len(list(triples((Any, hates, Any)))), 2) asserte(len(list(triples((Any, likes, Any)))), 5) # unbound predicates, objects with context asserte(len(list(c1triples((michel, Any, Any)))), 2) asserte(len(list(c1triples((bob, Any, Any)))), 3) asserte(len(list(c1triples((tarek, Any, Any)))), 2) # unbound predicates, objects without context, same results! asserte(len(list(triples((michel, Any, Any)))), 2) asserte(len(list(triples((bob, Any, Any)))), 3) asserte(len(list(triples((tarek, Any, Any)))), 2) # unbound subjects, predicates with context asserte(len(list(c1triples((Any, Any, pizza)))), 3) asserte(len(list(c1triples((Any, Any, cheese)))), 3) asserte(len(list(c1triples((Any, Any, michel)))), 1) # unbound subjects, predicates without context, same results! asserte(len(list(triples((Any, Any, pizza)))), 3) asserte(len(list(triples((Any, Any, cheese)))), 3) asserte(len(list(triples((Any, Any, michel)))), 1) # all unbound with context asserte(len(list(c1triples((Any, Any, Any)))), 7) # all unbound without context, same result! asserte(len(list(triples((Any, Any, Any)))), 7) for c in [graph, self.graph.get_context(c1)]: # unbound subjects asserte(set(c.subjects(likes, pizza)), set((michel, tarek))) asserte(set(c.subjects(hates, pizza)), set((bob,))) asserte(set(c.subjects(likes, cheese)), set([tarek, bob, michel])) asserte(set(c.subjects(hates, cheese)), set()) # unbound objects asserte(set(c.objects(michel, likes)), set([cheese, pizza])) asserte(set(c.objects(tarek, likes)), set([cheese, pizza])) asserte(set(c.objects(bob, hates)), set([michel, pizza])) asserte(set(c.objects(bob, likes)), set([cheese])) # unbound predicates asserte(set(c.predicates(michel, cheese)), set([likes])) asserte(set(c.predicates(tarek, cheese)), set([likes])) asserte(set(c.predicates(bob, pizza)), set([hates])) asserte(set(c.predicates(bob, michel)), set([hates])) asserte(set( c.subject_objects(hates)), set([(bob, pizza), (bob, michel)])) asserte( set(c.subject_objects(likes)), set( [(tarek, cheese), (michel, cheese), (michel, pizza), (bob, cheese), (tarek, pizza)])) asserte(set(c.predicate_objects( michel)), set([(likes, cheese), (likes, pizza)])) asserte(set(c.predicate_objects(bob)), set([(likes, cheese), (hates, pizza), (hates, michel)])) asserte(set(c.predicate_objects( tarek)), set([(likes, cheese), (likes, pizza)])) asserte(set(c.subject_predicates( pizza)), set([(bob, hates), (tarek, likes), (michel, likes)])) asserte(set(c.subject_predicates(cheese)), set([( bob, likes), (tarek, likes), (michel, likes)])) asserte(set(c.subject_predicates(michel)), set([(bob, hates)])) asserte(set(c), set( [(bob, hates, michel), (bob, likes, cheese), (tarek, likes, pizza), (michel, likes, pizza), (michel, likes, cheese), (bob, hates, pizza), (tarek, likes, cheese)])) # remove stuff and make sure the graph is empty again self.removeStuff() asserte(len(list(c1triples((Any, Any, Any)))), 0) asserte(len(list(triples((Any, Any, Any)))), 0)
import rdflib from rdflib.store import NO_STORE, VALID_STORE from rdflib import plugin, ConjunctiveGraph, Graph from rdflib.store import Store g = ConjunctiveGraph('Sleepycat') rt = g.open('company', create=False) if rt == NO_STORE: rt = g.open('company', create=True) g.parse("companies_sorted.ntriples", format="nt") print('Triples in graph after add: ', len(g)) g.close()
class ContextTestCase(unittest.TestCase): store = "default" slow = True tmppath = None def setUp(self): try: self.graph = ConjunctiveGraph(store=self.store) except ImportError: raise SkipTest("Dependencies for store '%s' not available!" % self.store) if self.store == "SQLite": _, self.tmppath = mkstemp(prefix="test", dir="/tmp", suffix=".sqlite") else: self.tmppath = mkdtemp() self.graph.open(self.tmppath, create=True) self.michel = URIRef(u"michel") self.tarek = URIRef(u"tarek") self.bob = URIRef(u"bob") self.likes = URIRef(u"likes") self.hates = URIRef(u"hates") self.pizza = URIRef(u"pizza") self.cheese = URIRef(u"cheese") self.c1 = URIRef(u"context-1") self.c2 = URIRef(u"context-2") # delete the graph for each test! self.graph.remove((None, None, None)) def tearDown(self): self.graph.close() if os.path.isdir(self.tmppath): shutil.rmtree(self.tmppath) else: os.remove(self.tmppath) def addStuff(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 graph = Graph(self.graph.store, c1) graph.add((tarek, likes, pizza)) graph.add((tarek, likes, cheese)) graph.add((michel, likes, pizza)) graph.add((michel, likes, cheese)) graph.add((bob, likes, cheese)) graph.add((bob, hates, pizza)) graph.add((bob, hates, michel)) # gasp! def removeStuff(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 graph = Graph(self.graph.store, c1) graph.remove((tarek, likes, pizza)) graph.remove((tarek, likes, cheese)) graph.remove((michel, likes, pizza)) graph.remove((michel, likes, cheese)) graph.remove((bob, likes, cheese)) graph.remove((bob, hates, pizza)) graph.remove((bob, hates, michel)) # gasp! def addStuffInMultipleContexts(self): c1 = self.c1 c2 = self.c2 triple = (self.pizza, self.hates, self.tarek) # revenge! # add to default context self.graph.add(triple) # add to context 1 graph = Graph(self.graph.store, c1) graph.add(triple) # add to context 2 graph = Graph(self.graph.store, c2) graph.add(triple) def testConjunction(self): if self.store == "SQLite": raise SkipTest("Skipping known issue with __len__") self.addStuffInMultipleContexts() triple = (self.pizza, self.likes, self.pizza) # add to context 1 graph = Graph(self.graph.store, self.c1) graph.add(triple) self.assertEqual(len(self.graph), len(graph)) def testAdd(self): self.addStuff() def testRemove(self): self.addStuff() self.removeStuff() def testLenInOneContext(self): c1 = self.c1 # make sure context is empty self.graph.remove_context(self.graph.get_context(c1)) graph = Graph(self.graph.store, c1) oldLen = len(self.graph) for i in range(0, 10): graph.add((BNode(), self.hates, self.hates)) self.assertEqual(len(graph), oldLen + 10) self.assertEqual(len(self.graph.get_context(c1)), oldLen + 10) self.graph.remove_context(self.graph.get_context(c1)) self.assertEqual(len(self.graph), oldLen) self.assertEqual(len(graph), 0) def testLenInMultipleContexts(self): if self.store == "SQLite": raise SkipTest("Skipping known issue with __len__") oldLen = len(self.graph) self.addStuffInMultipleContexts() # addStuffInMultipleContexts is adding the same triple to # three different contexts. So it's only + 1 self.assertEqual(len(self.graph), oldLen + 1) graph = Graph(self.graph.store, self.c1) self.assertEqual(len(graph), oldLen + 1) def testRemoveInMultipleContexts(self): c1 = self.c1 c2 = self.c2 triple = (self.pizza, self.hates, self.tarek) # revenge! self.addStuffInMultipleContexts() # triple should be still in store after removing it from c1 + c2 self.assertTrue(triple in self.graph) graph = Graph(self.graph.store, c1) graph.remove(triple) self.assertTrue(triple in self.graph) graph = Graph(self.graph.store, c2) graph.remove(triple) self.assertTrue(triple in self.graph) self.graph.remove(triple) # now gone! self.assertTrue(triple not in self.graph) # add again and see if remove without context removes all triples! self.addStuffInMultipleContexts() self.graph.remove(triple) self.assertTrue(triple not in self.graph) def testContexts(self): triple = (self.pizza, self.hates, self.tarek) # revenge! self.addStuffInMultipleContexts() def cid(c): return c.identifier self.assertTrue(self.c1 in map(cid, self.graph.contexts())) self.assertTrue(self.c2 in map(cid, self.graph.contexts())) contextList = list(map(cid, list(self.graph.contexts(triple)))) self.assertTrue(self.c1 in contextList, (self.c1, contextList)) self.assertTrue(self.c2 in contextList, (self.c2, contextList)) def testRemoveContext(self): c1 = self.c1 self.addStuffInMultipleContexts() self.assertEqual(len(Graph(self.graph.store, c1)), 1) self.assertEqual(len(self.graph.get_context(c1)), 1) self.graph.remove_context(self.graph.get_context(c1)) self.assertTrue(self.c1 not in self.graph.contexts()) def testRemoveAny(self): Any = None self.addStuffInMultipleContexts() self.graph.remove((Any, Any, Any)) self.assertEqual(len(self.graph), 0) def testTriples(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 asserte = self.assertEqual triples = self.graph.triples graph = self.graph c1graph = Graph(self.graph.store, c1) c1triples = c1graph.triples Any = None self.addStuff() # unbound subjects with context asserte(len(list(c1triples((Any, likes, pizza)))), 2) asserte(len(list(c1triples((Any, hates, pizza)))), 1) asserte(len(list(c1triples((Any, likes, cheese)))), 3) asserte(len(list(c1triples((Any, hates, cheese)))), 0) # unbound subjects without context, same results! asserte(len(list(triples((Any, likes, pizza)))), 2) asserte(len(list(triples((Any, hates, pizza)))), 1) asserte(len(list(triples((Any, likes, cheese)))), 3) asserte(len(list(triples((Any, hates, cheese)))), 0) # unbound objects with context asserte(len(list(c1triples((michel, likes, Any)))), 2) asserte(len(list(c1triples((tarek, likes, Any)))), 2) asserte(len(list(c1triples((bob, hates, Any)))), 2) asserte(len(list(c1triples((bob, likes, Any)))), 1) # unbound objects without context, same results! asserte(len(list(triples((michel, likes, Any)))), 2) asserte(len(list(triples((tarek, likes, Any)))), 2) asserte(len(list(triples((bob, hates, Any)))), 2) asserte(len(list(triples((bob, likes, Any)))), 1) # unbound predicates with context asserte(len(list(c1triples((michel, Any, cheese)))), 1) asserte(len(list(c1triples((tarek, Any, cheese)))), 1) asserte(len(list(c1triples((bob, Any, pizza)))), 1) asserte(len(list(c1triples((bob, Any, michel)))), 1) # unbound predicates without context, same results! asserte(len(list(triples((michel, Any, cheese)))), 1) asserte(len(list(triples((tarek, Any, cheese)))), 1) asserte(len(list(triples((bob, Any, pizza)))), 1) asserte(len(list(triples((bob, Any, michel)))), 1) # unbound subject, objects with context asserte(len(list(c1triples((Any, hates, Any)))), 2) asserte(len(list(c1triples((Any, likes, Any)))), 5) # unbound subject, objects without context, same results! asserte(len(list(triples((Any, hates, Any)))), 2) asserte(len(list(triples((Any, likes, Any)))), 5) # unbound predicates, objects with context asserte(len(list(c1triples((michel, Any, Any)))), 2) asserte(len(list(c1triples((bob, Any, Any)))), 3) asserte(len(list(c1triples((tarek, Any, Any)))), 2) # unbound predicates, objects without context, same results! asserte(len(list(triples((michel, Any, Any)))), 2) asserte(len(list(triples((bob, Any, Any)))), 3) asserte(len(list(triples((tarek, Any, Any)))), 2) # unbound subjects, predicates with context asserte(len(list(c1triples((Any, Any, pizza)))), 3) asserte(len(list(c1triples((Any, Any, cheese)))), 3) asserte(len(list(c1triples((Any, Any, michel)))), 1) # unbound subjects, predicates without context, same results! asserte(len(list(triples((Any, Any, pizza)))), 3) asserte(len(list(triples((Any, Any, cheese)))), 3) asserte(len(list(triples((Any, Any, michel)))), 1) # all unbound with context asserte(len(list(c1triples((Any, Any, Any)))), 7) # all unbound without context, same result! asserte(len(list(triples((Any, Any, Any)))), 7) for c in [graph, self.graph.get_context(c1)]: # unbound subjects asserte(set(c.subjects(likes, pizza)), set((michel, tarek))) asserte(set(c.subjects(hates, pizza)), set((bob, ))) asserte(set(c.subjects(likes, cheese)), set([tarek, bob, michel])) asserte(set(c.subjects(hates, cheese)), set()) # unbound objects asserte(set(c.objects(michel, likes)), set([cheese, pizza])) asserte(set(c.objects(tarek, likes)), set([cheese, pizza])) asserte(set(c.objects(bob, hates)), set([michel, pizza])) asserte(set(c.objects(bob, likes)), set([cheese])) # unbound predicates asserte(set(c.predicates(michel, cheese)), set([likes])) asserte(set(c.predicates(tarek, cheese)), set([likes])) asserte(set(c.predicates(bob, pizza)), set([hates])) asserte(set(c.predicates(bob, michel)), set([hates])) asserte(set(c.subject_objects(hates)), set([(bob, pizza), (bob, michel)])) asserte( set(c.subject_objects(likes)), set([ (tarek, cheese), (michel, cheese), (michel, pizza), (bob, cheese), (tarek, pizza), ]), ) asserte(set(c.predicate_objects(michel)), set([(likes, cheese), (likes, pizza)])) asserte( set(c.predicate_objects(bob)), set([(likes, cheese), (hates, pizza), (hates, michel)]), ) asserte(set(c.predicate_objects(tarek)), set([(likes, cheese), (likes, pizza)])) asserte( set(c.subject_predicates(pizza)), set([(bob, hates), (tarek, likes), (michel, likes)]), ) asserte( set(c.subject_predicates(cheese)), set([(bob, likes), (tarek, likes), (michel, likes)]), ) asserte(set(c.subject_predicates(michel)), set([(bob, hates)])) asserte( set(c), set([ (bob, hates, michel), (bob, likes, cheese), (tarek, likes, pizza), (michel, likes, pizza), (michel, likes, cheese), (bob, hates, pizza), (tarek, likes, cheese), ]), ) # remove stuff and make sure the graph is empty again self.removeStuff() asserte(len(list(c1triples((Any, Any, Any)))), 0) asserte(len(list(triples((Any, Any, Any)))), 0)
class MirbaseDB(object): def __init__(self, db_path): self.g = ConjunctiveGraph() self.path = db_path self.choices = set() self.labels = {} def create_graph(self): self.g.open(self.path + "data.rdf", create=True) data = self.parse_mirbase(self.path) #g = ConjunctiveGraph(store="SPARQLUpdateStore") # g.bind() mirna_class = URIRef("http://purl.obolibrary.org/obo/SO_0000276") for mid in data: mirna_instance = URIRef(MIRBASE + data[mid]["acc"]) self.g.add((mirna_instance, RDF.type, mirna_class)) label = Literal(data[mid]["name"]) self.g.add((mirna_instance, RDFS.label, label)) description = Literal(data[mid]["description"]) self.g.add((mirna_instance, RDFS.comment, description)) for p in data[mid]["previous_names"]: if p.strip(): previous_name = Literal(p) self.g.add((mirna_instance, MIRBASE["previous_acc"], previous_name)) for mature in data[mid]["mature"]: mature_instance = URIRef(MIRBASE + data[mid]["mature"][mature]["acc"]) self.g.add((mature_instance, RDF.type, mirna_class)) mature_label = Literal(data[mid]["mature"][mature]["name"]) self.g.add((mature_instance, RDFS.label, mature_label)) for mature_p in data[mid]["mature"][mature]["previous_names"]: if mature_p.strip(): mature_previous_name = Literal(mature_p) self.g.add((mature_instance, MIRBASE["previous_acc"], mature_previous_name)) self.g.add((mirna_instance, MIRBASE["stemloopOf"], mature_instance)) def parse_mirbase(self, mirbase_root): mirna_dic = {} with open(mirbase_root + "mirna.txt") as mirnas: for m in mirnas: props = m.strip().split("\t") mname = props[2] mid = props[0] macc = props[1] mdesc = props[4] mprev = props[3].split(";") if int(props[-1]) != 22: # not h**o sapiens continue mirna_dic[mid] = {} mirna_dic[mid]["name"] = mname mirna_dic[mid]["acc"] = macc mirna_dic[mid]["previous_names"] = mprev mirna_dic[mid]["description"] = mdesc mature_dic = {} with open(mirbase_root + "mirna_mature.txt") as mirnas: for m in mirnas: props = m.strip().split("\t") mname = props[1] mid = props[0] macc = props[3] # mdesc = props[4] mprev = props[2].split(";") if not mname.startswith("hsa-"): # not h**o sapiens continue mature_dic[mid] = {} mature_dic[mid]["name"] = mname mature_dic[mid]["previous_names"] = mprev mature_dic[mid]["acc"] = macc with open(mirbase_root + "mirna_pre_mature.txt") as mirnas: for m in mirnas: props = m.strip().split("\t") mid, matureid = props[:2] if mid in mirna_dic: if "mature" not in mirna_dic[mid]: mirna_dic[mid]["mature"] = {} mirna_dic[mid]["mature"][matureid] = mature_dic[matureid] # pp.pprint(mirna_dic) return mirna_dic def map_label(self, label): label = label.lower() label = label.replace("microrna", "mir") label = label.replace("mirna", "mir") if not label.startswith("hsa-"): label = "hsa-" + label result = process.extractOne(label, self.choices) # result = process.extract(label, choices, limit=3) """if result[1] != 100: print print "original:", label.encode("utf-8"), result # if label[-1].isdigit(): # label += "a" # else: new_label = label + "-1" revised_result = process.extractOne(new_label, self.choices) if revised_result[1] != 100: new_label = label + "a" revised_result = process.extractOne(new_label, self.choices) if revised_result[1] > result[1]: result = revised_result print "revised:", label.encode("utf-8"), result""" return result def load_graph(self): self.g.load(self.path + "data.rdf") # print "Opened graph with {} triples".format(len(self.g)) self.get_label_to_acc() self.choices = self.labels.keys() def get_label_to_acc(self): for subj, pred, obj in self.g.triples((None, RDFS.label, None)): self.labels[str(obj)] = str(subj) for subj, pred, obj in self.g.triples((None, RDFS.label, None)): self.labels[str(obj)] = str(subj) def save_graph(self): self.g.serialize(self.path + "data.rdf", format='pretty-xml') print('Triples in graph after add: ', len(self.g)) self.g.close()
class Command(BaseCommand): args = "<path_to_skos_file path_to_skos_file>..." help = "import skos ref in rdflib alchemy store" def __init__(self): super(Command, self).__init__() self.ident = "jocondelab" #'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. #'NAME': '', # Or path to database file if using sqlite3. # The following settings are not used with sqlite3: #'USER': '', #'PASSWORD': '', #'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP. #'PORT': '' db_settings = connections['default'].settings_dict sa_db_settings = { 'engine': 'postgresql+psycopg2' if db_settings['ENGINE'] == "django.db.backends.postgresql_psycopg2" else db_settings['ENGINE'], 'user': db_settings['USER'], 'password': db_settings['PASSWORD'], 'port': db_settings['PORT'] if db_settings['PORT'] else "5432", 'host': db_settings['HOST'] if db_settings['HOST'] else "localhost", 'name': db_settings['NAME'] } self.connect_config = "%(engine)s://%(user)s:%(password)s@%(host)s:%(port)s/%(name)s"%sa_db_settings self.store = plugin.get("SQLAlchemy", Store)(identifier=self.ident) self.graph = ConjunctiveGraph(self.store, identifier=self.ident) self.graph.open(self.connect_config, create=True) def handle(self, *args, **options): #import pydevd #@UnresolvedImport #pydevd.settrace(suspend=True) for skos_path, public_id in zip(args[::2],args[1::2]): filepath = os.path.abspath(skos_path) self.stdout.write("Importing %s" % filepath) self.graph.parse(filepath, publicID=public_id, format='xml') self.stdout.write("graph size %d" % len(self.graph)) self.graph.commit() self.graph.close() self.store = plugin.get("SQLAlchemy", Store)(identifier=self.ident) self.graph = ConjunctiveGraph(self.store, identifier=self.ident) self.graph.open(self.connect_config, create=False) self.stdout.write("correct alt labels") litteral_statements = self.store.tables['literal_statements'] with self.store.engine.connect() as connection: q = litteral_statements.select().where(litteral_statements.c.predicate == "http://www.w3.org/2004/02/skos/core#altLabel") for row in connection.execute(q): if row['object'] and row['object'] != row['object'].strip(): u_q = litteral_statements.update().where(and_( litteral_statements.c.subject == row['subject'], litteral_statements.c.predicate == row['predicate'], litteral_statements.c.object == row['object'], litteral_statements.c.context == row['context'], litteral_statements.c.termComb == row['termcomb'], litteral_statements.c.objLanguage == row['objlanguage'], litteral_statements.c.objDatatype == row['objdatatype'] )).values(object = row['object'].strip() ) #u_q_compiled = u_q.compile() #self.stdout.write("UPDATE QUERY for %s : %s : %s - %s" % (row['subject'], row['object'], str(u_q_compiled), repr(u_q_compiled.params))) connection.execute(u_q) self.stdout.write("graph size %d" % len(self.graph)) self.stdout.write("graph contexts %s" % repr([g for g in self.graph.contexts()]))
class MirbaseDB(object): def __init__(self, db_path): self.g = ConjunctiveGraph() self.path = db_path self.choices = set() self.labels = {} def create_graph(self): self.g.open(self.path + mirbasegraph_name, create=True) data = self.parse_mirbase(self.path) # g = ConjunctiveGraph(store="SPARQLUpdateStore") # g.bind() mirna_class = URIRef("http://purl.obolibrary.org/obo/SO_0000276") for mid in data: mirna_instance = URIRef(MIRBASE + data[mid]["acc"]) self.g.add((mirna_instance, RDF.type, mirna_class)) label = Literal(data[mid]["name"]) self.g.add((mirna_instance, RDFS.label, label)) description = Literal(data[mid]["description"]) self.g.add((mirna_instance, RDFS.comment, description)) for p in data[mid]["previous_names"]: if p.strip(): previous_name = Literal(p) self.g.add((mirna_instance, MIRBASE["previous_acc"], previous_name)) for mature in data[mid]["mature"]: mature_instance = URIRef(MIRBASE + data[mid]["mature"][mature]["acc"]) self.g.add((mature_instance, RDF.type, mirna_class)) mature_label = Literal(data[mid]["mature"][mature]["name"]) self.g.add((mature_instance, RDFS.label, mature_label)) for mature_p in data[mid]["mature"][mature]["previous_names"]: if mature_p.strip(): mature_previous_name = Literal(mature_p) self.g.add((mature_instance, MIRBASE["previous_acc"], mature_previous_name)) self.g.add((mirna_instance, MIRBASE["stemloopOf"], mature_instance)) self.get_label_to_acc() self.choices = self.labels.keys() goa_data = self.parse_goa_gaf("data/goa_human_rna.gaf") for label in self.labels: if label in goa_data: for go_id in goa_data[label]: mirna_instance = self.labels[label] self.g.add((mirna_instance, MIRBASE["goa"], Literal(go_id))) def parse_goa_gaf(self, gaf_file): goa_dic = {} with open(gaf_file) as gaf: for l in gaf: if l[0] != "!": values = l.split("\t") db_id = values[1] go_id = values[4] mirna_id = values[9].replace(".", "") mirna_id = mirna_id.replace("H**o sapiens ", "") mirna_id = mirna_id.replace("(human) ", "") mirna_id = mirna_id.replace("microRNA miR", "miR") if not mirna_id.startswith("hsa-"): mirna_id = "hsa-" + mirna_id mirna = self.map_label(mirna_id) if mirna[1] != 100: print mirna_id, mirna if mirna[0] not in goa_dic: goa_dic[mirna[0]] = [] goa_dic[mirna[0]].append(go_id) print "# of mirnas with gos: {}".format(len(goa_dic)) return goa_dic def parse_mirbase(self, mirbase_root): mirna_dic = {} with open(mirbase_root + "mirna.txt") as mirnas: for m in mirnas: props = m.strip().split("\t") mname = props[2] mid = props[0] macc = props[1] mdesc = props[4] mprev = props[3].split(";") if int(props[-1]) != 22: # not h**o sapiens continue mirna_dic[mid] = {} mirna_dic[mid]["name"] = mname mirna_dic[mid]["acc"] = macc mirna_dic[mid]["previous_names"] = mprev mirna_dic[mid]["description"] = mdesc mature_dic = {} with open(mirbase_root + "mirna_mature.txt") as mirnas: for m in mirnas: props = m.strip().split("\t") mname = props[1] mid = props[0] macc = props[3] # mdesc = props[4] mprev = props[2].split(";") if not mname.startswith("hsa-"): # not h**o sapiens continue mature_dic[mid] = {} mature_dic[mid]["name"] = mname mature_dic[mid]["previous_names"] = mprev mature_dic[mid]["acc"] = macc with open(mirbase_root + "mirna_pre_mature.txt") as mirnas: for m in mirnas: props = m.strip().split("\t") mid, matureid = props[:2] if mid in mirna_dic: if "mature" not in mirna_dic[mid]: mirna_dic[mid]["mature"] = {} mirna_dic[mid]["mature"][matureid] = mature_dic[matureid] # pp.pprint(mirna_dic) return mirna_dic def map_label(self, label): global mirbasedic result = ("", 0) # first check if a chebi mappings dictionary is loaded in memory if label in mirbasedic: result = mirbasedic[label] else: new_label = label.lower() if new_label.startswith("h-"): new_label = new_label[2:] elif new_label.startswith("pre-"): new_label = new_label[4:] new_label = new_label.replace("microrna", "mir") new_label = new_label.replace("mirna", "mir") if "-" not in new_label: # fix cases like miR200c if " " in new_label: new_label = new_label.replace(" ", "-") else: x = re.split("(\d+)", new_label) new_label = x[0] + "-" + "".join(x[1:]) else: mirna = new_label.split("-") if len(mirna[1]) > 2: # print "mapping", mirna[1] if mirna[1][-3].isdigit() and mirna[1][-2].isalpha() and mirna[1][-1].isdigit(): # print "match!", new_label = mirna[0] + "-" + mirna[1][:-1] + "-" + mirna[1][-1] # print new_label if not new_label.startswith("hsa-"): new_label = "hsa-" + new_label if label[0].isdigit(): new_label = "hsa-mir-" + label elif label[0] == "-" and label[1:].isdigit(): new_label = "hsa-mir" + label result = process.extractOne(new_label, self.choices) # print result # result = process.extract(label, choices, limit=3) print result if result[1] != 100: # print # print "original:", new_label.encode("utf-8"), result # if label[-1].isdigit(): # label += "a" # else: new_new_label = new_label + "-1" revised_result = process.extractOne(new_new_label, self.choices) # logging.info(str(revised_result)) if revised_result[1] != 100: new_new_label = new_label + "a" revised_result = process.extractOne(new_new_label, self.choices) # logging.info(str(revised_result)) if revised_result[1] != 100: new_new_label += "-1" revised_result2 = process.extractOne(new_new_label, self.choices) # logging.info(str(revised_result2)) if revised_result2[1] > revised_result[1]: revised_result = revised_result2 if revised_result[1] > result[1]: result = revised_result new_label = new_new_label # print "revised:", new_label.encode("utf-8"), result # print "mapped {} to {} to {}".format(label, new_label, result) if result[1] == 100: mirbasedic[label] = result return result def load_graph(self): if os.path.isfile(self.path + mirbasegraph_name): logging.info("Loading miRbase...") self.g.load(self.path + mirbasegraph_name) # print "Opened graph with {} triples".format(len(self.g)) self.get_label_to_acc() self.choices = self.labels.keys() logging.info("done.") else: logging.info("miRBase graph not found") def get_label_to_acc(self): for subj, pred, obj in self.g.triples((None, RDFS.label, None)): self.labels[str(obj)] = subj for subj, pred, obj in self.g.triples((None, RDFS.label, None)): self.labels[str(obj)] = subj def save_graph(self): self.g.serialize(self.path + mirbasegraph_name, format="pretty-xml") print "Triples in graph after add: ", len(self.g) self.g.close()