1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
import unittest
from tempfile import mkdtemp
from rdflib.graph import Graph, ConjunctiveGraph
from rdflib.term import URIRef, BNode
class ContextTestCase(unittest.TestCase):
#store = 'Memory'
store = 'default'
slowtest = 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)
if __name__ == '__main__':
unittest.main()
|