def update_list(): """ Reads the triple store looking for twitter usernames and adds them to the dev8d list. """ g = ConjunctiveGraph("Sleepycat") g.open("store") # get oauth client in order credentials = get_credentials() consumer = oauth.Consumer(credentials['key'], credentials['secret']) client = oauth.Client(consumer, credentials['access_token']) list_update_url = 'http://api.twitter.com/1/%s/%s/members.json' % \ (credentials['list_owner'], credentials['list_name']) # create the list if necessary create_list(credentials['list_owner'], credentials['list_name'], client) # look at all the twitter usernames and add them to the list for twitter_username in g.objects(predicate=w['Twitter']): id = twitter_user_id(twitter_username, client) if id: logging.info("adding %s (%s) to list" % (twitter_username, id)) body = "id=%s" % id resp, content = client.request(list_update_url, 'POST', body=body) else: logging.error("unable to get twitter id for %s" % twitter_username) g.close()
class TestBDBTransactions(unittest.TestCase): non_core = True def setUp(self): self.graph = ConjunctiveGraph(store="BerkeleyDB") self.path = mkdtemp() self.graph.open(self.path, create=True) def tearDown(self): 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 __manyOpsManyThreads(self, worker, workers=10, triples=1000, input=[]): all_ops = [] pool = [] for i in range(0, workers): t = Thread(target=worker, args=(all_ops, self.graph, triples), kwargs={'input':input}) pool.append(t) t.start() for t in pool: t.join() return all_ops def testAddManyManyThreads(self): # TODO: sometimes this test leads to TypeError exceptions? w = 4 t = 1000 self.__manyOpsManyThreads(worker_add, workers=w, triples=t) #print "graph size after finish: ", len(self.graph) self.failUnless(len(self.graph) == w*t) def testRemove(self): ops = self.__manyOpsManyThreads(worker_add, workers=1, triples=10) self.__manyOpsManyThreads(worker_remove, workers=1, triples=7, input=ops) #print "graph size after finish: ", len(self.graph) self.failUnless(len(self.graph) == 3) def testRemoveAll(self): ops = self.__manyOpsManyThreads(worker_add, workers=1, triples=10) try: self.graph.remove((None, None, None)) except Exception, e: #print "Could not remove all: ", e raise e #print "graph size after finish: ", len(self.graph) self.failUnless(len(self.graph) == 0)
def testModel(self): g = ConjunctiveGraph() g.parse(data=test_data, format="n3") i = 0 for s, p, o in g: if isinstance(s, Graph): i += 1 self.assertEquals(i, 3) self.assertEquals(len(list(g.contexts())), 13) g.close()
class TestBDBOptimized: non_core = True bsddb = True def setUp(self): self.graph = ConjunctiveGraph(store="BDBOptimized") self.path = mkdtemp() self.graph.open(self.path, create=True) def tearDown(self): self.graph.close()
def testModel(self): g = ConjunctiveGraph() g.parse(data=test_data, format="n3") i = 0 for s, p, o in g: if isinstance(s, Graph): i += 1 self.assertEquals(i, 3) self.assertEquals(len(list(g.contexts())), 13) g.close()
def rdf_description(name, notation='xml'): """ Funtion takes title of node, and rdf notation. """ valid_formats = ["xml", "n3", "ntriples", "trix"] default_graph_uri = "http://gstudio.gnowledge.org/rdfstore" configString = "/var/tmp/rdfstore" # Get the Sleepycat plugin. store = plugin.get('Sleepycat', Store)('rdfstore') # Open previously created store, or create it if it doesn't exist yet graph = Graph(store="Sleepycat", identifier=URIRef(default_graph_uri)) path = mkdtemp() rt = graph.open(path, create=False) if rt == NO_STORE: #There is no underlying Sleepycat infrastructure, create it graph.open(path, create=True) else: assert rt == VALID_STORE, "The underlying store is corrupt" # Now we'll add some triples to the graph & commit the changes rdflib = Namespace('http://sbox.gnowledge.org/gstudio/') graph.bind("gstudio", "http://gnowledge.org/") exclusion_fields = [ "id", "rght", "node_ptr_id", "image", "lft", "_state", "_altnames_cache", "_tags_cache", "nid_ptr_id", "_mptt_cached_fields" ] node = Objecttype.objects.get(title=name) node_dict = node.__dict__ subject = str(node_dict['id']) for key in node_dict: if key not in exclusion_fields: predicate = str(key) pobject = str(node_dict[predicate]) graph.add((rdflib[subject], rdflib[predicate], Literal(pobject))) graph.commit() print graph.serialize(format=notation) graph.close()
def rdf_description(name, notation='xml' ): """ Funtion takes title of node, and rdf notation. """ valid_formats = ["xml", "n3", "ntriples", "trix"] default_graph_uri = "http://gstudio.gnowledge.org/rdfstore" configString = "/var/tmp/rdfstore" # Get the Sleepycat plugin. store = plugin.get('Sleepycat', Store)('rdfstore') # Open previously created store, or create it if it doesn't exist yet graph = Graph(store="Sleepycat", identifier = URIRef(default_graph_uri)) path = mkdtemp() rt = graph.open(path, create=False) if rt == NO_STORE: #There is no underlying Sleepycat infrastructure, create it graph.open(path, create=True) else: assert rt == VALID_STORE, "The underlying store is corrupt" # Now we'll add some triples to the graph & commit the changes rdflib = Namespace('http://sbox.gnowledge.org/gstudio/') graph.bind("gstudio", "http://gnowledge.org/") exclusion_fields = ["id", "rght", "node_ptr_id", "image", "lft", "_state", "_altnames_cache", "_tags_cache", "nid_ptr_id", "_mptt_cached_fields"] node=Objecttype.objects.get(title=name) node_dict=node.__dict__ subject=str(node_dict['id']) for key in node_dict: if key not in exclusion_fields: predicate=str(key) pobject=str(node_dict[predicate]) graph.add((rdflib[subject], rdflib[predicate], Literal(pobject))) graph.commit() print graph.serialize(format=notation) graph.close()
class ContextTestCase(unittest.TestCase): #store = 'Memory' store = 'default' slow = True tmppath = None def setUp(self): 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)
def rdf_all(notation='xml'): """ Funtion takes title of node, and rdf notation. """ valid_formats = ["xml", "n3", "ntriples", "trix"] default_graph_uri = "http://gstudio.gnowledge.org/rdfstore" configString = "/var/tmp/rdfstore" # Get the IOMemory plugin. store = plugin.get('IOMemory', Store)('rdfstore') # Open previously created store, or create it if it doesn't exist yet graph = Graph(store="IOMemory", identifier = URIRef(default_graph_uri)) path = mkdtemp() rt = graph.open(path, create=False) if rt == NO_STORE: graph.open(path, create=True) else: assert rt == VALID_STORE, "The underlying store is corrupt" # Now we'll add some triples to the graph & commit the changes graph.bind("gstudio", "http://gnowledge.org/") exclusion_fields = ["id", "rght", "node_ptr_id", "image", "lft", "_state", "_altnames_cache", "_tags_cache", "nid_ptr_id", "_mptt_cached_fields"] for node in NID.objects.all(): node_dict=node.ref.__dict__ node_type = node.reftype try: if (node_type=='Gbobject'): node=Gbobject.objects.get(title=node) elif (node_type=='None'): node=Gbobject.objects.get(title=node) elif (node_type=='Processes'): node=Gbobject.objects.get(title=node) elif (node_type=='System'): node=Gbobject.objects.get(title=node) rdflib=link(node) url_addr=link1(node) a=fstore_dump(url_addr) elif (node_type=='Objecttype'): node=Objecttype.objects.get(title=node) elif (node_type=='Attributetype'): node=Attributetype.objects.get(title=node) elif (node_type=='Complement'): node=Complement.objects.get(title=node) elif (node_type=='Union'): node=Union.objects.get(title=node) elif (node_type=='Intersection'): node=Intersection.objects.get(title=node) elif (node_type=='Expression'): node=Expression.objects.get(title=node) elif (node_type=='Processtype'): node=Processtype.objects.get(title=node) elif (node_type=='Systemtype'): node=Systemtype.objects.get(title=node) elif (node_type=='AttributeSpecification'): node=AttributeSpecification.objects.get(title=node) elif (node_type=='RelationSpecification'): node=RelationSpecification.objects.get(title=node) rdflib=link(node) url_addr=link1(node) a=fstore_dump(url_addr) if(node_type=='Attribute'): node=Attribute.objects.get(title=node) rdflib = Namespace('http://sbox.gnowledge.org/gstudio/') elif(node_type=='Relationtype' ): node=Relationtype.objects.get(title=node) rdflib = Namespace('http://sbox.gnowledge.org/gstudio/') elif(node_type=='Metatype'): node=Metatype.objects.get(title=node) rdflib = Namespace('http://sbox.gnowledge.org/gstudio/') url_addr='http://sbox.gnowledge.org/gstudio/' a=fstore_dump(url_addr) except: if(node_type=='Attribute'): rdflib= Namespace('http://sbox.gnowledge.org/gstudio/') if(node_type=='Relationtype' ): rdflib= Namespace('http://sbox.gnowledge.org/gstudio/') if(node_type=='Metatype'): rdflib= Namespace('http://sbox.gnowledge.org/gstudio/') subject=str(node_dict['id']) for key in node_dict: if key not in exclusion_fields: predicate=str(key) pobject=str(node_dict[predicate]) graph.add((rdflib[subject], rdflib[predicate], Literal(pobject))) rdf_code=graph.serialize(format=notation) #path to store the rdf in a file #x = os.path.join(os.path.dirname(__file__), 'rdffiles.rdf') graph.commit() graph.close()
import rdflib from rdflib.graph import ConjunctiveGraph as Graph #from rdflib.graph import Graph configString = "/tmp/rdfstore12" graph = Graph(store="KyotoCabinet") print graph.store path = configString rt = graph.open(path, create=False) print "Triples in graph: ", len(graph) print "first 20 are as follows:" i=20 for subj, pred, obj in graph: print(subj, pred, obj) i-=1 if i==0: break graph.close()
def testFormulaStore(store="default", configString=None): try: g = ConjunctiveGraph(store=store) except ImportError: raise SkipTest("Dependencies for store '%s' not available!" % store) if configString: g.destroy(configString) g.open(configString) else: if store == 'SQLite': _, path = mkstemp(prefix='test', dir='/tmp', suffix='.sqlite') g.open(path, create=True) else: g.open(mkdtemp(), create=True) g.parse(data=testN3, format="n3") try: for s, p, o in g.triples((None, implies, None)): formulaA = s formulaB = o assert type(formulaA) == QuotedGraph and type(formulaB) == QuotedGraph # a = URIRef('http://test/a') b = URIRef('http://test/b') c = URIRef('http://test/c') d = URIRef('http://test/d') v = Variable('y') universe = ConjunctiveGraph(g.store) #test formula as terms assert len(list(universe.triples((formulaA, implies, formulaB)))) == 1 #test variable as term and variable roundtrip assert len(list(formulaB.triples((None, None, v)))) == 1 for s, p, o in formulaB.triples((None, d, None)): if o != c: assert isinstance(o, Variable) assert o == v s = list(universe.subjects(RDF.type, RDFS.Class))[0] assert isinstance(s, BNode) assert len(list(universe.triples((None, implies, None)))) == 1 assert len(list(universe.triples((None, RDF.type, None)))) == 1 assert len(list(formulaA.triples((None, RDF.type, None)))) == 1 assert len(list(formulaA.triples((None, None, None)))) == 2 assert len(list(formulaB.triples((None, None, None)))) == 2 assert len(list(universe.triples((None, None, None)))) == 3 assert len(list(formulaB.triples( (None, URIRef('http://test/d'), None)))) == 2 assert len(list(universe.triples( (None, URIRef('http://test/d'), None)))) == 1 # #context tests # #test contexts with triple argument # assert len(list(universe.contexts((a, d, c)))) == 1, \ # [ct for ct in universe.contexts((a, d, c))] # FAIL: test.test_graph_formula.testFormulaStores('SQLite',) # -------------------------------------------------------------------- # Traceback (most recent call last): # File ".../site-packages/nose/case.py", line 197, in runTest # self.test(*self.arg) # File ".../test_graph_formula.py", line 80, in testFormulaStore # [ct for ct in universe.contexts((a, d, c))] # AssertionError: [ # <Graph identifier=N52fd4417ef7641089b2e4045ef19ad87 # (<class 'rdflib.graph.Graph'>)>, # <Graph identifier=_:Formula16 (<class 'rdflib.graph.Graph'>)> # ] #Remove test cases universe.remove((None, implies, None)) assert len(list(universe.triples((None, implies, None)))) == 0 assert len(list(formulaA.triples((None, None, None)))) == 2 assert len(list(formulaB.triples((None, None, None)))) == 2 formulaA.remove((None, b, None)) assert len(list(formulaA.triples((None, None, None)))) == 1 formulaA.remove((None, RDF.type, None)) assert len(list(formulaA.triples((None, None, None)))) == 0 universe.remove((None, RDF.type, RDFS.Class)) #remove_context tests universe.remove_context(formulaB) assert len(list(universe.triples((None, RDF.type, None)))) == 0 assert len(universe) == 1 assert len(formulaB) == 0 universe.remove((None, None, None)) assert len(universe) == 0 g.close() if store == 'SQLite': os.unlink(path) else: g.store.destroy(configString) except: g.close() if store == 'SQLite': os.unlink(path) else: g.store.destroy(configString) raise
class ContextTest(test.TestCase): """ Testing different contexts. Heavily based on https://github.com/RDFLib/rdflib-postgresql/blob/master/test/context_case.py """ store_name = "Django" storetest = True path = "" 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) 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)) 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)) def addStuffInMultipleContexts(self): c1 = self.c1 c2 = self.c2 triple = (self.pizza, self.hates, self.tarek) # 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(graph), 2) self.assertEquals(len(self.graph), 2) 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 _ 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) 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) self.addStuffInMultipleContexts() def cid(c): if not isinstance(c, basestring): return c.identifier return c self.assertIn(self.c1, [cid(c) for c in self.graph.contexts()]) self.assertIn(self.c2, [cid(c) for c in self.graph.contexts()]) contextList = [cid(c) for c in self.graph.contexts(triple)] self.assertIn(self.c1, contextList) self.assertIn(self.c2, 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): triples = self.graph.triples graph = self.graph c1graph = Graph(self.graph.store, self.c1) c1triples = c1graph.triples Any = None self.addStuff() # unbound subjects with context self.assertEquals(len(list(c1triples((Any, self.likes, self.pizza)))), 2) self.assertEquals(len(list(c1triples((Any, self.hates, self.pizza)))), 1) self.assertEquals(len(list(c1triples((Any, self.likes, self.cheese)))), 3) self.assertEquals(len(list(c1triples((Any, self.hates, self.cheese)))), 0) # unbound subjects without context, same results! self.assertEquals(len(list(triples((Any, self.likes, self.pizza)))), 2) self.assertEquals(len(list(triples((Any, self.hates, self.pizza)))), 1) self.assertEquals(len(list(triples((Any, self.likes, self.cheese)))), 3) self.assertEquals(len(list(triples((Any, self.hates, self.cheese)))), 0) # unbound objects with context self.assertEquals(len(list(c1triples((self.michel, self.likes, Any)))), 2) self.assertEquals(len(list(c1triples((self.tarek, self.likes, Any)))), 2) self.assertEquals(len(list(c1triples((self.bob, self.hates, Any)))), 2) self.assertEquals(len(list(c1triples((self.bob, self.likes, Any)))), 1) # unbound objects without context, same results! self.assertEquals(len(list(triples((self.michel, self.likes, Any)))), 2) self.assertEquals(len(list(triples((self.tarek, self.likes, Any)))), 2) self.assertEquals(len(list(triples((self.bob, self.hates, Any)))), 2) self.assertEquals(len(list(triples((self.bob, self.likes, Any)))), 1) # unbound predicates with context self.assertEquals(len(list(c1triples((self.michel, Any, self.cheese)))), 1) self.assertEquals(len(list(c1triples((self.tarek, Any, self.cheese)))), 1) self.assertEquals(len(list(c1triples((self.bob, Any, self.pizza)))), 1) self.assertEquals(len(list(c1triples((self.bob, Any, self.michel)))), 1) # unbound predicates without context, same results! self.assertEquals(len(list(triples((self.michel, Any, self.cheese)))), 1) self.assertEquals(len(list(triples((self.tarek, Any, self.cheese)))), 1) self.assertEquals(len(list(triples((self.bob, Any, self.pizza)))), 1) self.assertEquals(len(list(triples((self.bob, Any, self.michel)))), 1) # unbound subject, objects with context self.assertEquals(len(list(c1triples((Any, self.hates, Any)))), 2) self.assertEquals(len(list(c1triples((Any, self.likes, Any)))), 5) # unbound subject, objects without context, same results! self.assertEquals(len(list(triples((Any, self.hates, Any)))), 2) self.assertEquals(len(list(triples((Any, self.likes, Any)))), 5) # unbound predicates, objects with context self.assertEquals(len(list(c1triples((self.michel, Any, Any)))), 2) self.assertEquals(len(list(c1triples((self.bob, Any, Any)))), 3) self.assertEquals(len(list(c1triples((self.tarek, Any, Any)))), 2) # unbound predicates, objects without context, same results! self.assertEquals(len(list(triples((self.michel, Any, Any)))), 2) self.assertEquals(len(list(triples((self.bob, Any, Any)))), 3) self.assertEquals(len(list(triples((self.tarek, Any, Any)))), 2) # unbound subjects, predicates with context self.assertEquals(len(list(c1triples((Any, Any, self.pizza)))), 3) self.assertEquals(len(list(c1triples((Any, Any, self.cheese)))), 3) self.assertEquals(len(list(c1triples((Any, Any, self.michel)))), 1) # unbound subjects, predicates without context, same results! self.assertEquals(len(list(triples((Any, Any, self.pizza)))), 3) self.assertEquals(len(list(triples((Any, Any, self.cheese)))), 3) self.assertEquals(len(list(triples((Any, Any, self.michel)))), 1) # all unbound with context self.assertEquals(len(list(c1triples((Any, Any, Any)))), 7) # all unbound without context, same result! self.assertEquals(len(list(triples((Any, Any, Any)))), 7) for c in [graph, self.get_context(self.c1)]: # unbound subjects self.assertEquals(set(c.subjects(self.likes, self.pizza)), {self.michel, self.tarek}) self.assertEquals(set(c.subjects(self.hates, self.pizza)), {self.bob}) self.assertEquals(set(c.subjects(self.likes, self.cheese)), {self.tarek, self.bob, self.michel}) self.assertEquals(set(c.subjects(self.hates, self.cheese)), set()) # unbound objects self.assertEquals(set(c.objects(self.michel, self.likes)), {self.cheese, self.pizza}) self.assertEquals(set(c.objects(self.tarek, self.likes)), {self.cheese, self.pizza}) self.assertEquals(set(c.objects(self.bob, self.hates)), {self.michel, self.pizza}) self.assertEquals(set(c.objects(self.bob, self.likes)), {self.cheese}) # unbound predicates self.assertEquals(set(c.predicates(self.michel, self.cheese)), {self.likes}) self.assertEquals(set(c.predicates(self.tarek, self.cheese)), {self.likes}) self.assertEquals(set(c.predicates(self.bob, self.pizza)), {self.hates}) self.assertEquals(set(c.predicates(self.bob, self.michel)), {self.hates}) self.assertEquals(set(c.subject_objects(self.hates)), {(self.bob, self.pizza), (self.bob, self.michel)}) self.assertEquals(set(c.subject_objects(self.likes)), {(self.tarek, self.cheese), (self.michel, self.cheese), (self.michel, self.pizza), (self.bob, self.cheese), (self.tarek, self.pizza)}) self.assertEquals(set(c.predicate_objects(self.michel)), {(self.likes, self.cheese), (self.likes, self.pizza)}) self.assertEquals(set(c.predicate_objects(self.bob)), {(self.likes, self.cheese), (self.hates, self.pizza), (self.hates, self.michel)}) self.assertEquals(set(c.predicate_objects(self.tarek)), {(self.likes, self.cheese), (self.likes, self.pizza)}) self.assertEquals(set(c.subject_predicates(self.pizza)), {(self.bob, self.hates), (self.tarek, self.likes), (self.michel, self.likes)}) self.assertEquals(set(c.subject_predicates(self.cheese)), {(self.bob, self.likes), (self.tarek, self.likes), (self.michel, self.likes)}) self.assertEquals(set(c.subject_predicates(self.michel)), {(self.bob, self.hates)}) self.assertEquals(set(c), {(self.bob, self.hates, self.michel), (self.bob, self.likes, self.cheese), (self.tarek, self.likes, self.pizza), (self.michel, self.likes, self.pizza), (self.michel, self.likes, self.cheese), (self.bob, self.hates, self.pizza), (self.tarek, self.likes, self.cheese)}) # remove stuff and make sure the graph is empty again self.removeStuff() self.assertEquals(len(list(c1triples((Any, Any, Any)))), 0) self.assertEquals(len(list(triples((Any, Any, Any)))), 0)
class ContextTestCase(unittest.TestCase): #store = 'Memory' store = 'default' slow = True def setUp(self): 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: path = a_tmp_dir = mkdtemp() self.graph.open(path, 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() 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 Project: def __init__(self, uri, storePath): if (uri[-1] != "/"): raise SemPipeException("A Module must be a directory and its URI must end with a /") self.n = URIRef(uri) self.g = ConjunctiveGraph('IOMemory') self.storePath = storePath if storePath and os.path.exists(storePath+"/store.nquads"): self.g.parse(storePath + "/store.nq", format='nquads') self.confGraph = self.g.get_context(URIRef("sempipe:confgraph")) #self.storePath = storePath ## Get the Sleepycat plugin. #self.store = plugin.get('Sleepycat', Store)('rdfstore') ## Open previously created store, or create it if it doesn't exist yet #self.g = ConjunctiveGraph(store="Sleepycat", # identifier = URIRef(self.default_graph_uri)) ##path = mkdtemp() #rt = self.g.open(self.storePath, create=False) #if rt == NO_STORE: # # There is no underlying Sleepycat infrastructure, create it # self.g.open(self.storePath, create=True) #else: # assert rt == VALID_STORE, "The underlying store is corrupt" else: #Aggregate graphs self.confGraph = self.g.get_context(URIRef("sempipe:confgraph")) self._loadconf() for graph in self.confGraph.objects(self.n, semp.dataGraph): self.loadData(graph) for updateList in self.confGraph.objects(self.n, semp.update): for updateInstruction in Collection(self.confGraph, updateList): self.updateGraph(str(updateInstruction)) self.commit() # Cache HostedSpaces self.hostedSpaces = [] res = self.confGraph.query(""" SELECT ?baseURI ?mapTo ?index ?htaccess { ?baseURI a semp:HostedSpace ; semp:mapTo ?mapTo ; semp:mapIndexTo ?index ; semp:mapHTAccessTo ?htaccess . } """, initNs={"semp": semp}) for s in res: self.hostedSpaces.append(HostedSpace._make(s)) def __str__(self): return str(self.n) def __repr__(self): return "{0}({1},{2})".format(self.__class__.__name__, repr(self.n), repr(self.storePath)) def _loadconf(self, uri=None): """Loads a graph and all config-graphs it references as configuration graphs @param uri: a URIRef, defaults to self.n+SempPipe.conffilename""" uri = uri or URIRef(self.n + conffilename) if self.g.get_context(uri): print("ConfGraph {} already in database".format(uri), file=sys.stderr) return print("Loading {} as config graph".format(uri), file=sys.stderr) newgraph = self.g.parse(uri, format="n3") self.confGraph += newgraph self.confGraph.add((uri, rdf.type, semp.ConfGraph)) imports = set(newgraph.objects(uri, semp.confGraph)) imports |= set(newgraph.objects(self.n, semp.confGraph)) imports = filter(lambda x: not self.g.get_context(x), imports) #Recursively load additional graphs for imp in imports: self._loadconf(imp) def loadData(self, url): """Loads a data graph""" return parse(self.g, url) def updateGraph(self, sparql): try: self.g.update(sparql) except: raise SemPipeException("Update instruction failed:\n{}".format(str(sparql))) def hostedSpace(self, resource, reverse=False): """Picks the best matching hostedSpace for the given resource. If reverse is set, resource is considered to be a path relative to the buildDir and the corresponding URIRef is returned.""" if reverse: hostedSpaces = filter(lambda s: resource.startswith(self.buildDir + s.mapTo), self.hostedSpaces) else: hostedSpaces = filter(lambda s: resource.startswith(s.baseURI), self.hostedSpaces) # Find the best match, which is the most specific one: try: return max(hostedSpaces, key=lambda s: len(s.baseURI)) except ValueError: raise SemPipeException("No hosted space found for {}".format(resource)) def contentLocation(self, base, ending): if str(base)[-1] == '/': index = self.hostedSpace(base).index return str(base) + index + ending else: return str(base) + ending @property def buildDir(self): return next(self.confGraph.objects(self.n, semp.buildDir)) def buildLocation(self, resource): """Determines the filename in the build directory corresponding to a URI.""" hs = self.hostedSpace(resource) return self.buildDir + hs.mapTo + resource[len(hs.baseURI):] def buildLocationToResource(self, buildLocation): """Determines the filename in the build directory corresponding to a URI.""" if not buildLocation.startswith(self.buildDir): raise SemPipeException("{} is not in buildDir".format(buildLocation)) hs = self.hostedSpace(buildLocation, reverse=True) return URIRef(hs.baseURI + buildLocation[len(self.buildDir + hs.mapTo):]) def copy(self, source, dest): """Publish a resource by copying a file Note that dest is the URI where the resource should be published, the corresponding directory in the build directory is derived automatically.""" dest = self.buildLocation(dest) print("copy {0} to {1}".format(source, dest), file=sys.stderr) directory = dest.rsplit("/",1)[0] directory = fileurl2path(directory) print(" Making shure directory {0} exists".format(directory), file=sys.stderr) os.makedirs(directory, mode=0o777, exist_ok=True) shutil.copy(fileurl2path(source), fileurl2path(dest)) print(" done", file=sys.stderr) def write(self, dest, data): """Publishes a file with contents data""" dest = self.buildLocation(dest) print("writing data to {0}".format(dest), file=sys.stderr) directory = dest.rsplit("/",1)[0] directory = fileurl2path(directory) print(" Making shure directory {0} exists".format(directory), file=sys.stderr) os.makedirs(directory, mode=0o777, exist_ok=True) with open(fileurl2path(dest), mode="wb") as f: f.write(data) print(" done", file=sys.stderr) def buildResource(self, resource): """Looks up the description of the resource and builds it Creates all representations of the resource and adds information to the .htaccess if required. semp:Reource type of a Resource semp:subject What the page is mainly about. This is used by semp:Render to know which one is the root node. semp:source points to a source file semp:representation A variant of the resource, obtainable by content nogtiation semp:content-type indicates the targetted content type semp:buildCommand tells how to build the representation. Use semp:Render to render with fresnel Lenses and an XSLT. Use semp:Raw to just take the surce file. semp:content-type used on a source file or representation to indicate the content type """ representations = self.confGraph.objects(resource, semp.representation) for r in representations: content_type = next(self.confGraph.objects(r, semp["content-type"])) try: source = next(self.confGraph.objects(r, semp.source)) except(StopIteration): source = None try: language = next(self.confGraph.objects(r, semp.language)) except(StopIteration): language = None try: quality = next(self.confGraph.objects(r, semp.quality)) except(StopIteration): quality = None contentLocation = URIRef(self.contentLocation(resource, self.defaultEnding(content_type, language))) if semp.Raw in self.confGraph.objects(r, semp.buildCommand): self.copy(source, contentLocation) elif semp.Render in self.confGraph.objects(r, semp.buildCommand): #fresnelGraph = Graph() #multiparse(fresnelGraph, self.confGraph.objects(r, semp.fresnelGraph)) #instanceGraph = Graph() #parse(instanceGraph, source) #multiparse(instanceGraph, self.confGraph.objects(r, semp.additionalData)) fresnelGraph = self.g instanceGraph = self.g ctx = Fresnel.Context(fresnelGraph=fresnelGraph, instanceGraph=instanceGraph) box = Fresnel.ContainerBox(ctx) box.append(resource) box.select() box.portray() tree = box.transform() #Fresnel.prettify(tree) # results in bad whitespace self.write(contentLocation, etree.tostring(tree,encoding="UTF-8",xml_declaration=True)) elif semp.Serialize in self.confGraph.objects(r, semp.buildCommand): graph = self.g.get_context(resource) self.write(contentLocation, graph.serialize()) else: raise SemPipeException("Failed to produce representation {0} of {1}".format(r, resource)) try: xslt_files = Collection(self.confGraph, next(self.confGraph.objects(r, semp.transformation))) buildloc = self.buildLocation(contentLocation) for xslt_file in xslt_files: command = ["xsltproc", "--output", fileurl2path(buildloc), fileurl2path(str(xslt_file)), fileurl2path(buildloc)] print("Running transformation", *command, file=sys.stderr) subprocess.call(command) except (StopIteration): pass #write typemap typemap = self.typemap(resource) if typemap is not None: self.write(resource, typemap) def typemap(self, resource): """ Returns the contents of a type-map file for all representations of the given resource. Returns None if no typemap is necessary. """ representations = sorted(self.confGraph.objects(resource, semp.representation)) typemap_url = lambda url: str(url).rsplit("/", 1)[-1] typemap = ["URI: {}\n\n".format(typemap_url(resource))] typemap_needed = False for r in representations: content_type = next(self.confGraph.objects(r, semp["content-type"])) try: source = next(self.confGraph.objects(r, semp.source)) except(StopIteration): source = None try: language = next(self.confGraph.objects(r, semp.language)) except(StopIteration): language = None try: quality = next(self.confGraph.objects(r, semp.quality)) except(StopIteration): quality = None contentLocation = URIRef(self.contentLocation(resource, self.defaultEnding(content_type, language))) typemap.append("URI: {}\n".format(typemap_url(contentLocation))) typemap.append("Content-type: {}".format(content_type)) if quality is not None: typemap[-1] += "; q={}\n".format(quality) typemap_needed = True else: typemap[-1] += "\n" if language is not None: typemap.append("Content-language: {}\n".format(language)) typemap.append("\n") if typemap_needed: return "".join(typemap).encode("UTF-8") else: return None def defaultEnding(self, content_type=None, language=None): cts = { "application/rdf+xml": ".rdf", "application/xhtml+xml": ".xhtml", "text/html": ".html", None: "" } if content_type: typeendings = list(self.confGraph.objects(URIRef("http://purl.org/NET/mediatypes/" + content_type), semp.defaultExtension)) if len(typeendings) > 1: raise SemPipeException("ambiguous extension for content-type {} in confGraph.".format(content_type)) elif len(typeendings) < 1: raise SemPipeException("No extension for content-type {} found".format(content_type)) else: typeending = typeendings[0] else: typeending = "" return ("." + language if language else "") + "." + typeending def write_htaccess(self): """Writes all required .htaccess files.""" # First generate the directives for each resource filesinfo = []; resources = self.resources for resource in resources: info = []; filesinfo.append((resource, info)); if self.typemap(resource) is not None: info.append("SetHandler type-map\n") # Generate the .htaccess files htaccessfiles = dict() for resource, info in filter(lambda x: x[1], filesinfo): directory, filename = resource.rsplit("/", 1) ht = htaccessfiles.setdefault(directory, []) ht.append('<Files "{}">\n'.format(filename)) ht += info ht.append('</Files>\n') for directory, ht in htaccessfiles.items(): print("Writing a .htaccess in {}".format(directory), file=sys.stderr) filename = self.hostedSpace(resource).htaccess self.write(directory + "/" + filename, "".join(ht).encode("UTF-8")) def publish(self): import getpass import subprocess """Walks through HostedSpaces and upload the respective files from the build diretory. (Instead we should walk through the build directory. Will be changed later.)""" hostedSpacesQuery = """ SELECT ?space ?method ?command ?invocation WHERE { ?space a semp:HostedSpace . ?space semp:publishMethod ?method . ?method semp:command ?command . ?method semp:invocation ?invocation . }""" askForQuery = """ SELECT ?variable WHERE { { ?method semp:askFor ?variable . } UNION { ?method semp:askForHidden ?variable . } }""" #?hidden #{ ?method semp:askFor ?variable . } #UNION #{ ?method semp:askForHidden ?variable . # BIND ("true"^^xsd:boolean as ?hidden) } for spaceRow in self.confGraph.query(hostedSpacesQuery, initNs={"semp": semp}).bindings: space = spaceRow[Variable("?space")] method = spaceRow[Variable("?method")] answers = dict() for question in self.confGraph.query(askForQuery, initNs={"semp": semp}, initBindings={"method": method}).bindings: answers[question[Variable("?variable")]] = getpass.getpass("{} for method {}".format(question[Variable("?variable")], method)) spacedir = self.buildLocation(space) command = [] for arg in Collection(self.confGraph, spaceRow[Variable("command")]): command.append(str(arg).format("",fileurl2path(spacedir),str(space),**answers)) print("Running {}".format(command[0]), file=sys.stderr) subprocess.call(command) @property def resources(self): return self.confGraph.subjects(rdf.type, semp.Resource) def commit(self): self.g.commit() if self.storePath: self.g.serialize(destination=self.storePath+"/store.nq", format='nquads', encoding='UTF-8') def serialize(self): return self.g.serialize() def close(self): self.g.close()
def testFormulaStore(store="default", configString=None): try: g = ConjunctiveGraph(store=store) except ImportError: raise SkipTest("Dependencies for store '%s' not available!"%store) if configString: g.destroy(configString) g.open(configString) else: g.open(mkdtemp(), create=True) g.parse(data=testN3, format="n3") print g.store try: for s,p,o in g.triples((None,implies,None)): formulaA = s formulaB = o assert type(formulaA)==QuotedGraph and type(formulaB)==QuotedGraph a = URIRef('http://test/a') b = URIRef('http://test/b') c = URIRef('http://test/c') d = URIRef('http://test/d') v = Variable('y') universe = ConjunctiveGraph(g.store) #test formula as terms assert len(list(universe.triples((formulaA,implies,formulaB))))==1 #test variable as term and variable roundtrip assert len(list(formulaB.triples((None,None,v))))==1 for s,p,o in formulaB.triples((None,d,None)): if o != c: assert isinstance(o,Variable) assert o == v s = list(universe.subjects(RDF.type, RDFS.Class))[0] assert isinstance(s,BNode) assert len(list(universe.triples((None,implies,None)))) == 1 assert len(list(universe.triples((None,RDF.type,None)))) ==1 assert len(list(formulaA.triples((None,RDF.type,None))))==1 assert len(list(formulaA.triples((None,None,None))))==2 assert len(list(formulaB.triples((None,None,None))))==2 assert len(list(universe.triples((None,None,None))))==3 assert len(list(formulaB.triples((None,URIRef('http://test/d'),None))))==2 assert len(list(universe.triples((None,URIRef('http://test/d'),None))))==1 #context tests #test contexts with triple argument assert len(list(universe.contexts((a,d,c))))==1 #Remove test cases universe.remove((None,implies,None)) assert len(list(universe.triples((None,implies,None))))==0 assert len(list(formulaA.triples((None,None,None))))==2 assert len(list(formulaB.triples((None,None,None))))==2 formulaA.remove((None,b,None)) assert len(list(formulaA.triples((None,None,None))))==1 formulaA.remove((None,RDF.type,None)) assert len(list(formulaA.triples((None,None,None))))==0 universe.remove((None,RDF.type,RDFS.Class)) #remove_context tests universe.remove_context(formulaB) assert len(list(universe.triples((None,RDF.type,None))))==0 assert len(universe)==1 assert len(formulaB)==0 universe.remove((None,None,None)) assert len(universe)==0 g.close() g.store.destroy(configString) except: g.close() g.store.destroy(configString) raise
def rdf_description(notation='xml'): """ Funtion takes title of node, and rdf notation. """ name = 'student' valid_formats = ["xml", "n3", "ntriples", "trix"] default_graph_uri = "http://gstudio.gnowledge.org/rdfstore" # default_graph_uri = "http://example.com/" configString = "/var/tmp/rdfstore" # Get the Sleepycat plugin. store = plugin.get('IOMemory', Store)('rdfstore') # Open previously created store, or create it if it doesn't exist yet graph = Graph(store="IOMemory", identifier=URIRef(default_graph_uri)) path = mkdtemp() rt = graph.open(path, create=False) if rt == NO_STORE: #There is no underlying Sleepycat infrastructure, create it graph.open(path, create=True) else: assert rt == VALID_STORE, "The underlying store is corrupt" # Now we'll add some triples to the graph & commit the changes # rdflib = Namespace('http://sbox.gnowledge.org/gstudio/') graph.bind("gstudio", "http://gnowledge.org/") exclusion_fields = [ "id", "rght", "node_ptr_id", "image", "lft", "_state", "_altnames_cache", "_tags_cache", "nid_ptr_id", "_mptt_cached_fields" ] node_type = get_nodetype() if (node_type == 'gbobject'): node = Gbobject.objects.get(title=name) elif (node_type == 'objecttype'): node = Objecttype.objects.get(title=name) elif (node_type == 'metatype'): node = Metatype.objects.get(title=name) elif (node_type == 'attributetype'): node = Attributetype.objects.get(title=name) elif (node_type == 'relationtype'): node = Relationtype.objects.get(title=name) elif (node_type == 'attribute'): node = Attribute.objects.get(title=name) elif (node_type == 'complement'): node = Complement.objects.get(title=name) elif (node_type == 'union'): node = Union.objects.get(title=name) elif (node_type == 'intersection'): node = Intersection.objects.get(title=name) elif (node_type == 'expression'): node = Expression.objects.get(title=name) elif (node_type == 'processtype'): node = Processtype.objects.get(title=name) elif (node_type == 'systemtype'): node = Systemtype.objects.get(title=name) node_url = node.get_absolute_url() site_add = node.sites.all() a = site_add[0] host_name = a.name #host_name=name link = 'http://' #Concatenating the above variables will give the url address. url_add = link + host_name + node_url rdflib = Namespace(url_add) # node=Objecttype.objects.get(title=name) node_dict = node.__dict__ subject = str(node_dict['id']) for key in node_dict: if key not in exclusion_fields: predicate = str(key) pobject = str(node_dict[predicate]) graph.add((rdflib[subject], rdflib[predicate], Literal(pobject))) rdf_code = graph.serialize(format=notation) #graph = rdflib.Graph("IOMemory") #graph.open("store", create=True) #graph.parse(rdf_code) # print out all the triples in the graph # for subject, predicate, object in graph: # print subject, predicate, object # store.add(self,(subject, predicate, object),context) graph.commit() print rdf_code graph.close()
def testFormulaStore(store="default", configString=None): g = ConjunctiveGraph(store=store) if configString: g.destroy(configString) g.open(configString) else: g.open(mkdtemp(), create=True) g.parse(data=testN3, format="n3") print g.store try: for s,p,o in g.triples((None,implies,None)): formulaA = s formulaB = o assert type(formulaA)==QuotedGraph and type(formulaB)==QuotedGraph a = URIRef('http://test/a') b = URIRef('http://test/b') c = URIRef('http://test/c') d = URIRef('http://test/d') v = Variable('y') universe = ConjunctiveGraph(g.store) #test formula as terms assert len(list(universe.triples((formulaA,implies,formulaB))))==1 #test variable as term and variable roundtrip assert len(list(formulaB.triples((None,None,v))))==1 for s,p,o in formulaB.triples((None,d,None)): if o != c: assert isinstance(o,Variable) assert o == v s = list(universe.subjects(RDF.type, RDFS.Class))[0] assert isinstance(s,BNode) assert len(list(universe.triples((None,implies,None)))) == 1 assert len(list(universe.triples((None,RDF.type,None)))) ==1 assert len(list(formulaA.triples((None,RDF.type,None))))==1 assert len(list(formulaA.triples((None,None,None))))==2 assert len(list(formulaB.triples((None,None,None))))==2 assert len(list(universe.triples((None,None,None))))==3 assert len(list(formulaB.triples((None,URIRef('http://test/d'),None))))==2 assert len(list(universe.triples((None,URIRef('http://test/d'),None))))==1 #context tests #test contexts with triple argument assert len(list(universe.contexts((a,d,c))))==1 #Remove test cases universe.remove((None,implies,None)) assert len(list(universe.triples((None,implies,None))))==0 assert len(list(formulaA.triples((None,None,None))))==2 assert len(list(formulaB.triples((None,None,None))))==2 formulaA.remove((None,b,None)) assert len(list(formulaA.triples((None,None,None))))==1 formulaA.remove((None,RDF.type,None)) assert len(list(formulaA.triples((None,None,None))))==0 universe.remove((None,RDF.type,RDFS.Class)) #remove_context tests universe.remove_context(formulaB) assert len(list(universe.triples((None,RDF.type,None))))==0 assert len(universe)==1 assert len(formulaB)==0 universe.remove((None,None,None)) assert len(universe)==0 g.close() g.store.destroy(configString) except: g.close() g.store.destroy(configString) raise
#!/usr/bin/env python from rdflib.graph import ConjunctiveGraph # Test basic SPARQL aggregation of the RDFa data # see also http://identi.ca/notice/17728953 http://identi.ca/notice/17729227 g = ConjunctiveGraph("Sleepycat") g.open("store", create=True) g.parse("http://inkdroid.org/journal/network", format='rdfa', lax=True) #g.parse("http://danbri.org/words/network", format='rdfa', lax=True) g.parse("http://danbri.org/words/2009/12/29/523", format='rdfa', lax=True) g.parse("http://melvincarvalho.com/blog/installed-f2f-plugin/", format='rdfa', lax=True) q1="""PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?src1 ?src2 ?x WHERE { GRAPH ?src1 { ?gr1 foaf:member [ foaf:openid ?x ] } GRAPH ?src2 { ?gr2 foaf:member [ foaf:openid ?x ] } FILTER ( ?src1 != ?src2 ) }""" for src1, src2, x in g.query(q1): print src1, src2, x g.close()
#!/usr/bin/env python # queries an RDF quadstore from rdflib.graph import ConjunctiveGraph g = ConjunctiveGraph("Sleepycat") g.open("store", create=True) q1 = """PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?src1 ?src2 ?x WHERE { GRAPH ?src1 { ?gr1 foaf:member [ foaf:openid ?x ] } GRAPH ?src2 { ?gr2 foaf:member [ foaf:openid ?x ] } FILTER ( ?src1 != ?src2 ) }""" for src1, src2, x in g.query(q1): print src1, src2, x g.close()
class TestKyotoCabinetConjunctiveGraphCore(unittest.TestCase): def setUp(self): store = "KyotoCabinet" self.graph = ConjunctiveGraph(store=store) self.path = configString self.graph.open(self.path, create=True) def tearDown(self): self.graph.destroy(self.path) try: self.graph.close() except: pass if getattr(self, "path", False) 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 test_namespaces(self): self.graph.bind("dc", "http://http://purl.org/dc/elements/1.1/") self.graph.bind("foaf", "http://xmlns.com/foaf/0.1/") self.assert_(len(list(self.graph.namespaces())) == 5) self.assert_(("foaf", rdflib.term.URIRef(u"http://xmlns.com/foaf/0.1/")) in list(self.graph.namespaces())) def test_play_journal(self): self.assertRaises(NotImplementedError, self.graph.store.play_journal, {"graph": self.graph}) def test_readable_index(self): print(readable_index(111)) def test_triples_context_reset(self): michel = rdflib.URIRef(u"michel") likes = rdflib.URIRef(u"likes") pizza = rdflib.URIRef(u"pizza") cheese = rdflib.URIRef(u"cheese") self.graph.add((michel, likes, pizza)) self.graph.add((michel, likes, cheese)) self.graph.commit() ntriples = self.graph.triples((None, None, None), context=self.graph.store) self.assert_(len(list(ntriples)) == 2) def test_remove_context_reset(self): michel = rdflib.URIRef(u"michel") likes = rdflib.URIRef(u"likes") pizza = rdflib.URIRef(u"pizza") cheese = rdflib.URIRef(u"cheese") self.graph.add((michel, likes, pizza)) self.graph.add((michel, likes, cheese)) self.graph.commit() self.graph.store.remove((michel, likes, cheese), self.graph.store) self.graph.commit() self.assert_(len(list(self.graph.triples((None, None, None), context=self.graph.store))) == 1) def test_remove_db_exception(self): michel = rdflib.URIRef(u"michel") likes = rdflib.URIRef(u"likes") pizza = rdflib.URIRef(u"pizza") cheese = rdflib.URIRef(u"cheese") self.graph.add((michel, likes, pizza)) self.graph.add((michel, likes, cheese)) self.graph.commit() self.graph.store.__len__(context=self.graph.store) self.assert_(len(list(self.graph.triples((None, None, None), context=self.graph.store))) == 2)
def rdf_description(name, notation='xml'): """ Funtion takes title of node, and rdf notation. """ valid_formats = ["xml", "n3", "ntriples", "trix"] default_graph_uri = "http://gstudio.gnowledge.org/rdfstore" # default_graph_uri = "http://example.com/" configString = "/var/tmp/rdfstore" # Get the IOMemory plugin. store = plugin.get('IOMemory', Store)('rdfstore') # Open previously created store, or create it if it doesn't exist yet graph = Graph(store="IOMemory", identifier=URIRef(default_graph_uri)) path = mkdtemp() rt = graph.open(path, create=False) if rt == NO_STORE: graph.open(path, create=True) else: assert rt == VALID_STORE, "The underlying store is corrupt" # Now we'll add some triples to the graph & commit the changes #rdflib = Namespace('http://sbox.gnowledge.org/gstudio/') graph.bind("gstudio", "http://gnowledge.org/") exclusion_fields = [ "id", "rght", "node_ptr_id", "image", "lft", "_state", "_altnames_cache", "_tags_cache", "nid_ptr_id", "_mptt_cached_fields" ] #verifies the type of node node = NID.objects.get(title=name) node_type = node.reftype if (node_type == 'Gbobject'): node = Gbobject.objects.get(title=name) rdflib = link(node) elif (node_type == 'None'): node = Gbobject.objects.get(title=name) rdflib = link(node) elif (node_type == 'Processes'): node = Gbobject.objects.get(title=name) rdflib = link(node) elif (node_type == 'System'): node = Gbobject.objects.get(title=name) rdflib = link(node) elif (node_type == 'Objecttype'): node = Objecttype.objects.get(title=name) rdflib = link(node) elif (node_type == 'Attributetype'): node = Attributetype.objects.get(title=name) rdflib = link(node) elif (node_type == 'Complement'): node = Complement.objects.get(title=name) rdflib = link(node) elif (node_type == 'Union'): node = Union.objects.get(title=name) rdflib = link(node) elif (node_type == 'Intersection'): node = Intersection.objects.get(title=name) rdflib = link(node) elif (node_type == 'Expression'): node = Expression.objects.get(title=name) rdflib = link(node) elif (node_type == 'Processtype'): node = Processtype.objects.get(title=name) rdflib = link(node) elif (node_type == 'Systemtype'): node = Systemtype.objects.get(title=name) rdflib = link(node) elif (node_type == 'AttributeSpecification'): node = AttributeSpecification.objects.get(title=name) rdflib = link(node) elif (node_type == 'RelationSpecification'): node = RelationSpecification.objects.get(title=name) rdflib = link(node) elif (node_type == 'Attribute'): node = Attribute.objects.get(title=name) rdflib = Namespace('http://sbox.gnowledge.org/gstudio/') elif (node_type == 'Relationtype'): node = Relationtype.objects.get(title=name) rdflib = Namespace('http://sbox.gnowledge.org/gstudio/') elif (node_type == 'Metatype'): node = Metatype.objects.get(title=name) rdflib = Namespace('http://sbox.gnowledge.org/gstudio/') else: rdflib = Namespace('http://sbox.gnowledge.org/gstudio/') node_dict = node.__dict__ subject = str(node_dict['id']) for key in node_dict: if key not in exclusion_fields: predicate = str(key) pobject = str(node_dict[predicate]) graph.add((rdflib[subject], rdflib[predicate], Literal(pobject))) rdf_code = graph.serialize(format=notation) graph.commit() print rdf_code graph.close()
class TestLevelDBConjunctiveGraphCore(unittest.TestCase): def setUp(self): store = "LevelDB" self.graph = ConjunctiveGraph(store=store) self.path = configString self.graph.open(self.path, create=True) def tearDown(self): self.graph.destroy(self.path) try: self.graph.close() except: pass if getattr(self, 'path', False) 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 test_namespaces(self): self.graph.bind("dc", "http://http://purl.org/dc/elements/1.1/") self.graph.bind("foaf", "http://xmlns.com/foaf/0.1/") self.assert_(len(list(self.graph.namespaces())) == 5) self.assert_(('foaf', rdflib.term.URIRef(u'http://xmlns.com/foaf/0.1/') ) in list(self.graph.namespaces())) def test_readable_index(self): print(readable_index(111)) def test_triples_context_reset(self): michel = rdflib.URIRef(u'michel') likes = rdflib.URIRef(u'likes') pizza = rdflib.URIRef(u'pizza') cheese = rdflib.URIRef(u'cheese') self.graph.add((michel, likes, pizza)) self.graph.add((michel, likes, cheese)) self.graph.commit() ntriples = self.graph.triples((None, None, None), context=self.graph.store) self.assert_(len(list(ntriples)) == 2) def test_remove_context_reset(self): michel = rdflib.URIRef(u'michel') likes = rdflib.URIRef(u'likes') pizza = rdflib.URIRef(u'pizza') cheese = rdflib.URIRef(u'cheese') self.graph.add((michel, likes, pizza)) self.graph.add((michel, likes, cheese)) self.graph.commit() self.graph.store.remove((michel, likes, cheese), self.graph.store) self.graph.commit() self.assert_( len( list( self.graph.triples((None, None, None), context=self.graph.store))) == 1) def test_remove_db_exception(self): michel = rdflib.URIRef(u'michel') likes = rdflib.URIRef(u'likes') pizza = rdflib.URIRef(u'pizza') cheese = rdflib.URIRef(u'cheese') self.graph.add((michel, likes, pizza)) self.graph.add((michel, likes, cheese)) self.graph.commit() self.graph.store.__len__(context=self.graph.store) self.assert_( len( list( self.graph.triples((None, None, None), context=self.graph.store))) == 2)
class ContextTest(TestCase): """ Testing different contexts. Heavily based on https://github.com/RDFLib/rdflib-postgresql/blob/master/test/context_case.py """ # noqa: E501 store_name = "Django" storetest = True path = "" 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) self.graph.close() def get_context(self, identifier): assert isinstance(identifier, URIRef) or isinstance(identifier, BNode), type(identifier) # noqa: E501 return Graph(store=self.graph.store, identifier=identifier, namespace_manager=self) # noqa: E501 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)) 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)) def addStuffInMultipleContexts(self): c1 = self.c1 c2 = self.c2 triple = (self.pizza, self.hates, self.tarek) # 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(graph), 2) self.assertEquals(len(self.graph), 2) 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 _ 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) 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) self.addStuffInMultipleContexts() def cid(c): if not isinstance(c, str): return c.identifier return c self.assertIn(self.c1, [cid(c) for c in self.graph.contexts()]) self.assertIn(self.c2, [cid(c) for c in self.graph.contexts()]) contextList = [cid(c) for c in self.graph.contexts(triple)] self.assertIn(self.c1, contextList) self.assertIn(self.c2, 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): triples = self.graph.triples graph = self.graph c1graph = Graph(self.graph.store, self.c1) c1triples = c1graph.triples Any = None self.addStuff() # unbound subjects with context self.assertEquals( len( list( c1triples((Any, self.likes, self.pizza)) ) ), 2 ) self.assertEquals( len( list( c1triples((Any, self.hates, self.pizza)) ) ), 1 ) self.assertEquals( len( list( c1triples((Any, self.likes, self.cheese)) ) ), 3 ) self.assertEquals( len( list( c1triples((Any, self.hates, self.cheese)) ) ), 0 ) # unbound subjects without context, same results! self.assertEquals(len(list(triples((Any, self.likes, self.pizza)))), 2) self.assertEquals(len(list(triples((Any, self.hates, self.pizza)))), 1) self.assertEquals(len(list(triples((Any, self.likes, self.cheese)))), 3) self.assertEquals(len(list(triples((Any, self.hates, self.cheese)))), 0) # unbound objects with context self.assertEquals(len(list(c1triples((self.michel, self.likes, Any)))), 2) self.assertEquals(len(list(c1triples((self.tarek, self.likes, Any)))), 2) self.assertEquals(len(list(c1triples((self.bob, self.hates, Any)))), 2) self.assertEquals(len(list(c1triples((self.bob, self.likes, Any)))), 1) # unbound objects without context, same results! self.assertEquals(len(list(triples((self.michel, self.likes, Any)))), 2) self.assertEquals(len(list(triples((self.tarek, self.likes, Any)))), 2) self.assertEquals(len(list(triples((self.bob, self.hates, Any)))), 2) self.assertEquals(len(list(triples((self.bob, self.likes, Any)))), 1) # unbound predicates with context self.assertEquals(len(list(c1triples((self.michel, Any, self.cheese)))), 1) self.assertEquals(len(list(c1triples((self.tarek, Any, self.cheese)))), 1) self.assertEquals(len(list(c1triples((self.bob, Any, self.pizza)))), 1) self.assertEquals(len(list(c1triples((self.bob, Any, self.michel)))), 1) # unbound predicates without context, same results! self.assertEquals(len(list(triples((self.michel, Any, self.cheese)))), 1) self.assertEquals(len(list(triples((self.tarek, Any, self.cheese)))), 1) self.assertEquals(len(list(triples((self.bob, Any, self.pizza)))), 1) self.assertEquals(len(list(triples((self.bob, Any, self.michel)))), 1) # unbound subject, objects with context self.assertEquals(len(list(c1triples((Any, self.hates, Any)))), 2) self.assertEquals(len(list(c1triples((Any, self.likes, Any)))), 5) # unbound subject, objects without context, same results! self.assertEquals(len(list(triples((Any, self.hates, Any)))), 2) self.assertEquals(len(list(triples((Any, self.likes, Any)))), 5) # unbound predicates, objects with context self.assertEquals(len(list(c1triples((self.michel, Any, Any)))), 2) self.assertEquals(len(list(c1triples((self.bob, Any, Any)))), 3) self.assertEquals(len(list(c1triples((self.tarek, Any, Any)))), 2) # unbound predicates, objects without context, same results! self.assertEquals(len(list(triples((self.michel, Any, Any)))), 2) self.assertEquals(len(list(triples((self.bob, Any, Any)))), 3) self.assertEquals(len(list(triples((self.tarek, Any, Any)))), 2) # unbound subjects, predicates with context self.assertEquals(len(list(c1triples((Any, Any, self.pizza)))), 3) self.assertEquals(len(list(c1triples((Any, Any, self.cheese)))), 3) self.assertEquals(len(list(c1triples((Any, Any, self.michel)))), 1) # unbound subjects, predicates without context, same results! self.assertEquals(len(list(triples((Any, Any, self.pizza)))), 3) self.assertEquals(len(list(triples((Any, Any, self.cheese)))), 3) self.assertEquals(len(list(triples((Any, Any, self.michel)))), 1) # all unbound with context self.assertEquals(len(list(c1triples((Any, Any, Any)))), 7) # all unbound without context, same result! self.assertEquals(len(list(triples((Any, Any, Any)))), 7) for c in [graph, self.get_context(self.c1)]: # unbound subjects self.assertEquals(set(c.subjects(self.likes, self.pizza)), {self.michel, self.tarek}) self.assertEquals(set(c.subjects(self.hates, self.pizza)), {self.bob}) self.assertEquals(set(c.subjects(self.likes, self.cheese)), {self.tarek, self.bob, self.michel}) self.assertEquals(set(c.subjects(self.hates, self.cheese)), set()) # unbound objects self.assertEquals(set(c.objects(self.michel, self.likes)), {self.cheese, self.pizza}) self.assertEquals(set(c.objects(self.tarek, self.likes)), {self.cheese, self.pizza}) self.assertEquals(set(c.objects(self.bob, self.hates)), {self.michel, self.pizza}) self.assertEquals(set(c.objects(self.bob, self.likes)), {self.cheese}) # unbound predicates self.assertEquals( set( c.predicates(self.michel, self.cheese) ), {self.likes} ) self.assertEquals( set( c.predicates(self.tarek, self.cheese) ), {self.likes} ) self.assertEquals(set(c.predicates(self.bob, self.pizza)), {self.hates}) self.assertEquals(set(c.predicates(self.bob, self.michel)), {self.hates}) self.assertEquals(set(c.subject_objects(self.hates)), {(self.bob, self.pizza), (self.bob, self.michel)}) self.assertEquals(set(c.subject_objects(self.likes)), {(self.tarek, self.cheese), (self.michel, self.cheese), (self.michel, self.pizza), (self.bob, self.cheese), (self.tarek, self.pizza)}) self.assertEquals(set(c.predicate_objects(self.michel)), {(self.likes, self.cheese), (self.likes, self.pizza)}) self.assertEquals(set(c.predicate_objects(self.bob)), {(self.likes, self.cheese), (self.hates, self.pizza), (self.hates, self.michel)}) self.assertEquals(set(c.predicate_objects(self.tarek)), {(self.likes, self.cheese), (self.likes, self.pizza)}) self.assertEquals(set(c.subject_predicates(self.pizza)), {(self.bob, self.hates), (self.tarek, self.likes), (self.michel, self.likes)}) self.assertEquals(set(c.subject_predicates(self.cheese)), {(self.bob, self.likes), (self.tarek, self.likes), (self.michel, self.likes)}) self.assertEquals(set(c.subject_predicates(self.michel)), {(self.bob, self.hates)}) self.assertEquals(set(c), {(self.bob, self.hates, self.michel), (self.bob, self.likes, self.cheese), (self.tarek, self.likes, self.pizza), (self.michel, self.likes, self.pizza), (self.michel, self.likes, self.cheese), (self.bob, self.hates, self.pizza), (self.tarek, self.likes, self.cheese)}) # remove stuff and make sure the graph is empty again self.removeStuff() self.assertEquals(len(list(c1triples((Any, Any, Any)))), 0) self.assertEquals(len(list(triples((Any, Any, Any)))), 0)
def rdf_description(notation='xml' ): """ Funtion takes title of node, and rdf notation. """ name='student' valid_formats = ["xml", "n3", "ntriples", "trix"] default_graph_uri = "http://gstudio.gnowledge.org/rdfstore" # default_graph_uri = "http://example.com/" configString = "/var/tmp/rdfstore" # Get the Sleepycat plugin. store = plugin.get('IOMemory', Store)('rdfstore') # Open previously created store, or create it if it doesn't exist yet graph = Graph(store="IOMemory", identifier = URIRef(default_graph_uri)) path = mkdtemp() rt = graph.open(path, create=False) if rt == NO_STORE: #There is no underlying Sleepycat infrastructure, create it graph.open(path, create=True) else: assert rt == VALID_STORE, "The underlying store is corrupt" # Now we'll add some triples to the graph & commit the changes # rdflib = Namespace('http://sbox.gnowledge.org/gstudio/') graph.bind("gstudio", "http://gnowledge.org/") exclusion_fields = ["id", "rght", "node_ptr_id", "image", "lft", "_state", "_altnames_cache", "_tags_cache", "nid_ptr_id", "_mptt_cached_fields"] node_type=get_nodetype() if (node_type=='gbobject'): node=Gbobject.objects.get(title=name) elif (node_type=='objecttype'): node=Objecttype.objects.get(title=name) elif (node_type=='metatype'): node=Metatype.objects.get(title=name) elif (node_type=='attributetype'): node=Attributetype.objects.get(title=name) elif (node_type=='relationtype'): node=Relationtype.objects.get(title=name) elif (node_type=='attribute'): node=Attribute.objects.get(title=name) elif (node_type=='complement'): node=Complement.objects.get(title=name) elif (node_type=='union'): node=Union.objects.get(title=name) elif (node_type=='intersection'): node=Intersection.objects.get(title=name) elif (node_type=='expression'): node=Expression.objects.get(title=name) elif (node_type=='processtype'): node=Processtype.objects.get(title=name) elif (node_type=='systemtype'): node=Systemtype.objects.get(title=name) node_url=node.get_absolute_url() site_add= node.sites.all() a = site_add[0] host_name =a.name #host_name=name link='http://' #Concatenating the above variables will give the url address. url_add=link+host_name+node_url rdflib = Namespace(url_add) # node=Objecttype.objects.get(title=name) node_dict=node.__dict__ subject=str(node_dict['id']) for key in node_dict: if key not in exclusion_fields: predicate=str(key) pobject=str(node_dict[predicate]) graph.add((rdflib[subject], rdflib[predicate], Literal(pobject))) rdf_code=graph.serialize(format=notation) #graph = rdflib.Graph("IOMemory") #graph.open("store", create=True) #graph.parse(rdf_code) # print out all the triples in the graph # for subject, predicate, object in graph: # print subject, predicate, object # store.add(self,(subject, predicate, object),context) graph.commit() print rdf_code graph.close()