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(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 testN3Store(store="default", configString=None): storetest = True g = ConjunctiveGraph(store=store) if configString is not None: g.destroy(configString) g.open(configString, create=True) g.parse(data=testN3, format="n3") # op = g.serialize(format="n3") # print(op) formulaA = BNode() formulaB = BNode() try: for s,p,o in g.triples((None,implies,None)): formulaA = s formulaB = o assert type(formulaA)==QuotedGraph and type(formulaB)==QuotedGraph a = URIRef('http://test/a') b = URIRef('http://test/b') c = URIRef('http://test/c') d = URIRef('http://test/d') v = Variable('y') universe = ConjunctiveGraph(g.store) #test formula as terms assert len(list(universe.triples((formulaA,implies,formulaB))))==1 #test variable as term and variable roundtrip assert len(list(formulaB.triples((None,None,v))))==1 for s,p,o in formulaB.triples((None,d,None)): if o != c: assert isinstance(o,Variable) assert o == v s = list(universe.subjects(RDF.type, RDFS.Class))[0] assert isinstance(s,BNode) assert len(list(universe.triples((None,implies,None)))) == 1 assert len(list(universe.triples((None,RDF.type,None)))) ==1 assert len(list(formulaA.triples((None,RDF.type,None))))==1 assert len(list(formulaA.triples((None,None,None))))==2 assert len(list(formulaB.triples((None,None,None))))==2 assert len(list(universe.triples((None,None,None))))==3 assert len(list(formulaB.triples((None,URIRef('http://test/d'),None))))==2 assert len(list(universe.triples((None,URIRef('http://test/d'),None))))==1 #context tests #test contexts with triple argument assert len(list(universe.contexts((a,d,c))))==1 #Remove test cases universe.remove((None,implies,None)) assert len(list(universe.triples((None,implies,None))))==0 assert len(list(formulaA.triples((None,None,None))))==2 assert len(list(formulaB.triples((None,None,None))))==2 formulaA.remove((None,b,None)) assert len(list(formulaA.triples((None,None,None))))==1 formulaA.remove((None,RDF.type,None)) assert len(list(formulaA.triples((None,None,None))))==0 universe.remove((None,RDF.type,RDFS.Class)) #remove_context tests universe.remove_context(formulaB) assert len(list(universe.triples((None,RDF.type,None))))==0 assert len(universe)==1 assert len(formulaB)==0 universe.remove((None,None,None)) assert len(universe)==0 g.store.destroy(configString) except: g.store.destroy(configString) raise
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)
def test_n3_store(self): # Thorough test suite for formula-aware store implies = URIRef("http://www.w3.org/2000/10/swap/log#implies") testN3 = """ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix : <http://test/> . {:a :b :c;a :foo} => {:a :d :c,?y}. _:foo a rdfs:Class. :a :d :c.""" g = self.open_graph() g.parse(data=testN3, format="n3") formulaA = BNode() formulaB = BNode() for s,o in g.subject_objects(predicate=implies): 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(formulaB.triples((None,d,None)))) == 2 assert len(list(universe.triples((None,None,None)))) == 3 assert len(list(universe.triples((None,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
def testN3Store(store="default", configString=None): storetest = True del storetest g = ConjunctiveGraph(store=store) if configString: # g.destroy(configString) g.open(configString, create=True) g.parse(data=testN3, format="n3") # op = g.serialize(format="n3") # print(op) formulaA = BNode() formulaB = BNode() try: for s, p, o in g.triples((None, implies, None)): formulaA = s formulaB = o assert type(formulaA) == QuotedGraph \ and type(formulaB) == QuotedGraph a = URIRef('http://test/a') b = URIRef('http://test/b') c = URIRef('http://test/c') d = URIRef('http://test/d') v = Variable('y') universe = ConjunctiveGraph(g.store) #test formula as terms assert len(list(universe.triples((formulaA, implies, formulaB)))) == 1 #test variable as term and variable roundtrip assert len(list(formulaB.triples((None, None, v)))) == 1 for s, p, o in formulaB.triples((None, d, None)): if o != c: assert isinstance(o, Variable) assert o == v s = list(universe.subjects(RDF.type, RDFS.Class))[0] assert isinstance(s, BNode) assert len(list(universe.triples((None, implies, None)))) == 1 assert len(list(universe.triples((None, RDF.type, None)))) == 1 assert len(list(formulaA.triples((None, RDF.type, None)))) == 1 assert len(list(formulaA.triples((None, None, None)))) == 2 assert len(list(formulaB.triples((None, None, None)))) == 2 assert len(list(universe.triples((None, None, None)))) == 3 assert len( list(formulaB.triples((None, URIRef('http://test/d'), None)))) == 2 assert len( list(universe.triples((None, URIRef('http://test/d'), None)))) == 1 #context tests #test contexts with triple argument assert len(list(universe.contexts((a, d, c)))) == 1 #Remove test cases universe.remove((None, implies, None)) assert len(list(universe.triples((None, implies, None)))) == 0 assert len(list(formulaA.triples((None, None, None)))) == 2 assert len(list(formulaB.triples((None, None, None)))) == 2 formulaA.remove((None, b, None)) assert len(list(formulaA.triples((None, None, None)))) == 1 formulaA.remove((None, RDF.type, None)) assert len(list(formulaA.triples((None, None, None)))) == 0 universe.remove((None, RDF.type, RDFS.Class)) #remove_context tests universe.remove_context(formulaB) assert len(list(universe.triples((None, RDF.type, None)))) == 0 assert len(universe) == 1 assert len(formulaB) == 0 universe.remove((None, None, None)) assert len(universe) == 0 g.store.destroy(configString) except: g.store.destroy(configString) raise
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)
class InMemoryStorage(object): def __init__(self): store = IOMemory() self.g = ConjunctiveGraph(store=store) self.g.bind("lada",ns_lada) self.g.bind('data', ns_data) self.g.bind('cube', ns_cube) self.g.bind('qb', ns_cube) self.g.bind('lcd', ns_lcd) self.g.bind('xsd', ns_xsd) self.g.bind('qb4cc', ns_qb4cc) self.g.bind('skos', ns_skos) self.initNs = { 'lada': ns_lada, 'data': ns_data, 'qb': ns_cube, 'lcd': ns_lcd, 'xsd': ns_xsd, 'qb4cc': ns_qb4cc, 'skos': ns_skos } def _concatenate_graphs(self, graphs): source = Graph() for g in graphs: if g in graph_dict: source += self.g.get_context(graph_dict[g]) elif type(g) is URIRef: source += self.g.get_context(g) return source def add_triple(self, triple, context): if context: if type(context) is str: self.g.get_context(graph_dict[context]).add(triple) else: self.g.get_context(context).add(triple) else: self.g.add(triple) def add_graph(self, graph, context): if context: g = None if type(context) is str: g = self.g.get_context(graph_dict[context]) else: g = self.g.get_context(context) g += graph else: self.g += graph def add_file(self, file, format, context): if context: if type(context) is str: self.g.get_context(graph_dict[context]).parse(file, format=format) else: self.g.get_context(context).parse(file, format=format) else: self.g.parse(file, format=format) def query(self, queryString, contexts): if contexts: if type(contexts) is list: return self._concatenate_graphs(contexts).query(queryString, initNs=self.initNs) elif type(contexts) is str: return self.g.get_context(graph_dict[contexts]).query(queryString, initNs=self.initNs) else: return self.g.get_context(contexts).query(queryString, initNs=self.initNs) else: return self.g.query(queryString, initNs=self.initNs) def value(self, subject, predicate, context): if context: if type(context) is str: return self.g.get_context(graph_dict[context]).value(subject, predicate) else: return self.g.get_context(context).value(subject, predicate) else: return self.g.value(subject, predicate) def remove(self, triple_pattern, contexts): if contexts: if type(contexts) is list: self._concatenate_graphs(contexts).remove(triple_pattern) else: self.g.get_context(graph_dict[contexts]).remove(triple_pattern) else: self.g.remove(triple_pattern) def clear(self, context): if context: if type(context) is str: self.g.remove_context(self.g.get_context(graph_dict[context])) else: self.g.remove_context(self.g.get_context(context)) else: self.g.remove( (None, None, None) ) def count_triples(self): c = 0; for s, p, o in self.g: c = c +1; return c def export(self, context): if type(context) is str: self.g.get_context(graph_dict[context]).serialize(context + ".ttl", format="turtle")
def test_rdflib_mysql_test(self): """ test taken from rdflib/test/test_mysql.py """ implies = URIRef("http://www.w3.org/2000/10/swap/log#implies") testN3=""" @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix : <http://test/> . {:a :b :c;a :foo} => {:a :d :c,?y}. _:foo a rdfs:Class. :a :d :c.""" #Thorough test suite for formula-aware store g = self.rdflib_graph g.parse(data=testN3, format="n3") #print g.store for s,p,o in g.triples((None,implies,None)): formulaA = s formulaB = o self.assertTrue(type(formulaA)==QuotedGraph and type(formulaB)==QuotedGraph) a = URIRef('http://test/a') b = URIRef('http://test/b') c = URIRef('http://test/c') d = URIRef('http://test/d') v = Variable('y') universe = ConjunctiveGraph(g.store) #test formula as terms self.assertTrue(len(list(universe.triples((formulaA,implies,formulaB))))==1) #test variable as term and variable roundtrip self.assertTrue(len(list(formulaB.triples((None,None,v))))==1) for s,p,o in formulaB.triples((None,d,None)): if o != c: self.assertTrue(isinstance(o,Variable)) self.assertTrue(o == v) s = list(universe.subjects(RDF.type, RDFS.Class))[0] self.assertTrue(isinstance(s,BNode)) self.assertTrue( len(list(universe.triples((None,implies,None)))) == 1) self.assertTrue( len(list(universe.triples((None,RDF.type,None)))) ==1) self.assertTrue( len(list(formulaA.triples((None,RDF.type,None))))==1) self.assertTrue( len(list(formulaA.triples((None,None,None))))==2) self.assertTrue( len(list(formulaB.triples((None,None,None))))==2) self.assertTrue( len(list(universe.triples((None,None,None))))==3) self.assertTrue( len(list(formulaB.triples((None,URIRef('http://test/d'),None))))==2) self.assertTrue( len(list(universe.triples((None,URIRef('http://test/d'),None))))==1) #context tests #test contexts with triple argument self.assertTrue( len(list(universe.contexts((a,d,c))))==1) #Remove test cases universe.remove((None,implies,None)) self.assertTrue( len(list(universe.triples((None,implies,None))))==0) self.assertTrue( len(list(formulaA.triples((None,None,None))))==2) self.assertTrue( len(list(formulaB.triples((None,None,None))))==2) formulaA.remove((None,b,None)) self.assertTrue( len(list(formulaA.triples((None,None,None))))==1) formulaA.remove((None,RDF.type,None)) self.assertTrue( len(list(formulaA.triples((None,None,None))))==0) universe.remove((None,RDF.type,RDFS.Class)) #remove_context tests universe.remove_context(formulaB) self.assertTrue( len(list(universe.triples((None,RDF.type,None))))==0) self.assertTrue( len(universe)==1) self.assertTrue( len(list(formulaB.triples((None,None,None))))==0) universe.remove((None,None,None)) self.assertTrue( len(universe)==0)
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)
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